KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import org.iso_relax.verifier.Verifier;
30 import org.iso_relax.verifier.VerifierConfigurationException;
31 import org.iso_relax.verifier.VerifierFactory;
32 import org.xml.sax.ErrorHandler JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import org.xml.sax.EntityResolver JavaDoc;
39
40 import java.io.OutputStreamWriter JavaDoc;
41
42
43
44 class RNGValidator
45 {
46     void validate(InputSource JavaDoc schema, InputSource JavaDoc src, Writer JavaDoc w) throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc{
47          validate(schema, src, new MyErrorHandler(w));
48     }
49
50     void validate(InputSource JavaDoc schema, InputSource JavaDoc src, XMLReader JavaDoc rdr, Writer JavaDoc w) throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc{
51          validate(schema, src, rdr, new MyErrorHandler(w) );
52     }
53
54
55     void validate(InputSource JavaDoc schema, InputSource JavaDoc src, ErrorHandler JavaDoc eh) throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc{
56         getVerifier(schema, eh).verify(src);
57     }
58
59         /**
60          * Validate the given source against the given schema, using
61          * the given reader to parser the source, handling errors
62          * using the given {@link ErrorHandler}
63          * @param schema the RELAX NG schema to be used to validate
64          * the source document
65          * @param src the source document to be validated
66          * @param rdr the reader used to parse the source document
67          * @param errorHandler used to handle errors during the parse
68          * and validation
69          * @throws VerifierConfigurationException if the verifier
70          * cannot be configured
71          * @throws SAXException if there's a fatal exception from the
72          * SAX layer
73          * @throws IOException if the IO fails
74          */

75     void validate(InputSource JavaDoc schema, InputSource JavaDoc src, XMLReader JavaDoc rdr, ErrorHandler JavaDoc errorHandler) throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc{
76         rdr.setContentHandler(getVerifier(schema, rdr.getEntityResolver(), errorHandler).getVerifierHandler());
77         rdr.parse(src);
78     }
79
80     private Verifier getVerifier(InputSource JavaDoc schema, ErrorHandler JavaDoc eh)
81         throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc {
82         return getVerifier(schema, null, eh);
83     }
84     private Verifier getVerifier(InputSource JavaDoc schema, EntityResolver JavaDoc er, ErrorHandler JavaDoc eh)
85         throws VerifierConfigurationException, SAXException JavaDoc, IOException JavaDoc {
86             // As of the 20030108 release of jarv this method doesn't
87
// work. The new release added a class loader and that
88
// seems to screw things up.
89
// final VerifierFactory f =
90
// VerifierFactory.newInstance("http://relaxng.org/ns/structure/1.0");
91
final VerifierFactory f = new com.sun.msv.verifier.jarv.TheFactoryImpl();
92         final Verifier verifier = f.newVerifier(schema);
93         verifier.setErrorHandler(eh);
94         verifier.setEntityResolver(er);
95         return verifier;
96     }
97     
98
99     private static class MyErrorHandler implements ErrorHandler JavaDoc
100     {
101         private PrintWriter JavaDoc out;
102         MyErrorHandler(Writer JavaDoc out) {
103             this.out = new PrintWriter JavaDoc(out);
104         }
105
106           /**
107            * Returns a string describing parse exception details
108            */

109         private String JavaDoc getParseExceptionInfo(SAXParseException JavaDoc spe) {
110             String JavaDoc systemId = spe.getSystemId();
111             if (systemId == null) {
112                 systemId = "null";
113             }
114             String JavaDoc info = "URI=" + systemId +
115             " Line=" + spe.getLineNumber() +
116             ": " + spe.getMessage();
117             return info;
118         }
119
120           // The following methods are standard SAX ErrorHandler methods.
121
// See SAX documentation for more info.
122

123         public void warning(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
124             out.println("Warning: " + getParseExceptionInfo(spe));
125         }
126         
127         public void error(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
128             String JavaDoc message = "Error: " + getParseExceptionInfo(spe);
129             out.println(message);
130         }
131
132         public void fatalError(SAXParseException JavaDoc spe) throws SAXException JavaDoc {
133             String JavaDoc message = "Fatal Error: " + getParseExceptionInfo(spe);
134             throw new SAXException JavaDoc(message);
135         }
136     }
137
138
139 }
140
Popular Tags