1 19 20 package org.netbeans.modules.j2ee.dd.impl.web; 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.netbeans.modules.j2ee.dd.api.web.DDProvider; 27 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 28 import org.netbeans.modules.j2ee.dd.impl.common.ParseUtils; 29 import org.openide.filesystems.FileObject; 30 import org.openide.util.NbBundle; 31 import org.xml.sax.*; 32 33 34 41 public class WebParseUtils { 42 43 44 46 public static String getVersion(java.io.InputStream is) throws java.io.IOException , SAXException { 47 return ParseUtils.getVersion(is, new VersionHandler(), DDResolver.getInstance()); 48 } 49 50 52 public static String getVersion(InputSource is) throws IOException , SAXException { 53 return ParseUtils.getVersion(is, new VersionHandler(), DDResolver.getInstance()); 54 } 55 56 private static class VersionHandler extends org.xml.sax.helpers.DefaultHandler { 57 public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException { 58 if ("web-app".equals(rawName)) { String version = atts.getValue("version"); throw new SAXException(ParseUtils.EXCEPTION_PREFIX+(version==null?WebApp.VERSION_2_3:version)); 61 } 62 } 63 } 64 65 private static class DDResolver implements EntityResolver { 66 static DDResolver resolver; 67 static synchronized DDResolver getInstance() { 68 if (resolver==null) { 69 resolver=new DDResolver(); 70 } 71 return resolver; 72 } 73 public InputSource resolveEntity(String publicId, String systemId) { 74 String resource=null; 75 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")) { 81 resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_4.xsd"; } else if (systemId!=null && systemId.endsWith("web-app_2_5.xsd")) { 83 resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_5.xsd"; } 85 if (resource==null) return null; 86 java.net.URL url = this.getClass().getResource(resource); 87 return new InputSource(url.toString()); 88 } 89 } 90 91 public static SAXParseException parse(FileObject fo) 92 throws org.xml.sax.SAXException , java.io.IOException { 93 return parse(new InputSource(fo.getInputStream())); 95 } 96 97 public static SAXParseException parse (InputSource is) 98 throws org.xml.sax.SAXException , java.io.IOException { 99 return ParseUtils.parseDD(is, DDResolver.getInstance()); 100 } 101 102 103 } 104 | Popular Tags |