KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > common > CMLeaf


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.validators.common;
59
60 import org.enhydra.apache.xerces.framework.XMLContentSpec;
61 import org.enhydra.apache.xerces.utils.QName;
62 import org.enhydra.apache.xerces.utils.StringPool;
63
64 /**
65  * Content model leaf node.
66  *
67  * @version $Id: CMLeaf.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
68  */

69 public class CMLeaf
70     extends CMNode {
71
72     //
73
// Data
74
//
75

76     /** This is the element that this leaf represents. */
77     private QName fElement = new QName();
78
79     /**
80      * Part of the algorithm to convert a regex directly to a DFA
81      * numbers each leaf sequentially. If its -1, that means its an
82      * epsilon node. Zero and greater are non-epsilon positions.
83      */

84     private int fPosition = -1;
85
86     //
87
// Constructors
88
//
89

90     /** Constructs a content model leaf. */
91     public CMLeaf(QName element, int position) throws CMException {
92         super(XMLContentSpec.CONTENTSPECNODE_LEAF);
93
94         // Store the element index and position
95
fElement.setValues(element);
96         fPosition = position;
97     }
98
99     /** Constructs a content model leaf. */
100     public CMLeaf(QName element) throws CMException {
101         super(XMLContentSpec.CONTENTSPECNODE_LEAF);
102
103         // Store the element index and position
104
fElement.setValues(element);
105     }
106
107     //
108
// Package methods
109
//
110

111     final QName getElement()
112     {
113         return fElement;
114     }
115
116     final int getPosition()
117     {
118         return fPosition;
119     }
120
121     final void setPosition(int newPosition)
122     {
123         fPosition = newPosition;
124     }
125
126     //
127
// CMNode methods
128
//
129

130     // package
131

132     boolean isNullable() throws CMException
133     {
134         // Leaf nodes are never nullable unless its an epsilon node
135
return (fPosition == -1);
136     }
137
138     String JavaDoc toString(StringPool stringPool)
139     {
140         StringBuffer JavaDoc strRet = new StringBuffer JavaDoc(fElement.toString());
141         strRet.append(" (");
142         strRet.append(stringPool.toString(fElement.uri));
143         strRet.append(',');
144         strRet.append(stringPool.toString(fElement.localpart));
145         strRet.append(')');
146         if (fPosition >= 0)
147         {
148             strRet.append
149             (
150                 " (Pos:"
151                 + new Integer JavaDoc(fPosition).toString()
152                 + ")"
153             );
154         }
155         return strRet.toString();
156     }
157
158     // protected
159

160     protected void calcFirstPos(CMStateSet toSet) throws CMException
161     {
162         // If we are an epsilon node, then the first pos is an empty set
163
if (fPosition == -1)
164             toSet.zeroBits();
165
166         // Otherwise, its just the one bit of our position
167
else
168             toSet.setBit(fPosition);
169     }
170
171     protected void calcLastPos(CMStateSet toSet) throws CMException
172     {
173         // If we are an epsilon node, then the last pos is an empty set
174
if (fPosition == -1)
175             toSet.zeroBits();
176
177         // Otherwise, its just the one bit of our position
178
else
179             toSet.setBit(fPosition);
180     }
181
182 } // class CMLeaf
183
Popular Tags