1 22 package org.jboss.mq.xml; 23 24 import java.util.Vector ; 25 import javax.xml.parsers.SAXParser ; 26 import javax.xml.parsers.SAXParserFactory ; 27 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.XMLReader ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 50 public class XElementProducer { 51 52 private XElementConsumer consumer; 53 private Vector targetRecords = new Vector (); 54 private Handler handler = new Handler (); 55 private Exception thrownError = null; 56 57 63 public XElementProducer( XElementConsumer consumerObject ) { 64 consumer = consumerObject; 65 } 66 67 73 public void addElementRecord( String name ) { 74 targetRecords.addElement( name ); 75 } 76 77 80 public void clearElementRecords() { 81 targetRecords.removeAllElements(); 82 } 83 84 92 public void parse( java.io.InputStream is ) 93 throws Exception { 94 if ( consumer == null ) { 95 throw new NullPointerException (); 96 } 97 try { 98 99 SAXParserFactory factory = SAXParserFactory.newInstance(); 100 SAXParser parser = factory.newSAXParser(); 101 102 if ( consumer instanceof org.xml.sax.ErrorHandler ) { 103 XMLReader reader = parser.getXMLReader(); 104 reader.setErrorHandler( ( org.xml.sax.ErrorHandler )consumer ); 105 } 106 thrownError = null; 107 parser.parse( new InputSource ( is ), handler ); 108 } catch ( SAXException e ) { 109 if ( thrownError != null ) { 110 throw thrownError; 111 } else { 112 throw e; 113 } 114 } 115 } 116 117 125 public void parse( java.net.URL url ) 126 throws Exception { 127 if ( consumer == null ) { 128 throw new NullPointerException (); 129 } 130 try { 131 SAXParserFactory factory = SAXParserFactory.newInstance(); 132 SAXParser parser = factory.newSAXParser(); 133 134 if ( consumer instanceof org.xml.sax.ErrorHandler ) { 135 XMLReader reader = parser.getXMLReader(); 136 reader.setErrorHandler( ( org.xml.sax.ErrorHandler )consumer ); 137 } 138 thrownError = null; 139 parser.parse( url.toExternalForm(), handler ); 140 } catch ( SAXException e ) { 141 if ( thrownError != null ) { 142 throw thrownError; 143 } else { 144 throw e; 145 } 146 } 147 } 148 149 176 class Handler extends DefaultHandler { 177 private XElement currentXElement; 178 179 public void startDocument() 180 throws SAXException { 181 try { 182 consumer.documentStartEvent(); 183 } catch ( Exception e ) { 184 thrownError = e; 185 throw new SAXException ( e.toString() ); 186 } 187 } 188 189 public void endDocument() 190 throws SAXException { 191 try { 192 consumer.documentEndEvent(); 193 } catch ( Exception e ) { 194 thrownError = e; 195 throw new SAXException ( e.toString() ); 196 } 197 } 198 199 public void startElement( String uri, String localName, String qname, Attributes atts ) 200 throws SAXException { 201 if ( currentXElement != null ) { 202 XElement o = new XElement( qname, atts ); 203 currentXElement.addElement( o ); 204 currentXElement = o; 205 } else { 206 if ( targetRecords.size() == 0 ) { 207 currentXElement = new XElement( qname, atts ); 208 } else { 209 for ( int i = 0; i < targetRecords.size(); i++ ) { 210 if ( qname.equals( targetRecords.elementAt( i ) ) ) { 211 currentXElement = new XElement( qname, atts ); 212 break; 213 } 214 } 215 } 216 } 217 } 218 219 public void endElement( String uri, String localName, String qName ) 220 throws SAXException { 221 if ( currentXElement != null ) { 222 if ( !qName.equals( currentXElement.getName() ) ) { 224 throw new SAXException ( "XElement parsing sanitity check failed" ); 225 } 226 XElement t = currentXElement; 227 currentXElement = currentXElement.getParent(); 228 if ( currentXElement == null ) { 229 try { 230 consumer.recordReadEvent( t ); 231 } catch ( Exception e ) { 232 thrownError = e; 233 throw new SAXException ( e.toString() ); 234 } 235 } 236 } 237 } 238 239 public void characters( char[] chars, int start, int length ) { 240 if ( length == 0 ) { 241 return; 242 } 243 if ( currentXElement != null ) { 244 currentXElement.add( new String ( chars, start, length ) ); 245 } 246 } 247 } 248 249 } 250 | Popular Tags |