KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > program > InElementItem


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.relaxng.program;
30
31 import com.caucho.relaxng.RelaxException;
32 import com.caucho.util.L10N;
33 import com.caucho.xml.QName;
34
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Represents a match inside an element with a continuation.
40  *
41  * In the following example, the "c, d" would be eltItem and "e"
42  * would be the contItem.
43  *
44  * <pre>a { b . c, d } e</pre>
45  */

46 public class InElementItem extends Item {
47   protected final static L10N L = new L10N(InElementItem.class);
48
49   private final Item _eltItem;
50   private final Item _contItem;
51
52   private int _hashCode;
53
54   private InElementItem(Item eltItem, Item contItem)
55   {
56     _eltItem = eltItem;
57     _contItem = contItem;
58   }
59
60   public static InElementItem create(Item eltItem, Item contItem)
61   {
62     if (eltItem == null)
63       return null;
64     else if (contItem == null)
65       return null;
66     else
67       return new InElementItem(eltItem, contItem);
68   }
69
70   public Item getFirst()
71   {
72     return _eltItem;
73   }
74
75   public Item getSecond()
76   {
77     return _contItem;
78   }
79   
80   public Item getElementItem()
81   {
82     return _eltItem;
83   }
84
85   public Item getContinuationItem()
86   {
87     return _contItem;
88   }
89
90   /**
91    * Interleaves a continuation.
92    */

93   public Item interleaveContinuation(Item cont)
94   {
95     return create(_eltItem, InterleaveItem.create(cont, _contItem));
96   }
97
98   /**
99    * Adds a continuation
100    */

101   public Item inElementContinuation(Item cont)
102   {
103     return create(_eltItem, create(_contItem, cont));
104   }
105
106   /**
107    * Adds a continuation
108    */

109   public Item groupContinuation(Item cont)
110   {
111     return create(_eltItem, GroupItem.create(_contItem, cont));
112   }
113
114   /**
115    * Returns the first set, the set of element names possible.
116    */

117   public void firstSet(HashSet JavaDoc<QName> set)
118   {
119     _eltItem.firstSet(set);
120   }
121
122   /**
123    * Returns the first set, the set of element names possible.
124    */

125   public void requiredFirstSet(HashSet JavaDoc<QName> set)
126   {
127     _eltItem.requiredFirstSet(set);
128   }
129   
130   /**
131    * Allows empty if both allow empty.
132    */

133   public boolean allowEmpty()
134   {
135     return _eltItem.allowEmpty();
136   }
137   
138
139   /**
140    * Return all possible child items or null
141    */

142   public Iterator JavaDoc<Item> getItemsIterator()
143   {
144     return itemIterator( _eltItem );
145   }
146
147   /**
148    * Returns the next item when an element of the given name is returned
149    *
150    * @param name the name of the element
151    *
152    * @return the program for handling the element
153    */

154   public Item startElement(QName name)
155     throws RelaxException
156   {
157     Item nextElt = _eltItem.startElement(name);
158
159     if (nextElt == null)
160       return null;
161     else if (nextElt == _eltItem)
162       return this;
163     else
164       return nextElt.inElementContinuation(_contItem);
165   }
166   
167   /**
168    * Returns the next item when some text data is available.
169    *
170    * @param string the text data
171    *
172    * @return the program for handling the element
173    */

174   public Item text(String JavaDoc string)
175     throws RelaxException
176   {
177     Item nextElt = _eltItem.text(string);
178
179     if (nextElt == null)
180       return null;
181     else if (nextElt == _eltItem)
182       return this;
183     else
184       return create(nextElt, _contItem);
185   }
186
187   /**
188    * Returns the first set, the set of attribute names possible.
189    */

190   public void attributeSet(HashSet JavaDoc<QName> set)
191   {
192     _eltItem.attributeSet(set);
193   }
194   
195   /**
196    * Sets an attribute.
197    *
198    * @param name the name of the attribute
199    * @param value the value of the attribute
200    *
201    * @return the program for handling the element
202    */

203   public boolean allowAttribute(QName name, String JavaDoc value)
204     throws RelaxException
205   {
206     return _eltItem.allowAttribute(name, value);
207   }
208   
209   /**
210    * Sets an attribute.
211    *
212    * @param name the name of the attribute
213    * @param value the value of the attribute
214    *
215    * @return the program for handling the element
216    */

217   public Item setAttribute(QName name, String JavaDoc value)
218     throws RelaxException
219   {
220     Item nextElt = _eltItem.setAttribute(name, value);
221
222     if (nextElt == null)
223       return create(EmptyItem.create(), _contItem);
224     else if (nextElt == _eltItem)
225       return this;
226     else
227       return create(nextElt, _contItem);
228   }
229
230   /**
231    * Returns true if the element is allowed to end here.
232    */

233   public Item attributeEnd()
234   {
235     Item nextElt = _eltItem.attributeEnd();
236
237     if (nextElt == null)
238       return null;
239     else if (nextElt == _eltItem)
240       return this;
241     else
242       return create(nextElt, _contItem);
243   }
244   
245   /**
246    * Returns the next item when the element is completes.
247    *
248    * @return the program for handling the element
249    */

250   public Item endElement()
251     throws RelaxException
252   {
253     if (_eltItem.allowEmpty())
254       return _contItem;
255     else
256       return null;
257   }
258   
259   /**
260    * Returns true if the element is allowed somewhere in the item.
261    * allowsElement is used for error messages to give more information
262    * in cases of order dependency.
263    *
264    * @param name the name of the element
265    *
266    * @return true if the element is allowed somewhere
267    */

268   public boolean allowsElement(QName name)
269   {
270     return _eltItem.allowsElement(name);
271   }
272
273   /**
274    * Returns the pretty printed syntax.
275    */

276   public String JavaDoc toSyntaxDescription(int depth)
277   {
278     return _eltItem.toSyntaxDescription(depth);
279   }
280
281   /**
282    * Returns true if the syntax description is simple
283    */

284   public boolean isSimpleSyntax()
285   {
286     return _eltItem.isSimpleSyntax();
287   }
288
289   /**
290    * Returns the hash code for the empty item.
291    */

292   public int hashCode()
293   {
294     if (_hashCode == 0)
295       _hashCode = calculateHashCode();
296     
297     return _hashCode;
298   }
299
300   /**
301    * Returns the hash code for the empty item.
302    */

303   private int calculateHashCode()
304   {
305     return _eltItem.hashCode() * 65521 + _contItem.hashCode();
306   }
307
308   /**
309    * Returns true if the object is an empty item.
310    */

311   public boolean equals(Object JavaDoc o)
312   {
313     if (this == o)
314       return true;
315     
316     if (! (o instanceof InElementItem))
317       return false;
318
319     InElementItem seq = (InElementItem) o;
320
321     return _eltItem.equals(seq._eltItem) && _contItem.equals(seq._contItem);
322   }
323
324   public String JavaDoc toString()
325   {
326     return "InElementItem[" + _eltItem + ",cont=" + _contItem + "]";
327   }
328 }
329
330
Popular Tags