KickJava   Java API By Example, From Geeks To Geeks.

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


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 Software Foundation, 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.relaxng.RelaxException;
33 import com.caucho.relaxng.pattern.ElementPattern;
34 import com.caucho.util.L10N;
35 import com.caucho.xml.QName;
36
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * Generates programs from patterns.
42  */

43 public class ElementItem extends Item {
44   protected final static L10N L = new L10N(ElementItem.class);
45
46   private ElementPattern _element;
47
48   private NameClassItem _nameItem;
49   private Item _item;
50   private Item _childrenItem;
51
52   public ElementItem(ElementPattern element, NameClassItem nameItem)
53   {
54     _element = element;
55     _nameItem = nameItem;
56   }
57
58   public NameClassItem getNameClassItem()
59   {
60     return _nameItem;
61   }
62
63   public void setChildrenItem(Item item)
64   {
65     _childrenItem = item;
66   }
67
68   /**
69    * Returns the first set, the set of element names possible.
70    */

71   public void firstSet(HashSet JavaDoc<QName> set)
72   {
73     _nameItem.firstSet(set);
74   }
75
76   /**
77    * Returns the first set, the set of element names possible.
78    */

79   public void requiredFirstSet(HashSet JavaDoc<QName> set)
80   {
81     _nameItem.firstSet(set);
82   }
83   
84   /**
85    * The element does not allow the empty match.
86    */

87   public boolean allowEmpty()
88   {
89     return false;
90   }
91   
92   /**
93    * Return all possible child items or null
94    */

95   public Iterator JavaDoc<Item> getItemsIterator()
96   {
97     if (_item == null) {
98       _item = InElementItem.create(_childrenItem,
99                                    EmptyItem.create());
100     }
101
102     return itemIterator( _item );
103   }
104
105
106   /**
107    * Returns the next item on the match.
108    *
109    * @param name the name of the element
110    */

111   public Item startElement(QName name)
112     throws RelaxException
113   {
114     if (! _nameItem.matches(name))
115       return null;
116
117     if (_item == null) {
118       _item = InElementItem.create(_childrenItem,
119                                    EmptyItem.create());
120     }
121
122     return _item;
123   }
124   
125   /**
126    * Returns true if the element is allowed somewhere in the item.
127    * allowsElement is used for error messages to give more information
128    * in cases of order dependency.
129    *
130    * @param name the name of the element
131    *
132    * @return true if the element is allowed somewhere
133    */

134   public boolean allowsElement(QName name)
135   {
136     return _nameItem.matches(name);
137   }
138
139   /**
140    * Returns the pretty printed syntax.
141    */

142   public String JavaDoc toSyntaxDescription(int depth)
143   {
144     return _nameItem.toSyntaxDescription("");
145   }
146
147   /**
148    * Returns true for an element with simple syntax.
149    */

150   protected boolean isSimpleSyntax()
151   {
152     return true;
153   }
154
155   /**
156    * Returns the hash code for the empty item.
157    */

158   public int hashCode()
159   {
160     return 87 + _element.getDefName().hashCode();
161   }
162
163   /**
164    * Returns true if the object is an empty item.
165    */

166   public boolean equals(Object JavaDoc o)
167   {
168     if (this == o)
169       return true;
170     
171     if (! (o instanceof ElementItem))
172       return false;
173
174     ElementItem elt = (ElementItem) o;
175
176     return _element.getDefName().equals(elt._element.getDefName());
177   }
178
179   public String JavaDoc toString()
180   {
181     return "ElementItem[" + _nameItem + "]";
182   }
183 }
184
185
Popular Tags