1 57 package com.sun.org.apache.xerces.internal.jaxp.validation.xs; 58 59 import java.io.IOException ; 60 import java.util.Locale ; 61 62 import javax.xml.XMLConstants ; 63 import javax.xml.transform.Source ; 64 import javax.xml.validation.Schema ; 65 import javax.xml.validation.SchemaFactory ; 66 67 import com.sun.org.apache.xerces.internal.impl.Constants; 68 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader; 69 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter; 70 import com.sun.org.apache.xerces.internal.jaxp.validation.ReadonlyGrammarPool; 71 import com.sun.org.apache.xerces.internal.jaxp.validation.Util; 72 import com.sun.org.apache.xerces.internal.jaxp.validation.XercesConstants; 73 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper; 74 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper; 75 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter; 76 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl; 77 import com.sun.org.apache.xerces.internal.xni.XNIException; 78 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 79 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 80 import org.w3c.dom.ls.LSResourceResolver ; 81 import org.xml.sax.ErrorHandler ; 82 import org.xml.sax.SAXException ; 83 import org.xml.sax.SAXParseException ; 84 import com.sun.org.apache.xerces.internal.util.SecurityManager; 85 import org.xml.sax.SAXNotRecognizedException ; 86 import org.xml.sax.SAXNotSupportedException ; 87 88 94 public class SchemaFactoryImpl extends SchemaFactory { 95 96 private final XMLSchemaLoader loader = new XMLSchemaLoader(); 97 private static XSMessageFormatter messageFormatter = new XSMessageFormatter(); 98 101 private ErrorHandler errorHandler; 102 103 private LSResourceResolver resourceResolver; 104 105 private SAXParseException lastException; 106 107 private final SecurityManager secureProcessing ; 108 109 private boolean enableSP; 110 111 public SchemaFactoryImpl() { 112 secureProcessing = new SecurityManager (); 113 loader.setErrorHandler(new ErrorHandlerWrapper(new ErrorHandler () { 115 public void warning(SAXParseException exception) throws SAXException { 116 if( errorHandler!=null ) errorHandler.warning(exception); 117 } 118 119 public void error(SAXParseException exception) throws SAXException { 120 lastException = exception; 121 if( errorHandler!=null ) errorHandler.error(exception); 122 else throw exception; 123 } 124 125 public void fatalError(SAXParseException exception) throws SAXException { 126 lastException = exception; 127 if( errorHandler!=null ) errorHandler.fatalError(exception); 128 else throw exception; 129 } 130 })); 131 } 132 133 134 146 public boolean isSchemaLanguageSupported(String schemaLanguage) { 147 148 if (schemaLanguage == null) { 149 throw new NullPointerException ( 150 messageFormatter.formatMessage(Locale.getDefault(), 151 "SchemaLanguageSupportedErrorWhenNull", 152 new Object [] {this.getClass().getName()})); 153 } 154 155 if (schemaLanguage.length() == 0) { 156 throw new IllegalArgumentException ( 157 messageFormatter.formatMessage(Locale.getDefault(), 158 "SchemaLanguageSupportedErrorWhenLength", 159 new Object [] {this.getClass().getName()})); 160 } 161 162 if (schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI) 164 || schemaLanguage.equals(XMLConstants.RELAXNG_NS_URI)) { 165 return true; 166 } 167 168 return false; 170 } 171 172 public LSResourceResolver getResourceResolver() { 173 return resourceResolver; 174 } 175 176 public void setResourceResolver(LSResourceResolver resourceResolver) { 177 this.resourceResolver = resourceResolver; 178 loader.setEntityResolver(new DOMEntityResolverWrapper(resourceResolver)); 179 } 180 181 public ErrorHandler getErrorHandler() { 182 return errorHandler; 183 } 184 185 public void setErrorHandler(ErrorHandler errorHandler) { 186 this.errorHandler = errorHandler; 187 } 188 189 190 191 public Schema newSchema( Source [] schemas ) throws SAXException { 192 193 lastException = null; 194 195 XMLGrammarPool pool = new XMLGrammarPoolImpl(); 197 loader.setProperty(XercesConstants.XMLGRAMMAR_POOL,pool); 198 loader.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING,true); 199 if(enableSP) 200 loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,secureProcessing); 201 else 202 loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,null); 203 204 for( int i=0; i<schemas.length; i++ ) { 205 try { 206 loader.loadGrammar(schemas[i]); 207 } catch (XNIException e) { 208 throw Util.toSAXException(e); 210 } catch (IOException e) { 211 SAXParseException se = new SAXParseException (e.getMessage(),null,e); 213 errorHandler.error(se); 214 throw se; } 216 } 217 218 if( lastException!=null ) 220 throw lastException; 221 222 return new SchemaImpl(new ReadonlyGrammarPool(pool),true); 224 } 225 226 public Schema newSchema() throws SAXException { 227 return new SchemaImpl(new XMLGrammarPoolImpl() { 229 public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) { 230 String sid1 = desc1.getExpandedSystemId(); 231 String sid2 = desc2.getExpandedSystemId(); 232 if( sid1!=null && sid2!=null ) 233 return sid1.equals(sid2); 234 if( sid1==null && sid2==null ) 235 return true; 236 return false; 237 } 238 public int hashCode(XMLGrammarDescription desc) { 239 String s = desc.getExpandedSystemId(); 240 if(s!=null) return s.hashCode(); 241 return 0; 242 } 243 }, false); 244 } 245 246 public void setFeature(String name, boolean value) throws SAXNotRecognizedException , SAXNotSupportedException { 247 if(name==null) throw new NullPointerException (SAXMessageFormatter.formatMessage(Locale.getDefault(), 248 "nullparameter",new Object [] {"setFeature(String,boolean)"})); 249 if(name.equals(Constants.FEATURE_SECURE_PROCESSING)){ 250 enableSP = value; 251 }else throw new SAXNotRecognizedException (SAXMessageFormatter.formatMessage(Locale.getDefault(), 252 "feature-not-supported", new Object [] {name})); 253 254 } 255 256 public boolean getFeature(String name) throws SAXNotRecognizedException , SAXNotSupportedException { 257 if(name==null) throw new NullPointerException (SAXMessageFormatter.formatMessage(Locale.getDefault(), 258 "nullparameter",new Object [] {"getFeature(String)"})); 259 if(name.equals(Constants.FEATURE_SECURE_PROCESSING)) 260 return enableSP; 261 else throw new SAXNotRecognizedException (SAXMessageFormatter.formatMessage(Locale.getDefault(), 262 "feature-not-supported", new Object [] {name})); 263 } 264 } 265 | Popular Tags |