KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > netbeans > module > XMLUtils


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module;
21
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.SAXParseException JavaDoc;
27 import org.xml.sax.XMLReader JavaDoc;
28
29 public class XMLUtils {
30
31
32     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
33         private int errorType=-1;
34         SAXParseException JavaDoc error;
35
36         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
37             if (errorType<0) {
38                 errorType=0;
39                 error=sAXParseException;
40             }
41             //throw sAXParseException;
42
}
43         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
44             if (errorType<1) {
45                 errorType=1;
46                 error=sAXParseException;
47             }
48             //throw sAXParseException;
49
}
50         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
51             errorType=2;
52             throw sAXParseException;
53         }
54
55         public int getErrorType() {
56             return errorType;
57         }
58         public SAXParseException JavaDoc getError() {
59             return error;
60         }
61     }
62
63     public static SAXParseException JavaDoc parse (InputSource JavaDoc is)
64             throws org.xml.sax.SAXException JavaDoc, ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
65         XMLUtils.ErrorHandler errorHandler = new XMLUtils.ErrorHandler();
66         try {
67             javax.xml.parsers.SAXParserFactory JavaDoc factory = javax.xml.parsers.SAXParserFactory.newInstance();
68             javax.xml.parsers.SAXParser JavaDoc saxParser = null;
69             XMLReader JavaDoc reader = null;
70             
71             //Set namespaceAware to true to get a parser that corresponds to
72
// the default SAX2 namespace feature setting. This is necessary
73
// because the default value from JAXP 1.0 was defined to be false.
74
factory.setNamespaceAware(true);
75             //factory.setValidating(true);
76

77             saxParser = factory.newSAXParser();
78                 
79             
80             
81             reader = saxParser.getXMLReader();
82             
83             reader.setErrorHandler(errorHandler);
84             //reader.setFeature("http://xml.org/sax/features/validation", true); // NOI18N
85
//reader.setFeature("http://xml.org/sax/features/namespaces", true); // NOI18N
86
reader.parse(is);
87             SAXParseException JavaDoc error = errorHandler.getError();
88             if (error!=null) return error;
89         } catch (SAXException JavaDoc ex) {
90             throw ex;
91         } catch (ParserConfigurationException JavaDoc ex) {
92             throw ex;
93         }
94         return null;
95     }
96    
97 }
98
99
Popular Tags