1 19 package org.netbeans.modules.j2ee.websphere6.dd.loaders; 20 21 import javax.xml.parsers.ParserConfigurationException ; 22 import javax.xml.parsers.SAXParser ; 23 import javax.xml.parsers.SAXParserFactory ; 24 import org.openide.filesystems.FileObject; 26 27 import org.netbeans.modules.schema2beans.BaseBean; 28 29 33 import java.io.*; 34 import org.xml.sax.*; 35 37 38 43 public class DDUtils { 44 45 private static final String EXCEPTION_PREFIX="version"; 47 55 56 57 58 60 public static String getVersion(InputSource is) throws IOException, SAXException { 61 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 62 fact.setValidating(false); 63 try { 64 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 65 XMLReader reader = parser.getXMLReader(); 66 reader.setContentHandler(new VersionHandler()); 67 reader.setEntityResolver(DDResolver.getInstance()); 68 try { 69 reader.parse(is); 70 } catch (SAXException ex) { 71 String message = ex.getMessage(); 72 if (message!=null && message.startsWith(EXCEPTION_PREFIX)) 73 return message.substring(EXCEPTION_PREFIX.length()); 74 else throw new SAXException(org.openide.util.NbBundle.getMessage(DDUtils.class, "MSG_cannotParse"),ex); 75 } 76 throw new SAXException(org.openide.util.NbBundle.getMessage(DDUtils.class, "MSG_cannotFindRoot")); 77 } catch(javax.xml.parsers.ParserConfigurationException ex) { 78 throw new SAXException(org.openide.util.NbBundle.getMessage(DDUtils.class, "MSG_parserProblem"),ex); 79 } 80 } 81 82 private static class VersionHandler extends org.xml.sax.helpers.DefaultHandler { 83 public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException { 84 if ("WebAppBinging".equals(rawName)) { String version = atts.getValue("version"); throw new SAXException(EXCEPTION_PREFIX); 87 } 88 } 89 } 90 91 private static class DDResolver implements EntityResolver { 92 static DDResolver resolver; 93 static synchronized DDResolver getInstance() { 94 if (resolver==null) { 95 resolver=new DDResolver(); 96 } 97 return resolver; 98 } 99 public InputSource resolveEntity (String publicId, String systemId) { 100 String resource=null; 101 if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId)) { resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_3.dtd"; } else if ("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_2.dtd"; } else if (systemId!=null && systemId.endsWith("web-app_2_4.xsd")) { 107 resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_4.xsd"; } 109 if (resource==null) return null; 110 java.net.URL url = this.getClass().getResource(resource); 111 return new InputSource(url.toString()); 112 } 113 } 114 115 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 116 private int errorType=-1; 117 SAXParseException error; 118 119 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 120 if (errorType<0) { 121 errorType=0; 122 error=sAXParseException; 123 } 124 } 126 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 127 if (errorType<1) { 128 errorType=1; 129 error=sAXParseException; 130 } 131 } 133 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 134 errorType=2; 135 throw sAXParseException; 136 } 137 138 public int getErrorType() { 139 return errorType; 140 } 141 public SAXParseException getError() { 142 return error; 143 } 144 } 145 146 public static SAXParseException parse (InputSource is) 147 throws org.xml.sax.SAXException , java.io.IOException { 148 ErrorHandler errorHandler = new ErrorHandler(); 149 try { 150 SAXParser parser = createSAXParserFactory().newSAXParser(); 151 XMLReader reader = parser.getXMLReader(); 152 reader.setErrorHandler(errorHandler); 153 reader.parse(is); 158 SAXParseException error = errorHandler.getError(); 159 if (error!=null) return error; 160 } catch (ParserConfigurationException ex) { 161 throw new SAXException(ex.getMessage()); 162 } catch (SAXException ex) { 163 throw ex; 164 } 165 return null; 166 } 167 168 170 private static SAXParserFactory createSAXParserFactory() throws ParserConfigurationException { 171 try { 172 SAXParserFactory fact = SAXParserFactory.newInstance(); 173 if (fact!=null) { 174 try { 175 fact.getClass().getMethod("getSchema", new Class []{}); return fact; 177 } catch (NoSuchMethodException ex) {} 178 } 179 return (SAXParserFactory ) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); } catch (Exception ex) { 181 throw new ParserConfigurationException (ex.getMessage()); 182 } 183 } 184 } | Popular Tags |