KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > web > WebParseUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.dd.impl.web;
21
22 import java.io.IOException JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 /** Class that collects XML parsing utility methods for web applications. It is
35  * implementation private for this module, however it is also intended to be used by
36  * the DDLoaders modules, which requires tighter coupling with ddapi and has an
37  * implementation dependency on it.
38  *
39  * @author Petr Jiricka
40  */

41 public class WebParseUtils {
42   
43     
44     /** Parsing just for detecting the version SAX parser used
45      */

46     public static String JavaDoc getVersion(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc, SAXException {
47         return ParseUtils.getVersion(is, new VersionHandler(), DDResolver.getInstance());
48     }
49     
50     /** Parsing just for detecting the version SAX parser used
51     */

52     public static String JavaDoc getVersion(InputSource is) throws IOException JavaDoc, SAXException {
53         return ParseUtils.getVersion(is, new VersionHandler(), DDResolver.getInstance());
54     }
55     
56     private static class VersionHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
57         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes atts) throws SAXException {
58             if ("web-app".equals(rawName)) { //NOI18N
59
String JavaDoc version = atts.getValue("version"); //NOI18N
60
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 JavaDoc publicId, String JavaDoc systemId) {
74             String JavaDoc resource=null;
75             // return a proper input source
76
if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId)) { //NOI18N
77
resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_3.dtd"; //NOI18N
78
} else if ("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { //NOI18N
79
resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_2.dtd"; //NOI18N
80
} 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"; //NOI18N
82
} 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"; //NOI18N
84
}
85             if (resource==null) return null;
86             java.net.URL JavaDoc 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 JavaDoc, java.io.IOException JavaDoc {
93         // no need to close the stream, will be closed by the parser, see @org.xml.sax.InputSource
94
return parse(new InputSource(fo.getInputStream()));
95     }
96     
97     public static SAXParseException parse (InputSource is)
98             throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
99         return ParseUtils.parseDD(is, DDResolver.getInstance());
100     }
101    
102   
103 }
104
Popular Tags