KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > common > ParseUtils


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.common;
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.openide.filesystems.FileObject;
27 import org.openide.util.NbBundle;
28 import org.xml.sax.*;
29
30
31 /** Class that collects XML parsing utility methods for web applications. It is
32  * implementation private for this module, however it is also intended to be used by
33  * the DDLoaders modules, which requires tighter coupling with ddapi and has an
34  * implementation dependency on it.
35  *
36  * @author Petr Jiricka
37  */

38 public class ParseUtils {
39   
40     public static final String JavaDoc EXCEPTION_PREFIX="version:"; //NOI18N
41

42     /** Parsing just for detecting the version SAX parser used
43      */

44     public static String JavaDoc getVersion(java.io.InputStream JavaDoc is, org.xml.sax.helpers.DefaultHandler JavaDoc versionHandler,
45         EntityResolver ddResolver) throws java.io.IOException JavaDoc, SAXException {
46         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
47         fact.setValidating(false);
48         try {
49             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
50             XMLReader reader = parser.getXMLReader();
51             reader.setContentHandler(versionHandler);
52             reader.setEntityResolver(ddResolver);
53             try {
54                 reader.parse(new InputSource(is));
55             } catch (SAXException ex) {
56                 is.close();
57                 String JavaDoc message = ex.getMessage();
58                 if (message!=null && message.startsWith(EXCEPTION_PREFIX))
59                     return message.substring(EXCEPTION_PREFIX.length());
60                 else throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"),ex);
61             }
62             is.close();
63             throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
64         } catch(javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
65             throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_parserProblem"),ex);
66         }
67     }
68     
69     /** Parsing just for detecting the version SAX parser used
70     */

71     public static String JavaDoc getVersion(InputSource is, org.xml.sax.helpers.DefaultHandler JavaDoc versionHandler,
72         EntityResolver ddResolver) throws IOException JavaDoc, SAXException {
73         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
74         fact.setValidating(false);
75         try {
76             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
77             XMLReader reader = parser.getXMLReader();
78             reader.setContentHandler(versionHandler);
79             reader.setEntityResolver(ddResolver);
80             try {
81                 reader.parse(is);
82             } catch (SAXException ex) {
83                 String JavaDoc message = ex.getMessage();
84                 if (message!=null && message.startsWith(EXCEPTION_PREFIX))
85                     return message.substring(EXCEPTION_PREFIX.length());
86                 else throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"),ex);
87             }
88             throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
89         } catch(javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
90             throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_parserProblem"),ex);
91         }
92     }
93     
94     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
95         private int errorType=-1;
96         SAXParseException error;
97         
98         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
99             if (errorType<0) {
100                 errorType=0;
101                 error=sAXParseException;
102             }
103             //throw sAXParseException;
104
}
105         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
106             if (errorType<1) {
107                 errorType=1;
108                 error=sAXParseException;
109             }
110             //throw sAXParseException;
111
}
112         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
113             errorType=2;
114             throw sAXParseException;
115         }
116         
117         public int getErrorType() {
118             return errorType;
119         }
120         public SAXParseException getError() {
121             return error;
122         }
123     }
124     
125     public static SAXParseException parseDD(InputSource is, EntityResolver ddResolver)
126             throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
127         ErrorHandler errorHandler = new ErrorHandler();
128         try {
129             SAXParser JavaDoc parser = createSAXParserFactory().newSAXParser();
130             XMLReader reader = parser.getXMLReader();
131             reader.setErrorHandler(errorHandler);
132             reader.setEntityResolver(ddResolver);
133             reader.setFeature("http://apache.org/xml/features/validation/schema", true); // NOI18N
134
reader.setFeature("http://xml.org/sax/features/validation", true); // NOI18N
135
reader.setFeature("http://xml.org/sax/features/namespaces", true); // NOI18N
136
reader.parse(is);
137             SAXParseException error = errorHandler.getError();
138             if (error!=null) return error;
139         } catch (ParserConfigurationException JavaDoc ex) {
140             SAXException sax = new SAXException(ex.getMessage(), ex);
141             sax.initCause(ex);
142             throw sax;
143         } catch (SAXException ex) {
144             throw ex;
145         } catch (IllegalArgumentException JavaDoc ex) {
146             // yes, this may happen, see issue #71738
147
SAXException sax = new SAXException(ex.getMessage(), ex);
148             sax.initCause(ex);
149             throw sax;
150         }
151         return null;
152     }
153     
154     /** Method that retrieves SAXParserFactory to get the parser prepared to validate against XML schema
155      */

156     private static SAXParserFactory JavaDoc createSAXParserFactory() throws ParserConfigurationException JavaDoc {
157         try {
158             SAXParserFactory JavaDoc fact = SAXParserFactory.newInstance();
159             if (fact!=null) {
160                 try {
161                     fact.getClass().getMethod("getSchema", new Class JavaDoc[]{}); //NOI18N
162
return fact;
163                 } catch (NoSuchMethodException JavaDoc ex) {}
164             }
165             return (SAXParserFactory JavaDoc) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); // NOI18N
166
} catch (Exception JavaDoc ex) {
167             throw new ParserConfigurationException JavaDoc(ex.getMessage());
168         }
169     }
170     
171   
172 }
173
Popular Tags