KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > BufferState


1 package org.sapia.util.xml.idefix;
2
3
4 // Import of Sapia's utility classes
5
// ---------------------------------
6
import org.sapia.util.xml.Attribute;
7 import org.sapia.util.xml.Namespace;
8
9 // Import of Sun's JDK classes
10
// ---------------------------
11
import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14
15 /**
16  *
17  *
18  * @author Jean-Cedric Desrochers
19  *
20  * <dl>
21  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
22  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
23  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
24  * </dl>
25  */

26 public class BufferState {
27   /////////////////////////////////////////////////////////////////////////////////////////
28
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
29
/////////////////////////////////////////////////////////////////////////////////////////
30

31   /** The parent buffer state. */
32   private BufferState _theParent;
33
34   /** The element namespace of this buffer state. */
35   private Namespace _theElementNamespace;
36
37   /** The element name of this buffer state. */
38   private String JavaDoc _theElementName;
39
40   /** The list of attributes of this buffer state. */
41   private List JavaDoc _theAttributes;
42
43   /** Indicates if this buffer state will get more attributes or not. */
44   private boolean _isGettingMoreAttribute;
45
46   /** The current content. */
47   private StringBuffer JavaDoc _theContent;
48
49   /** The list of declared namespace URIs of this buffer state. */
50   private List JavaDoc _theDeclaredNamespaces;
51
52   /** The XML string of the nested elements of this string buffer. */
53   private StringBuffer JavaDoc _theNestedXmlString;
54
55   /** Indicates if the start element of this buffer state was generated or not. */
56   private boolean _isStartElementGenerated;
57
58   /////////////////////////////////////////////////////////////////////////////////////////
59
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
60
/////////////////////////////////////////////////////////////////////////////////////////
61

62   /**
63    * Creates a new BufferState instance with the passed in attributes.
64    *
65    * @param aNamespace The element namespace.
66    * @param anElementName The element name.
67    */

68   public BufferState(BufferState aParent, Namespace aNamespace,
69     String JavaDoc anElementName) {
70     _theParent = aParent;
71     _theElementNamespace = aNamespace;
72     _theElementName = anElementName;
73     _theAttributes = new ArrayList JavaDoc();
74     _isGettingMoreAttribute = true;
75     _theDeclaredNamespaces = new ArrayList JavaDoc();
76     _isStartElementGenerated = false;
77   }
78
79   /**
80    * Returns the parent state of this buffer state.
81    *
82    * @return The parent state of this buffer state.
83    */

84   public BufferState getParent() {
85     return _theParent;
86   }
87
88   /**
89    * Returns the element namespace of this buffer state.
90    *
91    * @return The element namespace of this buffer state.
92    */

93   public Namespace getElementNamespace() {
94     return _theElementNamespace;
95   }
96
97   /**
98    * Returns the element name of this buffer state.
99    *
100    * @return The element name of this buffer state.
101    */

102   public String JavaDoc getElementName() {
103     return _theElementName;
104   }
105
106   /**
107    * Returns the list of attributes of this buffer state.
108    *
109    * @return A list of <CODE>Attribute</CODE> objects.
110    * @see Attribute
111    */

112   public List JavaDoc getAttributes() {
113     return _theAttributes;
114   }
115
116   /**
117    * Returns <CODE>true</CODE> if this buffer state expect more attributes, <CODE>false</CODE> otherwise.
118    *
119    * @return <CODE>true</CODE> if this buffer state expect more attributes, <CODE>false</CODE> otherwise.
120    */

121   public boolean isGettingMoreAttribute() {
122     return _isGettingMoreAttribute;
123   }
124
125   /**
126    * Return <CODE>true</CODE> if the element of this buffer state is empty.
127    *
128    * @return True if the element of this buffer state is empty.
129    */

130   public boolean isElementEmpty() {
131     return ((_theContent == null) || (_theContent.length() == 0)) &&
132     ((_theNestedXmlString == null) || (_theNestedXmlString.length() == 0));
133   }
134
135   /**
136    * Returns <CODE>true</CODE> if the buffer state does not contains nested XML.
137    *
138    * @return <CODE>true</CODE> if the buffer state does not contains nested XML.
139    */

140   public boolean isNestedXMLEmpty() {
141     return (_theNestedXmlString == null) ||
142     (_theNestedXmlString.length() == 0);
143   }
144
145   /**
146    * Returns <CODE>true</CODE> if the buffer state does not have some XML content.
147    *
148    * @return <CODE>true</CODE> if the buffer state does not have some XML content.
149    */

150   public boolean isContentEmpty() {
151     return (_theContent == null) || (_theContent.length() == 0);
152   }
153
154   /**
155    * Returns the string buffer of the content of this buffer state.
156    *
157    * @return The string buffer of the content of this buffer state.
158    */

159   public StringBuffer JavaDoc getContent() {
160     if (_theContent == null) {
161       _theContent = new StringBuffer JavaDoc();
162     }
163
164     return _theContent;
165   }
166
167   /**
168    * Returns the list of declared namespace of this buffer state.
169    *
170    * @return The list of <CODE>Namespace</CODE> objects of this buffer state.
171    * @see Namespace
172    */

173   public List JavaDoc getDeclaredNamespaces() {
174     return _theDeclaredNamespaces;
175   }
176
177   /**
178    * Returns the XML string of the nested elements of this buffer state.
179    *
180    * @return The XML string of the nested elements of this buffer state.
181    */

182   public StringBuffer JavaDoc getNestedXmlString() {
183     if (_theNestedXmlString == null) {
184       _theNestedXmlString = new StringBuffer JavaDoc();
185     }
186
187     return _theNestedXmlString;
188   }
189
190   /**
191    * Returns <CODE>true</CODE> if the start element was generated for this buffer state, <CODE>false</CODE> otherwise.
192    *
193    * @return <CODE>true</CODE> if the start element was generated for this buffer state, <CODE>false</CODE> otherwise.
194    */

195   public boolean isStartElementGenerated() {
196     return _isStartElementGenerated;
197   }
198
199   /////////////////////////////////////////////////////////////////////////////////////////
200
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
201
/////////////////////////////////////////////////////////////////////////////////////////
202

203   /**
204    * Adds the attribute passed in to this buffer state.
205    *
206    * @param anAttribute The attribute to add.
207    */

208   public void addAttribute(Attribute anAttribute) {
209     _theAttributes.add(anAttribute);
210   }
211
212   /**
213    * Adds the attribute passed in to this buffer state.
214    *
215    * @param anIndex The index at which to add the attribute.
216    * @param anAttribute The attribute to add.
217    */

218   public void addAttribute(int anIndex, Attribute anAttribute) {
219     _theAttributes.add(anIndex, anAttribute);
220   }
221
222   /**
223    * Removes the attribute passed in from this buffer state.
224    *
225    * @param anAttribute The attribute to remove.
226    */

227   public void removeAttribute(Attribute anAttribute) {
228     _theAttributes.remove(anAttribute);
229   }
230
231   /**
232    * Removes all the attribute from this buffer state.
233    */

234   public void clearAttributes() {
235     _theAttributes.clear();
236   }
237
238   /**
239    * Tells the buffer state that it will get or it wont get any more attributes.
240    *
241    * @param isGettingMoreAttribute The new value for the indicator.
242    */

243   public void setIsGettingMoreAttribute(boolean isGettingMoreAttribute) {
244     _isGettingMoreAttribute = isGettingMoreAttribute;
245   }
246
247   /**
248    * Adds the declared namespace passed in to this buffer state.
249    *
250    * @param aNamespace The declared namespace to add.
251    */

252   public void addDeclaredNamespace(Namespace aNamespace) {
253     if (!isNamespaceDeclared(aNamespace)) {
254       _theDeclaredNamespaces.add(aNamespace);
255     }
256   }
257
258   /**
259    * Returns true if this buffer state has declared the namespace passed in.
260    * Otherwise it delegates the call to the parent buffer state. If it has no parent,
261    * it return false.
262    *
263    * @return True if the namespace passed in is delcared or not.
264    */

265   protected boolean isNamespaceDeclared(Namespace aNamespace) {
266     if (_theDeclaredNamespaces.contains(aNamespace)) {
267       return true;
268     } else if (_theParent != null) {
269       return _theParent.isNamespaceDeclared(aNamespace);
270     } else {
271       return false;
272     }
273   }
274
275   /**
276    * Removes the declared namespace passed in from this buffer state.
277    *
278    * @param aNamespace The declared namespace to remove.
279    */

280   public void removeDeclaredNamespace(Namespace aNamespace) {
281     _theDeclaredNamespaces.remove(aNamespace);
282   }
283
284   /**
285    * Removes all the declared namespace from this buffer state.
286    */

287   public void clearDelcaredNamespaces() {
288     _theDeclaredNamespaces.clear();
289   }
290
291   /**
292    * Changes the buffer that contains the XML string of the nested elements.
293    *
294    * @param aBuffer The new buffer for the nested XML string.
295    * @exception IllegalStateException If the buffer for the nested XML was already set.
296    */

297   public void setNestedXmlString(StringBuffer JavaDoc aBuffer) {
298     if ((_theNestedXmlString != null) && (aBuffer != _theNestedXmlString)) {
299       throw new IllegalStateException JavaDoc(
300         "Could not overwrite the buffer of the nested XML");
301     }
302
303     _theNestedXmlString = aBuffer;
304   }
305
306   /**
307    * Indicates if the start element of this buffer state was generated or not.
308    *
309    * @param isStartElementGenerated The new value for the indicator.
310    */

311   public void setIsStartElementGenerated(boolean isStartElementGenerated) {
312     _isStartElementGenerated = isStartElementGenerated;
313   }
314 }
315
Popular Tags