package org.jboss.mq.xml;
import java.util.Vector;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class XElementProducer {
private XElementConsumer consumer;
private Vector targetRecords = new Vector();
private Handler handler = new Handler();
private Exception thrownError = null;
public XElementProducer( XElementConsumer consumerObject ) {
consumer = consumerObject;
}
public void addElementRecord( String name ) {
targetRecords.addElement( name );
}
public void clearElementRecords() {
targetRecords.removeAllElements();
}
public void parse( java.io.InputStream is )
throws Exception {
if ( consumer == null ) {
throw new NullPointerException();
}
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
if ( consumer instanceof org.xml.sax.ErrorHandler ) {
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler( ( org.xml.sax.ErrorHandler )consumer );
}
thrownError = null;
parser.parse( new InputSource( is ), handler );
} catch ( SAXException e ) {
if ( thrownError != null ) {
throw thrownError;
} else {
throw e;
}
}
}
public void parse( java.net.URL url )
throws Exception {
if ( consumer == null ) {
throw new NullPointerException();
}
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
if ( consumer instanceof org.xml.sax.ErrorHandler ) {
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler( ( org.xml.sax.ErrorHandler )consumer );
}
thrownError = null;
parser.parse( url.toExternalForm(), handler );
} catch ( SAXException e ) {
if ( thrownError != null ) {
throw thrownError;
} else {
throw e;
}
}
}
class Handler extends DefaultHandler {
private XElement currentXElement;
public void startDocument()
throws SAXException {
try {
consumer.documentStartEvent();
} catch ( Exception e ) {
thrownError = e;
throw new SAXException( e.toString() );
}
}
public void endDocument()
throws SAXException {
try {
consumer.documentEndEvent();
} catch ( Exception e ) {
thrownError = e;
throw new SAXException( e.toString() );
}
}
public void startElement( String uri, String localName, String qname, Attributes atts )
throws SAXException {
if ( currentXElement != null ) {
XElement o = new XElement( qname, atts );
currentXElement.addElement( o );
currentXElement = o;
} else {
if ( targetRecords.size() == 0 ) {
currentXElement = new XElement( qname, atts );
} else {
for ( int i = 0; i < targetRecords.size(); i++ ) {
if ( qname.equals( targetRecords.elementAt( i ) ) ) {
currentXElement = new XElement( qname, atts );
break;
}
}
}
}
}
public void endElement( String uri, String localName, String qName )
throws SAXException {
if ( currentXElement != null ) {
if ( !qName.equals( currentXElement.getName() ) ) {
throw new SAXException( "XElement parsing sanitity check failed" );
}
XElement t = currentXElement;
currentXElement = currentXElement.getParent();
if ( currentXElement == null ) {
try {
consumer.recordReadEvent( t );
} catch ( Exception e ) {
thrownError = e;
throw new SAXException( e.toString() );
}
}
}
}
public void characters( char[] chars, int start, int length ) {
if ( length == 0 ) {
return;
}
if ( currentXElement != null ) {
currentXElement.add( new String( chars, start, length ) );
}
}
}
}