|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream; |
|
|
|
import com.sun.org.apache.xerces.internal.impl.Constants; |
|
import com.sun.org.apache.xerces.internal.impl.PropertyManager; |
|
import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; |
|
import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; |
|
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
|
import com.sun.org.apache.xerces.internal.util.URI; |
|
import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; |
|
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; |
|
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import jdk.xml.internal.SecuritySupport; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class XMLEntityStorage { |
|
|
|
|
|
protected static final String ERROR_REPORTER = |
|
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; |
|
|
|
|
|
protected static final String WARN_ON_DUPLICATE_ENTITYDEF = |
|
Constants.XERCES_FEATURE_PREFIX +Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; |
|
|
|
|
|
|
|
*/ |
|
protected boolean fWarnDuplicateEntityDef; |
|
|
|
|
|
protected Map<String, Entity> fEntities = new HashMap<>(); |
|
|
|
protected Entity.ScannedEntity fCurrentEntity ; |
|
|
|
private XMLEntityManager fEntityManager; |
|
|
|
|
|
|
|
*/ |
|
protected XMLErrorReporter fErrorReporter; |
|
protected PropertyManager fPropertyManager ; |
|
|
|
|
|
protected boolean fInExternalSubset = false; |
|
|
|
|
|
public XMLEntityStorage(PropertyManager propertyManager) { |
|
fPropertyManager = propertyManager ; |
|
} |
|
|
|
/** Creates a new instance of XMLEntityStorage */ |
|
|
|
fCurrentEntity = currentEntity ;*/ |
|
public XMLEntityStorage(XMLEntityManager entityManager) { |
|
fEntityManager = entityManager; |
|
} |
|
|
|
public void reset(PropertyManager propertyManager){ |
|
|
|
fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY); |
|
fEntities.clear(); |
|
fCurrentEntity = null; |
|
|
|
} |
|
|
|
public void reset(){ |
|
fEntities.clear(); |
|
fCurrentEntity = null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void reset(XMLComponentManager componentManager) |
|
throws XMLConfigurationException { |
|
|
|
|
|
// xerces features |
|
|
|
fWarnDuplicateEntityDef = componentManager.getFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); |
|
|
|
fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); |
|
|
|
fEntities.clear(); |
|
fCurrentEntity = null; |
|
|
|
} // reset(XMLComponentManager) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Entity getEntity(String name) { |
|
return fEntities.get(name); |
|
} // getEntity(String) |
|
|
|
public Map<String, Entity> getEntities() { |
|
return fEntities; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addInternalEntity(String name, String text) { |
|
if (!fEntities.containsKey(name)) { |
|
Entity entity = new Entity.InternalEntity(name, text, fInExternalSubset); |
|
fEntities.put(name, entity); |
|
} |
|
else{ |
|
if(fWarnDuplicateEntityDef){ |
|
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
new Object[]{ name }, |
|
XMLErrorReporter.SEVERITY_WARNING ); |
|
} |
|
} |
|
} // addInternalEntity(String,String) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addExternalEntity(String name, |
|
String publicId, String literalSystemId, |
|
String baseSystemId) { |
|
if (!fEntities.containsKey(name)) { |
|
if (baseSystemId == null) { |
|
// search for the first external entity on the stack |
|
//xxx commenting the 'size' variable.. |
|
/** |
|
* int size = fEntityStack.size(); |
|
* if (size == 0 && fCurrentEntity != null && fCurrentEntity.entityLocation != null) { |
|
* baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
* } |
|
*/ |
|
|
|
|
|
if (fCurrentEntity != null && fCurrentEntity.entityLocation != null) { |
|
baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
} |
|
/** |
|
* for (int i = size - 1; i >= 0 ; i--) { |
|
* ScannedEntity externalEntity = |
|
* (ScannedEntity)fEntityStack.elementAt(i); |
|
* if (externalEntity.entityLocation != null && externalEntity.entityLocation.getExpandedSystemId() != null) { |
|
* baseSystemId = externalEntity.entityLocation.getExpandedSystemId(); |
|
* break; |
|
* } |
|
* } |
|
*/ |
|
} |
|
|
|
fCurrentEntity = fEntityManager.getCurrentEntity(); |
|
Entity entity = new Entity.ExternalEntity(name, |
|
new XMLResourceIdentifierImpl(publicId, literalSystemId, |
|
baseSystemId, expandSystemId(literalSystemId, baseSystemId)), |
|
null, fInExternalSubset); |
|
//TODO :: Forced to pass true above remove it. |
|
//(fCurrentEntity == null) ? fasle : fCurrentEntity.isEntityDeclInExternalSubset()); |
|
|
|
fEntities.put(name, entity); |
|
} |
|
else{ |
|
if(fWarnDuplicateEntityDef){ |
|
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
new Object[]{ name }, |
|
XMLErrorReporter.SEVERITY_WARNING ); |
|
} |
|
} |
|
|
|
} // addExternalEntity(String,String,String,String) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isExternalEntity(String entityName) { |
|
|
|
Entity entity = fEntities.get(entityName); |
|
if (entity == null) { |
|
return false; |
|
} |
|
return entity.isExternal(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isEntityDeclInExternalSubset(String entityName) { |
|
|
|
Entity entity = fEntities.get(entityName); |
|
if (entity == null) { |
|
return false; |
|
} |
|
return entity.isEntityDeclInExternalSubset(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addUnparsedEntity(String name, |
|
String publicId, String systemId, |
|
String baseSystemId, String notation) { |
|
|
|
fCurrentEntity = fEntityManager.getCurrentEntity(); |
|
if (!fEntities.containsKey(name)) { |
|
Entity entity = new Entity.ExternalEntity(name, |
|
new XMLResourceIdentifierImpl(publicId, systemId, baseSystemId, null), |
|
notation, fInExternalSubset); |
|
fEntities.put(name, entity); |
|
} |
|
else{ |
|
if(fWarnDuplicateEntityDef){ |
|
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
new Object[]{ name }, |
|
XMLErrorReporter.SEVERITY_WARNING ); |
|
} |
|
} |
|
} // addUnparsedEntity(String,String,String,String) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isUnparsedEntity(String entityName) { |
|
|
|
Entity entity = fEntities.get(entityName); |
|
if (entity == null) { |
|
return false; |
|
} |
|
return entity.isUnparsed(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isDeclaredEntity(String entityName) { |
|
|
|
Entity entity = fEntities.get(entityName); |
|
return entity != null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static String expandSystemId(String systemId) { |
|
return expandSystemId(systemId, null); |
|
} // expandSystemId(String):String |
|
|
|
|
|
private static String gUserDir; |
|
|
|
private static String gEscapedUserDir; |
|
|
|
private static boolean gNeedEscaping[] = new boolean[128]; |
|
|
|
private static char gAfterEscaping1[] = new char[128]; |
|
|
|
private static char gAfterEscaping2[] = new char[128]; |
|
private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', |
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
|
|
|
static { |
|
for (int i = 0; i <= 0x1f; i++) { |
|
gNeedEscaping[i] = true; |
|
gAfterEscaping1[i] = gHexChs[i >> 4]; |
|
gAfterEscaping2[i] = gHexChs[i & 0xf]; |
|
} |
|
gNeedEscaping[0x7f] = true; |
|
gAfterEscaping1[0x7f] = '7'; |
|
gAfterEscaping2[0x7f] = 'F'; |
|
char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}', |
|
'|', '\\', '^', '~', '[', ']', '`'}; |
|
int len = escChs.length; |
|
char ch; |
|
for (int i = 0; i < len; i++) { |
|
ch = escChs[i]; |
|
gNeedEscaping[ch] = true; |
|
gAfterEscaping1[ch] = gHexChs[ch >> 4]; |
|
gAfterEscaping2[ch] = gHexChs[ch & 0xf]; |
|
} |
|
} |
|
// To escape the "user.dir" system property, by using %HH to represent |
|
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%' |
|
// and '"'. It's a static method, so needs to be synchronized. |
|
// this method looks heavy, but since the system property isn't expected |
|
// to change often, so in most cases, we only need to return the string |
|
// that was escaped before. |
|
// According to the URI spec, non-ASCII characters (whose value >= 128) |
|
// need to be escaped too. |
|
// REVISIT: don't know how to escape non-ASCII characters, especially |
|
|
|
private static synchronized String getUserDir() { |
|
|
|
String userDir = ""; |
|
try { |
|
userDir = SecuritySupport.getSystemProperty("user.dir"); |
|
} |
|
catch (SecurityException se) { |
|
} |
|
|
|
|
|
if (userDir.length() == 0) |
|
return ""; |
|
|
|
// compute the new escaped value if the new property value doesn't |
|
|
|
if (userDir.equals(gUserDir)) { |
|
return gEscapedUserDir; |
|
} |
|
|
|
|
|
gUserDir = userDir; |
|
|
|
char separator = java.io.File.separatorChar; |
|
userDir = userDir.replace(separator, '/'); |
|
|
|
int len = userDir.length(), ch; |
|
StringBuilder buffer = new StringBuilder(len*3); |
|
|
|
if (len >= 2 && userDir.charAt(1) == ':') { |
|
ch = Character.toUpperCase(userDir.charAt(0)); |
|
if (ch >= 'A' && ch <= 'Z') { |
|
buffer.append('/'); |
|
} |
|
} |
|
|
|
|
|
int i = 0; |
|
for (; i < len; i++) { |
|
ch = userDir.charAt(i); |
|
|
|
if (ch >= 128) |
|
break; |
|
if (gNeedEscaping[ch]) { |
|
buffer.append('%'); |
|
buffer.append(gAfterEscaping1[ch]); |
|
buffer.append(gAfterEscaping2[ch]); |
|
// record the fact that it's escaped |
|
} |
|
else { |
|
buffer.append((char)ch); |
|
} |
|
} |
|
|
|
|
|
if (i < len) { |
|
|
|
byte[] bytes = null; |
|
byte b; |
|
try { |
|
bytes = userDir.substring(i).getBytes("UTF-8"); |
|
} catch (java.io.UnsupportedEncodingException e) { |
|
|
|
return userDir; |
|
} |
|
len = bytes.length; |
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
b = bytes[i]; |
|
|
|
if (b < 0) { |
|
ch = b + 256; |
|
buffer.append('%'); |
|
buffer.append(gHexChs[ch >> 4]); |
|
buffer.append(gHexChs[ch & 0xf]); |
|
} |
|
else if (gNeedEscaping[b]) { |
|
buffer.append('%'); |
|
buffer.append(gAfterEscaping1[b]); |
|
buffer.append(gAfterEscaping2[b]); |
|
} |
|
else { |
|
buffer.append((char)b); |
|
} |
|
} |
|
} |
|
|
|
|
|
if (!userDir.endsWith("/")) |
|
buffer.append('/'); |
|
|
|
gEscapedUserDir = buffer.toString(); |
|
|
|
return gEscapedUserDir; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static String expandSystemId(String systemId, String baseSystemId) { |
|
|
|
|
|
if (systemId == null || systemId.length() == 0) { |
|
return systemId; |
|
} |
|
|
|
try { |
|
new URI(systemId); |
|
return systemId; |
|
} catch (URI.MalformedURIException e) { |
|
// continue on... |
|
} |
|
|
|
String id = fixURI(systemId); |
|
|
|
|
|
URI base = null; |
|
URI uri = null; |
|
try { |
|
if (baseSystemId == null || baseSystemId.length() == 0 || |
|
baseSystemId.equals(systemId)) { |
|
String dir = getUserDir(); |
|
base = new URI("file", "", dir, null, null); |
|
} |
|
else { |
|
try { |
|
base = new URI(fixURI(baseSystemId)); |
|
} |
|
catch (URI.MalformedURIException e) { |
|
if (baseSystemId.indexOf(':') != -1) { |
|
// for xml schemas we might have baseURI with |
|
|
|
base = new URI("file", "", fixURI(baseSystemId), null, null); |
|
} |
|
else { |
|
String dir = getUserDir(); |
|
dir = dir + fixURI(baseSystemId); |
|
base = new URI("file", "", dir, null, null); |
|
} |
|
} |
|
} |
|
|
|
uri = new URI(base, id); |
|
} |
|
catch (Exception e) { |
|
// let it go through |
|
|
|
} |
|
|
|
if (uri == null) { |
|
return systemId; |
|
} |
|
return uri.toString(); |
|
|
|
} // expandSystemId(String,String):String |
|
// |
|
// Protected static methods |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected static String fixURI(String str) { |
|
|
|
|
|
str = str.replace(java.io.File.separatorChar, '/'); |
|
|
|
|
|
if (str.length() >= 2) { |
|
char ch1 = str.charAt(1); |
|
|
|
if (ch1 == ':') { |
|
char ch0 = Character.toUpperCase(str.charAt(0)); |
|
if (ch0 >= 'A' && ch0 <= 'Z') { |
|
str = "/" + str; |
|
} |
|
} |
|
|
|
else if (ch1 == '/' && str.charAt(0) == '/') { |
|
str = "file:" + str; |
|
} |
|
} |
|
|
|
|
|
return str; |
|
|
|
} // fixURI(String):String |
|
|
|
|
|
public void startExternalSubset() { |
|
fInExternalSubset = true; |
|
} |
|
|
|
public void endExternalSubset() { |
|
fInExternalSubset = false; |
|
} |
|
} |