KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.relaxng.program;
31
32 import com.caucho.log.Log;
33 import com.caucho.relaxng.RelaxException;
34 import com.caucho.util.CharBuffer;
35 import com.caucho.util.L10N;
36 import com.caucho.xml.QName;
37
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.NoSuchElementException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Generates programs from patterns.
45  */

46 abstract public class Item {
47   protected final static L10N L = new L10N(Item.class);
48   protected final static Logger JavaDoc log = Log.open(Item.class);
49
50   private static final Iterator JavaDoc<Item> EMPTY_ITEM_ITERATOR;
51   
52   /**
53    * Adds to the first set the set of element names possible.
54    */

55   public void firstSet(HashSet JavaDoc<QName> set)
56   {
57   }
58
59   /**
60    * Adds to the first set the set of element names required.
61    */

62   public void requiredFirstSet(HashSet JavaDoc<QName> set)
63   {
64     if (! allowEmpty())
65       firstSet(set);
66   }
67
68   /**
69    * Returns true if the item can match empty.
70    */

71   public boolean allowEmpty()
72   {
73     return false;
74   }
75   
76   /**
77    * Return all possible child items
78    */

79   public Iterator JavaDoc<Item> getItemsIterator()
80   {
81     return emptyItemIterator();
82   }
83
84   protected Iterator JavaDoc<Item> emptyItemIterator()
85   {
86     return EMPTY_ITEM_ITERATOR;
87   }
88
89   protected Iterator JavaDoc<Item> itemIterator( final Item item )
90   {
91     if (item == null)
92       return emptyItemIterator();
93
94     return new Iterator JavaDoc<Item>() {
95       private boolean _done;
96
97       public boolean hasNext()
98       {
99         return !_done;
100       }
101
102       public Item next()
103       {
104         if ( ! hasNext() )
105           throw new NoSuchElementException JavaDoc();
106
107         _done = true;
108
109         return item;
110       }
111
112       public void remove()
113       {
114         throw new UnsupportedOperationException JavaDoc();
115       }
116     };
117   }
118
119   /**
120    * Returns the next item when an element of the given name is returned
121    *
122    * @param name the name of the element
123    *
124    * @return the program for handling the element
125    */

126   public Item startElement(QName name)
127     throws RelaxException
128   {
129     return null;
130   }
131   
132   /**
133    * Returns true if the element is allowed somewhere in the item.
134    * allowsElement is used for error messages to give more information
135    * in cases of order dependency.
136    *
137    * @param name the name of the element
138    *
139    * @return true if the element is allowed somewhere
140    */

141   public boolean allowsElement(QName name)
142   {
143     return false;
144   }
145
146   /**
147    * Adds to the first set, the set of attribute names possible.
148    */

149   public void attributeSet(HashSet JavaDoc<QName> set)
150   {
151   }
152   
153   /**
154    * Sets an attribute.
155    *
156    * @param name the name of the attribute
157    * @param value the value of the attribute
158    *
159    * @return the program for handling the element
160    */

161   public boolean allowAttribute(QName name, String JavaDoc value)
162     throws RelaxException
163   {
164     return false;
165   }
166   
167   /**
168    * Sets an attribute.
169    *
170    * @param name the name of the attribute
171    * @param value the value of the attribute
172    *
173    * @return the program for handling the element
174    */

175   public Item setAttribute(QName name, String JavaDoc value)
176     throws RelaxException
177   {
178     return this;
179   }
180
181   /**
182    * Returns true if the item can match empty.
183    */

184   public Item attributeEnd()
185   {
186     return this;
187   }
188
189   /**
190    * Adds text.
191    */

192   public Item text(String JavaDoc text)
193     throws RelaxException
194   {
195     return null;
196   }
197   
198   /**
199    * Returns the next item when the element closes
200    */

201   public Item endElement()
202     throws RelaxException
203   {
204     if (allowEmpty())
205       return EmptyItem.create();
206     else
207       return null;
208   }
209
210   /**
211    * Interleaves a continuation.
212    */

213   public Item interleaveContinuation(Item cont)
214   {
215     throw new IllegalStateException JavaDoc(String.valueOf(getClass().getName()));
216   }
217
218   /**
219    * Interleaves a continuation.
220    */

221   public Item inElementContinuation(Item cont)
222   {
223     throw new IllegalStateException JavaDoc(String.valueOf(getClass().getName()));
224   }
225
226   /**
227    * Appends a group to a continuation.
228    */

229   public Item groupContinuation(Item cont)
230   {
231     throw new IllegalStateException JavaDoc(String.valueOf(getClass().getName()));
232   }
233
234   /**
235    * Returns the pretty printed syntax.
236    */

237   public String JavaDoc toSyntaxDescription(int depth)
238   {
239     return toString();
240   }
241
242   /**
243    * Returns true for an element with simple syntax.
244    */

245   protected boolean isSimpleSyntax()
246   {
247     return false;
248   }
249
250   /**
251    * Adds a syntax newline.
252    */

253   protected void addSyntaxNewline(CharBuffer cb, int depth)
254   {
255     cb.append('\n');
256     for (int i = 0; i < depth; i++)
257       cb.append(' ');
258   }
259
260   /**
261    * Throws an error.
262    */

263   protected RelaxException error(String JavaDoc msg)
264   {
265     return new RelaxException(msg);
266   }
267
268   static {
269     EMPTY_ITEM_ITERATOR =
270       new Iterator JavaDoc<Item>() {
271     public boolean hasNext()
272     {
273       return false;
274     }
275
276     public Item next()
277     {
278       throw new NoSuchElementException JavaDoc();
279     }
280
281     public void remove()
282     {
283       throw new UnsupportedOperationException JavaDoc();
284     }
285       };
286   }
287 }
288
289
Popular Tags