1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * The Apache Software License, Version 1.1
   7  *
   8  *
   9  * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  10  * reserved.
  11  *
  12  * Redistribution and use in source and binary forms, with or without
  13  * modification, are permitted provided that the following conditions
  14  * are met:
  15  *
  16  * 1. Redistributions of source code must retain the above copyright
  17  *    notice, this list of conditions and the following disclaimer.
  18  *
  19  * 2. Redistributions in binary form must reproduce the above copyright
  20  *    notice, this list of conditions and the following disclaimer in
  21  *    the documentation and/or other materials provided with the
  22  *    distribution.
  23  *
  24  * 3. The end-user documentation included with the redistribution,
  25  *    if any, must include the following acknowledgment:
  26  *       "This product includes software developed by the
  27  *        Apache Software Foundation (http://www.apache.org/)."
  28  *    Alternately, this acknowledgment may appear in the software itself,
  29  *    if and wherever such third-party acknowledgments normally appear.
  30  *
  31  * 4. The names "Xerces" and "Apache Software Foundation" must
  32  *    not be used to endorse or promote products derived from this
  33  *    software without prior written permission. For written
  34  *    permission, please contact apache@apache.org.
  35  *
  36  * 5. Products derived from this software may not be called "Apache",
  37  *    nor may "Apache" appear in their name, without prior written
  38  *    permission of the Apache Software Foundation.
  39  *
  40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51  * SUCH DAMAGE.
  52  * ====================================================================
  53  *
  54  * This software consists of voluntary contributions made by many
  55  * individuals on behalf of the Apache Software Foundation and was
  56  * originally based on software copyright (c) 1999, International
  57  * Business Machines, Inc., http://www.apache.org.  For more
  58  * information on the Apache Software Foundation, please see
  59  * <http://www.apache.org/>.
  60  */
  61 
  62 package com.sun.org.apache.xerces.internal.impl.dtd.models;
  63 
  64 import com.sun.org.apache.xerces.internal.impl.dtd.XMLContentSpec;
  65 import com.sun.org.apache.xerces.internal.xni.QName;
  66 
  67 /**
  68  * Content model leaf node.
  69  *
  70  * @xerces.internal
  71  *
  72  */
  73 public class CMLeaf
  74     extends CMNode {
  75 
  76     //
  77     // Data
  78     //
  79 
  80     /** This is the element that this leaf represents. */
  81     private QName fElement = new QName();
  82 
  83     /**
  84      * Part of the algorithm to convert a regex directly to a DFA
  85      * numbers each leaf sequentially. If its -1, that means its an
  86      * epsilon node. Zero and greater are non-epsilon positions.
  87      */
  88     private int fPosition = -1;
  89 
  90     //
  91     // Constructors
  92     //
  93 
  94     /** Constructs a content model leaf. */
  95     public CMLeaf(QName element, int position)  {
  96         super(XMLContentSpec.CONTENTSPECNODE_LEAF);
  97 
  98         // Store the element index and position
  99         fElement.setValues(element);
 100         fPosition = position;
 101     }
 102 
 103     /** Constructs a content model leaf. */
 104     public CMLeaf(QName element)  {
 105         super(XMLContentSpec.CONTENTSPECNODE_LEAF);
 106 
 107         // Store the element index and position
 108         fElement.setValues(element);
 109     }
 110 
 111     //
 112     // Package methods
 113     //
 114 
 115     final QName getElement()
 116     {
 117         return fElement;
 118     }
 119 
 120     final int getPosition()
 121     {
 122         return fPosition;
 123     }
 124 
 125     final void setPosition(int newPosition)
 126     {
 127         fPosition = newPosition;
 128     }
 129 
 130     //
 131     // CMNode methods
 132     //
 133 
 134     // package
 135 
 136     public boolean isNullable()
 137     {
 138         // Leaf nodes are never nullable unless its an epsilon node
 139         return (fPosition == -1);
 140     }
 141 
 142     public String toString()
 143     {
 144         StringBuffer strRet = new StringBuffer(fElement.toString());
 145         strRet.append(" (");
 146         strRet.append(fElement.uri);
 147         strRet.append(',');
 148         strRet.append(fElement.localpart);
 149         strRet.append(')');
 150         if (fPosition >= 0)
 151         {
 152             strRet.append
 153             (
 154                 " (Pos:"
 155                 + new Integer(fPosition).toString()
 156                 + ")"
 157             );
 158         }
 159         return strRet.toString();
 160     }
 161 
 162     // protected
 163 
 164     protected void calcFirstPos(CMStateSet toSet)
 165     {
 166         // If we are an epsilon node, then the first pos is an empty set
 167         if (fPosition == -1)
 168             toSet.zeroBits();
 169 
 170         // Otherwise, its just the one bit of our position
 171         else
 172             toSet.setBit(fPosition);
 173     }
 174 
 175     protected void calcLastPos(CMStateSet toSet)
 176     {
 177         // If we are an epsilon node, then the last pos is an empty set
 178         if (fPosition == -1)
 179             toSet.zeroBits();
 180 
 181         // Otherwise, its just the one bit of our position
 182         else
 183             toSet.setBit(fPosition);
 184     }
 185 
 186 } // class CMLeaf