1 16 package org.apache.cocoon.components.parser; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.ComponentException; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.component.Composable; 22 import org.apache.cocoon.components.resolver.Resolver; 23 import org.apache.cocoon.xml.AbstractXMLProducer; 24 import org.apache.xerces.dom.DocumentImpl; 25 import org.apache.xerces.dom.DocumentTypeImpl; 26 import org.apache.xerces.parsers.DOMParser; 27 import org.apache.xerces.parsers.SAXParser; 28 import org.w3c.dom.Document ; 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 34 import java.io.IOException ; 35 36 45 public class XercesParser extends AbstractXMLProducer 46 implements Parser, ErrorHandler , Composable, Disposable { 47 48 49 final SAXParser parser; 50 51 52 protected ComponentManager manager; 53 54 55 protected Resolver resolver = null; 56 57 public XercesParser () 58 throws SAXException { 59 this.parser = new SAXParser(); 60 61 this.parser.setFeature("http://xml.org/sax/features/validation", false); 62 this.parser.setFeature("http://xml.org/sax/features/namespaces", true); 63 } 64 65 68 public void compose(ComponentManager manager) throws ComponentException { 69 this.manager = manager; 70 if (getLogger().isDebugEnabled()) { 71 getLogger().debug("Looking up " + Resolver.ROLE); 72 } 73 if ( manager.hasComponent( Resolver.ROLE ) ) { 74 this.resolver = (Resolver)manager.lookup(Resolver.ROLE); 75 } 76 } 77 78 81 public void dispose() { 82 if (this.manager != null) { 83 this.manager.release( this.resolver ); 84 } 85 } 86 87 public void parse(InputSource in) 88 throws SAXException , IOException { 89 this.parser.setProperty("http://xml.org/sax/properties/lexical-handler", 90 super.lexicalHandler); 91 this.parser.setErrorHandler(this); 92 this.parser.setContentHandler(super.contentHandler); 93 if(this.resolver != null) 94 this.parser.setEntityResolver(this.resolver); 95 this.parser.parse(in); 96 } 97 98 101 public Document newDocument() { 102 return(newDocument(null,null,null)); 103 } 104 105 108 public Document newDocument(String name) { 109 return(newDocument(name,null,null)); 110 } 111 112 116 public Document newDocument(String name, String pub, String sys) { 117 DocumentImpl doc=new DocumentImpl(); 118 if ((pub!=null)||(sys!=null)) { 119 DocumentTypeImpl dtd=new DocumentTypeImpl(doc,name,pub,sys); 120 doc.appendChild(dtd); 121 } else if (name!=null) { 122 DocumentTypeImpl dtd=new DocumentTypeImpl(doc,name); 123 doc.appendChild(dtd); 124 } 125 return(doc); 126 } 127 128 131 public Document parseDocument(InputSource input) throws SAXException , IOException { 132 DOMParser parser = null; 133 134 try { 135 parser = new DOMParser(); 136 137 parser.setFeature("http://xml.org/sax/features/validation",false); 138 parser.setFeature("http://xml.org/sax/features/namespaces",true); 139 140 parser.parse(input); 141 } catch (Exception pce) { 142 getLogger().error("Could not build DocumentBuilder", pce); 143 return null; 144 } 145 146 return parser.getDocument(); 147 } 148 149 152 public void error(SAXParseException e) 153 throws SAXException { 154 throw new SAXException ("Error parsing "+e.getSystemId()+" (line "+ 155 e.getLineNumber()+" col. "+e.getColumnNumber()+ 156 "): "+e.getMessage(),e); 157 } 158 159 162 public void fatalError(SAXParseException e) 163 throws SAXException { 164 throw new SAXException ("Fatal error parsing "+e.getSystemId()+" (line "+ 165 e.getLineNumber()+" col. "+e.getColumnNumber()+ 166 "): "+e.getMessage(),e); 167 } 168 169 172 public void warning(SAXParseException e) 173 throws SAXException { 174 throw new SAXException ("Warning parsing "+e.getSystemId()+" (line "+ 175 e.getLineNumber()+" col. "+e.getColumnNumber()+ 176 "): "+e.getMessage(),e); 177 } 178 } 179 | Popular Tags |