001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.validation.jing;
018:
019: import java.io.IOException;
020:
021: import org.apache.avalon.framework.thread.ThreadSafe;
022: import org.apache.cocoon.components.validation.Schema;
023: import org.apache.cocoon.components.validation.Validator;
024: import org.apache.cocoon.components.validation.impl.AbstractSchemaParser;
025: import org.apache.cocoon.components.validation.impl.DraconianErrorHandler;
026: import org.apache.excalibur.source.Source;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029:
030: import com.thaiopensource.util.PropertyMap;
031: import com.thaiopensource.util.PropertyMapBuilder;
032: import com.thaiopensource.validate.IncorrectSchemaException;
033: import com.thaiopensource.validate.SchemaReader;
034: import com.thaiopensource.validate.ValidateProperty;
035: import com.thaiopensource.validate.rng.SAXSchemaReader;
036:
037: /**
038: * <p>A {@link SchemaParser} implementation for the RELAX NG grammar using the
039: * <a href="http://www.thaiopensource.com/relaxng/jing.html">JING</a> validation
040: * engine.</p>
041: *
042: */
043: public class JingSchemaParser extends AbstractSchemaParser implements
044: ThreadSafe {
045:
046: /**
047: * <p>Create a new {@link JingSchemaParser} instance.</p>
048: */
049: public JingSchemaParser() {
050: super ();
051: }
052:
053: /**
054: * <p>Parse the specified {@link Source} and return a new {@link Schema}.</p>
055: *
056: * <p>The returned {@link Schema} must be able to validate multiple documents
057: * via multiple invocations of {@link Schema#createValidator(ErrorHandler)}.</p>
058: *
059: * @param source the {@link Source} associated with the {@link Schema} to return.
060: * @return a <b>non-null</b> {@link Schema} instance.
061: * @throws SAXException if a grammar error occurred parsing the schema.
062: * @throws IOException if an I/O error occurred parsing the schema.
063: * @throws IllegalArgumentException if the specified grammar type is not one
064: * of the grammar types returned by the
065: * {@link #getSupportedGrammars()} method.
066: */
067: public Schema parseSchema(Source source, String grammar)
068: throws SAXException, IOException {
069: if (!Validator.GRAMMAR_RELAX_NG.equals(grammar)) {
070: throw new IllegalArgumentException("Unsupported grammar "
071: + grammar);
072: }
073:
074: SchemaReader schemaReader = SAXSchemaReader.getInstance();
075: JingResolver context = new JingResolver(sourceResolver,
076: entityResolver);
077: InputSource input = context.resolveSource(source);
078:
079: try {
080: /* Create a simple property map builder */
081: PropertyMapBuilder builder = new PropertyMapBuilder();
082: ValidateProperty.ENTITY_RESOLVER.put(builder, context);
083: ValidateProperty.XML_READER_CREATOR.put(builder, context);
084: ValidateProperty.ERROR_HANDLER.put(builder,
085: DraconianErrorHandler.INSTANCE);
086: PropertyMap validatorProperties = builder.toPropertyMap();
087:
088: /* Parse, rewrap, and return the schema */
089: final com.thaiopensource.validate.Schema schema;
090: schema = schemaReader.createSchema(input,
091: validatorProperties);
092: return new JingSchema(schema, context.close());
093:
094: } catch (IncorrectSchemaException exception) {
095: String message = "Incorrect schema \"" + source.getURI()
096: + "\"";
097: throw new SAXException(message, exception);
098: }
099: }
100:
101: /**
102: * <p>Return an array of {@link String}s containing all schema grammars
103: * supported by this {@link SchemaParser}.</p>
104: *
105: * <p>The {@link JingSchemaParser} supports only the
106: * {@link Validator#GRAMMAR_RELAX_NG RELAX NG} grammar.</p>
107: */
108: public String[] getSupportedGrammars() {
109: /* Jing supports spec 1.0 and 0.9 */
110: return new String[] { Validator.GRAMMAR_RELAX_NG,
111: "http://relaxng.org/ns/structure/0.9" };
112: }
113: }
|