KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > confix > ConfixHandlerState


1 package org.sapia.util.xml.confix;
2
3
4 // Import of Sapia's utility classes
5
// ---------------------------------
6
import org.sapia.util.xml.XmlUtil;
7 import org.sapia.util.xml.parser.HandlerContextIF;
8 import org.sapia.util.xml.parser.HandlerStateIF;
9
10 // Imports of David Meggison's SAX classes
11
// ---------------------------------------
12
import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.SAXException JavaDoc;
14
15
16 /**
17  * Implements a <code>HandlerStateIF</code> that is used by the <code>SAXProcessor</code>.
18  *
19  * @author JC Desrochers
20  *
21  * <dl>
22  * <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>
23  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
24  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
25  * </dl>
26  */

27 public class ConfixHandlerState implements HandlerStateIF {
28   /////////////////////////////////////////////////////////////////////////////////////////
29
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
30
/////////////////////////////////////////////////////////////////////////////////////////
31

32   /** Indicates if this handler state is currently parsing. */
33   private boolean _isParsing;
34
35   /** Indicates if this handler state is currently parsing a child element. */
36   private boolean _isParsingChild;
37
38   /** The SAX processor that manage this handler state. */
39   private SAXProcessor _theProcessor;
40
41   /** Buffer that contains the characters of the element beign parsed. */
42   private StringBuffer JavaDoc _theContent;
43
44   /** The parent object that represent the parent element beign parsed. */
45   private Object JavaDoc _theParentObject;
46
47   /** The current object that represent the element beign parsed. */
48   private Object JavaDoc _theCurrentObject;
49
50   /** Indicates if the current object has been assigned to its parent. */
51   private boolean _isCurrentObjectAssigned;
52
53   /////////////////////////////////////////////////////////////////////////////////////////
54
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
55
/////////////////////////////////////////////////////////////////////////////////////////
56

57   /**
58    * Creates a new ConfixHandlerState instance
59    */

60   public ConfixHandlerState(SAXProcessor aProcessor, Object JavaDoc aParentObject) {
61     if (aParentObject == null) {
62       throw new IllegalArgumentException JavaDoc("The parent object passed in is null");
63     }
64
65     _theContent = new StringBuffer JavaDoc();
66     _theProcessor = aProcessor;
67     _theParentObject = aParentObject;
68   }
69
70   /**
71    * Creates a new ConfixHandlerState instance
72    */

73   public ConfixHandlerState(SAXProcessor aProcessor) {
74     _theContent = new StringBuffer JavaDoc();
75     _theProcessor = aProcessor;
76   }
77
78   /////////////////////////////////////////////////////////////////////////////////////////
79
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
80
/////////////////////////////////////////////////////////////////////////////////////////
81

82   /**
83    * Returns the result object of this handler state.
84    *
85    * @return The result object of this handler state.
86    * @exception IllegalStateException If this handler state is currently parsing.
87    */

88   public Object JavaDoc getResult() {
89     if (_isParsing == true) {
90       throw new IllegalStateException JavaDoc("This handler state is currently parsing");
91     }
92
93     return _theCurrentObject;
94   }
95
96   /////////////////////////////////////////////////////////////////////////////////////////
97
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
98
/////////////////////////////////////////////////////////////////////////////////////////
99

100   /**
101    * Receives the notification of the the start of an element.
102    *
103    * @param aContext The handler context.
104    * @param anUri The namespace URI associated with the element
105    * @param aLocalName The element type local name.
106    * @param aQualifiedName The element type qualified name.
107    * @param someAttributes The specified or defaulted attributes.
108    * @exception SAXException If an exception occurs.
109    */

110   public void startElement(HandlerContextIF aContext, String JavaDoc anUri,
111     String JavaDoc aLocalName, String JavaDoc aQualifiedName, Attributes JavaDoc someAttributes)
112     throws SAXException JavaDoc {
113     if (!_isParsing) {
114       _isParsing = true;
115
116       CreationStatus aStatus = null;
117       String JavaDoc prefix = XmlUtil.extractPrefix(aQualifiedName);
118
119       try {
120         // Creating the object
121
aStatus = _theProcessor.getObjectFactory().newObjectFor(prefix,
122             anUri, aLocalName, _theParentObject);
123
124         _theCurrentObject = aStatus.getCreated();
125         _isCurrentObjectAssigned = aStatus.wasAssigned();
126
127         // Process the attributes of the element
128
for (int i = 0; i < someAttributes.getLength(); i++) {
129           String JavaDoc anAttributeName = someAttributes.getLocalName(i);
130           String JavaDoc anAttributeValue = someAttributes.getValue(i);
131
132           AbstractXMLProcessor.invokeSetter(aLocalName, _theCurrentObject,
133             anAttributeName, anAttributeValue);
134         }
135       } catch (ObjectCreationException oce) {
136         if (_theParentObject == null) {
137           String JavaDoc aMessage = "Unable to create an object for the element " +
138             aQualifiedName;
139
140           throw new SAXException JavaDoc(aMessage, oce);
141         }
142       } catch (ConfigurationException ce) {
143         String JavaDoc aMessage = "Unable to process the content of the element " +
144           aLocalName;
145
146         throw new SAXException JavaDoc(aMessage, ce);
147       }
148     } else {
149       HandlerStateIF aChildHandler;
150
151       if (_theCurrentObject instanceof HandlerStateIF) {
152         aChildHandler = (HandlerStateIF) _theCurrentObject;
153       } else {
154         aChildHandler = new ConfixHandlerState(_theProcessor, _theCurrentObject);
155       }
156
157       _isParsingChild = true;
158       aContext.setCurrentState(aChildHandler, anUri, aLocalName,
159         aQualifiedName, someAttributes);
160     }
161   }
162
163   /**
164    * Receives the notification of the the end of an element.
165    *
166    * @param aContext The handler context.
167    * @param anUri The namespace URI associated with the element
168    * @param aLocalName The element type local name.
169    * @param aQualifiedName The element type qualified name.
170    * @exception SAXException If an exception occurs.
171    */

172   public void endElement(HandlerContextIF aContext, String JavaDoc anUri,
173     String JavaDoc aLocalName, String JavaDoc aQualifiedName) throws SAXException JavaDoc {
174     if (_isParsingChild) {
175       _isParsingChild = false;
176     } else {
177       String JavaDoc aValue = _theContent.toString().trim();
178
179       if (aValue.length() > 0) {
180         if (_theCurrentObject != null) {
181           try {
182             AbstractXMLProcessor.invokeSetter(aLocalName, _theCurrentObject,
183               "Text", aValue);
184           } catch (ConfigurationException ce) {
185             String JavaDoc aMessage = "The object " +
186               _theCurrentObject.getClass().getName() +
187               " does not accept free text";
188
189             throw new SAXException JavaDoc(aMessage, ce);
190           }
191         } else {
192           try {
193             AbstractXMLProcessor.invokeSetter(_theParentObject.getClass()
194                                                               .getName(),
195               _theParentObject, aLocalName, aValue);
196             _isCurrentObjectAssigned = true;
197           } catch (ConfigurationException ce) {
198             String JavaDoc aMessage = "Unable to process the content of the element " +
199               aLocalName;
200
201             throw new SAXException JavaDoc(aMessage, ce);
202           }
203         }
204       } else if (_theCurrentObject == null) {
205         String JavaDoc aMessage = "Unable to create an object for the element " +
206           aQualifiedName;
207
208         throw new SAXException JavaDoc(aMessage);
209       }
210
211       if (_theCurrentObject instanceof ObjectCreationCallback) {
212         try {
213           _theCurrentObject = ((ObjectCreationCallback) _theCurrentObject).onCreate();
214         } catch (ConfigurationException e) {
215           throw new SAXException JavaDoc("Could not create object", e);
216         }
217       }
218
219       // assign obj to parent through setXXX or addXXX
220
if ((_theParentObject != null) && !_isCurrentObjectAssigned &&
221             !(_theCurrentObject instanceof NullObject)) {
222         try {
223           AbstractXMLProcessor.assignToParent(_theParentObject,
224             _theCurrentObject, aLocalName);
225         } catch (ConfigurationException ce) {
226           StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(
227               "Unable to assign the object ").append(_theCurrentObject)
228                                                                                  .append(" to the parent ")
229                                                                                  .append(_theParentObject.getClass()
230                                                                                                          .getName());
231
232           throw new SAXException JavaDoc(aBuffer.toString(), ce);
233         }
234       }
235
236       if (_theCurrentObject instanceof NullObject) {
237         _theCurrentObject = null;
238       }
239
240       _isParsing = false;
241       aContext.removeCurrentState(anUri, aLocalName, aQualifiedName);
242     }
243   }
244
245   /**
246    * Receives the notification of character data inside an element.
247    *
248    * @param aContext The handler context.
249    * @param someChars The characters.
250    * @param anOffset The start position in the character array.
251    * @param aLength The number of characters to use from the character array.
252    * @exception SAXException If an exception occurs.
253    */

254   public void characters(HandlerContextIF aContext, char[] someChars,
255     int anOffset, int length) throws SAXException JavaDoc {
256     _theContent.append(someChars, anOffset, length);
257   }
258
259   /**
260    * Receives the notification of ignorable whitespace in element content.
261    *
262    * @param aContext The handler context.
263    * @param someChars The whitespace characters.
264    * @param anOffset The start position in the character array.
265    * @param aLength The number of characters to use from the character array.
266    * @exception SAXException If an exception occurs.
267    */

268   public void ignorableWhitespace(HandlerContextIF aContext, char[] someChars,
269     int anOffset, int aLength) throws SAXException JavaDoc {
270     // IGNORING WHITESPACES...
271
}
272 }
273
Popular Tags