18 * limitations under the License. |
18 * limitations under the License. |
19 */ |
19 */ |
20 |
20 |
21 package com.sun.org.apache.xerces.internal.impl ; |
21 package com.sun.org.apache.xerces.internal.impl ; |
22 |
22 |
|
23 import com.sun.org.apache.xerces.internal.impl.Constants; |
|
24 import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader; |
|
25 import com.sun.org.apache.xerces.internal.impl.io.UCSReader; |
|
26 import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader; |
|
27 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
|
28 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler; |
|
29 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; |
|
30 import com.sun.org.apache.xerces.internal.util.*; |
|
31 import com.sun.org.apache.xerces.internal.util.SecurityManager; |
|
32 import com.sun.org.apache.xerces.internal.util.URI; |
|
33 import com.sun.org.apache.xerces.internal.utils.SecuritySupport; |
|
34 import com.sun.org.apache.xerces.internal.xni.Augmentations; |
|
35 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; |
|
36 import com.sun.org.apache.xerces.internal.xni.XNIException; |
|
37 import com.sun.org.apache.xerces.internal.xni.parser.*; |
|
38 import com.sun.xml.internal.stream.Entity; |
23 import com.sun.xml.internal.stream.StaxEntityResolverWrapper; |
39 import com.sun.xml.internal.stream.StaxEntityResolverWrapper; |
24 import com.sun.xml.internal.stream.StaxXMLInputSource; |
40 import com.sun.xml.internal.stream.StaxXMLInputSource; |
25 import com.sun.xml.internal.stream.XMLEntityStorage; |
41 import com.sun.xml.internal.stream.XMLEntityStorage; |
26 import java.io.*; |
42 import java.io.*; |
27 import java.io.BufferedReader; |
|
28 import java.util.*; |
|
29 |
|
30 import java.io.IOException; |
|
31 import java.io.InputStream; |
|
32 import java.io.InputStreamReader; |
|
33 import java.io.Reader; |
|
34 import java.io.StringReader; |
|
35 import java.lang.reflect.Method; |
43 import java.lang.reflect.Method; |
36 import java.net.HttpURLConnection; |
44 import java.net.HttpURLConnection; |
|
45 import java.net.URISyntaxException; |
37 import java.net.URL; |
46 import java.net.URL; |
38 import java.net.URLConnection; |
47 import java.net.URLConnection; |
39 import java.net.URISyntaxException; |
|
40 import java.util.Hashtable; |
48 import java.util.Hashtable; |
41 import java.util.Iterator; |
49 import java.util.Iterator; |
42 import java.util.Locale; |
50 import java.util.Locale; |
43 import java.util.Map; |
51 import java.util.Map; |
44 import java.util.Stack; |
52 import java.util.Stack; |
45 |
53 import javax.xml.XMLConstants; |
46 |
|
47 import com.sun.org.apache.xerces.internal.impl.io.*; |
|
48 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
|
49 import com.sun.org.apache.xerces.internal.util.*; |
|
50 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; |
|
51 import com.sun.org.apache.xerces.internal.xni.XNIException; |
|
52 import com.sun.org.apache.xerces.internal.xni.parser.*; |
|
53 import com.sun.org.apache.xerces.internal.impl.Constants; |
|
54 import com.sun.org.apache.xerces.internal.utils.SecuritySupport; |
|
55 import com.sun.xml.internal.stream.Entity; |
|
56 import com.sun.org.apache.xerces.internal.xni.Augmentations; |
|
57 |
|
58 import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader; |
|
59 import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader; |
|
60 import com.sun.org.apache.xerces.internal.impl.io.UCSReader; |
|
61 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler; |
|
62 import com.sun.org.apache.xerces.internal.util.HTTPInputSource; |
|
63 import com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler; |
|
64 |
|
65 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; |
|
66 import com.sun.org.apache.xerces.internal.util.SecurityManager; |
|
67 import com.sun.org.apache.xerces.internal.util.URI; |
|
68 |
54 |
69 |
55 |
70 /** |
56 /** |
71 * Will keep track of current entity. |
57 * Will keep track of current entity. |
72 * |
58 * |
138 |
124 |
139 /** Feature identifier: warn on duplicate EntityDef */ |
125 /** Feature identifier: warn on duplicate EntityDef */ |
140 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = |
126 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = |
141 Constants.XERCES_FEATURE_PREFIX +Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; |
127 Constants.XERCES_FEATURE_PREFIX +Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; |
142 |
128 |
|
129 /** Feature identifier: load external DTD. */ |
|
130 protected static final String LOAD_EXTERNAL_DTD = |
|
131 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; |
|
132 |
143 // property identifiers |
133 // property identifiers |
144 |
134 |
145 /** Property identifier: symbol table. */ |
135 /** Property identifier: symbol table. */ |
146 protected static final String SYMBOL_TABLE = |
136 protected static final String SYMBOL_TABLE = |
147 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; |
137 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; |
171 |
161 |
172 /** property identifier: security manager. */ |
162 /** property identifier: security manager. */ |
173 protected static final String SECURITY_MANAGER = |
163 protected static final String SECURITY_MANAGER = |
174 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; |
164 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; |
175 |
165 |
176 protected static final String PARSER_SETTINGS = |
166 protected static final String PARSER_SETTINGS = |
177 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; |
167 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; |
|
168 |
|
169 /** property identifier: access external dtd. */ |
|
170 protected static final String ACCESS_EXTERNAL_DTD = XMLConstants.ACCESS_EXTERNAL_DTD; |
|
171 |
|
172 /** access external dtd: file protocol */ |
|
173 static final String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT; |
|
174 |
|
175 |
178 // recognized features and properties |
176 // recognized features and properties |
179 |
177 |
180 /** Recognized features. */ |
178 /** Recognized features. */ |
181 private static final String[] RECOGNIZED_FEATURES = { |
179 private static final String[] RECOGNIZED_FEATURES = { |
182 VALIDATION, |
180 VALIDATION, |
963 ri.setValues(publicId, literalSystemId, baseSystemId, expandedSystemId); |
968 ri.setValues(publicId, literalSystemId, baseSystemId, expandedSystemId); |
964 if(DEBUG_RESOLVER){ |
969 if(DEBUG_RESOLVER){ |
965 System.out.println("BEFORE Calling resolveEntity") ; |
970 System.out.println("BEFORE Calling resolveEntity") ; |
966 } |
971 } |
967 |
972 |
|
973 fISCreatedByResolver = false; |
968 //either of Stax or Xerces would be null |
974 //either of Stax or Xerces would be null |
969 if(fStaxEntityResolver != null){ |
975 if(fStaxEntityResolver != null){ |
970 staxInputSource = fStaxEntityResolver.resolveEntity(ri); |
976 staxInputSource = fStaxEntityResolver.resolveEntity(ri); |
|
977 if(staxInputSource != null) { |
|
978 fISCreatedByResolver = true; |
|
979 } |
971 } |
980 } |
972 |
981 |
973 if(fEntityResolver != null){ |
982 if(fEntityResolver != null){ |
974 xmlInputSource = fEntityResolver.resolveEntity(ri); |
983 xmlInputSource = fEntityResolver.resolveEntity(ri); |
|
984 if(xmlInputSource != null) { |
|
985 fISCreatedByResolver = true; |
|
986 } |
975 } |
987 } |
976 |
988 |
977 if(xmlInputSource != null){ |
989 if(xmlInputSource != null){ |
978 //wrap this XMLInputSource to StaxInputSource |
990 //wrap this XMLInputSource to StaxInputSource |
979 staxInputSource = new StaxXMLInputSource(xmlInputSource); |
991 staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver); |
980 } |
992 } |
981 |
993 |
982 // do default resolution |
994 // do default resolution |
983 //this works for both stax & Xerces, if staxInputSource is null, it means parser need to revert to default resolution |
995 //this works for both stax & Xerces, if staxInputSource is null, it means parser need to revert to default resolution |
984 if (staxInputSource == null) { |
996 if (staxInputSource == null) { |
1106 return; |
1118 return; |
1107 } |
1119 } |
1108 |
1120 |
1109 // should we skip external entities? |
1121 // should we skip external entities? |
1110 boolean external = entity.isExternal(); |
1122 boolean external = entity.isExternal(); |
|
1123 Entity.ExternalEntity externalEntity = null; |
|
1124 String extLitSysId = null, extBaseSysId = null, expandedSystemId = null; |
1111 if (external) { |
1125 if (external) { |
|
1126 externalEntity = (Entity.ExternalEntity)entity; |
|
1127 extLitSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getLiteralSystemId() : null); |
|
1128 extBaseSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getBaseSystemId() : null); |
|
1129 expandedSystemId = expandSystemId(extLitSysId, extBaseSysId); |
1112 boolean unparsed = entity.isUnparsed(); |
1130 boolean unparsed = entity.isUnparsed(); |
1113 boolean parameter = entityName.startsWith("%"); |
1131 boolean parameter = entityName.startsWith("%"); |
1114 boolean general = !parameter; |
1132 boolean general = !parameter; |
1115 if (unparsed || (general && !fExternalGeneralEntities) || |
1133 if (unparsed || (general && !fExternalGeneralEntities) || |
1116 (parameter && !fExternalParameterEntities)) { |
1134 (parameter && !fExternalParameterEntities)) { |
1117 |
1135 |
1118 if (fEntityHandler != null) { |
1136 if (fEntityHandler != null) { |
1119 fResourceIdentifier.clear(); |
1137 fResourceIdentifier.clear(); |
1120 final String encoding = null; |
1138 final String encoding = null; |
1121 Entity.ExternalEntity externalEntity = (Entity.ExternalEntity)entity; |
|
1122 //REVISIT: since we're storing expandedSystemId in the |
|
1123 // externalEntity, how could this have got here if it wasn't already |
|
1124 // expanded??? - neilg |
|
1125 String extLitSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getLiteralSystemId() : null); |
|
1126 String extBaseSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getBaseSystemId() : null); |
|
1127 String expandedSystemId = expandSystemId(extLitSysId, extBaseSysId); |
|
1128 fResourceIdentifier.setValues( |
1139 fResourceIdentifier.setValues( |
1129 (externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
1140 (externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
1130 extLitSysId, extBaseSysId, expandedSystemId); |
1141 extLitSysId, extBaseSysId, expandedSystemId); |
1131 fEntityAugs.removeAllItems(); |
1142 fEntityAugs.removeAllItems(); |
1132 fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
1143 fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
1160 |
1171 |
1161 if (fEntityHandler != null) { |
1172 if (fEntityHandler != null) { |
1162 fResourceIdentifier.clear(); |
1173 fResourceIdentifier.clear(); |
1163 final String encoding = null; |
1174 final String encoding = null; |
1164 if (external) { |
1175 if (external) { |
1165 Entity.ExternalEntity externalEntity = (Entity.ExternalEntity)entity; |
|
1166 // REVISIT: for the same reason above... |
|
1167 String extLitSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getLiteralSystemId() : null); |
|
1168 String extBaseSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getBaseSystemId() : null); |
|
1169 String expandedSystemId = expandSystemId(extLitSysId, extBaseSysId); |
|
1170 fResourceIdentifier.setValues( |
1176 fResourceIdentifier.setValues( |
1171 (externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
1177 (externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
1172 extLitSysId, extBaseSysId, expandedSystemId); |
1178 extLitSysId, extBaseSysId, expandedSystemId); |
1173 } |
1179 } |
1174 fEntityAugs.removeAllItems(); |
1180 fEntityAugs.removeAllItems(); |
1186 // resolve external entity |
1192 // resolve external entity |
1187 StaxXMLInputSource staxInputSource = null; |
1193 StaxXMLInputSource staxInputSource = null; |
1188 XMLInputSource xmlInputSource = null ; |
1194 XMLInputSource xmlInputSource = null ; |
1189 |
1195 |
1190 if (external) { |
1196 if (external) { |
1191 Entity.ExternalEntity externalEntity = (Entity.ExternalEntity)entity; |
|
1192 staxInputSource = resolveEntityAsPerStax(externalEntity.entityLocation); |
1197 staxInputSource = resolveEntityAsPerStax(externalEntity.entityLocation); |
1193 /** xxx: Waiting from the EG |
1198 /** xxx: Waiting from the EG |
1194 * //simply return if there was entity resolver registered and application |
1199 * //simply return if there was entity resolver registered and application |
1195 * //returns either XMLStreamReader or XMLEventReader. |
1200 * //returns either XMLStreamReader or XMLEventReader. |
1196 * if(staxInputSource.hasXMLStreamOrXMLEventReader()) return ; |
1201 * if(staxInputSource.hasXMLStreamOrXMLEventReader()) return ; |
1197 */ |
1202 */ |
1198 xmlInputSource = staxInputSource.getXMLInputSource() ; |
1203 xmlInputSource = staxInputSource.getXMLInputSource() ; |
|
1204 if (!fISCreatedByResolver) { |
|
1205 //let the not-LoadExternalDTD or not-SupportDTD process to handle the situation |
|
1206 if (fLoadExternalDTD) { |
|
1207 String accessError = SecuritySupport.checkAccess(expandedSystemId, fAccessExternalDTD, Constants.ACCESS_EXTERNAL_ALL); |
|
1208 if (accessError != null) { |
|
1209 fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
1210 "AccessExternalEntity", |
|
1211 new Object[] { SecuritySupport.sanitizePath(expandedSystemId), accessError }, |
|
1212 XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
1213 } |
|
1214 } |
|
1215 } |
1199 } |
1216 } |
1200 // wrap internal entity |
1217 // wrap internal entity |
1201 else { |
1218 else { |
1202 Entity.InternalEntity internalEntity = (Entity.InternalEntity)entity; |
1219 Entity.InternalEntity internalEntity = (Entity.InternalEntity)entity; |
1203 Reader reader = new StringReader(internalEntity.text); |
1220 Reader reader = new StringReader(internalEntity.text); |
1398 fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER); |
1415 fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER); |
1399 } catch (XMLConfigurationException e) { |
1416 } catch (XMLConfigurationException e) { |
1400 fStaxEntityResolver = null; |
1417 fStaxEntityResolver = null; |
1401 } |
1418 } |
1402 |
1419 |
|
1420 // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd |
|
1421 fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue(); |
|
1422 |
|
1423 // JAXP 1.5 feature |
|
1424 fAccessExternalDTD = (String) propertyManager.getProperty(ACCESS_EXTERNAL_DTD); |
|
1425 |
1403 // initialize state |
1426 // initialize state |
1404 //fStandalone = false; |
1427 //fStandalone = false; |
1405 fEntities.clear(); |
1428 fEntities.clear(); |
1406 fEntityStack.removeAllElements(); |
1429 fEntityStack.removeAllElements(); |
1407 fCurrentEntity = null; |
1430 fCurrentEntity = null; |
1408 fValidation = false; |
1431 fValidation = false; |
1409 fExternalGeneralEntities = true; |
1432 fExternalGeneralEntities = true; |
1410 fExternalParameterEntities = true; |
1433 fExternalParameterEntities = true; |
1411 fAllowJavaEncodings = true ; |
1434 fAllowJavaEncodings = true ; |
1412 |
|
1413 //test(); |
|
1414 } |
1435 } |
1415 |
1436 |
1416 /** |
1437 /** |
1417 * Resets the component. The component can query the component manager |
1438 * Resets the component. The component can query the component manager |
1418 * about any features and properties that affect the operation of the |
1439 * about any features and properties that affect the operation of the |
1451 |
1472 |
1452 // xerces features |
1473 // xerces features |
1453 fAllowJavaEncodings = componentManager.getFeature(ALLOW_JAVA_ENCODINGS, false); |
1474 fAllowJavaEncodings = componentManager.getFeature(ALLOW_JAVA_ENCODINGS, false); |
1454 fWarnDuplicateEntityDef = componentManager.getFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); |
1475 fWarnDuplicateEntityDef = componentManager.getFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); |
1455 fStrictURI = componentManager.getFeature(STANDARD_URI_CONFORMANT, false); |
1476 fStrictURI = componentManager.getFeature(STANDARD_URI_CONFORMANT, false); |
|
1477 fLoadExternalDTD = componentManager.getFeature(LOAD_EXTERNAL_DTD, true); |
1456 |
1478 |
1457 // xerces properties |
1479 // xerces properties |
1458 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); |
1480 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); |
1459 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); |
1481 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); |
1460 fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER, null); |
1482 fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER, null); |
1461 fStaxEntityResolver = (StaxEntityResolverWrapper)componentManager.getProperty(STAX_ENTITY_RESOLVER, null); |
1483 fStaxEntityResolver = (StaxEntityResolverWrapper)componentManager.getProperty(STAX_ENTITY_RESOLVER, null); |
1462 fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null); |
1484 fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null); |
1463 fSecurityManager = (SecurityManager)componentManager.getProperty(SECURITY_MANAGER, null); |
1485 fSecurityManager = (SecurityManager)componentManager.getProperty(SECURITY_MANAGER, null); |
|
1486 |
|
1487 // JAXP 1.5 feature |
|
1488 fAccessExternalDTD = (String) componentManager.getProperty(ACCESS_EXTERNAL_DTD, EXTERNAL_ACCESS_DEFAULT); |
1464 |
1489 |
1465 //reset general state |
1490 //reset general state |
1466 reset(); |
1491 reset(); |
1467 |
1492 |
1468 fEntityScanner.reset(componentManager); |
1493 fEntityScanner.reset(componentManager); |