1 16 package org.apache.cocoon.components.validation.jaxp; 17 18 import java.io.IOException ; 19 import java.util.HashSet ; 20 import java.util.Set ; 21 22 import javax.xml.XMLConstants ; 23 import javax.xml.transform.sax.SAXSource ; 24 import javax.xml.validation.SchemaFactory ; 25 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.cocoon.components.validation.Schema; 31 import org.apache.cocoon.components.validation.SchemaParser; 32 import org.apache.cocoon.components.validation.Validator; 33 import org.apache.cocoon.components.validation.impl.AbstractSchemaParser; 34 import org.apache.cocoon.components.validation.impl.DraconianErrorHandler; 35 import org.apache.excalibur.source.Source; 36 import org.xml.sax.ErrorHandler ; 37 import org.xml.sax.SAXException ; 38 39 45 public class JaxpSchemaParser extends AbstractSchemaParser 46 implements Configurable, ThreadSafe { 47 48 49 private String className = null; 50 51 private String [] grammars = null; 52 53 56 public JaxpSchemaParser() { 57 super(); 58 } 59 60 84 public void configure(Configuration conf) 85 throws ConfigurationException { 86 this.className = conf.getChild("factory-class").getValue(); 87 final SchemaFactory fact; 88 try { 89 fact = (SchemaFactory ) Class.forName(this.className).newInstance(); 90 } catch (Exception exception) { 91 String message = "Unable to instantiate factory " + this.className; 92 throw new ConfigurationException(message, conf, exception); 93 } 94 95 96 Configuration languages[] = conf.getChild("grammars").getChildren("grammar"); 97 Set grammars = new HashSet (); 98 if (languages.length > 0) { 99 100 101 for (int x = 0; x < languages.length; x++) { 102 String language = languages[x].getValue(); 103 if (fact.isSchemaLanguageSupported(language)) { 104 grammars.add(language); 105 continue; 106 } 107 108 String message = "JAXP SchemaFactory \"" + this.className + "\" " + 109 "does not support configured grammar " + language; 110 throw new ConfigurationException(message, languages[x]); 111 } 112 } else { 113 114 115 if (fact.isSchemaLanguageSupported(XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 116 grammars.add(Validator.GRAMMAR_XML_SCHEMA); 117 } 118 if (fact.isSchemaLanguageSupported(XMLConstants.RELAXNG_NS_URI)) { 119 grammars.add(Validator.GRAMMAR_RELAX_NG); 120 } 121 if (fact.isSchemaLanguageSupported(XMLConstants.XML_DTD_NS_URI)) { 122 grammars.add(Validator.GRAMMAR_XML_DTD); 123 } 124 } 125 126 127 this.grammars = (String []) grammars.toArray(new String [grammars.size()]); 128 } 129 130 144 public Schema parseSchema(Source source, String grammar) 145 throws SAXException , IOException { 146 final SchemaFactory factory; 147 try { 148 factory = (SchemaFactory ) Class.forName(this.className).newInstance(); 149 } catch (Exception exception) { 150 String message = "Unable to instantiate factory " + this.className; 151 throw new SAXException (message, exception); 152 } 153 154 JaxpResolver r = new JaxpResolver(this.sourceResolver, this.entityResolver); 155 SAXSource s = new SAXSource (r.resolveSource(source)); 156 factory.setErrorHandler(DraconianErrorHandler.INSTANCE); 157 factory.setResourceResolver(r); 158 159 return new JaxpSchema(factory.newSchema(s), r.close()); 160 } 161 162 public String [] getSupportedGrammars() { 163 return this.grammars; 164 } 165 } 166 | Popular Tags |