KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > validation > jing > JingSchemaParser


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.validation.jing;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.avalon.framework.thread.ThreadSafe;
21 import org.apache.cocoon.components.validation.Schema;
22 import org.apache.cocoon.components.validation.SchemaParser;
23 import org.apache.cocoon.components.validation.Validator;
24 import org.apache.cocoon.components.validation.impl.AbstractSchemaParser;
25 import org.apache.cocoon.components.validation.impl.DraconianErrorHandler;
26 import org.apache.excalibur.source.Source;
27 import org.xml.sax.ErrorHandler JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import com.thaiopensource.util.PropertyMap;
32 import com.thaiopensource.util.PropertyMapBuilder;
33 import com.thaiopensource.validate.IncorrectSchemaException;
34 import com.thaiopensource.validate.SchemaReader;
35 import com.thaiopensource.validate.ValidateProperty;
36 import com.thaiopensource.validate.rng.SAXSchemaReader;
37
38 /**
39  * <p>A {@link SchemaParser} implementation for the RELAX NG grammar using the
40  * <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a> validation
41  * engine.</p>
42  *
43  * @author <a HREF="mailto:pier@betaversion.org">Pier Fumagalli</a>
44  */

45 public class JingSchemaParser extends AbstractSchemaParser implements ThreadSafe {
46
47     /**
48      * <p>Create a new {@link JingSchemaParser} instance.</p>
49      */

50     public JingSchemaParser() {
51         super();
52     }
53
54     /**
55      * <p>Parse the specified {@link Source} and return a new {@link Schema}.</p>
56      *
57      * <p>The returned {@link Schema} must be able to validate multiple documents
58      * via multiple invocations of {@link Schema#createValidator(ErrorHandler)}.</p>
59      *
60      * @param source the {@link Source} associated with the {@link Schema} to return.
61      * @return a <b>non-null</b> {@link Schema} instance.
62      * @throws SAXException if a grammar error occurred parsing the schema.
63      * @throws IOException if an I/O error occurred parsing the schema.
64      * @throws IllegalArgumentException if the specified grammar type is not one
65      * of the grammar types returned by the
66      * {@link #getSupportedGrammars()} method.
67      */

68     public Schema parseSchema(Source source, String JavaDoc grammar)
69     throws SAXException JavaDoc, IOException JavaDoc {
70         if (! Validator.GRAMMAR_RELAX_NG.equals(grammar)) {
71             throw new IllegalArgumentException JavaDoc("Unsupported grammar " + grammar);
72         }
73
74         SchemaReader schemaReader = SAXSchemaReader.getInstance();
75         JingResolver context = new JingResolver(sourceResolver, entityResolver);
76         InputSource JavaDoc input = context.resolveSource(source);
77
78         try {
79             /* Create a simple property map builder */
80             PropertyMapBuilder builder = new PropertyMapBuilder();
81             ValidateProperty.ENTITY_RESOLVER.put(builder, context);
82             ValidateProperty.XML_READER_CREATOR.put(builder, context);
83             ValidateProperty.ERROR_HANDLER.put(builder,
84                                                DraconianErrorHandler.INSTANCE);
85             PropertyMap validatorProperties = builder.toPropertyMap();
86
87             /* Parse, rewrap, and return the schema */
88             final com.thaiopensource.validate.Schema schema;
89             schema = schemaReader.createSchema(input, validatorProperties);
90             return new JingSchema(schema, context.close());
91
92         } catch (IncorrectSchemaException exception) {
93             String JavaDoc message = "Incorrect schema \"" + source.getURI() + "\"";
94             throw new SAXException JavaDoc(message, exception);
95         }
96     }
97
98     /**
99      * <p>Return an array of {@link String}s containing all schema grammars
100      * supported by this {@link SchemaParser}.</p>
101      *
102      * <p>The {@link JingSchemaParser} supports only the
103      * {@link Validator#GRAMMAR_RELAX_NG RELAX NG} grammar.</p>
104      */

105     public String JavaDoc[] getSupportedGrammars() {
106         /* Jing supports spec 1.0 and 0.9 */
107         return new String JavaDoc[] { Validator.GRAMMAR_RELAX_NG,
108                               "http://relaxng.org/ns/structure/0.9" };
109     }
110 }
111
Popular Tags