1 17 package org.apache.excalibur.xml.impl; 18 19 import java.io.IOException ; 20 21 import org.apache.avalon.framework.activity.Initializable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 import org.apache.xerces.dom.DocumentImpl; 25 import org.apache.xerces.parsers.DOMParser; 26 import org.apache.xerces.parsers.SAXParser; 27 import org.w3c.dom.Document ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.ErrorHandler ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.ext.LexicalHandler ; 34 35 39 public final class XercesParser 40 extends AbstractLogEnabled 41 implements org.apache.excalibur.xml.sax.SAXParser, org.apache.excalibur.xml.dom.DOMParser, 42 ErrorHandler , ThreadSafe, Initializable 43 { 44 public void initialize() 45 throws Exception 46 { 47 final String message = 48 "WARNING: XercesParser has been deprecated in favour of " + 49 "JaxpParser. Please use JaxpParser unless it is incompatible" + 50 "with your environment"; 51 getLogger().warn( message ); 52 } 53 54 public void parse( final InputSource in, 55 final ContentHandler consumer ) 56 throws SAXException , IOException 57 { 58 if( consumer instanceof LexicalHandler ) 59 { 60 parse( in, consumer, (LexicalHandler )consumer ); 61 } 62 else 63 { 64 parse( in, consumer, null ); 65 } 66 } 67 68 73 public void parse( final InputSource in, 74 final ContentHandler contentHandler, 75 final LexicalHandler lexicalHandler ) 76 throws SAXException , IOException 77 { 78 final SAXParser parser = createSAXParser(); 79 80 if( null != lexicalHandler ) 81 { 82 parser.setProperty( "http://xml.org/sax/properties/lexical-handler", 83 lexicalHandler ); 84 } 85 parser.setErrorHandler( this ); 86 parser.setContentHandler( contentHandler ); 87 parser.parse( in ); 88 } 89 90 93 public Document parseDocument( final InputSource input ) 94 throws SAXException , IOException 95 { 96 try 97 { 98 final DOMParser parser = new DOMParser(); 99 parser.setFeature( "http://xml.org/sax/features/validation", false ); 100 parser.setFeature( "http://xml.org/sax/features/namespaces", true ); 101 parser.setFeature( "http://xml.org/sax/features/namespace-prefixes", 102 true ); 103 104 parser.parse( input ); 105 106 return parser.getDocument(); 107 } 108 catch( final Exception e ) 109 { 110 final String message = "Could not build DocumentBuilder"; 111 getLogger().error( message, e ); 112 return null; 113 } 114 } 115 116 119 public Document createDocument() 120 throws SAXException 121 { 122 return new DocumentImpl(); 123 } 124 125 128 public void error( final SAXParseException spe ) 129 throws SAXException 130 { 131 final String message = 132 "Error parsing " + spe.getSystemId() + " (line " + 133 spe.getLineNumber() + " col. " + spe.getColumnNumber() + 134 "): " + spe.getMessage(); 135 throw new SAXException ( message, spe ); 136 } 137 138 141 public void fatalError( final SAXParseException spe ) 142 throws SAXException 143 { 144 final String message = 145 "Fatal error parsing " + spe.getSystemId() + " (line " + 146 spe.getLineNumber() + " col. " + spe.getColumnNumber() + 147 "): " + spe.getMessage(); 148 throw new SAXException ( message, spe ); 149 } 150 151 154 public void warning( final SAXParseException spe ) 155 throws SAXException 156 { 157 final String message = 158 "Warning parsing " + spe.getSystemId() + " (line " + 159 spe.getLineNumber() + " col. " + spe.getColumnNumber() + 160 "): " + spe.getMessage(); 161 throw new SAXException ( message, spe ); 162 } 163 164 170 private SAXParser createSAXParser() 171 throws SAXException 172 { 173 final SAXParser parser = new SAXParser(); 174 parser.setFeature( "http://xml.org/sax/features/validation", false ); 175 parser.setFeature( "http://xml.org/sax/features/namespaces", true ); 176 parser.setFeature( "http://xml.org/sax/features/namespace-prefixes", 177 true ); 178 return parser; 179 } 180 } 181 | Popular Tags |