1 16 17 package org.apache.xerces.impl.xs; 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 import java.util.Hashtable ; 28 import java.util.Locale ; 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 31 32 import org.apache.xerces.dom.DOMErrorImpl; 33 import org.apache.xerces.dom.DOMMessageFormatter; 34 import org.apache.xerces.dom.DOMStringListImpl; 35 import org.apache.xerces.impl.Constants; 36 import org.apache.xerces.impl.XMLEntityManager; 37 import org.apache.xerces.impl.XMLErrorReporter; 38 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 39 import org.apache.xerces.impl.xs.models.CMBuilder; 40 import org.apache.xerces.impl.xs.models.CMNodeFactory; 41 import org.apache.xerces.impl.xs.traversers.XSDHandler; 42 import org.apache.xerces.util.DOMEntityResolverWrapper; 43 import org.apache.xerces.util.DOMErrorHandlerWrapper; 44 import org.apache.xerces.util.DefaultErrorHandler; 45 import org.apache.xerces.util.ParserConfigurationSettings; 46 import org.apache.xerces.util.SymbolTable; 47 import org.apache.xerces.util.XMLSymbols; 48 import org.apache.xerces.xni.XNIException; 49 import org.apache.xerces.xni.grammars.Grammar; 50 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 51 import org.apache.xerces.xni.grammars.XMLGrammarLoader; 52 import org.apache.xerces.xni.grammars.XMLGrammarPool; 53 import org.apache.xerces.xni.grammars.XSGrammar; 54 import org.apache.xerces.xni.parser.XMLComponent; 55 import org.apache.xerces.xni.parser.XMLComponentManager; 56 import org.apache.xerces.xni.parser.XMLConfigurationException; 57 import org.apache.xerces.xni.parser.XMLEntityResolver; 58 import org.apache.xerces.xni.parser.XMLErrorHandler; 59 import org.apache.xerces.xni.parser.XMLInputSource; 60 import org.apache.xerces.xs.LSInputList; 61 import org.apache.xerces.xs.StringList; 62 import org.apache.xerces.xs.XSLoader; 63 import org.apache.xerces.xs.XSModel; 64 import org.w3c.dom.DOMConfiguration ; 65 import org.w3c.dom.DOMError ; 66 import org.w3c.dom.DOMErrorHandler ; 67 import org.w3c.dom.DOMStringList ; 68 import org.w3c.dom.DOMException ; 69 import org.w3c.dom.ls.LSInput ; 70 import org.w3c.dom.ls.LSResourceResolver ; 71 import org.xml.sax.InputSource ; 72 73 90 91 public class XMLSchemaLoader implements XMLGrammarLoader, XMLComponent, 92 XSLoader, DOMConfiguration { 94 95 97 98 protected static final String SCHEMA_FULL_CHECKING = 99 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 100 101 102 protected static final String CONTINUE_AFTER_FATAL_ERROR = 103 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 104 105 106 protected static final String ALLOW_JAVA_ENCODINGS = 107 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 108 109 110 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 111 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 112 113 114 protected static final String VALIDATE_ANNOTATIONS = 115 Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE; 116 117 118 protected static final String DISALLOW_DOCTYPE = 119 Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE; 120 121 122 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = 123 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 124 125 126 protected static final String HONOUR_ALL_SCHEMALOCATIONS = 127 Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE; 128 129 protected static final String AUGMENT_PSVI = 130 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 131 132 protected static final String PARSER_SETTINGS = 133 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 134 135 private static final String [] RECOGNIZED_FEATURES = { 137 SCHEMA_FULL_CHECKING, 138 AUGMENT_PSVI, 139 CONTINUE_AFTER_FATAL_ERROR, 140 ALLOW_JAVA_ENCODINGS, 141 STANDARD_URI_CONFORMANT_FEATURE, 142 DISALLOW_DOCTYPE, 143 GENERATE_SYNTHETIC_ANNOTATIONS, 144 VALIDATE_ANNOTATIONS, 145 HONOUR_ALL_SCHEMALOCATIONS 146 }; 147 148 150 151 public static final String SYMBOL_TABLE = 152 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 153 154 155 public static final String ERROR_REPORTER = 156 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 157 158 159 protected static final String ERROR_HANDLER = 160 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 161 162 163 public static final String ENTITY_RESOLVER = 164 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 165 166 167 public static final String XMLGRAMMAR_POOL = 168 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 169 170 171 protected static final String SCHEMA_LOCATION = 172 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION; 173 174 175 protected static final String SCHEMA_NONS_LOCATION = 176 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION; 177 178 179 protected static final String JAXP_SCHEMA_SOURCE = 180 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 181 182 protected static final String SECURITY_MANAGER = 183 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 184 185 protected static final String ENTITY_MANAGER = 186 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 187 188 private static final String [] RECOGNIZED_PROPERTIES = { 190 ENTITY_MANAGER, 191 SYMBOL_TABLE, 192 ERROR_REPORTER, 193 ERROR_HANDLER, 194 ENTITY_RESOLVER, 195 XMLGRAMMAR_POOL, 196 SCHEMA_LOCATION, 197 SCHEMA_NONS_LOCATION, 198 JAXP_SCHEMA_SOURCE, 199 SECURITY_MANAGER 200 }; 201 202 204 private ParserConfigurationSettings fLoaderConfig = new ParserConfigurationSettings(); 206 private SymbolTable fSymbolTable = null; 207 private XMLErrorReporter fErrorReporter = new XMLErrorReporter (); 208 private XMLEntityManager fEntityManager = null; 209 private XMLEntityResolver fUserEntityResolver = null; 210 private XMLGrammarPool fGrammarPool = null; 211 private String fExternalSchemas = null; 212 private String fExternalNoNSSchema = null; 213 private Object fJAXPSource = null; 215 private boolean fIsCheckedFully = false; 217 private boolean fJAXPProcessed = false; 219 private boolean fSettingsChanged = true; 221 222 private XSDHandler fSchemaHandler; 224 private XSGrammarBucket fGrammarBucket; 225 private XSDeclarationPool fDeclPool = null; 226 private SubstitutionGroupHandler fSubGroupHandler; 227 private CMBuilder fCMBuilder; 228 private XSDDescription fXSDDescription = new XSDDescription(); 229 230 private Hashtable fJAXPCache; 231 private Locale fLocale = Locale.getDefault(); 232 233 private DOMStringList fRecognizedParameters = null; 235 236 237 private DOMErrorHandlerWrapper fErrorHandler = null; 238 239 240 private DOMEntityResolverWrapper fResourceResolver = null; 241 242 public XMLSchemaLoader() { 244 this( new SymbolTable(), null, new XMLEntityManager(), null, null, null); 245 } 246 247 public XMLSchemaLoader(SymbolTable symbolTable) { 248 this( symbolTable, null, new XMLEntityManager(), null, null, null); 249 } 250 251 259 XMLSchemaLoader(XMLErrorReporter errorReporter, 260 XSGrammarBucket grammarBucket, 261 SubstitutionGroupHandler sHandler, CMBuilder builder) { 262 this(null, errorReporter, null, grammarBucket, sHandler, builder); 263 } 264 265 XMLSchemaLoader(SymbolTable symbolTable, 266 XMLErrorReporter errorReporter, 267 XMLEntityManager entityResolver, 268 XSGrammarBucket grammarBucket, 269 SubstitutionGroupHandler sHandler, 270 CMBuilder builder) { 271 272 fLoaderConfig.addRecognizedFeatures(RECOGNIZED_FEATURES); 274 fLoaderConfig.addRecognizedProperties(RECOGNIZED_PROPERTIES); 275 if (symbolTable != null){ 276 fLoaderConfig.setProperty(SYMBOL_TABLE, symbolTable); 277 } 278 279 if(errorReporter == null) { 280 errorReporter = new XMLErrorReporter (); 281 errorReporter.setLocale(fLocale); 282 errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler()); 283 284 } 285 fErrorReporter = errorReporter; 286 if(fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 288 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 289 } 290 fLoaderConfig.setProperty(ERROR_REPORTER, fErrorReporter); 291 fEntityManager = entityResolver; 292 if (fEntityManager != null){ 294 fLoaderConfig.setProperty(ENTITY_MANAGER, fEntityManager); 295 } 296 297 fLoaderConfig.setFeature(AUGMENT_PSVI, true); 299 300 if(grammarBucket == null ) { 301 grammarBucket = new XSGrammarBucket(); 302 } 303 fGrammarBucket = grammarBucket; 304 if(sHandler == null) { 305 sHandler = new SubstitutionGroupHandler(fGrammarBucket); 306 } 307 fSubGroupHandler = sHandler; 308 309 CMNodeFactory nodeFactory = new CMNodeFactory() ; 311 312 if(builder == null) { 313 builder = new CMBuilder(nodeFactory); 314 } 315 fCMBuilder = builder; 316 fSchemaHandler = new XSDHandler(fGrammarBucket); 317 fDeclPool = new XSDeclarationPool(); 318 fJAXPCache = new Hashtable (); 319 320 fSettingsChanged = true; 321 } 322 323 328 public String [] getRecognizedFeatures() { 329 return (String [])(RECOGNIZED_FEATURES.clone()); 330 } 332 339 public boolean getFeature(String featureId) 340 throws XMLConfigurationException { 341 return fLoaderConfig.getFeature(featureId); 342 } 344 353 public void setFeature(String featureId, 354 boolean state) throws XMLConfigurationException { 355 fSettingsChanged = true; 356 if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) { 357 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, state); 358 } 359 else if(featureId.equals(GENERATE_SYNTHETIC_ANNOTATIONS)) { 360 fSchemaHandler.setGenerateSyntheticAnnotations(state); 361 } 362 fLoaderConfig.setFeature(featureId, state); 363 } 365 370 public String [] getRecognizedProperties() { 371 return (String [])(RECOGNIZED_PROPERTIES.clone()); 372 } 374 381 public Object getProperty(String propertyId) 382 throws XMLConfigurationException { 383 return fLoaderConfig.getProperty(propertyId); 384 } 386 395 public void setProperty(String propertyId, 396 Object state) throws XMLConfigurationException { 397 fSettingsChanged = true; 398 fLoaderConfig.setProperty(propertyId, state); 399 if(propertyId.equals( JAXP_SCHEMA_SOURCE)) { 400 fJAXPSource = state; 401 fJAXPProcessed = false; 402 } 403 else if(propertyId.equals( XMLGRAMMAR_POOL)) { 404 fGrammarPool = (XMLGrammarPool)state; 405 } 406 else if (propertyId.equals(SCHEMA_LOCATION)){ 407 fExternalSchemas = (String )state; 408 } 409 else if (propertyId.equals(SCHEMA_NONS_LOCATION)){ 410 fExternalNoNSSchema = (String ) state; 411 } 412 else if (propertyId.equals(ENTITY_RESOLVER)){ 413 fEntityManager.setProperty(ENTITY_RESOLVER, state); 414 } 415 else if (propertyId.equals(ERROR_REPORTER)){ 416 fErrorReporter = (XMLErrorReporter)state; 417 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 418 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 419 } 420 } 421 } 423 431 public void setLocale(Locale locale) { 432 fLocale = locale; 433 fErrorReporter.setLocale(locale); 434 } 436 437 public Locale getLocale() { 438 return fLocale; 439 } 441 446 public void setErrorHandler(XMLErrorHandler errorHandler) { 447 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 448 } 450 451 public XMLErrorHandler getErrorHandler() { 452 return fErrorReporter.getErrorHandler(); 453 } 455 460 public void setEntityResolver(XMLEntityResolver entityResolver) { 461 fUserEntityResolver = entityResolver; 462 fLoaderConfig.setProperty(ENTITY_RESOLVER, entityResolver); 463 fEntityManager.setProperty(ENTITY_RESOLVER, entityResolver); 464 } 466 467 public XMLEntityResolver getEntityResolver() { 468 return fUserEntityResolver; 469 } 471 481 public void loadGrammar(XMLInputSource source[]) 482 throws IOException , XNIException { 483 int numSource = source.length; 484 for (int i = 0; i < numSource; ++i) { 485 loadGrammar(source[i]); 486 } 487 } 488 489 499 public Grammar loadGrammar(XMLInputSource source) 500 throws IOException , XNIException { 501 502 506 reset(fLoaderConfig); 507 fSettingsChanged = false; 508 XSDDescription desc = new XSDDescription(); 509 desc.fContextType = XSDDescription.CONTEXT_PREPARSE; 510 desc.setBaseSystemId(source.getBaseSystemId()); 511 desc.setLiteralSystemId( source.getSystemId()); 512 Hashtable locationPairs = new Hashtable (); 514 processExternalHints(fExternalSchemas, fExternalNoNSSchema, 518 locationPairs, fErrorReporter); 519 SchemaGrammar grammar = loadSchema(desc, source, locationPairs); 520 521 if(grammar != null && fGrammarPool != null) { 522 fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, fGrammarBucket.getGrammars()); 523 if(fIsCheckedFully && fJAXPCache.get(grammar) != grammar) { 526 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 527 } 528 } 529 return grammar; 530 } 532 543 SchemaGrammar loadSchema(XSDDescription desc, 544 XMLInputSource source, 545 Hashtable locationPairs) throws IOException , XNIException { 546 547 if(!fJAXPProcessed) { 550 processJAXPSchemaSource(locationPairs); 551 } 552 SchemaGrammar grammar = fSchemaHandler.parseSchema(source, desc, locationPairs); 553 554 return grammar; 555 } 557 569 public static XMLInputSource resolveDocument(XSDDescription desc, Hashtable locationPairs, 570 XMLEntityResolver entityResolver) throws IOException { 571 String loc = null; 572 if (desc.getContextType() == XSDDescription.CONTEXT_IMPORT || 574 desc.fromInstance()) { 575 String namespace = desc.getTargetNamespace(); 577 String ns = namespace == null ? XMLSymbols.EMPTY_STRING : namespace; 578 LocationArray tempLA = (LocationArray)locationPairs.get(ns); 580 if(tempLA != null) 581 loc = tempLA.getFirstLocation(); 582 } 583 584 if (loc == null) { 587 String [] hints = desc.getLocationHints(); 588 if (hints != null && hints.length > 0) 589 loc = hints[0]; 590 } 591 592 String expandedLoc = XMLEntityManager.expandSystemId(loc, desc.getBaseSystemId(), false); 593 desc.setLiteralSystemId(loc); 594 desc.setExpandedSystemId(expandedLoc); 595 return entityResolver.resolveEntity(desc); 596 } 597 598 public static void processExternalHints(String sl, String nsl, 600 Hashtable locations, 601 XMLErrorReporter er) { 602 if (sl != null) { 603 try { 604 XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_SCHEMALOCATION); 608 attrDecl.fType.validate(sl, null, null); 610 if (!tokenizeSchemaLocationStr(sl, locations)) { 611 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 613 "SchemaLocation", 614 new Object []{sl}, 615 XMLErrorReporter.SEVERITY_WARNING); 616 } 617 } 618 catch (InvalidDatatypeValueException ex) { 619 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 621 ex.getKey(), ex.getArgs(), 622 XMLErrorReporter.SEVERITY_WARNING); 623 } 624 } 625 626 if (nsl != null) { 627 try { 628 XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION); 630 attrDecl.fType.validate(nsl, null, null); 631 LocationArray la = ((LocationArray)locations.get(XMLSymbols.EMPTY_STRING)); 632 if(la == null) { 633 la = new LocationArray(); 634 locations.put(XMLSymbols.EMPTY_STRING, la); 635 } 636 la.addLocation(nsl); 637 } 638 catch (InvalidDatatypeValueException ex) { 639 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 641 ex.getKey(), ex.getArgs(), 642 XMLErrorReporter.SEVERITY_WARNING); 643 } 644 } 645 } 646 public static boolean tokenizeSchemaLocationStr(String schemaStr, Hashtable locations) { 654 if (schemaStr!= null) { 655 StringTokenizer t = new StringTokenizer (schemaStr, " \n\t\r"); 656 String namespace, location; 657 while (t.hasMoreTokens()) { 658 namespace = t.nextToken (); 659 if (!t.hasMoreTokens()) { 660 return false; } 662 location = t.nextToken(); 663 LocationArray la = ((LocationArray)locations.get(namespace)); 664 if(la == null) { 665 la = new LocationArray(); 666 locations.put(namespace, la); 667 } 668 la.addLocation(location); 669 } 670 } 671 return true; 672 } 674 684 private void processJAXPSchemaSource(Hashtable locationPairs) throws IOException { 685 fJAXPProcessed = true; 686 if (fJAXPSource == null) { 687 return; 688 } 689 690 Class componentType = fJAXPSource.getClass().getComponentType(); 691 XMLInputSource xis = null; 692 String sid = null; 693 if (componentType == null) { 694 if(fJAXPSource instanceof InputStream || 696 fJAXPSource instanceof InputSource) { 697 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(fJAXPSource); 698 if(g != null) { 699 fGrammarBucket.putGrammar(g); 700 return; 701 } 702 } 703 fXSDDescription.reset(); 704 xis = xsdToXMLInputSource(fJAXPSource); 705 sid = xis.getSystemId(); 706 fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE; 707 if (sid != null) { 708 fXSDDescription.setBaseSystemId(xis.getBaseSystemId()); 709 fXSDDescription.setLiteralSystemId(sid); 710 fXSDDescription.setExpandedSystemId(sid); 711 fXSDDescription.fLocationHints = new String []{sid}; 712 } 713 SchemaGrammar g = loadSchema(fXSDDescription, xis, locationPairs); 714 if (g != null){ 716 if(fJAXPSource instanceof InputStream || 717 fJAXPSource instanceof InputSource) { 718 fJAXPCache.put(fJAXPSource, g); 719 if(fIsCheckedFully) { 720 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 721 } 722 } 723 fGrammarBucket.putGrammar(g); 724 } 725 return ; 726 } else if ( (componentType != Object .class) && 727 (componentType != String .class) && 728 (componentType != File .class) && 729 (componentType != InputStream .class) && 730 (componentType != InputSource.class) 731 ) { 732 throw new XMLConfigurationException( 734 XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+ 735 "\" property cannot have an array of type {"+componentType.getName()+ 736 "}. Possible types of the array supported are Object, String, File, "+ 737 "InputStream, InputSource."); 738 } 739 740 Object [] objArr = (Object []) fJAXPSource; 743 Vector jaxpSchemaSourceNamespaces = new Vector () ; 745 for (int i = 0; i < objArr.length; i++) { 746 if(objArr[i] instanceof InputStream || 747 objArr[i] instanceof InputSource) { 748 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(objArr[i]); 749 if (g != null) { 750 fGrammarBucket.putGrammar(g); 751 continue; 752 } 753 } 754 fXSDDescription.reset(); 755 xis = xsdToXMLInputSource(objArr[i]); 756 sid = xis.getSystemId(); 757 fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE; 758 if (sid != null) { 759 fXSDDescription.setBaseSystemId(xis.getBaseSystemId()); 760 fXSDDescription.setLiteralSystemId(sid); 761 fXSDDescription.setExpandedSystemId(sid); 762 fXSDDescription.fLocationHints = new String []{sid}; 763 } 764 String targetNamespace = null ; 765 SchemaGrammar grammar = fSchemaHandler.parseSchema(xis,fXSDDescription, locationPairs); 767 768 if(fIsCheckedFully) { 769 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 770 } 771 if(grammar != null){ 772 targetNamespace = grammar.getTargetNamespace() ; 773 if(jaxpSchemaSourceNamespaces.contains(targetNamespace)){ 774 throw new java.lang.IllegalArgumentException ( 776 " When using array of Objects as the value of SCHEMA_SOURCE property , " + 777 "no two Schemas should share the same targetNamespace. " ); 778 } 779 else{ 780 jaxpSchemaSourceNamespaces.add(targetNamespace) ; 781 } 782 if(objArr[i] instanceof InputStream || 783 objArr[i] instanceof InputSource) { 784 fJAXPCache.put(objArr[i], grammar); 785 } 786 fGrammarBucket.putGrammar(grammar); 787 } 788 else{ 789 } 791 } 792 } 794 private XMLInputSource xsdToXMLInputSource( 795 Object val) 796 { 797 if (val instanceof String ) { 798 String loc = (String ) val; 801 fXSDDescription.reset(); 802 fXSDDescription.setValues(null, loc, null, null); 803 XMLInputSource xis = null; 804 try { 805 xis = fEntityManager.resolveEntity(fXSDDescription); 806 } catch (IOException ex) { 807 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 808 "schema_reference.4", 809 new Object [] { loc }, XMLErrorReporter.SEVERITY_ERROR); 810 } 811 if (xis == null) { 812 return new XMLInputSource(null, loc, null); 815 } 816 return xis; 817 } else if (val instanceof InputSource) { 818 return saxToXMLInputSource((InputSource) val); 819 } else if (val instanceof InputStream ) { 820 return new XMLInputSource(null, null, null, 821 (InputStream ) val, null); 822 } else if (val instanceof File ) { 823 File file = (File ) val; 824 InputStream is = null; 825 try { 826 is = new BufferedInputStream (new FileInputStream (file)); 827 } catch (FileNotFoundException ex) { 828 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 829 "schema_reference.4", new Object [] { file.toString() }, 830 XMLErrorReporter.SEVERITY_ERROR); 831 } 832 return new XMLInputSource(null, null, null, is, null); 833 } 834 throw new XMLConfigurationException( 835 XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+ 836 "\" property cannot have a value of type {"+val.getClass().getName()+ 837 "}. Possible types of the value supported are String, File, InputStream, "+ 838 "InputSource OR an array of these types."); 839 } 840 841 842 844 private static XMLInputSource saxToXMLInputSource(InputSource sis) { 845 String publicId = sis.getPublicId(); 846 String systemId = sis.getSystemId(); 847 848 Reader charStream = sis.getCharacterStream(); 849 if (charStream != null) { 850 return new XMLInputSource(publicId, systemId, null, charStream, 851 null); 852 } 853 854 InputStream byteStream = sis.getByteStream(); 855 if (byteStream != null) { 856 return new XMLInputSource(publicId, systemId, null, byteStream, 857 sis.getEncoding()); 858 } 859 860 return new XMLInputSource(publicId, systemId, null); 861 } 862 863 static class LocationArray{ 864 865 int length ; 866 String [] locations = new String [2]; 867 868 public void resize(int oldLength , int newLength){ 869 String [] temp = new String [newLength] ; 870 System.arraycopy(locations, 0, temp, 0, Math.min(oldLength, newLength)); 871 locations = temp ; 872 length = Math.min(oldLength, newLength); 873 } 874 875 public void addLocation(String location){ 876 if(length >= locations.length ){ 877 resize(length, Math.max(1, length*2)); 878 } 879 locations[length++] = location; 880 } 882 public String [] getLocationArray(){ 883 if(length < locations.length ){ 884 resize(locations.length, length); 885 } 886 return locations; 887 } 889 public String getFirstLocation(){ 890 return length > 0 ? locations[0] : null; 891 } 892 893 public int getLength(){ 894 return length ; 895 } 896 897 } 899 902 public Boolean getFeatureDefault(String featureId) { 903 if (featureId.equals(AUGMENT_PSVI)){ 904 return Boolean.TRUE; 905 } 906 return null; 907 } 908 909 912 public Object getPropertyDefault(String propertyId) { 913 return null; 915 } 916 917 920 public void reset(XMLComponentManager componentManager) throws XMLConfigurationException { 921 922 fGrammarBucket.reset(); 923 924 fSubGroupHandler.reset(); 925 926 boolean parser_settings; 927 try { 928 parser_settings = componentManager.getFeature(PARSER_SETTINGS); 929 } 930 catch (XMLConfigurationException e){ 931 parser_settings = true; 932 } 933 if (!parser_settings || !fSettingsChanged){ 934 initGrammarBucket(); 936 return; 937 } 938 939 fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER); 943 944 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 946 947 boolean psvi = true; 948 try { 949 psvi = componentManager.getFeature(AUGMENT_PSVI); 950 } catch (XMLConfigurationException e) { 951 psvi = false; 952 } 953 954 if (!psvi) { 955 fDeclPool.reset(); 956 fCMBuilder.setDeclPool(fDeclPool); 957 fSchemaHandler.setDeclPool(fDeclPool); 958 } else { 959 fCMBuilder.setDeclPool(null); 960 fSchemaHandler.setDeclPool(null); 961 } 962 963 try { 965 fExternalSchemas = (String ) componentManager.getProperty(SCHEMA_LOCATION); 966 fExternalNoNSSchema = 967 (String ) componentManager.getProperty(SCHEMA_NONS_LOCATION); 968 } catch (XMLConfigurationException e) { 969 fExternalSchemas = null; 970 fExternalNoNSSchema = null; 971 } 972 try { 974 fJAXPSource = componentManager.getProperty(JAXP_SCHEMA_SOURCE); 975 fJAXPProcessed = false; 976 977 } catch (XMLConfigurationException e) { 978 fJAXPSource = null; 979 fJAXPProcessed = false; 980 } 981 982 try { 984 fGrammarPool = (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL); 985 } catch (XMLConfigurationException e) { 986 fGrammarPool = null; 987 } 988 initGrammarBucket(); 989 try { 991 boolean fatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR); 992 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, fatalError); 993 } catch (XMLConfigurationException e) { 994 } 995 try { 997 fIsCheckedFully = componentManager.getFeature(SCHEMA_FULL_CHECKING); 998 } 999 catch (XMLConfigurationException e){ 1000 fIsCheckedFully = false; 1001 } 1002 try { 1004 fSchemaHandler.setGenerateSyntheticAnnotations(componentManager.getFeature(GENERATE_SYNTHETIC_ANNOTATIONS)); 1005 } 1006 catch (XMLConfigurationException e) { 1007 fSchemaHandler.setGenerateSyntheticAnnotations(false); 1008 } 1009 fSchemaHandler.reset(componentManager); 1010 } 1011 1012 private void initGrammarBucket(){ 1013 if(fGrammarPool != null) { 1014 Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); 1015 for (int i = 0; i < initialGrammars.length; i++) { 1016 if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) { 1019 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 1022 "GrammarConflict", null, 1023 XMLErrorReporter.SEVERITY_WARNING); 1024 } 1025 } 1026 } 1027 } 1028 1029 1030 1033 public DOMConfiguration getConfig() { 1034 return this; 1035 } 1036 1037 1040 public XSModel load(LSInput is) { 1041 try { 1042 Grammar g = loadGrammar(dom2xmlInputSource(is)); 1043 return ((XSGrammar) g).toXSModel(); 1044 } catch (Exception e) { 1045 reportDOMFatalError(e); 1046 return null; 1047 } 1048 } 1049 1050 1053 public XSModel loadInputList(LSInputList is) { 1054 int length = is.getLength(); 1055 if (length == 0) { 1056 return null; 1057 } 1058 SchemaGrammar[] gs = new SchemaGrammar[length]; 1059 for (int i = 0; i < length; i++) { 1060 try { 1061 gs[i] = (SchemaGrammar) loadGrammar(dom2xmlInputSource(is.item(i))); 1062 } catch (Exception e) { 1063 reportDOMFatalError(e); 1064 return null; 1065 } 1066 } 1067 return new XSModelImpl(gs); 1068 } 1069 1070 1073 public XSModel loadURI(String uri) { 1074 try { 1075 Grammar g = loadGrammar(new XMLInputSource(null, uri, null)); 1076 return ((XSGrammar)g).toXSModel(); 1077 } 1078 catch (Exception e){ 1079 reportDOMFatalError(e); 1080 return null; 1081 } 1082 } 1083 1084 1087 public XSModel loadURIList(StringList uriList) { 1088 int length = uriList.getLength(); 1089 if (length == 0) { 1090 return null; 1091 } 1092 SchemaGrammar[] gs = new SchemaGrammar[length]; 1093 for (int i = 0; i < length; i++) { 1094 try { 1095 gs[i] = 1096 (SchemaGrammar) loadGrammar(new XMLInputSource(null, uriList.item(i), null)); 1097 } catch (Exception e) { 1098 reportDOMFatalError(e); 1099 return null; 1100 } 1101 } 1102 return new XSModelImpl(gs); 1103 } 1104 1105 void reportDOMFatalError(Exception e) { 1106 if (fErrorHandler != null) { 1107 DOMErrorImpl error = new DOMErrorImpl(); 1108 error.fException = e; 1109 error.fMessage = e.getMessage(); 1110 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1111 fErrorHandler.getErrorHandler().handleError(error); 1112 } 1113 } 1114 1115 1118 public boolean canSetParameter(String name, Object value) { 1119 if(value instanceof Boolean ){ 1120 if (name.equals(Constants.DOM_VALIDATE) || 1121 name.equals(SCHEMA_FULL_CHECKING) || 1122 name.equals(VALIDATE_ANNOTATIONS) || 1123 name.equals(CONTINUE_AFTER_FATAL_ERROR) || 1124 name.equals(ALLOW_JAVA_ENCODINGS) || 1125 name.equals(STANDARD_URI_CONFORMANT_FEATURE) || 1126 name.equals(GENERATE_SYNTHETIC_ANNOTATIONS) || 1127 name.equals(HONOUR_ALL_SCHEMALOCATIONS)) { 1128 return true; 1129 1130 } 1131 return false; 1132 } 1133 if (name.equals(Constants.DOM_ERROR_HANDLER) || 1134 name.equals(Constants.DOM_RESOURCE_RESOLVER) || 1135 name.equals(SYMBOL_TABLE) || 1136 name.equals(ERROR_REPORTER) || 1137 name.equals(ERROR_HANDLER) || 1138 name.equals(ENTITY_RESOLVER) || 1139 name.equals(XMLGRAMMAR_POOL) || 1140 name.equals(SCHEMA_LOCATION) || 1141 name.equals(SCHEMA_NONS_LOCATION) || 1142 name.equals(JAXP_SCHEMA_SOURCE)) { 1143 return true; 1144 } 1145 return false; 1146 } 1147 1148 1151 public Object getParameter(String name) throws DOMException { 1152 1153 if (name.equals(Constants.DOM_ERROR_HANDLER)){ 1154 return (fErrorHandler != null) ? fErrorHandler.getErrorHandler() : null; 1155 } 1156 else if (name.equals(Constants.DOM_RESOURCE_RESOLVER)) { 1157 return (fResourceResolver != null) ? fResourceResolver.getEntityResolver() : null; 1158 } 1159 1160 try { 1161 boolean feature = getFeature(name); 1162 return (feature) ? Boolean.TRUE : Boolean.FALSE; 1163 } catch (Exception e) { 1164 Object property; 1165 try { 1166 property = getProperty(name); 1167 return property; 1168 } catch (Exception ex) { 1169 String msg = 1170 DOMMessageFormatter.formatMessage( 1171 DOMMessageFormatter.DOM_DOMAIN, 1172 "FEATURE_NOT_SUPPORTED", 1173 new Object [] { name }); 1174 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1175 } 1176 } 1177 } 1178 1179 1182 public DOMStringList getParameterNames() { 1183 if (fRecognizedParameters == null){ 1184 Vector v = new Vector (); 1185 v.add(Constants.DOM_VALIDATE); 1186 v.add(Constants.DOM_ERROR_HANDLER); 1187 v.add(Constants.DOM_RESOURCE_RESOLVER); 1188 v.add(SYMBOL_TABLE); 1189 v.add(ERROR_REPORTER); 1190 v.add(ERROR_HANDLER); 1191 v.add(ENTITY_RESOLVER); 1192 v.add(XMLGRAMMAR_POOL); 1193 v.add(SCHEMA_LOCATION); 1194 v.add(SCHEMA_NONS_LOCATION); 1195 v.add(JAXP_SCHEMA_SOURCE); 1196 v.add(SCHEMA_FULL_CHECKING); 1197 v.add(CONTINUE_AFTER_FATAL_ERROR); 1198 v.add(ALLOW_JAVA_ENCODINGS); 1199 v.add(STANDARD_URI_CONFORMANT_FEATURE); 1200 v.add(VALIDATE_ANNOTATIONS); 1201 v.add(GENERATE_SYNTHETIC_ANNOTATIONS); 1202 v.add(HONOUR_ALL_SCHEMALOCATIONS); 1203 fRecognizedParameters = new DOMStringListImpl(v); 1204 } 1205 return fRecognizedParameters; 1206 } 1207 1208 1211 public void setParameter(String name, Object value) throws DOMException { 1212 if (value instanceof Boolean ) { 1213 boolean state = ((Boolean ) value).booleanValue(); 1214 if (name.equals("validate") && state) { 1215 return; 1216 } 1217 try { 1218 setFeature(name, state); 1219 } catch (Exception e) { 1220 String msg = 1221 DOMMessageFormatter.formatMessage( 1222 DOMMessageFormatter.DOM_DOMAIN, 1223 "FEATURE_NOT_SUPPORTED", 1224 new Object [] { name }); 1225 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1226 } 1227 return; 1228 } 1229 if (name.equals(Constants.DOM_ERROR_HANDLER)) { 1230 if (value instanceof DOMErrorHandler ) { 1231 try { 1232 fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler ) value); 1233 setErrorHandler(fErrorHandler); 1234 } catch (XMLConfigurationException e) { 1235 } 1236 } else { 1237 String msg = 1239 DOMMessageFormatter.formatMessage( 1240 DOMMessageFormatter.DOM_DOMAIN, 1241 "FEATURE_NOT_SUPPORTED", 1242 new Object [] { name }); 1243 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1244 } 1245 return; 1246 1247 } 1248 if (name.equals(Constants.DOM_RESOURCE_RESOLVER)) { 1249 if (value instanceof LSResourceResolver ) { 1250 try { 1251 fResourceResolver = new DOMEntityResolverWrapper((LSResourceResolver ) value); 1252 setEntityResolver(fResourceResolver); 1253 } 1254 catch (XMLConfigurationException e) {} 1255 } else { 1256 String msg = 1258 DOMMessageFormatter.formatMessage( 1259 DOMMessageFormatter.DOM_DOMAIN, 1260 "FEATURE_NOT_SUPPORTED", 1261 new Object [] { name }); 1262 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1263 } 1264 return; 1265 } 1266 1267 try { 1268 setProperty(name, value); 1269 } catch (Exception ex) { 1270 1271 String msg = 1272 DOMMessageFormatter.formatMessage( 1273 DOMMessageFormatter.DOM_DOMAIN, 1274 "FEATURE_NOT_SUPPORTED", 1275 new Object [] { name }); 1276 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1277 1278 } 1279 1280 } 1281 1282 XMLInputSource dom2xmlInputSource(LSInput is) { 1283 XMLInputSource xis = null; 1285 1286 1292 1293 if (is.getCharacterStream() != null) { 1296 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1297 is.getBaseURI(), is.getCharacterStream(), 1298 "UTF-16"); 1299 } 1300 else if (is.getByteStream() != null) { 1302 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1303 is.getBaseURI(), is.getByteStream(), 1304 is.getEncoding()); 1305 } 1306 else if (is.getStringData() != null && is.getStringData().length() != 0) { 1309 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1310 is.getBaseURI(), new StringReader (is.getStringData()), 1311 "UTF-16"); 1312 } 1313 else { 1315 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1316 is.getBaseURI()); 1317 } 1318 1319 return xis; 1320 } 1321 1322} 1324 | Popular Tags |