1 11 package org.eclipse.ant.internal.ui.dtd; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.eclipse.ant.internal.ui.dtd.schema.SchemaFactory; 22 import org.xml.sax.EntityResolver ; 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.SAXNotRecognizedException ; 26 import org.xml.sax.SAXNotSupportedException ; 27 import org.xml.sax.XMLReader ; 28 import org.xml.sax.ext.DeclHandler ; 29 30 31 46 public class Parser { 47 48 49 public static final String NOT_SUPPORTED = AntDTDMessages.Parser_XML_parser_does_not_support_DeclHandler_1; 50 52 public static final String PARSE_ERROR = AntDTDMessages.Parser_Error_parsing_XML_document_or_DTD_2; 53 54 private static final String INTERNAL = "internal://usereader.objfac.com"; 56 67 public ISchema parse(InputSource inputSource, EntityResolver entityResolver) throws ParseError, IOException { 68 XMLReader parser = null; 69 SchemaFactory factory = new SchemaFactory(); 70 try { 71 parser = getXMLReader(); 72 DeclHandler handler = factory; 73 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); if (entityResolver != null) { 75 parser.setEntityResolver(entityResolver); 76 } 77 parser.parse(inputSource); 78 } catch (SAXNotRecognizedException e) { 79 throw new ParseError(NOT_SUPPORTED); 80 } catch (SAXNotSupportedException e) { 81 throw new ParseError(NOT_SUPPORTED); 82 } catch (SAXException e) { 83 factory.setErrorException(e); 87 } 88 89 return factory.getSchema(); 90 } 91 92 private XMLReader getXMLReader() throws ParseError { 93 SAXParser parser = null; 94 try { 95 parser = SAXParserFactory.newInstance().newSAXParser(); 96 return parser.getXMLReader(); 97 } catch (ParserConfigurationException e) { 98 throw new ParseError(e.getMessage()); 99 } catch (SAXException e) { 100 throw new ParseError(e.getMessage()); 101 } 102 } 103 104 114 public ISchema parse(String url) throws ParseError, IOException { 115 return parse(new InputSource (url), null); 116 } 117 118 128 public ISchema parse(Reader reader) throws ParseError, IOException { 129 return parse(new InputSource (reader), null); 130 } 131 132 144 public ISchema parseDTD(String pub, String sys, String root) throws ParseError, IOException { 145 return parse(new InputSource (new DTDReader(pub, sys, root)), null); 146 } 147 148 159 public ISchema parseDTD(Reader reader, String root) throws ParseError, IOException { 160 return parse(new InputSource (new DTDReader(INTERNAL, INTERNAL, root)), new DTDEntityResolver(reader)); 161 } 162 163 private static class DTDReader extends Reader { 164 private Reader fDelegate; 165 166 public DTDReader(String pub, String sys, String root) { 167 String document = "<!DOCTYPE "+root+" PUBLIC '"+pub+"' '"+sys+"'><"+root+"/>"; fDelegate = new StringReader (document); 169 } 170 171 174 public void close() throws IOException { 175 fDelegate.close(); 176 } 177 178 181 public int read(char[] cbuf, int off, int len) throws IOException { 182 return fDelegate.read(cbuf, off, len); 183 } 184 } 185 186 private static class DTDEntityResolver implements EntityResolver { 187 private Reader reader; 188 public DTDEntityResolver(Reader reader) { 189 this.reader = reader; 190 } 191 194 public InputSource resolveEntity(String publicId, String systemId) { 195 if (publicId.equals(INTERNAL) && systemId.equals(INTERNAL)) 196 return new InputSource (reader); 197 return null; 198 } 199 } 200 } | Popular Tags |