KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > dd > loaders > DDUtils


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 package org.netbeans.modules.j2ee.websphere6.dd.loaders;
20
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22 import javax.xml.parsers.SAXParser JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24 //import org.netbeans.modules.j2ee.websphere6.ddloaders.webbnd.*;
25
import org.openide.filesystems.FileObject;
26
27 import org.netbeans.modules.schema2beans.BaseBean;
28
29 /*import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
30 import org.netbeans.modules.j2ee.dd.impl.web.WebAppProxy;
31 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
32 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;*/

33 import java.io.*;
34 import org.xml.sax.*;
35 //import java.util.*;
36

37
38 /**
39  *
40  * @author mkuchtiak
41  * @author dlipin
42  */

43 public class DDUtils {
44
45     private static final String JavaDoc EXCEPTION_PREFIX="version"; //NOI18N
46

47     /** Finds a name similar to requested that uniquely identifies
48      * element between the other elements of the same name.
49      *
50      * @param elements checked elements
51      * @param identifier name of tag that contains identification value
52      * @param o object to be checked
53      * @return a free element name
54      */

55      
56       
57
58     /** Parsing just for detecting the version SAX parser used
59     */

60     public static String JavaDoc getVersion(InputSource is) throws IOException, SAXException {
61         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
62         fact.setValidating(false);
63         try {
64             javax.xml.parsers.SAXParser JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
83         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes atts) throws SAXException {
84             if ("WebAppBinging".equals(rawName)) { //NOI18N
85
String JavaDoc version = atts.getValue("version"); //NOI18N
86
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 JavaDoc publicId, String JavaDoc systemId) {
100             String JavaDoc resource=null;
101             // return a proper input source
102
if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId)) { //NOI18N
103
resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_3.dtd"; //NOI18N
104
} else if ("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { //NOI18N
105
resource="/org/netbeans/modules/j2ee/dd/impl/resources/web-app_2_2.dtd"; //NOI18N
106
} 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"; //NOI18N
108
}
109             if (resource==null) return null;
110             java.net.URL JavaDoc url = this.getClass().getResource(resource);
111             return new InputSource(url.toString());
112         }
113     }
114     
115     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
116         private int errorType=-1;
117         SAXParseException error;
118
119         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
120             if (errorType<0) {
121                 errorType=0;
122                 error=sAXParseException;
123             }
124             //throw sAXParseException;
125
}
126         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
127             if (errorType<1) {
128                 errorType=1;
129                 error=sAXParseException;
130             }
131             //throw sAXParseException;
132
}
133         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc, java.io.IOException JavaDoc {
148         ErrorHandler errorHandler = new ErrorHandler();
149         try {
150             SAXParser JavaDoc parser = createSAXParserFactory().newSAXParser();
151             XMLReader reader = parser.getXMLReader();
152             reader.setErrorHandler(errorHandler);
153             //reader.setEntityResolver(DDResolver.getInstance());
154
//reader.setFeature("http://apache.org/xml/features/validation/schema", true); // NOI18N
155
//reader.setFeature("http://xml.org/sax/features/validation", true); // NOI18N
156
//reader.setFeature("http://xml.org/sax/features/namespaces", true); // NOI18N
157
reader.parse(is);
158             SAXParseException error = errorHandler.getError();
159             if (error!=null) return error;
160         } catch (ParserConfigurationException JavaDoc ex) {
161             throw new SAXException(ex.getMessage());
162         } catch (SAXException ex) {
163             throw ex;
164         }
165         return null;
166     }
167     
168     /** Method that retrieves SAXParserFactory to get the parser prepared to validate against XML schema
169      */

170     private static SAXParserFactory JavaDoc createSAXParserFactory() throws ParserConfigurationException JavaDoc {
171         try {
172             SAXParserFactory JavaDoc fact = SAXParserFactory.newInstance();
173             if (fact!=null) {
174                 try {
175                     fact.getClass().getMethod("getSchema", new Class JavaDoc[]{}); //NOI18N
176
return fact;
177                 } catch (NoSuchMethodException JavaDoc ex) {}
178             }
179             return (SAXParserFactory JavaDoc) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); // NOI18N
180
} catch (Exception JavaDoc ex) {
181             throw new ParserConfigurationException JavaDoc(ex.getMessage());
182         }
183     }
184 }
Popular Tags