1 16 package org.apache.cocoon.components.validation.jing; 17 18 import java.io.IOException ; 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 ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 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 45 public class JingSchemaParser extends AbstractSchemaParser implements ThreadSafe { 46 47 50 public JingSchemaParser() { 51 super(); 52 } 53 54 68 public Schema parseSchema(Source source, String grammar) 69 throws SAXException , IOException { 70 if (! Validator.GRAMMAR_RELAX_NG.equals(grammar)) { 71 throw new IllegalArgumentException ("Unsupported grammar " + grammar); 72 } 73 74 SchemaReader schemaReader = SAXSchemaReader.getInstance(); 75 JingResolver context = new JingResolver(sourceResolver, entityResolver); 76 InputSource input = context.resolveSource(source); 77 78 try { 79 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 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 message = "Incorrect schema \"" + source.getURI() + "\""; 94 throw new SAXException (message, exception); 95 } 96 } 97 98 105 public String [] getSupportedGrammars() { 106 107 return new String [] { Validator.GRAMMAR_RELAX_NG, 108 "http://relaxng.org/ns/structure/0.9" }; 109 } 110 } 111 | Popular Tags |