KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.util.xml.confix;
2
3
4 // Import of Sapia's Utility classes
5
// ---------------------------------
6
import org.sapia.util.xml.ProcessingException;
7 import org.sapia.util.xml.parser.DefaultHandlerContext;
8 import org.sapia.util.xml.parser.DelegateHandlerContext;
9 import org.sapia.util.xml.parser.HandlerContextIF;
10 import org.sapia.util.xml.parser.StatefullSAXHandler;
11
12 // Imports of David Meggison's SAX classes
13
// ---------------------------------------
14
import org.xml.sax.SAXException JavaDoc;
15
16 import java.io.IOException JavaDoc;
17
18 // Import of Sun's JDK classes
19
// ---------------------------
20
import java.io.InputStream JavaDoc;
21
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24
25
26 /**
27  * An instance of this class receives SAX events and delegates object creation
28  * to an <code>ObjectFactoryIF</code>.
29  *
30  * @author JC Desrochers
31  *
32  * <dl>
33  * <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>
34  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
35  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
36  * </dl>
37  */

38 public class SAXProcessor extends AbstractXMLProcessor {
39   /////////////////////////////////////////////////////////////////////////////////////////
40
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
41
/////////////////////////////////////////////////////////////////////////////////////////
42

43   /** Initialization of the SAX parser factory instance. */
44   private static SAXParserFactory JavaDoc _theSAXParserFactory;
45
46   static {
47     _theSAXParserFactory = SAXParserFactory.newInstance();
48     _theSAXParserFactory.setNamespaceAware(true);
49     _theSAXParserFactory.setValidating(false);
50   }
51
52   /////////////////////////////////////////////////////////////////////////////////////////
53
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
54
/////////////////////////////////////////////////////////////////////////////////////////
55

56   /**
57    * Creates a new SAXProcessor instance with the attribute passed in.
58    *
59    * @param anObjectFactory The object factory of this processor.
60    */

61   public SAXProcessor(ObjectFactoryIF anObjectFactory) {
62     super(anObjectFactory);
63   }
64
65   /////////////////////////////////////////////////////////////////////////////////////////
66
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
67
/////////////////////////////////////////////////////////////////////////////////////////
68

69   /**
70    * This method takes an XML stream as input and returns an object
71    * representation of the passed-in XML.
72    *
73    * @param is an XML stream
74    * @return an object representation of the XML stream.
75    * @exception ProcessingException
76    */

77   public Object JavaDoc process(InputStream JavaDoc is) throws ProcessingException {
78     try {
79       // Initialize the SAX handler
80
ConfixHandlerState aHandlerState = new ConfixHandlerState(this);
81       HandlerContextIF aHandlerContext = new DefaultHandlerContext(new DelegateHandlerContext(
82             aHandlerState));
83       StatefullSAXHandler aSAXHandler = new StatefullSAXHandler(aHandlerContext);
84
85       // Parse the input stream and get the result
86
_theSAXParserFactory.newSAXParser().parse(is, aSAXHandler);
87
88       Object JavaDoc aResult = aHandlerState.getResult();
89
90       return aResult;
91     } catch (ParserConfigurationException JavaDoc pce) {
92       String JavaDoc aMessage = "Error getting the SAX parser to process the input stream";
93
94       throw new ProcessingException(aMessage, pce);
95     } catch (IOException JavaDoc ioe) {
96       String JavaDoc aMessage = "Error reading input stream to process";
97
98       throw new ProcessingException(aMessage, ioe);
99     } catch (SAXException JavaDoc se) {
100       String JavaDoc aMessage = "Error parsing the XML of the input stream.";
101
102       throw new ProcessingException(aMessage, se);
103     } finally {
104       try {
105         if (is != null) {
106           is.close();
107         }
108       } catch (IOException JavaDoc ioe) {
109         String JavaDoc aMessage = "Error closing the input stream to process.";
110
111         throw new ProcessingException(aMessage, ioe);
112       }
113     }
114   }
115 }
116
Popular Tags