36 import com.sun.org.apache.xerces.internal.xni.Augmentations;
37 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
38 import com.sun.org.apache.xerces.internal.xni.XNIException;
39 import com.sun.org.apache.xerces.internal.xni.parser.*;
40 import com.sun.xml.internal.stream.Entity;
41 import com.sun.xml.internal.stream.StaxEntityResolverWrapper;
42 import com.sun.xml.internal.stream.StaxXMLInputSource;
43 import com.sun.xml.internal.stream.XMLEntityStorage;
44 import java.io.*;
45 import java.lang.reflect.Method;
46 import java.net.HttpURLConnection;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49 import java.net.URLConnection;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.Locale;
53 import java.util.Map;
54 import java.util.Stack;
55 import java.util.StringTokenizer;
56 import javax.xml.stream.XMLInputFactory;
57
58
59 /**
60 * Will keep track of current entity.
61 *
62 * The entity manager handles the registration of general and parameter
63 * entities; resolves entities; and starts entities. The entity manager
64 * is a central component in a standard parser configuration and this
65 * class works directly with the entity scanner to manage the underlying
66 * xni.
67 * <p>
68 * This component requires the following features and properties from the
69 * component manager that uses it:
70 * <ul>
71 * <li>http://xml.org/sax/features/validation</li>
72 * <li>http://xml.org/sax/features/external-general-entities</li>
73 * <li>http://xml.org/sax/features/external-parameter-entities</li>
74 * <li>http://apache.org/xml/features/allow-java-encodings</li>
75 * <li>http://apache.org/xml/properties/internal/symbol-table</li>
76 * <li>http://apache.org/xml/properties/internal/error-reporter</li>
167
168 protected static final String PARSER_SETTINGS =
169 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
170
171 /** Property identifier: Security property manager. */
172 private static final String XML_SECURITY_PROPERTY_MANAGER =
173 Constants.XML_SECURITY_PROPERTY_MANAGER;
174
175 /** access external dtd: file protocol */
176 static final String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT;
177
178 // recognized features and properties
179
180 /** Recognized features. */
181 private static final String[] RECOGNIZED_FEATURES = {
182 VALIDATION,
183 EXTERNAL_GENERAL_ENTITIES,
184 EXTERNAL_PARAMETER_ENTITIES,
185 ALLOW_JAVA_ENCODINGS,
186 WARN_ON_DUPLICATE_ENTITYDEF,
187 STANDARD_URI_CONFORMANT
188 };
189
190 /** Feature defaults. */
191 private static final Boolean[] FEATURE_DEFAULTS = {
192 null,
193 Boolean.TRUE,
194 Boolean.TRUE,
195 Boolean.TRUE,
196 Boolean.FALSE,
197 Boolean.FALSE
198 };
199
200 /** Recognized properties. */
201 private static final String[] RECOGNIZED_PROPERTIES = {
202 SYMBOL_TABLE,
203 ERROR_REPORTER,
204 ENTITY_RESOLVER,
205 VALIDATION_MANAGER,
206 BUFFER_SIZE,
207 SECURITY_MANAGER,
208 XML_SECURITY_PROPERTY_MANAGER
209 };
210
211 /** Property defaults. */
212 private static final Object[] PROPERTY_DEFAULTS = {
213 null,
214 null,
215 null,
216 null,
217 new Integer(DEFAULT_BUFFER_SIZE),
218 null,
219 null
220 };
221
222 private static final String XMLEntity = "[xml]".intern();
223 private static final String DTDEntity = "[dtd]".intern();
224
225 // debugging
226
227 /**
228 * Debug printing of buffer. This debugging flag works best when you
229 * resize the DEFAULT_BUFFER_SIZE down to something reasonable like
230 * 64 characters.
231 */
232 private static final boolean DEBUG_BUFFER = false;
233
234 /** warn on duplicate Entity declaration.
235 * http://apache.org/xml/features/warn-on-duplicate-entitydef
236 */
237 protected boolean fWarnDuplicateEntityDef;
379 boolean fISCreatedByResolver = false;
380
381 // shared context
382
383 protected XMLEntityStorage fEntityStorage ;
384
385 protected final Object [] defaultEncoding = new Object[]{"UTF-8", null};
386
387
388 // temp vars
389
390 /** Resource identifer. */
391 private final XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl();
392
393 /** Augmentations for entities. */
394 private final Augmentations fEntityAugs = new AugmentationsImpl();
395
396 /** Pool of character buffers. */
397 private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
398
399 //
400 // Constructors
401 //
402
403 /**
404 * If this constructor is used to create the object, reset() should be invoked on this object
405 */
406 public XMLEntityManager() {
407 //for entity managers not created by parsers
408 fSecurityManager = new XMLSecurityManager(true);
409 fEntityStorage = new XMLEntityStorage(this) ;
410 setScannerVersion(Constants.XML_VERSION_1_0);
411 } // <init>()
412
413 /** Default constructor. */
414 public XMLEntityManager(PropertyManager propertyManager) {
415 fPropertyManager = propertyManager ;
416 //pass a reference to current entity being scanned
417 //fEntityStorage = new XMLEntityStorage(fCurrentEntity) ;
418 fEntityStorage = new XMLEntityStorage(this) ;
990 //either of Stax or Xerces would be null
991 if(fStaxEntityResolver != null){
992 staxInputSource = fStaxEntityResolver.resolveEntity(ri);
993 if(staxInputSource != null) {
994 fISCreatedByResolver = true;
995 }
996 }
997
998 if(fEntityResolver != null){
999 xmlInputSource = fEntityResolver.resolveEntity(ri);
1000 if(xmlInputSource != null) {
1001 fISCreatedByResolver = true;
1002 }
1003 }
1004
1005 if(xmlInputSource != null){
1006 //wrap this XMLInputSource to StaxInputSource
1007 staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
1008 }
1009
1010 // do default resolution
1011 //this works for both stax & Xerces, if staxInputSource is null,
1012 //it means parser need to revert to default resolution
1013 if (staxInputSource == null) {
1014 // REVISIT: when systemId is null, I think we should return null.
1015 // is this the right solution? -SG
1016 //if (systemId != null)
1017 staxInputSource = new StaxXMLInputSource(
1018 new XMLInputSource(publicId, literalSystemId, baseSystemId, true), false);
1019 }else if(staxInputSource.hasXMLStreamOrXMLEventReader()){
1020 //Waiting for the clarification from EG. - nb
1021 }
1022
1023 if (DEBUG_RESOLVER) {
1024 System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1025 System.err.println(" = " + xmlInputSource);
1026 }
1027
1028 return staxInputSource;
1029
1068 // REVISIT: why would the baseSystemId ever be null? if we
1069 // didn't have to make this check we wouldn't have to reuse the
1070 // fXMLResourceIdentifier object...
1071 if (baseSystemId == null && fCurrentEntity != null && fCurrentEntity.entityLocation != null) {
1072 baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId();
1073 if (baseSystemId != null)
1074 needExpand = true;
1075 }
1076 if (needExpand)
1077 expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false);
1078
1079 // give the entity resolver a chance
1080 XMLInputSource xmlInputSource = null;
1081
1082 if (fEntityResolver != null) {
1083 resourceIdentifier.setBaseSystemId(baseSystemId);
1084 resourceIdentifier.setExpandedSystemId(expandedSystemId);
1085 xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
1086 }
1087
1088 // do default resolution
1089 // REVISIT: what's the correct behavior if the user provided an entity
1090 // resolver (fEntityResolver != null), but resolveEntity doesn't return
1091 // an input source (xmlInputSource == null)?
1092 // do we do default resolution, or do we just return null? -SG
1093 if (xmlInputSource == null) {
1094 // REVISIT: when systemId is null, I think we should return null.
1095 // is this the right solution? -SG
1096 //if (systemId != null)
1097 xmlInputSource = new XMLInputSource(publicId, literalSystemId, baseSystemId, false);
1098 }
1099
1100 if (DEBUG_RESOLVER) {
1101 System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1102 System.err.println(" = " + xmlInputSource);
1103 }
1104
1105 return xmlInputSource;
1106
1107 } // resolveEntity(XMLResourceIdentifier):XMLInputSource
1425 print();
1426 System.out.println();
1427 }
1428
1429 } // endEntity()
1430
1431
1432 //
1433 // XMLComponent methods
1434 //
1435 public void reset(PropertyManager propertyManager){
1436 // xerces properties
1437 fSymbolTable = (SymbolTable)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY);
1438 fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY);
1439 try {
1440 fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER);
1441 } catch (XMLConfigurationException e) {
1442 fStaxEntityResolver = null;
1443 }
1444
1445 fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
1446 fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)).booleanValue();
1447 fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)).booleanValue();
1448
1449 // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd
1450 fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue();
1451
1452 // JAXP 1.5 feature
1453 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
1454 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1455
1456 fSecurityManager = (XMLSecurityManager)propertyManager.getProperty(SECURITY_MANAGER);
1457
1458 fLimitAnalyzer = new XMLLimitAnalyzer();
1459 //reset fEntityStorage
1460 fEntityStorage.reset(propertyManager);
1461 //reset XMLEntityReaderImpl
1462 fEntityScanner.reset(propertyManager);
1463
1464 // initialize state
1465 //fStandalone = false;
1466 fEntities.clear();
1467 fEntityStack.removeAllElements();
1468 fCurrentEntity = null;
1469 fValidation = false;
1470 fExternalGeneralEntities = true;
1518 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
1519 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
1520 fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER, null);
1521 fStaxEntityResolver = (StaxEntityResolverWrapper)componentManager.getProperty(STAX_ENTITY_RESOLVER, null);
1522 fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null);
1523 fSecurityManager = (XMLSecurityManager)componentManager.getProperty(SECURITY_MANAGER, null);
1524 entityExpansionIndex = fSecurityManager.getIndex(Constants.JDK_ENTITY_EXPANSION_LIMIT);
1525
1526 //StAX Property
1527 fSupportDTD = true;
1528 fReplaceEntityReferences = true;
1529 fSupportExternalEntities = true;
1530
1531 // JAXP 1.5 feature
1532 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null);
1533 if (spm == null) {
1534 spm = new XMLSecurityPropertyManager();
1535 }
1536 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1537
1538 //reset general state
1539 reset();
1540
1541 fEntityScanner.reset(componentManager);
1542 fEntityStorage.reset(componentManager);
1543
1544 } // reset(XMLComponentManager)
1545
1546 // reset general state. Should not be called other than by
1547 // a class acting as a component manager but not
1548 // implementing that interface for whatever reason.
1549 public void reset() {
1550 fLimitAnalyzer = new XMLLimitAnalyzer();
1551 // initialize state
1552 fStandalone = false;
1553 fEntities.clear();
1554 fEntityStack.removeAllElements();
1555 fEntityExpansionCount = 0;
1556
1557 fCurrentEntity = null;
1614 * @throws SAXNotRecognizedException The component should not throw
1615 * this exception.
1616 * @throws SAXNotSupportedException The component should not throw
1617 * this exception.
1618 */
1619 public void setFeature(String featureId, boolean state)
1620 throws XMLConfigurationException {
1621
1622 // xerces features
1623 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
1624 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
1625 if (suffixLength == Constants.ALLOW_JAVA_ENCODINGS_FEATURE.length() &&
1626 featureId.endsWith(Constants.ALLOW_JAVA_ENCODINGS_FEATURE)) {
1627 fAllowJavaEncodings = state;
1628 }
1629 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
1630 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
1631 fLoadExternalDTD = state;
1632 return;
1633 }
1634 }
1635
1636 } // setFeature(String,boolean)
1637
1638 /**
1639 * Sets the value of a property. This method is called by the component
1640 * manager any time after reset when a property changes value.
1641 * <p>
1642 * <strong>Note:</strong> Components should silently ignore properties
1643 * that do not affect the operation of the component.
1644 *
1645 * @param propertyId The property identifier.
1646 * @param value The value of the property.
1647 *
1648 * @throws SAXNotRecognizedException The component should not throw
1649 * this exception.
1650 * @throws SAXNotSupportedException The component should not throw
1651 * this exception.
1652 */
1653 public void setProperty(String propertyId, Object value){
1674 propertyId.endsWith(Constants.BUFFER_SIZE_PROPERTY)) {
1675 Integer bufferSize = (Integer)value;
1676 if (bufferSize != null &&
1677 bufferSize.intValue() > DEFAULT_XMLDECL_BUFFER_SIZE) {
1678 fBufferSize = bufferSize.intValue();
1679 fEntityScanner.setBufferSize(fBufferSize);
1680 fBufferPool.setExternalBufferSize(fBufferSize);
1681 }
1682 }
1683 if (suffixLength == Constants.SECURITY_MANAGER_PROPERTY.length() &&
1684 propertyId.endsWith(Constants.SECURITY_MANAGER_PROPERTY)) {
1685 fSecurityManager = (XMLSecurityManager)value;
1686 }
1687 }
1688
1689 //JAXP 1.5 properties
1690 if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER))
1691 {
1692 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value;
1693 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1694 }
1695 }
1696
1697 public void setLimitAnalyzer(XMLLimitAnalyzer fLimitAnalyzer) {
1698 this.fLimitAnalyzer = fLimitAnalyzer;
1699 }
1700
1701 /**
1702 * Returns a list of property identifiers that are recognized by
1703 * this component. This method may return null if no properties
1704 * are recognized by this component.
1705 */
1706 public String[] getRecognizedProperties() {
1707 return (String[])(RECOGNIZED_PROPERTIES.clone());
1708 } // getRecognizedProperties():String[]
1709 /**
1710 * Returns the default state for a feature, or null if this
1711 * component does not want to report a default value for this
1712 * feature.
1713 *
2049 *
2050 * @param systemId The systemId to be expanded.
2051 *
2052 * @return Returns the URI string representing the expanded system
2053 * identifier. A null value indicates that the given
2054 * system identifier is already expanded.
2055 *
2056 */
2057 public static String expandSystemId(String systemId, String baseSystemId,
2058 boolean strict)
2059 throws URI.MalformedURIException {
2060
2061 // check if there is a system id before
2062 // trying to expand it.
2063 if (systemId == null) {
2064 return null;
2065 }
2066
2067 // system id has to be a valid URI
2068 if (strict) {
2069
2070 try {
2071 // if it's already an absolute one, return it
2072 new URI(systemId);
2073 return systemId;
2074 }
2075 catch (URI.MalformedURIException ex) {
2076 }
2077 URI base = null;
2078 // if there isn't a base uri, use the working directory
2079 if (baseSystemId == null || baseSystemId.length() == 0) {
2080 base = new URI("file", "", getUserDir().toString(), null, null);
2081 }
2082 // otherwise, use the base uri
2083 else {
2084 try {
2085 base = new URI(baseSystemId);
2086 }
2087 catch (URI.MalformedURIException e) {
2088 // assume "base" is also a relative uri
2089 String dir = getUserDir().toString();
|
36 import com.sun.org.apache.xerces.internal.xni.Augmentations;
37 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
38 import com.sun.org.apache.xerces.internal.xni.XNIException;
39 import com.sun.org.apache.xerces.internal.xni.parser.*;
40 import com.sun.xml.internal.stream.Entity;
41 import com.sun.xml.internal.stream.StaxEntityResolverWrapper;
42 import com.sun.xml.internal.stream.StaxXMLInputSource;
43 import com.sun.xml.internal.stream.XMLEntityStorage;
44 import java.io.*;
45 import java.lang.reflect.Method;
46 import java.net.HttpURLConnection;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49 import java.net.URLConnection;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.Locale;
53 import java.util.Map;
54 import java.util.Stack;
55 import java.util.StringTokenizer;
56 import javax.xml.XMLConstants;
57 import javax.xml.catalog.CatalogException;
58 import javax.xml.catalog.CatalogFeatures;
59 import javax.xml.catalog.CatalogFeatures.Feature;
60 import javax.xml.catalog.CatalogManager;
61 import javax.xml.catalog.CatalogResolver;
62 import javax.xml.catalog.CatalogUriResolver;
63 import javax.xml.stream.XMLInputFactory;
64 import javax.xml.transform.Source;
65 import jdk.xml.internal.JdkXmlUtils;
66 import org.xml.sax.InputSource;
67
68
69 /**
70 * Will keep track of current entity.
71 *
72 * The entity manager handles the registration of general and parameter
73 * entities; resolves entities; and starts entities. The entity manager
74 * is a central component in a standard parser configuration and this
75 * class works directly with the entity scanner to manage the underlying
76 * xni.
77 * <p>
78 * This component requires the following features and properties from the
79 * component manager that uses it:
80 * <ul>
81 * <li>http://xml.org/sax/features/validation</li>
82 * <li>http://xml.org/sax/features/external-general-entities</li>
83 * <li>http://xml.org/sax/features/external-parameter-entities</li>
84 * <li>http://apache.org/xml/features/allow-java-encodings</li>
85 * <li>http://apache.org/xml/properties/internal/symbol-table</li>
86 * <li>http://apache.org/xml/properties/internal/error-reporter</li>
177
178 protected static final String PARSER_SETTINGS =
179 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
180
181 /** Property identifier: Security property manager. */
182 private static final String XML_SECURITY_PROPERTY_MANAGER =
183 Constants.XML_SECURITY_PROPERTY_MANAGER;
184
185 /** access external dtd: file protocol */
186 static final String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT;
187
188 // recognized features and properties
189
190 /** Recognized features. */
191 private static final String[] RECOGNIZED_FEATURES = {
192 VALIDATION,
193 EXTERNAL_GENERAL_ENTITIES,
194 EXTERNAL_PARAMETER_ENTITIES,
195 ALLOW_JAVA_ENCODINGS,
196 WARN_ON_DUPLICATE_ENTITYDEF,
197 STANDARD_URI_CONFORMANT,
198 XMLConstants.USE_CATALOG
199 };
200
201 /** Feature defaults. */
202 private static final Boolean[] FEATURE_DEFAULTS = {
203 null,
204 Boolean.TRUE,
205 Boolean.TRUE,
206 Boolean.TRUE,
207 Boolean.FALSE,
208 Boolean.FALSE,
209 JdkXmlUtils.USE_CATALOG_DEFAULT
210 };
211
212 /** Recognized properties. */
213 private static final String[] RECOGNIZED_PROPERTIES = {
214 SYMBOL_TABLE,
215 ERROR_REPORTER,
216 ENTITY_RESOLVER,
217 VALIDATION_MANAGER,
218 BUFFER_SIZE,
219 SECURITY_MANAGER,
220 XML_SECURITY_PROPERTY_MANAGER,
221 JdkXmlUtils.CATALOG_DEFER,
222 JdkXmlUtils.CATALOG_FILES,
223 JdkXmlUtils.CATALOG_PREFER,
224 JdkXmlUtils.CATALOG_RESOLVE
225 };
226
227 /** Property defaults. */
228 private static final Object[] PROPERTY_DEFAULTS = {
229 null,
230 null,
231 null,
232 null,
233 DEFAULT_BUFFER_SIZE,
234 null,
235 null,
236 null,
237 null,
238 null,
239 null
240 };
241
242 private static final String XMLEntity = "[xml]".intern();
243 private static final String DTDEntity = "[dtd]".intern();
244
245 // debugging
246
247 /**
248 * Debug printing of buffer. This debugging flag works best when you
249 * resize the DEFAULT_BUFFER_SIZE down to something reasonable like
250 * 64 characters.
251 */
252 private static final boolean DEBUG_BUFFER = false;
253
254 /** warn on duplicate Entity declaration.
255 * http://apache.org/xml/features/warn-on-duplicate-entitydef
256 */
257 protected boolean fWarnDuplicateEntityDef;
399 boolean fISCreatedByResolver = false;
400
401 // shared context
402
403 protected XMLEntityStorage fEntityStorage ;
404
405 protected final Object [] defaultEncoding = new Object[]{"UTF-8", null};
406
407
408 // temp vars
409
410 /** Resource identifer. */
411 private final XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl();
412
413 /** Augmentations for entities. */
414 private final Augmentations fEntityAugs = new AugmentationsImpl();
415
416 /** Pool of character buffers. */
417 private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
418
419 /** indicate whether Catalog should be used for resolving external resources */
420 private boolean fUseCatalog = true;
421 CatalogFeatures fCatalogFeatures;
422 CatalogResolver fCatalogResolver;
423 CatalogUriResolver fCatalogUriResolver;
424
425 private String fCatalogFile;
426 private String fDefer;
427 private String fPrefer;
428 private String fResolve;
429
430 //
431 // Constructors
432 //
433
434 /**
435 * If this constructor is used to create the object, reset() should be invoked on this object
436 */
437 public XMLEntityManager() {
438 //for entity managers not created by parsers
439 fSecurityManager = new XMLSecurityManager(true);
440 fEntityStorage = new XMLEntityStorage(this) ;
441 setScannerVersion(Constants.XML_VERSION_1_0);
442 } // <init>()
443
444 /** Default constructor. */
445 public XMLEntityManager(PropertyManager propertyManager) {
446 fPropertyManager = propertyManager ;
447 //pass a reference to current entity being scanned
448 //fEntityStorage = new XMLEntityStorage(fCurrentEntity) ;
449 fEntityStorage = new XMLEntityStorage(this) ;
1021 //either of Stax or Xerces would be null
1022 if(fStaxEntityResolver != null){
1023 staxInputSource = fStaxEntityResolver.resolveEntity(ri);
1024 if(staxInputSource != null) {
1025 fISCreatedByResolver = true;
1026 }
1027 }
1028
1029 if(fEntityResolver != null){
1030 xmlInputSource = fEntityResolver.resolveEntity(ri);
1031 if(xmlInputSource != null) {
1032 fISCreatedByResolver = true;
1033 }
1034 }
1035
1036 if(xmlInputSource != null){
1037 //wrap this XMLInputSource to StaxInputSource
1038 staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
1039 }
1040
1041 if (staxInputSource == null) {
1042 if (fCatalogFeatures == null) {
1043 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
1044 }
1045 fCatalogFile = fCatalogFeatures.get(Feature.FILES);
1046 if (fUseCatalog && fCatalogFile != null) {
1047 if (fCatalogResolver == null) {
1048 fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1049 }
1050 InputSource is = fCatalogResolver.resolveEntity(publicId, literalSystemId);
1051 if (is != null && !is.isEmpty()) {
1052 staxInputSource = new StaxXMLInputSource(new XMLInputSource(is, true), true);
1053 }
1054 }
1055 }
1056
1057 // do default resolution
1058 //this works for both stax & Xerces, if staxInputSource is null,
1059 //it means parser need to revert to default resolution
1060 if (staxInputSource == null) {
1061 // REVISIT: when systemId is null, I think we should return null.
1062 // is this the right solution? -SG
1063 //if (systemId != null)
1064 staxInputSource = new StaxXMLInputSource(
1065 new XMLInputSource(publicId, literalSystemId, baseSystemId, true), false);
1066 }else if(staxInputSource.hasXMLStreamOrXMLEventReader()){
1067 //Waiting for the clarification from EG. - nb
1068 }
1069
1070 if (DEBUG_RESOLVER) {
1071 System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1072 System.err.println(" = " + xmlInputSource);
1073 }
1074
1075 return staxInputSource;
1076
1115 // REVISIT: why would the baseSystemId ever be null? if we
1116 // didn't have to make this check we wouldn't have to reuse the
1117 // fXMLResourceIdentifier object...
1118 if (baseSystemId == null && fCurrentEntity != null && fCurrentEntity.entityLocation != null) {
1119 baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId();
1120 if (baseSystemId != null)
1121 needExpand = true;
1122 }
1123 if (needExpand)
1124 expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false);
1125
1126 // give the entity resolver a chance
1127 XMLInputSource xmlInputSource = null;
1128
1129 if (fEntityResolver != null) {
1130 resourceIdentifier.setBaseSystemId(baseSystemId);
1131 resourceIdentifier.setExpandedSystemId(expandedSystemId);
1132 xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
1133 }
1134
1135 if (xmlInputSource == null) {
1136 if (fCatalogFeatures == null) {
1137 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
1138 }
1139 fCatalogFile = fCatalogFeatures.get(Feature.FILES);
1140 if (fUseCatalog && fCatalogFile != null) {
1141 /*
1142 since the method can be called from various processors, both
1143 CatalogResolver and CatalogUriResolver are used to attempt to find
1144 a match
1145 */
1146 InputSource is = null;
1147 try {
1148 if (fCatalogResolver == null) {
1149 fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1150 }
1151 String pid = (publicId != null? publicId : resourceIdentifier.getNamespace());
1152 if (pid != null || literalSystemId != null) {
1153 is = fCatalogResolver.resolveEntity(pid, literalSystemId);
1154 }
1155 } catch (CatalogException e) {}
1156 if (is != null && !is.isEmpty()) {
1157 xmlInputSource = new XMLInputSource(is, true);
1158 } else if (literalSystemId != null) {
1159 if (fCatalogUriResolver == null) {
1160 fCatalogUriResolver = CatalogManager.catalogUriResolver(fCatalogFeatures);
1161 }
1162 Source source = fCatalogUriResolver.resolve(literalSystemId, baseSystemId);
1163 if (source != null && !source.isEmpty()) {
1164 xmlInputSource = new XMLInputSource(publicId, source.getSystemId(), baseSystemId, true);
1165 }
1166 }
1167 }
1168 }
1169
1170 // do default resolution
1171 // REVISIT: what's the correct behavior if the user provided an entity
1172 // resolver (fEntityResolver != null), but resolveEntity doesn't return
1173 // an input source (xmlInputSource == null)?
1174 // do we do default resolution, or do we just return null? -SG
1175 if (xmlInputSource == null) {
1176 // REVISIT: when systemId is null, I think we should return null.
1177 // is this the right solution? -SG
1178 //if (systemId != null)
1179 xmlInputSource = new XMLInputSource(publicId, literalSystemId, baseSystemId, false);
1180 }
1181
1182 if (DEBUG_RESOLVER) {
1183 System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1184 System.err.println(" = " + xmlInputSource);
1185 }
1186
1187 return xmlInputSource;
1188
1189 } // resolveEntity(XMLResourceIdentifier):XMLInputSource
1507 print();
1508 System.out.println();
1509 }
1510
1511 } // endEntity()
1512
1513
1514 //
1515 // XMLComponent methods
1516 //
1517 public void reset(PropertyManager propertyManager){
1518 // xerces properties
1519 fSymbolTable = (SymbolTable)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY);
1520 fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY);
1521 try {
1522 fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER);
1523 } catch (XMLConfigurationException e) {
1524 fStaxEntityResolver = null;
1525 }
1526
1527 fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD));
1528 fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES));
1529 fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES));
1530
1531 // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd
1532 fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD));
1533
1534 //Use Catalog
1535 fUseCatalog = (Boolean)propertyManager.getProperty(XMLConstants.USE_CATALOG);
1536 fCatalogFile = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_FILES);
1537 fDefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
1538 fPrefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
1539 fResolve = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
1540
1541 // JAXP 1.5 feature
1542 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
1543 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1544
1545 fSecurityManager = (XMLSecurityManager)propertyManager.getProperty(SECURITY_MANAGER);
1546
1547 fLimitAnalyzer = new XMLLimitAnalyzer();
1548 //reset fEntityStorage
1549 fEntityStorage.reset(propertyManager);
1550 //reset XMLEntityReaderImpl
1551 fEntityScanner.reset(propertyManager);
1552
1553 // initialize state
1554 //fStandalone = false;
1555 fEntities.clear();
1556 fEntityStack.removeAllElements();
1557 fCurrentEntity = null;
1558 fValidation = false;
1559 fExternalGeneralEntities = true;
1607 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
1608 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
1609 fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER, null);
1610 fStaxEntityResolver = (StaxEntityResolverWrapper)componentManager.getProperty(STAX_ENTITY_RESOLVER, null);
1611 fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null);
1612 fSecurityManager = (XMLSecurityManager)componentManager.getProperty(SECURITY_MANAGER, null);
1613 entityExpansionIndex = fSecurityManager.getIndex(Constants.JDK_ENTITY_EXPANSION_LIMIT);
1614
1615 //StAX Property
1616 fSupportDTD = true;
1617 fReplaceEntityReferences = true;
1618 fSupportExternalEntities = true;
1619
1620 // JAXP 1.5 feature
1621 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null);
1622 if (spm == null) {
1623 spm = new XMLSecurityPropertyManager();
1624 }
1625 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1626
1627 //Use Catalog
1628 fUseCatalog = componentManager.getFeature(XMLConstants.USE_CATALOG, true);
1629 fCatalogFile = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_FILES);
1630 fDefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
1631 fPrefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
1632 fResolve = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
1633
1634 //reset general state
1635 reset();
1636
1637 fEntityScanner.reset(componentManager);
1638 fEntityStorage.reset(componentManager);
1639
1640 } // reset(XMLComponentManager)
1641
1642 // reset general state. Should not be called other than by
1643 // a class acting as a component manager but not
1644 // implementing that interface for whatever reason.
1645 public void reset() {
1646 fLimitAnalyzer = new XMLLimitAnalyzer();
1647 // initialize state
1648 fStandalone = false;
1649 fEntities.clear();
1650 fEntityStack.removeAllElements();
1651 fEntityExpansionCount = 0;
1652
1653 fCurrentEntity = null;
1710 * @throws SAXNotRecognizedException The component should not throw
1711 * this exception.
1712 * @throws SAXNotSupportedException The component should not throw
1713 * this exception.
1714 */
1715 public void setFeature(String featureId, boolean state)
1716 throws XMLConfigurationException {
1717
1718 // xerces features
1719 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
1720 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
1721 if (suffixLength == Constants.ALLOW_JAVA_ENCODINGS_FEATURE.length() &&
1722 featureId.endsWith(Constants.ALLOW_JAVA_ENCODINGS_FEATURE)) {
1723 fAllowJavaEncodings = state;
1724 }
1725 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
1726 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
1727 fLoadExternalDTD = state;
1728 return;
1729 }
1730 } else if (featureId.equals(XMLConstants.USE_CATALOG)) {
1731 fUseCatalog = state;
1732 }
1733
1734 } // setFeature(String,boolean)
1735
1736 /**
1737 * Sets the value of a property. This method is called by the component
1738 * manager any time after reset when a property changes value.
1739 * <p>
1740 * <strong>Note:</strong> Components should silently ignore properties
1741 * that do not affect the operation of the component.
1742 *
1743 * @param propertyId The property identifier.
1744 * @param value The value of the property.
1745 *
1746 * @throws SAXNotRecognizedException The component should not throw
1747 * this exception.
1748 * @throws SAXNotSupportedException The component should not throw
1749 * this exception.
1750 */
1751 public void setProperty(String propertyId, Object value){
1772 propertyId.endsWith(Constants.BUFFER_SIZE_PROPERTY)) {
1773 Integer bufferSize = (Integer)value;
1774 if (bufferSize != null &&
1775 bufferSize.intValue() > DEFAULT_XMLDECL_BUFFER_SIZE) {
1776 fBufferSize = bufferSize.intValue();
1777 fEntityScanner.setBufferSize(fBufferSize);
1778 fBufferPool.setExternalBufferSize(fBufferSize);
1779 }
1780 }
1781 if (suffixLength == Constants.SECURITY_MANAGER_PROPERTY.length() &&
1782 propertyId.endsWith(Constants.SECURITY_MANAGER_PROPERTY)) {
1783 fSecurityManager = (XMLSecurityManager)value;
1784 }
1785 }
1786
1787 //JAXP 1.5 properties
1788 if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER))
1789 {
1790 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value;
1791 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
1792 return;
1793 }
1794
1795 //Catalog properties
1796 if (propertyId.equals(JdkXmlUtils.CATALOG_FILES)) {
1797 fCatalogFile = (String)value;
1798 } else if (propertyId.equals(JdkXmlUtils.CATALOG_DEFER)) {
1799 fDefer = (String)value;
1800 } else if (propertyId.equals(JdkXmlUtils.CATALOG_PREFER)) {
1801 fPrefer = (String)value;
1802 } else if (propertyId.equals(JdkXmlUtils.CATALOG_RESOLVE)) {
1803 fResolve = (String)value;
1804 }
1805 }
1806
1807 public void setLimitAnalyzer(XMLLimitAnalyzer fLimitAnalyzer) {
1808 this.fLimitAnalyzer = fLimitAnalyzer;
1809 }
1810
1811 /**
1812 * Returns a list of property identifiers that are recognized by
1813 * this component. This method may return null if no properties
1814 * are recognized by this component.
1815 */
1816 public String[] getRecognizedProperties() {
1817 return (String[])(RECOGNIZED_PROPERTIES.clone());
1818 } // getRecognizedProperties():String[]
1819 /**
1820 * Returns the default state for a feature, or null if this
1821 * component does not want to report a default value for this
1822 * feature.
1823 *
2159 *
2160 * @param systemId The systemId to be expanded.
2161 *
2162 * @return Returns the URI string representing the expanded system
2163 * identifier. A null value indicates that the given
2164 * system identifier is already expanded.
2165 *
2166 */
2167 public static String expandSystemId(String systemId, String baseSystemId,
2168 boolean strict)
2169 throws URI.MalformedURIException {
2170
2171 // check if there is a system id before
2172 // trying to expand it.
2173 if (systemId == null) {
2174 return null;
2175 }
2176
2177 // system id has to be a valid URI
2178 if (strict) {
2179 try {
2180 // if it's already an absolute one, return it
2181 new URI(systemId);
2182 return systemId;
2183 }
2184 catch (URI.MalformedURIException ex) {
2185 }
2186 URI base = null;
2187 // if there isn't a base uri, use the working directory
2188 if (baseSystemId == null || baseSystemId.length() == 0) {
2189 base = new URI("file", "", getUserDir().toString(), null, null);
2190 }
2191 // otherwise, use the base uri
2192 else {
2193 try {
2194 base = new URI(baseSystemId);
2195 }
2196 catch (URI.MalformedURIException e) {
2197 // assume "base" is also a relative uri
2198 String dir = getUserDir().toString();
|