1 19 20 package org.netbeans.modules.j2ee.dd.impl.common; 21 22 import java.io.IOException ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 import org.openide.filesystems.FileObject; 27 import org.openide.util.NbBundle; 28 import org.xml.sax.*; 29 30 31 38 public class ParseUtils { 39 40 public static final String EXCEPTION_PREFIX="version:"; 42 44 public static String getVersion(java.io.InputStream is, org.xml.sax.helpers.DefaultHandler versionHandler, 45 EntityResolver ddResolver) throws java.io.IOException , SAXException { 46 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 47 fact.setValidating(false); 48 try { 49 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 50 XMLReader reader = parser.getXMLReader(); 51 reader.setContentHandler(versionHandler); 52 reader.setEntityResolver(ddResolver); 53 try { 54 reader.parse(new InputSource(is)); 55 } catch (SAXException ex) { 56 is.close(); 57 String message = ex.getMessage(); 58 if (message!=null && message.startsWith(EXCEPTION_PREFIX)) 59 return message.substring(EXCEPTION_PREFIX.length()); 60 else throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"),ex); 61 } 62 is.close(); 63 throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot")); 64 } catch(javax.xml.parsers.ParserConfigurationException ex) { 65 throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_parserProblem"),ex); 66 } 67 } 68 69 71 public static String getVersion(InputSource is, org.xml.sax.helpers.DefaultHandler versionHandler, 72 EntityResolver ddResolver) throws IOException , SAXException { 73 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 74 fact.setValidating(false); 75 try { 76 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 77 XMLReader reader = parser.getXMLReader(); 78 reader.setContentHandler(versionHandler); 79 reader.setEntityResolver(ddResolver); 80 try { 81 reader.parse(is); 82 } catch (SAXException ex) { 83 String message = ex.getMessage(); 84 if (message!=null && message.startsWith(EXCEPTION_PREFIX)) 85 return message.substring(EXCEPTION_PREFIX.length()); 86 else throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"),ex); 87 } 88 throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot")); 89 } catch(javax.xml.parsers.ParserConfigurationException ex) { 90 throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_parserProblem"),ex); 91 } 92 } 93 94 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 95 private int errorType=-1; 96 SAXParseException error; 97 98 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 99 if (errorType<0) { 100 errorType=0; 101 error=sAXParseException; 102 } 103 } 105 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 106 if (errorType<1) { 107 errorType=1; 108 error=sAXParseException; 109 } 110 } 112 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 113 errorType=2; 114 throw sAXParseException; 115 } 116 117 public int getErrorType() { 118 return errorType; 119 } 120 public SAXParseException getError() { 121 return error; 122 } 123 } 124 125 public static SAXParseException parseDD(InputSource is, EntityResolver ddResolver) 126 throws org.xml.sax.SAXException , java.io.IOException { 127 ErrorHandler errorHandler = new ErrorHandler(); 128 try { 129 SAXParser parser = createSAXParserFactory().newSAXParser(); 130 XMLReader reader = parser.getXMLReader(); 131 reader.setErrorHandler(errorHandler); 132 reader.setEntityResolver(ddResolver); 133 reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.parse(is); 137 SAXParseException error = errorHandler.getError(); 138 if (error!=null) return error; 139 } catch (ParserConfigurationException ex) { 140 SAXException sax = new SAXException(ex.getMessage(), ex); 141 sax.initCause(ex); 142 throw sax; 143 } catch (SAXException ex) { 144 throw ex; 145 } catch (IllegalArgumentException ex) { 146 SAXException sax = new SAXException(ex.getMessage(), ex); 148 sax.initCause(ex); 149 throw sax; 150 } 151 return null; 152 } 153 154 156 private static SAXParserFactory createSAXParserFactory() throws ParserConfigurationException { 157 try { 158 SAXParserFactory fact = SAXParserFactory.newInstance(); 159 if (fact!=null) { 160 try { 161 fact.getClass().getMethod("getSchema", new Class []{}); return fact; 163 } catch (NoSuchMethodException ex) {} 164 } 165 return (SAXParserFactory ) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); } catch (Exception ex) { 167 throw new ParserConfigurationException (ex.getMessage()); 168 } 169 } 170 171 172 } 173 | Popular Tags |