001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.relaxng.program;
030:
031: import com.caucho.relaxng.RelaxException;
032: import com.caucho.util.L10N;
033: import com.caucho.xml.QName;
034:
035: import java.util.HashSet;
036: import java.util.Iterator;
037:
038: /**
039: * Represents a match inside an element with a continuation.
040: *
041: * In the following example, the "c, d" would be eltItem and "e"
042: * would be the contItem.
043: *
044: * <pre>a { b . c, d } e</pre>
045: */
046: public class InElementItem extends Item {
047: protected final static L10N L = new L10N(InElementItem.class);
048:
049: private final Item _eltItem;
050: private final Item _contItem;
051:
052: private int _hashCode;
053:
054: private InElementItem(Item eltItem, Item contItem) {
055: _eltItem = eltItem;
056: _contItem = contItem;
057: }
058:
059: public static InElementItem create(Item eltItem, Item contItem) {
060: if (eltItem == null)
061: return null;
062: else if (contItem == null)
063: return null;
064: else
065: return new InElementItem(eltItem, contItem);
066: }
067:
068: public Item getFirst() {
069: return _eltItem;
070: }
071:
072: public Item getSecond() {
073: return _contItem;
074: }
075:
076: public Item getElementItem() {
077: return _eltItem;
078: }
079:
080: public Item getContinuationItem() {
081: return _contItem;
082: }
083:
084: /**
085: * Interleaves a continuation.
086: */
087: public Item interleaveContinuation(Item cont) {
088: return create(_eltItem, InterleaveItem.create(cont, _contItem));
089: }
090:
091: /**
092: * Adds a continuation
093: */
094: public Item inElementContinuation(Item cont) {
095: return create(_eltItem, create(_contItem, cont));
096: }
097:
098: /**
099: * Adds a continuation
100: */
101: public Item groupContinuation(Item cont) {
102: return create(_eltItem, GroupItem.create(_contItem, cont));
103: }
104:
105: /**
106: * Returns the first set, the set of element names possible.
107: */
108: public void firstSet(HashSet<QName> set) {
109: _eltItem.firstSet(set);
110: }
111:
112: /**
113: * Returns the first set, the set of element names possible.
114: */
115: public void requiredFirstSet(HashSet<QName> set) {
116: _eltItem.requiredFirstSet(set);
117: }
118:
119: /**
120: * Allows empty if both allow empty.
121: */
122: public boolean allowEmpty() {
123: return _eltItem.allowEmpty();
124: }
125:
126: /**
127: * Return all possible child items or null
128: */
129: public Iterator<Item> getItemsIterator() {
130: return itemIterator(_eltItem);
131: }
132:
133: /**
134: * Returns the next item when an element of the given name is returned
135: *
136: * @param name the name of the element
137: *
138: * @return the program for handling the element
139: */
140: public Item startElement(QName name) throws RelaxException {
141: Item nextElt = _eltItem.startElement(name);
142:
143: if (nextElt == null)
144: return null;
145: else if (nextElt == _eltItem)
146: return this ;
147: else
148: return nextElt.inElementContinuation(_contItem);
149: }
150:
151: /**
152: * Returns the next item when some text data is available.
153: *
154: * @param string the text data
155: *
156: * @return the program for handling the element
157: */
158: @Override
159: public Item text(CharSequence string) throws RelaxException {
160: Item nextElt = _eltItem.text(string);
161:
162: if (nextElt == null)
163: return null;
164: else if (nextElt == _eltItem)
165: return this ;
166: else
167: return create(nextElt, _contItem);
168: }
169:
170: /**
171: * Returns the first set, the set of attribute names possible.
172: */
173: public void attributeSet(HashSet<QName> set) {
174: _eltItem.attributeSet(set);
175: }
176:
177: /**
178: * Sets an attribute.
179: *
180: * @param name the name of the attribute
181: * @param value the value of the attribute
182: *
183: * @return the program for handling the element
184: */
185: public boolean allowAttribute(QName name, String value)
186: throws RelaxException {
187: return _eltItem.allowAttribute(name, value);
188: }
189:
190: /**
191: * Sets an attribute.
192: *
193: * @param name the name of the attribute
194: * @param value the value of the attribute
195: *
196: * @return the program for handling the element
197: */
198: public Item setAttribute(QName name, String value)
199: throws RelaxException {
200: Item nextElt = _eltItem.setAttribute(name, value);
201:
202: if (nextElt == null)
203: return create(EmptyItem.create(), _contItem);
204: else if (nextElt == _eltItem)
205: return this ;
206: else
207: return create(nextElt, _contItem);
208: }
209:
210: /**
211: * Returns true if the element is allowed to end here.
212: */
213: public Item attributeEnd() {
214: Item nextElt = _eltItem.attributeEnd();
215:
216: if (nextElt == null)
217: return null;
218: else if (nextElt == _eltItem)
219: return this ;
220: else
221: return create(nextElt, _contItem);
222: }
223:
224: /**
225: * Returns the next item when the element is completes.
226: *
227: * @return the program for handling the element
228: */
229: public Item endElement() throws RelaxException {
230: if (_eltItem.allowEmpty())
231: return _contItem;
232: else
233: return null;
234: }
235:
236: /**
237: * Returns true if the element is allowed somewhere in the item.
238: * allowsElement is used for error messages to give more information
239: * in cases of order dependency.
240: *
241: * @param name the name of the element
242: *
243: * @return true if the element is allowed somewhere
244: */
245: public boolean allowsElement(QName name) {
246: return _eltItem.allowsElement(name);
247: }
248:
249: /**
250: * Returns the pretty printed syntax.
251: */
252: public String toSyntaxDescription(int depth) {
253: return _eltItem.toSyntaxDescription(depth);
254: }
255:
256: /**
257: * Returns true if the syntax description is simple
258: */
259: public boolean isSimpleSyntax() {
260: return _eltItem.isSimpleSyntax();
261: }
262:
263: /**
264: * Returns the hash code for the empty item.
265: */
266: public int hashCode() {
267: if (_hashCode == 0)
268: _hashCode = calculateHashCode();
269:
270: return _hashCode;
271: }
272:
273: /**
274: * Returns the hash code for the empty item.
275: */
276: private int calculateHashCode() {
277: return _eltItem.hashCode() * 65521 + _contItem.hashCode();
278: }
279:
280: /**
281: * Returns true if the object is an empty item.
282: */
283: public boolean equals(Object o) {
284: if (this == o)
285: return true;
286:
287: if (!(o instanceof InElementItem))
288: return false;
289:
290: InElementItem seq = (InElementItem) o;
291:
292: return _eltItem.equals(seq._eltItem)
293: && _contItem.equals(seq._contItem);
294: }
295:
296: public String toString() {
297: return "InElementItem[" + _eltItem + ",cont=" + _contItem + "]";
298: }
299: }
|