1 package org.sapia.util.xml.confix; 2 3 4 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 import org.xml.sax.SAXException ; 15 16 import java.io.IOException ; 17 18 import java.io.InputStream ; 21 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.xml.parsers.SAXParserFactory ; 24 25 26 38 public class SAXProcessor extends AbstractXMLProcessor { 39 43 44 private static SAXParserFactory _theSAXParserFactory; 45 46 static { 47 _theSAXParserFactory = SAXParserFactory.newInstance(); 48 _theSAXParserFactory.setNamespaceAware(true); 49 _theSAXParserFactory.setValidating(false); 50 } 51 52 56 61 public SAXProcessor(ObjectFactoryIF anObjectFactory) { 62 super(anObjectFactory); 63 } 64 65 69 77 public Object process(InputStream is) throws ProcessingException { 78 try { 79 ConfixHandlerState aHandlerState = new ConfixHandlerState(this); 81 HandlerContextIF aHandlerContext = new DefaultHandlerContext(new DelegateHandlerContext( 82 aHandlerState)); 83 StatefullSAXHandler aSAXHandler = new StatefullSAXHandler(aHandlerContext); 84 85 _theSAXParserFactory.newSAXParser().parse(is, aSAXHandler); 87 88 Object aResult = aHandlerState.getResult(); 89 90 return aResult; 91 } catch (ParserConfigurationException pce) { 92 String aMessage = "Error getting the SAX parser to process the input stream"; 93 94 throw new ProcessingException(aMessage, pce); 95 } catch (IOException ioe) { 96 String aMessage = "Error reading input stream to process"; 97 98 throw new ProcessingException(aMessage, ioe); 99 } catch (SAXException se) { 100 String 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 ioe) { 109 String aMessage = "Error closing the input stream to process."; 110 111 throw new ProcessingException(aMessage, ioe); 112 } 113 } 114 } 115 } 116 | Popular Tags |