1 57 58 package com.sun.org.apache.xerces.internal.impl.xs; 59 60 import java.io.BufferedInputStream ; 61 import java.io.File ; 62 import java.io.FileInputStream ; 63 import java.io.FileNotFoundException ; 64 import java.io.IOException ; 65 import java.io.InputStream ; 66 import java.io.Reader ; 67 import java.io.StringReader ; 68 import java.util.Hashtable ; 69 import java.util.Locale ; 70 import java.util.StringTokenizer ; 71 import java.util.Vector ; 72 73 import javax.xml.transform.Source ; 74 75 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl; 76 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 77 import com.sun.org.apache.xerces.internal.dom.DOMStringListImpl; 78 import com.sun.org.apache.xerces.internal.impl.Constants; 79 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 80 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 81 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException; 82 import com.sun.org.apache.xerces.internal.impl.xs.models.CMBuilder; 83 import com.sun.org.apache.xerces.internal.impl.xs.models.CMNodeFactory; 84 import com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler; 85 import com.sun.org.apache.xerces.internal.util.DOMErrorHandlerWrapper; 86 import com.sun.org.apache.xerces.internal.util.DefaultErrorHandler; 87 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 88 import com.sun.org.apache.xerces.internal.util.SymbolTable; 89 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 90 import com.sun.org.apache.xerces.internal.xni.XNIException; 91 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 92 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 93 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader; 94 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 95 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar; 96 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 97 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 98 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 99 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 100 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 101 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 102 import com.sun.org.apache.xerces.internal.xs.LSInputList; 103 import com.sun.org.apache.xerces.internal.xs.StringList; 104 import com.sun.org.apache.xerces.internal.xs.XSLoader; 105 import com.sun.org.apache.xerces.internal.xs.XSModel; 106 import org.w3c.dom.DOMConfiguration ; 107 import org.w3c.dom.DOMError ; 108 import org.w3c.dom.DOMErrorHandler ; 109 import org.w3c.dom.DOMException ; 110 import org.w3c.dom.DOMStringList ; 111 import org.w3c.dom.ls.LSInput ; 112 import org.xml.sax.InputSource ; 113 114 129 130 public class XMLSchemaLoader implements XMLGrammarLoader, XMLComponent, 131 XSLoader, DOMConfiguration { 133 134 136 137 protected static final String SCHEMA_FULL_CHECKING = 138 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 139 140 141 protected static final String CONTINUE_AFTER_FATAL_ERROR = 142 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 143 144 145 protected static final String ALLOW_JAVA_ENCODINGS = 146 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 147 148 149 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 150 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 151 152 153 protected static final String DISALLOW_DOCTYPE = 154 Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE; 155 156 157 protected static final String AUGMENT_PSVI = 158 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 159 160 protected static final String PARSER_SETTINGS = 161 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 162 163 private static final String [] RECOGNIZED_FEATURES = { 165 SCHEMA_FULL_CHECKING, 166 AUGMENT_PSVI, 167 CONTINUE_AFTER_FATAL_ERROR, 168 ALLOW_JAVA_ENCODINGS, 169 STANDARD_URI_CONFORMANT_FEATURE, 170 DISALLOW_DOCTYPE 171 }; 172 173 175 176 public static final String SYMBOL_TABLE = 177 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 178 179 180 public static final String ERROR_REPORTER = 181 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 182 183 184 protected static final String ERROR_HANDLER = 185 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 186 187 188 public static final String ENTITY_RESOLVER = 189 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 190 191 192 public static final String XMLGRAMMAR_POOL = 193 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 194 195 196 protected static final String SCHEMA_LOCATION = 197 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION; 198 199 200 protected static final String SCHEMA_NONS_LOCATION = 201 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION; 202 203 204 protected static final String JAXP_SCHEMA_SOURCE = 205 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 206 207 protected static final String SECURITY_MANAGER = 208 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 209 210 protected static final String ENTITY_MANAGER = 211 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 212 213 private static final String [] RECOGNIZED_PROPERTIES = { 215 ENTITY_MANAGER, 216 SYMBOL_TABLE, 217 ERROR_REPORTER, 218 ERROR_HANDLER, 219 ENTITY_RESOLVER, 220 XMLGRAMMAR_POOL, 221 SCHEMA_LOCATION, 222 SCHEMA_NONS_LOCATION, 223 JAXP_SCHEMA_SOURCE, 224 SECURITY_MANAGER 225 }; 226 227 229 private ParserConfigurationSettings fLoaderConfig = new ParserConfigurationSettings(); 231 private SymbolTable fSymbolTable = null; 232 private XMLErrorReporter fErrorReporter = new XMLErrorReporter (); 233 private XMLEntityManager fEntityManager = null; 234 private XMLEntityResolver fUserEntityResolver = null; 235 private XMLGrammarPool fGrammarPool = null; 236 private String fExternalSchemas = null; 237 private String fExternalNoNSSchema = null; 238 private Object fJAXPSource = null; 240 private boolean fIsCheckedFully = false; 242 private boolean fJAXPProcessed = false; 244 private boolean fSettingsChanged = true; 246 247 private XSDHandler fSchemaHandler; 249 private XSGrammarBucket fGrammarBucket; 250 private XSDeclarationPool fDeclPool = null; 251 private SubstitutionGroupHandler fSubGroupHandler; 252 private CMBuilder fCMBuilder; 253 private XSDDescription fXSDDescription = new XSDDescription(); 254 255 private Hashtable fJAXPCache; 256 private Locale fLocale = Locale.getDefault(); 257 258 private DOMStringList fRecognizedParameters = null; 260 261 private DOMErrorHandlerWrapper fErrorHandler = null; 262 263 public XMLSchemaLoader() { 265 this( new SymbolTable(), null, new XMLEntityManager(), null, null, null); 266 } 267 268 public XMLSchemaLoader(SymbolTable symbolTable) { 269 this( symbolTable, null, new XMLEntityManager(), null, null, null); 270 } 271 272 280 XMLSchemaLoader(XMLErrorReporter errorReporter, 281 XSGrammarBucket grammarBucket, 282 SubstitutionGroupHandler sHandler, CMBuilder builder) { 283 this(null, errorReporter, new XMLEntityManager(), grammarBucket, sHandler, builder); 284 } 285 286 XMLSchemaLoader(SymbolTable symbolTable, 287 XMLErrorReporter errorReporter, 288 XMLEntityManager entityResolver, 289 XSGrammarBucket grammarBucket, 290 SubstitutionGroupHandler sHandler, 291 CMBuilder builder) { 292 293 fLoaderConfig.addRecognizedFeatures(RECOGNIZED_FEATURES); 295 fLoaderConfig.addRecognizedProperties(RECOGNIZED_PROPERTIES); 296 if (symbolTable != null){ 297 fLoaderConfig.setProperty(SYMBOL_TABLE, symbolTable); 298 } 299 300 if(errorReporter == null) { 301 errorReporter = new XMLErrorReporter (); 302 errorReporter.setLocale(fLocale); 303 errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler()); 304 305 } 306 fErrorReporter = errorReporter; 307 if(fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 309 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 310 } 311 fLoaderConfig.setProperty(ERROR_REPORTER, fErrorReporter); 312 fEntityManager = entityResolver; 313 if (fEntityManager != null){ 315 fLoaderConfig.setProperty(ENTITY_MANAGER, fEntityManager); 316 } 317 318 fLoaderConfig.setFeature(AUGMENT_PSVI, true); 320 321 if(grammarBucket == null ) { 322 grammarBucket = new XSGrammarBucket(); 323 } 324 fGrammarBucket = grammarBucket; 325 if(sHandler == null) { 326 sHandler = new SubstitutionGroupHandler(fGrammarBucket); 327 } 328 fSubGroupHandler = sHandler; 329 330 CMNodeFactory nodeFactory = new CMNodeFactory() ; 332 333 if(builder == null) { 334 builder = new CMBuilder(nodeFactory); 335 } 336 fCMBuilder = builder; 337 fSchemaHandler = new XSDHandler(fGrammarBucket); 338 fDeclPool = new XSDeclarationPool(); 339 fJAXPCache = new Hashtable (); 340 341 fSettingsChanged = true; 342 } 343 344 349 public String [] getRecognizedFeatures() { 350 return (String [])(RECOGNIZED_FEATURES.clone()); 351 } 353 360 public boolean getFeature(String featureId) 361 throws XMLConfigurationException { 362 return fLoaderConfig.getFeature(featureId); 363 } 365 374 public void setFeature(String featureId, 375 boolean state) throws XMLConfigurationException { 376 fSettingsChanged = true; 377 if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) { 378 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, state); 379 } 380 fLoaderConfig.setFeature(featureId, state); 381 } 383 388 public String [] getRecognizedProperties() { 389 return (String [])(RECOGNIZED_PROPERTIES.clone()); 390 } 392 399 public Object getProperty(String propertyId) 400 throws XMLConfigurationException { 401 return fLoaderConfig.getProperty(propertyId); 402 } 404 413 public void setProperty(String propertyId, 414 Object state) throws XMLConfigurationException { 415 fSettingsChanged = true; 416 fLoaderConfig.setProperty(propertyId, state); 417 if(propertyId.equals( JAXP_SCHEMA_SOURCE)) { 418 fJAXPSource = state; 419 fJAXPProcessed = false; 420 } 421 else if(propertyId.equals( XMLGRAMMAR_POOL)) { 422 fGrammarPool = (XMLGrammarPool)state; 423 } 424 else if (propertyId.equals(SCHEMA_LOCATION)){ 425 fExternalSchemas = (String )state; 426 } 427 else if (propertyId.equals(SCHEMA_NONS_LOCATION)){ 428 fExternalNoNSSchema = (String ) state; 429 } 430 else if (propertyId.equals(ENTITY_RESOLVER)){ 431 fEntityManager.setProperty(ENTITY_RESOLVER, state); 432 } 433 else if (propertyId.equals(ERROR_REPORTER)){ 434 fErrorReporter = (XMLErrorReporter)state; 435 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 436 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 437 } 438 } 439 } 441 449 public void setLocale(Locale locale) { 450 fLocale = locale; 451 fErrorReporter.setLocale(locale); 452 } 454 455 public Locale getLocale() { 456 return fLocale; 457 } 459 464 public void setErrorHandler(XMLErrorHandler errorHandler) { 465 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 466 } 468 469 public XMLErrorHandler getErrorHandler() { 470 return fErrorReporter.getErrorHandler(); 471 } 473 478 public void setEntityResolver(XMLEntityResolver entityResolver) { 479 fUserEntityResolver = entityResolver; 480 fLoaderConfig.setProperty(ENTITY_RESOLVER, entityResolver); 481 fEntityManager.setProperty(ENTITY_RESOLVER, entityResolver); 482 } 484 485 public XMLEntityResolver getEntityResolver() { 486 return fUserEntityResolver; 487 } 489 490 500 public Grammar loadGrammar(XMLInputSource source) throws IOException , XNIException { 501 return loadGrammar( source.toSource() ); 502 } 503 504 public Grammar loadGrammar(Source source) 505 throws IOException , XNIException { 506 507 511 reset(fLoaderConfig); 512 fSettingsChanged = false; 513 XSDDescription desc = new XSDDescription(); 514 desc.fContextType = XSDDescription.CONTEXT_PREPARSE; 515 desc.setLiteralSystemId( source.getSystemId()); 516 Hashtable locationPairs = new Hashtable (); 518 processExternalHints(fExternalSchemas, fExternalNoNSSchema, 522 locationPairs, fErrorReporter); 523 SchemaGrammar grammar = loadSchema(desc, source, locationPairs); 524 525 if(grammar != null && fGrammarPool != null) { 526 fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, fGrammarBucket.getGrammars()); 527 if(fIsCheckedFully && fJAXPCache.get(grammar) != grammar) { 530 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 531 } 532 } 533 return grammar; 534 } 536 547 SchemaGrammar loadSchema(XSDDescription desc, 548 Source source, 549 Hashtable locationPairs) throws IOException , XNIException { 550 551 if(!fJAXPProcessed) { 554 processJAXPSchemaSource(locationPairs); 555 } 556 SchemaGrammar grammar = fSchemaHandler.parseSchema(source, desc, locationPairs); 557 558 return grammar; 559 } 561 573 public static XMLInputSource resolveDocument(XSDDescription desc, Hashtable locationPairs, 574 XMLEntityResolver entityResolver) throws IOException { 575 String loc = null; 576 if (desc.getContextType() == XSDDescription.CONTEXT_IMPORT || 578 desc.fromInstance()) { 579 String namespace = desc.getTargetNamespace(); 581 String ns = namespace == null ? XMLSymbols.EMPTY_STRING : namespace; 582 LocationArray tempLA = (LocationArray)locationPairs.get(ns); 584 if(tempLA != null) 585 loc = tempLA.getFirstLocation(); 586 } 587 588 if (loc == null) { 591 String [] hints = desc.getLocationHints(); 592 if (hints != null && hints.length > 0) 593 loc = hints[0]; 594 } 595 596 String expandedLoc = XMLEntityManager.expandSystemId(loc, desc.getBaseSystemId(), false); 597 desc.setLiteralSystemId(loc); 598 desc.setExpandedSystemId(expandedLoc); 599 return entityResolver.resolveEntity(desc); 600 } 601 602 public static void processExternalHints(String sl, String nsl, 604 Hashtable locations, 605 XMLErrorReporter er) { 606 if (sl != null) { 607 try { 608 XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_SCHEMALOCATION); 612 attrDecl.fType.validate(sl, null, null); 614 if (!tokenizeSchemaLocationStr(sl, locations)) { 615 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 617 "SchemaLocation", 618 new Object []{sl}, 619 XMLErrorReporter.SEVERITY_WARNING); 620 } 621 } 622 catch (InvalidDatatypeValueException ex) { 623 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 625 ex.getKey(), ex.getArgs(), 626 XMLErrorReporter.SEVERITY_WARNING); 627 } 628 } 629 630 if (nsl != null) { 631 try { 632 XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION); 634 attrDecl.fType.validate(nsl, null, null); 635 LocationArray la = ((LocationArray)locations.get(XMLSymbols.EMPTY_STRING)); 636 if(la == null) { 637 la = new LocationArray(); 638 locations.put(XMLSymbols.EMPTY_STRING, la); 639 } 640 la.addLocation(nsl); 641 } 642 catch (InvalidDatatypeValueException ex) { 643 er.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 645 ex.getKey(), ex.getArgs(), 646 XMLErrorReporter.SEVERITY_WARNING); 647 } 648 } 649 } 650 public static boolean tokenizeSchemaLocationStr(String schemaStr, Hashtable locations) { 658 if (schemaStr!= null) { 659 StringTokenizer t = new StringTokenizer (schemaStr, " \n\t\r"); 660 String namespace, location; 661 while (t.hasMoreTokens()) { 662 namespace = t.nextToken (); 663 if (!t.hasMoreTokens()) { 664 return false; } 666 location = t.nextToken(); 667 LocationArray la = ((LocationArray)locations.get(namespace)); 668 if(la == null) { 669 la = new LocationArray(); 670 locations.put(namespace, la); 671 } 672 la.addLocation(location); 673 } 674 } 675 return true; 676 } 678 688 private void processJAXPSchemaSource(Hashtable locationPairs) throws IOException { 689 fJAXPProcessed = true; 690 if (fJAXPSource == null) { 691 return; 692 } 693 694 Class componentType = fJAXPSource.getClass().getComponentType(); 695 XMLInputSource xis = null; 696 String sid = null; 697 if (componentType == null) { 698 if(fJAXPSource instanceof InputStream || 700 fJAXPSource instanceof InputSource) { 701 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(fJAXPSource); 702 if(g != null) { 703 fGrammarBucket.putGrammar(g); 704 return; 705 } 706 } 707 fXSDDescription.reset(); 708 xis = xsdToXMLInputSource(fJAXPSource); 709 sid = xis.getSystemId(); 710 fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE; 711 if (sid != null) { 712 fXSDDescription.setBaseSystemId(xis.getBaseSystemId()); 713 fXSDDescription.setLiteralSystemId(sid); 714 fXSDDescription.setExpandedSystemId(sid); 715 fXSDDescription.fLocationHints = new String []{sid}; 716 } 717 SchemaGrammar g = loadSchema(fXSDDescription, xis.toSource(), locationPairs); 718 if (g != null){ 720 if(fJAXPSource instanceof InputStream || 721 fJAXPSource instanceof InputSource) { 722 fJAXPCache.put(fJAXPSource, g); 723 if(fIsCheckedFully) { 724 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 725 } 726 } 727 fGrammarBucket.putGrammar(g); 728 } 729 return ; 730 } else if ( (componentType != Object .class) && 731 (componentType != String .class) && 732 (componentType != File .class) && 733 (componentType != InputStream .class) && 734 (componentType != InputSource.class) 735 ) { 736 throw new XMLConfigurationException( 738 XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+ 739 "\" property cannot have an array of type {"+componentType.getName()+ 740 "}. Possible types of the array supported are Object, String, File, "+ 741 "InputStream, InputSource."); 742 } 743 744 Object [] objArr = (Object []) fJAXPSource; 747 Vector jaxpSchemaSourceNamespaces = new Vector () ; 749 for (int i = 0; i < objArr.length; i++) { 750 if(objArr[i] instanceof InputStream || 751 objArr[i] instanceof InputSource) { 752 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(objArr[i]); 753 if (g != null) { 754 fGrammarBucket.putGrammar(g); 755 continue; 756 } 757 } 758 fXSDDescription.reset(); 759 xis = xsdToXMLInputSource(objArr[i]); 760 sid = xis.getSystemId(); 761 fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE; 762 if (sid != null) { 763 fXSDDescription.setBaseSystemId(xis.getBaseSystemId()); 764 fXSDDescription.setLiteralSystemId(sid); 765 fXSDDescription.setExpandedSystemId(sid); 766 fXSDDescription.fLocationHints = new String []{sid}; 767 } 768 String targetNamespace = null ; 769 SchemaGrammar grammar = fSchemaHandler.parseSchema(xis.toSource(),fXSDDescription, locationPairs); 771 772 if(fIsCheckedFully) { 773 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter); 774 } 775 if(grammar != null){ 776 targetNamespace = grammar.getTargetNamespace() ; 777 if(jaxpSchemaSourceNamespaces.contains(targetNamespace)){ 778 throw new java.lang.IllegalArgumentException ( 780 " When using array of Objects as the value of SCHEMA_SOURCE property , " + 781 "no two Schemas should share the same targetNamespace. " ); 782 } 783 else{ 784 jaxpSchemaSourceNamespaces.add(targetNamespace) ; 785 } 786 if(objArr[i] instanceof InputStream || 787 objArr[i] instanceof InputSource) { 788 fJAXPCache.put(objArr[i], grammar); 789 } 790 fGrammarBucket.putGrammar(grammar); 791 } 792 else{ 793 } 795 } 796 } 798 private XMLInputSource xsdToXMLInputSource( 799 Object val) 800 { 801 if (val instanceof String ) { 802 String loc = (String ) val; 805 fXSDDescription.reset(); 806 fXSDDescription.setValues(null, loc, null, null); 807 XMLInputSource xis = null; 808 try { 809 xis = fEntityManager.resolveEntity(fXSDDescription); 810 } catch (IOException ex) { 811 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 812 "schema_reference.4", 813 new Object [] { loc }, XMLErrorReporter.SEVERITY_ERROR); 814 } 815 if (xis == null) { 816 return new XMLInputSource(null, loc, null); 819 } 820 return xis; 821 } else if (val instanceof InputSource) { 822 return saxToXMLInputSource((InputSource) val); 823 } else if (val instanceof InputStream ) { 824 return new XMLInputSource(null, null, null, 825 (InputStream ) val, null); 826 } else if (val instanceof File ) { 827 File file = (File ) val; 828 InputStream is = null; 829 try { 830 is = new BufferedInputStream (new FileInputStream (file)); 831 } catch (FileNotFoundException ex) { 832 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 833 "schema_reference.4", new Object [] { file.toString() }, 834 XMLErrorReporter.SEVERITY_ERROR); 835 } 836 return new XMLInputSource(null, null, null, is, null); 837 } 838 throw new XMLConfigurationException( 839 XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+ 840 "\" property cannot have a value of type {"+val.getClass().getName()+ 841 "}. Possible types of the value supported are String, File, InputStream, "+ 842 "InputSource OR an array of these types."); 843 } 844 845 846 848 private static XMLInputSource saxToXMLInputSource(InputSource sis) { 849 String publicId = sis.getPublicId(); 850 String systemId = sis.getSystemId(); 851 852 Reader charStream = sis.getCharacterStream(); 853 if (charStream != null) { 854 return new XMLInputSource(publicId, systemId, null, charStream, 855 null); 856 } 857 858 InputStream byteStream = sis.getByteStream(); 859 if (byteStream != null) { 860 return new XMLInputSource(publicId, systemId, null, byteStream, 861 sis.getEncoding()); 862 } 863 864 return new XMLInputSource(publicId, systemId, null); 865 } 866 867 static class LocationArray{ 868 869 int length ; 870 String [] locations = new String [2]; 871 872 public void resize(int oldLength , int newLength){ 873 String [] temp = new String [newLength] ; 874 System.arraycopy(locations, 0, temp, 0, Math.min(oldLength, newLength)); 875 locations = temp ; 876 length = Math.min(oldLength, newLength); 877 } 878 879 public void addLocation(String location){ 880 if(length >= locations.length ){ 881 resize(length, Math.max(1, length*2)); 882 } 883 locations[length++] = location; 884 } 886 public String [] getLocationArray(){ 887 if(length < locations.length ){ 888 resize(locations.length, length); 889 } 890 return locations; 891 } 893 public String getFirstLocation(){ 894 return length > 0 ? locations[0] : null; 895 } 896 897 public int getLength(){ 898 return length ; 899 } 900 901 } 903 906 public Boolean getFeatureDefault(String featureId) { 907 if (featureId.equals(AUGMENT_PSVI)){ 908 return Boolean.TRUE; 909 } 910 return null; 911 } 912 913 916 public Object getPropertyDefault(String propertyId) { 917 return null; 919 } 920 921 924 public void reset(XMLComponentManager componentManager) throws XMLConfigurationException { 925 926 fGrammarBucket.reset(); 927 928 fSubGroupHandler.reset(); 929 930 fIsCheckedFully = false; 932 boolean parser_settings; 933 try { 934 parser_settings = componentManager.getFeature(PARSER_SETTINGS); 935 } 936 catch (XMLConfigurationException e){ 937 parser_settings = true; 938 } 939 if (!parser_settings || !fSettingsChanged){ 940 initGrammarBucket(); 942 return; 943 } 944 945 fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER); 949 950 boolean psvi = true; 951 try { 952 psvi = componentManager.getFeature(AUGMENT_PSVI); 953 } catch (XMLConfigurationException e) { 954 psvi = false; 955 } 956 957 if (!psvi) { 958 fDeclPool.reset(); 959 fCMBuilder.setDeclPool(fDeclPool); 960 fSchemaHandler.setDeclPool(fDeclPool); 961 } else { 962 fCMBuilder.setDeclPool(null); 963 fSchemaHandler.setDeclPool(null); 964 } 965 966 try { 968 fExternalSchemas = (String ) componentManager.getProperty(SCHEMA_LOCATION); 969 fExternalNoNSSchema = 970 (String ) componentManager.getProperty(SCHEMA_NONS_LOCATION); 971 } catch (XMLConfigurationException e) { 972 fExternalSchemas = null; 973 fExternalNoNSSchema = null; 974 } 975 try { 977 fJAXPSource = componentManager.getProperty(JAXP_SCHEMA_SOURCE); 978 fJAXPProcessed = false; 979 980 } catch (XMLConfigurationException e) { 981 fJAXPSource = null; 982 fJAXPProcessed = false; 983 } 984 985 try { 987 fGrammarPool = (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL); 988 } catch (XMLConfigurationException e) { 989 fGrammarPool = null; 990 } 991 992 initGrammarBucket(); 993 try { 995 boolean fatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR); 996 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, fatalError); 997 } catch (XMLConfigurationException e) { 998 } 999 try { 1001 fIsCheckedFully = componentManager.getFeature(SCHEMA_FULL_CHECKING); 1002 } 1003 catch (XMLConfigurationException e){ 1004 fIsCheckedFully = false; 1005 } 1006 1007 fSchemaHandler.reset(componentManager); 1008 } 1009 1010 private void initGrammarBucket(){ 1011 if(fGrammarPool != null) { 1012 Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); 1013 for (int i = 0; i < initialGrammars.length; i++) { 1014 if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) { 1017 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 1020 "GrammarConflict", null, 1021 XMLErrorReporter.SEVERITY_WARNING); 1022 } 1023 } 1024 } 1025 } 1026 1027 1028 1031 public DOMConfiguration getConfig() { 1032 return this; 1033 } 1034 1035 1038 public XSModel load(LSInput is) { 1039 try { 1040 Grammar g = loadGrammar(dom2xmlInputSource(is)); 1041 return ((XSGrammar) g).toXSModel(); 1042 } catch (Exception e) { 1043 if (fErrorHandler != null) { 1044 DOMErrorImpl error = new DOMErrorImpl(); 1045 error.fException = e; 1046 error.fMessage = e.getMessage(); 1047 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1048 fErrorHandler.getErrorHandler().handleError(error); 1049 } 1050 return null; 1051 } 1052 } 1053 1054 1057 public XSModel loadInputList(LSInputList is) { 1058 int length = is.getLength(); 1059 if (length == 0) { 1060 return null; 1061 } 1062 SchemaGrammar[] gs = new SchemaGrammar[length]; 1063 for (int i = 0; i < length; i++) { 1064 try { 1065 gs[i] = (SchemaGrammar) loadGrammar(dom2xmlInputSource(is.item(i))); 1066 } catch (Exception e) { 1067 if (fErrorHandler != null) { 1068 DOMErrorImpl error = new DOMErrorImpl(); 1069 error.fException = e; 1070 error.fMessage = e.getMessage(); 1071 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1072 fErrorHandler.getErrorHandler().handleError(error); 1073 } 1074 return null; 1075 } 1076 } 1077 return new XSModelImpl(gs); 1078 } 1079 1080 1083 public XSModel loadURI(String uri) { 1084 try { 1085 Grammar g = loadGrammar(new XMLInputSource(null, uri, null)); 1086 return ((XSGrammar)g).toXSModel(); 1087 } 1088 catch (Exception e){ 1089 if (fErrorHandler != null) { 1090 DOMErrorImpl error = new DOMErrorImpl(); 1091 error.fException = e; 1092 error.fMessage = e.getMessage(); 1093 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1094 fErrorHandler.getErrorHandler().handleError(error); 1095 } 1096 return null; 1097 } 1098 } 1099 1100 1103 public XSModel loadURIList(StringList uriList) { 1104 int length = uriList.getLength(); 1105 if (length == 0) { 1106 return null; 1107 } 1108 SchemaGrammar[] gs = new SchemaGrammar[length]; 1109 for (int i = 0; i < length; i++) { 1110 try { 1111 gs[i] = 1112 (SchemaGrammar) loadGrammar(new XMLInputSource(null, uriList.item(i), null)); 1113 } catch (Exception e) { 1114 if (fErrorHandler != null) { 1115 DOMErrorImpl error = new DOMErrorImpl(); 1116 error.fException = e; 1117 error.fMessage = e.getMessage(); 1118 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1119 fErrorHandler.getErrorHandler().handleError(error); 1120 } 1121 return null; 1122 } 1123 } 1124 return new XSModelImpl(gs); 1125 } 1126 1127 1130 public boolean canSetParameter(String name, Object value) { 1131 if(value instanceof Boolean ){ 1132 if (name.equals(Constants.DOM_VALIDATE) || 1134 name.equals(SCHEMA_FULL_CHECKING) || 1135 name.equals(CONTINUE_AFTER_FATAL_ERROR) || 1136 name.equals(ALLOW_JAVA_ENCODINGS) || 1137 name.equals(STANDARD_URI_CONFORMANT_FEATURE)){ 1138 return true; 1139 1140 } 1141 return false; 1142 } 1143 if (name.equals(Constants.DOM_ERROR_HANDLER)|| 1144 name.equals(SYMBOL_TABLE) || 1145 name.equals(ERROR_REPORTER) || 1146 name.equals(ERROR_HANDLER) || 1147 name.equals(ENTITY_RESOLVER) || 1148 name.equals(XMLGRAMMAR_POOL) || 1149 name.equals(SCHEMA_LOCATION) || 1150 name.equals(SCHEMA_NONS_LOCATION) || 1151 name.equals(JAXP_SCHEMA_SOURCE)){ 1152 return true; 1153 } 1154 return false; 1155 } 1156 1157 1160 public Object getParameter(String name) throws DOMException { 1161 if (name.equals(Constants.DOM_ERROR_HANDLER)){ 1162 if (fErrorHandler != null){ 1163 return fErrorHandler.getErrorHandler(); 1164 } 1165 } 1166 try { 1167 boolean feature = getFeature(name); 1168 return (feature) ? Boolean.TRUE : Boolean.FALSE; 1169 } catch (Exception e) { 1170 Object property; 1171 try { 1172 property = getProperty(name); 1173 return property; 1174 } catch (Exception ex) { 1175 String msg = 1176 DOMMessageFormatter.formatMessage( 1177 DOMMessageFormatter.DOM_DOMAIN, 1178 "FEATURE_NOT_SUPPORTED", 1179 new Object [] { name }); 1180 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1181 } 1182 } 1183 } 1184 1185 1188 public DOMStringList getParameterNames() { 1189 if (fRecognizedParameters == null){ 1190 Vector v = new Vector (); 1191 v.add("validate"); 1192 v.add(SYMBOL_TABLE); 1193 v.add(ERROR_REPORTER); 1194 v.add(ERROR_HANDLER); 1195 v.add(ENTITY_RESOLVER); 1196 v.add(XMLGRAMMAR_POOL); 1197 v.add(SCHEMA_LOCATION); 1198 v.add(SCHEMA_NONS_LOCATION); 1199 v.add(JAXP_SCHEMA_SOURCE); 1200 v.add(SCHEMA_FULL_CHECKING); 1201 v.add(CONTINUE_AFTER_FATAL_ERROR); 1202 v.add(ALLOW_JAVA_ENCODINGS); 1203 v.add(STANDARD_URI_CONFORMANT_FEATURE); 1204 fRecognizedParameters = new DOMStringListImpl(v); 1205 } 1206 return fRecognizedParameters; 1207 } 1208 1209 1212 public void setParameter(String name, Object value) throws DOMException { 1213 if (value instanceof Boolean ) { 1214 boolean state = ((Boolean ) value).booleanValue(); 1215 if (name.equals("validate") && state) { 1216 return; 1217 } 1218 try { 1219 setFeature(name, state); 1220 } catch (Exception e) { 1221 String msg = 1222 DOMMessageFormatter.formatMessage( 1223 DOMMessageFormatter.DOM_DOMAIN, 1224 "FEATURE_NOT_SUPPORTED", 1225 new Object [] { name }); 1226 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1227 } 1228 return; 1229 } 1230 if (name.equals(Constants.DOM_ERROR_HANDLER)) { 1231 if (value instanceof DOMErrorHandler ) { 1232 try { 1233 fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler ) value); 1234 setErrorHandler(fErrorHandler); 1235 } catch (XMLConfigurationException e) { 1236 } 1237 } else { 1238 String msg = 1240 DOMMessageFormatter.formatMessage( 1241 DOMMessageFormatter.DOM_DOMAIN, 1242 "FEATURE_NOT_SUPPORTED", 1243 new Object [] { name }); 1244 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1245 } 1246 return; 1247 1248 } 1249 1250 try { 1251 setProperty(name, value); 1252 } catch (Exception ex) { 1253 1254 String msg = 1255 DOMMessageFormatter.formatMessage( 1256 DOMMessageFormatter.DOM_DOMAIN, 1257 "FEATURE_NOT_SUPPORTED", 1258 new Object [] { name }); 1259 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1260 1261 } 1262 1263 } 1264 1265 private XMLInputSource dom2xmlInputSource(LSInput is) { 1266 XMLInputSource xis = null; 1268 if (is.getStringData() != null) { 1271 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1272 is.getBaseURI(), new StringReader (is.getStringData()), 1273 "UTF-16"); 1274 } 1275 else if (is.getCharacterStream() != null) { 1278 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1279 is.getBaseURI(), is.getCharacterStream(), 1280 "UTF-16"); 1281 } 1282 else if (is.getByteStream() != null) { 1284 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1285 is.getBaseURI(), is.getByteStream(), 1286 is.getEncoding()); 1287 } 1288 else { 1290 xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), 1291 is.getBaseURI()); 1292 } 1293 1294 return xis; 1295 } 1296 1297} 1299 | Popular Tags |