KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > XMLReaderFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.io.PrintStream JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.XMLReader JavaDoc;
34
35
36
37 class XMLReaderFactory
38 {
39     static XMLReader JavaDoc newInstance(PrintStream JavaDoc out) throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
40         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
41         spf.setNamespaceAware(true);
42         spf.setValidating(false);
43         SAXParser JavaDoc saxParser = spf.newSAXParser();
44         XMLReader JavaDoc r= saxParser.getXMLReader();
45         r.setErrorHandler(new MyErrorHandler(out));
46         return r;
47     }
48
49
50         private static class MyErrorHandler implements ErrorHandler JavaDoc
51     {
52         private PrintStream JavaDoc out;
53         MyErrorHandler(PrintStream JavaDoc out) {
54             this.out = out;
55         }
56
57           /**
58            * Returns a string describing parse exception details
59            */

60         private String JavaDoc getParseExceptionInfo(SAXParseException JavaDoc spe) {
61             String JavaDoc systemId = spe.getSystemId();
62             if (systemId == null) {
63                 systemId = "null";
64             }
65             String JavaDoc info = "URI=" + systemId +
66             " Line=" + spe.getLineNumber() +
67             ": " + spe.getMessage();
68             return info;
69         }
70
71           // The following methods are standard SAX ErrorHandler methods.
72
// See SAX documentation for more info.
73

74         public void warning(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
75             out.println("Warning: " + getParseExceptionInfo(spe));
76         }
77         
78         public void error(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
79             String JavaDoc message = "Error: " + getParseExceptionInfo(spe);
80             out.println(message);
81 // throw new SAXException(message);
82
}
83
84         public void fatalError(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
85             String JavaDoc message = "Fatal Error: " + getParseExceptionInfo(spe);
86             throw new SAXException JavaDoc(message);
87         }
88     }
89
90         
91 }
92
Popular Tags