KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedWriter JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.io.Writer JavaDoc;
34 import javax.xml.transform.Result JavaDoc;
35 import javax.xml.transform.Source JavaDoc;
36 import javax.xml.transform.Transformer JavaDoc;
37 import javax.xml.transform.TransformerConfigurationException JavaDoc;
38 import javax.xml.transform.TransformerException JavaDoc;
39 import javax.xml.transform.TransformerFactory JavaDoc;
40 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
41 import javax.xml.transform.stream.StreamResult JavaDoc;
42 import javax.xml.transform.stream.StreamSource JavaDoc;
43
44 /**
45    This class implements the final transformation in the multi-step
46    Schematron validation process. It is assumed that the user knows
47    something about Schematron.
48
49    Schematron instances are not thread-safe. Each instance can only be
50    used in a single thread, but each instance can be used multiple
51    times in sequence.
52
53    We use the default Transformer, as per the
54    javax.xml.transform.TransformFactory class. It is important that
55    the transformer be compatable with the stylesheet being used, but
56    apart from that this class has no constraints on these objects. It
57    simply provides a convenient wrapper for obtaining a validator.
58 */

59
60 class Schematron
61 {
62     private Transformer JavaDoc transformer;
63
64       /**
65        * Construct an instance which will use the given resource name
66        * to find the schema to be used for subsequent analyses. The
67        * schemaName is looked up as a resource on the classpath using
68        * {@link Class.getResourceAsStream(schemaName)}.
69        * @param schemaName the name of the schema to be used
70        * @throws TransformerConfigurationException if the
71        * corresponding resource could not be found, or if the
72        * transformer could not be constructed.
73        * @throws TransformerFactoryConfigurationError if the
74        * underlying transformer factory could not be instantiated.
75        */

76     Schematron(final String JavaDoc schemaName) throws TransformerConfigurationException JavaDoc {
77         transformer = getTransformer(schemaName);
78     }
79         
80       /**
81        * Analyse the given source onto the given result, using the
82        * given schematron stylesheet, using XSLT transformations.
83        * @param src the XML source which is to be analyzed
84        * @param result the result onto which the analsis will be
85        * placed
86        * @throws TransformerException if there was a problem with
87        * the transformation.
88        */

89     void analyze(final Source JavaDoc src, final Result result) throws TransformerException JavaDoc{
90         transformer.transform(src, result);
91     }
92     
93     private Transformer JavaDoc getTransformer(final String JavaDoc schema) throws TransformerConfigurationException JavaDoc {
94         final InputStream JavaDoc is = this.getClass().getResourceAsStream(schema);
95         if (null == is){
96             throw new TransformerConfigurationException JavaDoc("Couldn't construct a transformer to perform validation because I couldn't find the resource named \""+schema+"\" from "+this.getClass().getName());
97         }
98         final TransformerFactory JavaDoc f = TransformerFactory.newInstance();
99
100         final Transformer JavaDoc t = f.newTransformer(new StreamSource JavaDoc(is));
101
102         return t;
103     }
104
105 }
106
107
Popular Tags