KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > client > ClientParseUtils


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.client;
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.client.AppClient;
27 import org.netbeans.modules.j2ee.dd.impl.common.ParseUtils;
28 import org.openide.filesystems.FileObject;
29 import org.openide.util.NbBundle;
30 import org.xml.sax.*;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33
34 /** Class that collects XML parsing utility methods for appclient 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 ClientParseUtils {
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 DefaultHandler JavaDoc {
57         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes atts) throws SAXException {
58             if ("application-client".equals(rawName)) { //NOI18N
59
String JavaDoc version = atts.getValue("version"); //NOI18N
60
throw new SAXException(ParseUtils.EXCEPTION_PREFIX+(version==null?AppClient.VERSION_1_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             if ("-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.3//EN".equals(publicId)) {
75                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/application-client_1_3.dtd"); //NOI18N
76
} else if ("http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd".equals(systemId)) {
77                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/application-client_1_4.xsd"); //NOI18N
78
} else if ("http://java.sun.com/xml/ns/javaee/application-client_5.xsd".equals(systemId)) {
79                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/application-client_5.xsd"); //NOI18N
80
} else {
81                 // use the default behaviour
82
return null;
83             }
84         }
85     }
86     
87     
88     public static SAXParseException parse(FileObject fo)
89     throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
90         // no need to close the stream, will be closed by the parser, see @org.xml.sax.InputSource
91
return parse(new InputSource(fo.getInputStream()));
92     }
93     
94     public static SAXParseException parse (InputSource is)
95             throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
96         return ParseUtils.parseDD(is, DDResolver.getInstance());
97     }
98    
99   
100 }
101
Popular Tags