KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > parser > StatefullSAXHandler


1 package org.sapia.util.xml.parser;
2
3
4 // Imports of David Meggison's SAX classes
5
// ---------------------------------------
6
import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.InputSource JavaDoc;
8 import org.xml.sax.Locator JavaDoc;
9 import org.xml.sax.SAXException JavaDoc;
10 import org.xml.sax.SAXParseException JavaDoc;
11 import org.xml.sax.helpers.DefaultHandler JavaDoc;
12
13
14 // Import of Apache's Log4j classes
15
// --------------------------------
16
//import org.apache.log4j.Logger;
17

18 /**
19  * The <CODE>StatefullSAXHandler</CODE> class is a SAX event handler. When one of is callback
20  * methods is called, it delegates the call to the current handler object; that current
21  * handler is refered as the "current state" of the handler. It accesses the <CODE>HandlerStateIF</CODE>
22  * instance by using a <CODE>HandlerContextIF</CODE>. That context is passed at creation
23  * and contains the stack of all the handlers as well as their result objects.
24  *
25  * @see HandlerContextIF
26  * @see HandlerStateIF
27  * @author Jean-Cedric Desrochers
28  * <dl>
29  * <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>
30  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
31  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
32  * </dl>
33  */

34 public class StatefullSAXHandler extends DefaultHandler JavaDoc {
35   /////////////////////////////////////////////////////////////////////////////////////////
36
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
37
/////////////////////////////////////////////////////////////////////////////////////////
38

39   /** Defines the logger instance for this class. */
40
41   //private static Logger _theLogger = Logger.getLogger(StatefullSAXHandler.class);
42
/////////////////////////////////////////////////////////////////////////////////////////
43
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
44
/////////////////////////////////////////////////////////////////////////////////////////
45

46   /** The context of this handler. */
47   private HandlerContextIF _theContext;
48
49   /** The locator of this parser. */
50   private Locator JavaDoc _theLocator;
51
52   /////////////////////////////////////////////////////////////////////////////////////////
53
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
54
/////////////////////////////////////////////////////////////////////////////////////////
55

56   /**
57    * Creates a new StatefullSAXHandler instance with the argument passed in.
58    *
59    * @param aContext The handler context.
60    */

61   public StatefullSAXHandler(HandlerContextIF aHandlerContext) {
62     _theContext = aHandlerContext;
63   }
64
65   /////////////////////////////////////////////////////////////////////////////////////////
66
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
67
/////////////////////////////////////////////////////////////////////////////////////////
68

69   /**
70    * Returns a textual description of the current document position with the following
71    * format: "<CODE>Document position: line number=x column number=y</CODE>".
72    *
73    * @return A textual description of the current document position.
74    */

75   public String JavaDoc getDocumentPosition() {
76     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
77     aBuffer.append("Document position: line number=")
78            .append(_theLocator.getLineNumber()).append(" column number=")
79            .append(_theLocator.getColumnNumber());
80
81     return aBuffer.toString();
82   }
83
84   /////////////////////////////////////////////////////////////////////////////////////////
85
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
86
/////////////////////////////////////////////////////////////////////////////////////////
87

88   /**
89    * Overrides the default implementation of the ContentHandler interface and allow the
90    * application to resolve external entities.
91    *
92    * @param aPublicID The public identifier of the external entity being referenced, or null if none was supplied.
93    * @param aSystemID The system identifier of the external entity being referenced.
94    */

95   public InputSource JavaDoc resolveEntity(String JavaDoc aPublicID, String JavaDoc aSystemID)
96     throws SAXException JavaDoc {
97     // Logging a debug message
98

99     /*if (_theLogger.isDebugEnabled()) {
100       StringBuffer aBuffer = new StringBuffer("Method resolveEntity() is beign called with");
101       aBuffer.append(" publicID=").append(aPublicID).
102               append(" systemID=").append(aSystemID);
103       _theLogger.debug(aBuffer.toString());
104     }*/

105     return null;
106   }
107
108   /**
109    * Overrides the default implementation of the ContentHandler interface and receive
110    * notification of a notation declaration event.
111    *
112    * @param aName The notation name.
113    * @param aPublicId The notation's public identifier, or null if none was given.
114    * @param aSystemId The notation's system identifier, or null if none was given.
115    */

116   public void notationDecl(String JavaDoc aName, String JavaDoc aPublicID, String JavaDoc aSystemID)
117     throws SAXException JavaDoc {
118     // Logging a debug message
119

120     /*if (_theLogger.isDebugEnabled()) {
121       StringBuffer aBuffer = new StringBuffer("Method notationDecl() is beign called with");
122       aBuffer.append(" name=").append(aName).
123               append(" publicID=").append(aPublicID).
124               append(" systemID=").append(aSystemID);
125       _theLogger.debug(aBuffer.toString());
126     }*/

127   }
128
129   /**
130    * Overrides the default implementation of the ContentHandler interface and receive
131    * notification of an unparsed entity declaration event.
132    *
133    * @param aName The unparsed entity name.
134    * @param aPublicId The notation's public identifier, or null if none was given.
135    * @param aSystemId The notation's system identifier, or null if none was given.
136    * @param aNotationName The name of the associated notation.
137    */

138   public void unparsedEntityDecl(String JavaDoc aName, String JavaDoc aPublicID,
139     String JavaDoc aSystemID, String JavaDoc aNotationName) throws SAXException JavaDoc {
140     // Logging a debug message
141

142     /*if (_theLogger.isDebugEnabled()) {
143       StringBuffer aBuffer = new StringBuffer("Method unparsedEntityDecl() is beign called with");
144       aBuffer.append(" name=").append(aName).
145               append(" publicID=").append(aPublicID).
146               append(" systemID=").append(aSystemID).
147               append(" notationName=").append(aNotationName);
148       _theLogger.debug(aBuffer.toString());
149     }*/

150   }
151
152   /**
153    * Overrides the default implementation of the ContentHandler interface and receives
154    * the Locator object for document positions.
155    *
156    * @param aLocator A locator for all SAX document events.
157    * @see org.xml.sax.ContentHandler#setDocumentLocator
158    */

159   public void setDocumentLocator(Locator JavaDoc aLocator) {
160     _theLocator = aLocator;
161   }
162
163   /**
164    * Overrides the default implementation of the ContentHandler interface and forwards the
165    * notification of the beginning of the document to the current HandleStateIF object of
166    * this parser.
167    *
168    * @exception SAXException Any SAX exception, possibly wrapping another exception.
169    * @see org.xml.sax.ContentHandler#startDocument
170    */

171   public void startDocument() throws SAXException JavaDoc {
172     // Logging a debug message
173

174     /*if (_theLogger.isDebugEnabled()) {
175       _theLogger.debug("Method startDocument() is beign called");
176     }*/

177   }
178
179   /**
180    * Overrides the default implementation of the ContentHandler interface and forwards the
181    * notification of the end of the document to the current HandleStateIF object of
182    * this parser.
183    *
184    * @exception SAXException Any SAX exception, possibly wrapping another exception.
185    * @see org.xml.sax.ContentHandler#endDocument
186    */

187   public void endDocument() throws SAXException JavaDoc {
188     // Logging a debug message
189

190     /*if (_theLogger.isDebugEnabled()) {
191       _theLogger.debug("Method endDocument() is beign called");
192     }*/

193   }
194
195   /**
196    * Overrides the default implementation of the ContentHandler interface and sets the
197    * namespace URI passed in as the current mapping for the given prefix.
198    *
199    * @param aPrefix The Namespace prefix being declared.
200    * @param anUri The Namespace URI mapped to the prefix.
201    * @exception SAXException Any SAX exception, possibly wrapping another exception.
202    */

203   public void startPrefixMapping(String JavaDoc aPrefix, String JavaDoc anUri)
204     throws SAXException JavaDoc {
205     // Logging an info message
206

207     /*if (_theLogger.isDebugEnabled()) {
208       _theLogger.debug("Starts mapping of the prefix [" + aPrefix +
209               "] to the namespace URI [" + anUri + "].");
210     }*/

211     try {
212       _theContext.startPrefixMapping(aPrefix, anUri);
213     } catch (RuntimeException JavaDoc re) {
214       final String JavaDoc aMessage = "RuntimeException caugh in method startPrefixMapping().";
215
216       //_theLogger.error(aMessage, re);
217
throw new SAXException JavaDoc(aMessage, re);
218     }
219   }
220
221   /**
222    * Overrides the default implementation of the ContentHandler interface and removes
223    * the current mapping of the prefix passed in.
224    *
225    * @param aPrefix The Namespace prefix being declared.
226    * @exception SAXException Any SAX exception, possibly wrapping another exception.
227    * @see org.xml.sax.ContentHandler#endPrefixMapping
228    */

229   public void endPrefixMapping(String JavaDoc aPrefix) throws SAXException JavaDoc {
230     // Logging an info message
231

232     /*if (_theLogger.isDebugEnabled()) {
233       _theLogger.debug("Ends mapping of the prefix [" + aPrefix + "].");
234     }*/

235     try {
236       _theContext.endPrefixMapping(aPrefix);
237     } catch (RuntimeException JavaDoc re) {
238       final String JavaDoc aMessage = "RuntimeException caugh in method endPrefixMapping().";
239
240       //_theLogger.error(aMessage, re);
241
throw new SAXException JavaDoc(aMessage, re);
242     }
243   }
244
245   /**
246    * Overrides the default implementation of the ContentHandler interface and forwards
247    * the notification of the the start of an element to the current HandleStateIF
248    * object of this parser.
249    *
250    * @param anUri The namespace URI associated with the element
251    * @param aLocalName The element type local name.
252    * @param aQualifiedName The element type qualified name.
253    * @param someAttributes The specified or defaulted attributes.
254    * @exception SAXException Any SAX exception, possibly wrapping another exception.
255    * @see org.xml.sax.ContentHandler#startElement
256    */

257   public void startElement(String JavaDoc anUri, String JavaDoc aLocalName,
258     String JavaDoc aQualifiedName, Attributes JavaDoc someAttributes) throws SAXException JavaDoc {
259     try {
260       _theContext.getCurrentState().startElement(_theContext, anUri,
261         aLocalName, aQualifiedName, someAttributes);
262     } catch (RuntimeException JavaDoc re) {
263       final String JavaDoc aMessage = "RuntimeException caugh in method startElement().";
264
265       //_theLogger.error(aMessage, re);
266
throw new SAXException JavaDoc(aMessage, re);
267     }
268   }
269
270   /**
271    * Overrides the default implementation of the ContentHandler interface and forwards
272    * the notification of the the end of an element to the current HandleStateIF
273    * object of this parser.
274    *
275    * @param anUri The namespace URI associated with the element
276    * @param aLocalName The element type local name.
277    * @param aQualifiedName The element type qualified name.
278    * @param someAttributes The specified or defaulted attributes.
279    * @exception SAXException Any SAX exception, possibly wrapping another exception.
280    * @see org.xml.sax.ContentHandler#endElement
281    */

282   public void endElement(String JavaDoc anUri, String JavaDoc aLocalName, String JavaDoc aQualifiedName)
283     throws SAXException JavaDoc {
284     try {
285       _theContext.getCurrentState().endElement(_theContext, anUri, aLocalName,
286         aQualifiedName);
287     } catch (RuntimeException JavaDoc re) {
288       final String JavaDoc aMessage = "RuntimeException caugh in method endElement().";
289
290       //_theLogger.error(aMessage, re);
291
throw new SAXException JavaDoc(aMessage, re);
292     }
293   }
294
295   /**
296    * Overrides the default implementation of the ContentHandler interface and forwards
297    * the notification of character data inside an element to the current HandleStateIF
298    * object of this parser.
299    *
300    * @param someChars The characters.
301    * @param anOffset The start position in the character array.
302    * @param aLength The number of characters to use from the character array.
303    * @exception SAXException Any SAX exception, possibly wrapping another exception.
304    * @see org.xml.sax.ContentHandler#characters
305    */

306   public void characters(char[] someChars, int anOffset, int aLength)
307     throws SAXException JavaDoc {
308     try {
309       _theContext.getCurrentState().characters(_theContext, someChars,
310         anOffset, aLength);
311     } catch (RuntimeException JavaDoc re) {
312       final String JavaDoc aMessage = "RuntimeException caugh in method character().";
313
314       //_theLogger.error(aMessage, re);
315
throw new SAXException JavaDoc(aMessage, re);
316     }
317   }
318
319   /**
320    * Overrides the default implementation of the ContentHandler interface and forwards
321    * the notification of ignorable whitespace in element content to the current
322    * HandleStateIF object of this parser.
323    *
324    * @param someChars The whitespace characters.
325    * @param anOffset The start position in the character array.
326    * @param aLength The number of characters to use from the character array.
327    * @exception SAXException Any SAX exception, possibly wrapping another exception.
328    * @see org.xml.sax.ContentHandler#ignorableWhitespace
329    */

330   public void ignorableWhitespace(char[] someChars, int anOffset, int aLength)
331     throws SAXException JavaDoc {
332     try {
333       _theContext.getCurrentState().ignorableWhitespace(_theContext, someChars,
334         anOffset, aLength);
335     } catch (RuntimeException JavaDoc re) {
336       final String JavaDoc aMessage = "RuntimeException caugh in method ignorableWhitespace().";
337
338       //_theLogger.error(aMessage, re);
339
throw new SAXException JavaDoc(aMessage, re);
340     }
341   }
342
343   /**
344    * Overrides the default implementation of the ErrorHandler interface logs a warning
345    * message of the warning and the current document position of the parser.
346    *
347    * @param aWarning The warning information encoded as an exception.
348    * @exception SAXException Any SAX exception, possibly wrapping another exception.
349    * @see org.xml.sax.ErrorHandler#warning
350    */

351   public void warning(SAXParseException JavaDoc aWarning) throws SAXException JavaDoc {
352     //_theLogger.warn(getDocumentPosition(), aWarning);
353
}
354
355   /**
356    * Overrides the default implementation of the ErrorHandler interface logs a critical
357    * message of the error and the current document position of the parser.
358    *
359    * @param anError The error information encoded as an exception.
360    * @exception SAXException Any SAX exception, possibly wrapping another exception.
361    * @see org.xml.sax.ErrorHandler#warning
362    * @see org.xml.sax.SAXParseException
363    */

364   public void error(SAXParseException JavaDoc anError) throws SAXException JavaDoc {
365     //_theLogger.error(getDocumentPosition(), anError);
366
}
367
368   /**
369    * Overrides the default implementation of the ErrorHandler interface logs an error
370    * message of the fatal error and the current document position of the parser.
371    *
372    * @param aFatalError The fatal error information encoded as an exception.
373    * @exception SAXException Any SAX exception, possibly wrapping another exception.
374    * @see org.xml.sax.ErrorHandler#fatalError
375    * @see org.xml.sax.SAXParseException
376    */

377   public void fatalError(SAXParseException JavaDoc aFatalError)
378     throws SAXException JavaDoc {
379     //_theLogger.error(getDocumentPosition(), aFatalError);
380
throw aFatalError;
381   }
382 }
383
Popular Tags