|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package javax.xml.catalog; |
|
|
|
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.Reader; |
|
import java.io.StringReader; |
|
import java.net.URL; |
|
import javax.xml.parsers.ParserConfigurationException; |
|
import javax.xml.parsers.SAXParserFactory; |
|
import javax.xml.transform.Source; |
|
import javax.xml.transform.sax.SAXSource; |
|
import org.w3c.dom.ls.LSInput; |
|
import org.xml.sax.InputSource; |
|
import org.xml.sax.SAXException; |
|
import org.xml.sax.XMLReader; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
final class CatalogResolverImpl implements CatalogResolver { |
|
Catalog catalog; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public CatalogResolverImpl(Catalog catalog) { |
|
this.catalog = catalog; |
|
} |
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public InputSource resolveEntity(String publicId, String systemId) { |
|
|
|
CatalogMessages.reportNPEOnNull("systemId", systemId); |
|
|
|
|
|
systemId = Normalizer.normalizeURI(Util.getNotNullOrEmpty(systemId)); |
|
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId))); |
|
|
|
|
|
if (systemId != null && systemId.startsWith(Util.URN)) { |
|
systemId = Normalizer.decodeURN(systemId); |
|
if (publicId != null && !publicId.equals(systemId)) { |
|
systemId = null; |
|
} else { |
|
publicId = systemId; |
|
systemId = null; |
|
} |
|
} |
|
|
|
CatalogImpl c = (CatalogImpl)catalog; |
|
String resolvedSystemId = Util.resolve(c, publicId, systemId); |
|
|
|
if (resolvedSystemId != null) { |
|
return new InputSource(resolvedSystemId); |
|
} |
|
|
|
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve(); |
|
switch (resolveType) { |
|
case IGNORE: |
|
return new InputSource(new StringReader("")); |
|
case STRICT: |
|
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH, |
|
new Object[]{publicId, systemId}); |
|
} |
|
|
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
*/ |
|
CatalogResolverImpl entityResolver; |
|
|
|
@Override |
|
public Source resolve(String href, String base) { |
|
CatalogMessages.reportNPEOnNull("href", href); |
|
|
|
href = Util.getNotNullOrEmpty(href); |
|
base = Util.getNotNullOrEmpty(base); |
|
|
|
String result = null; |
|
CatalogImpl c = (CatalogImpl)catalog; |
|
String uri = Normalizer.normalizeURI(href); |
|
if (uri == null) { |
|
return null; |
|
} |
|
|
|
|
|
if (uri != null && uri.startsWith(Util.URN)) { |
|
String publicId = Normalizer.decodeURN(uri); |
|
if (publicId != null) { |
|
result = Util.resolve(c, publicId, null); |
|
} |
|
} |
|
|
|
|
|
if (result == null) { |
|
|
|
int hashPos = uri.indexOf("#"); |
|
if (hashPos >= 0) { |
|
uri = uri.substring(0, hashPos); |
|
} |
|
|
|
|
|
result = Util.resolve(c, null, uri); |
|
} |
|
|
|
|
|
if (result == null) { |
|
GroupEntry.ResolveType resolveType = c.getResolve(); |
|
switch (resolveType) { |
|
case IGNORE: |
|
return new SAXSource(new InputSource(new StringReader(""))); |
|
case STRICT: |
|
CatalogMessages.reportError(CatalogMessages.ERR_NO_URI_MATCH, |
|
new Object[]{href, base}); |
|
} |
|
try { |
|
URL url = null; |
|
|
|
if (base == null) { |
|
url = new URL(uri); |
|
result = url.toString(); |
|
} else { |
|
URL baseURL = new URL(base); |
|
url = (href.length() == 0 ? baseURL : new URL(baseURL, uri)); |
|
result = url.toString(); |
|
} |
|
} catch (java.net.MalformedURLException mue) { |
|
CatalogMessages.reportError(CatalogMessages.ERR_CREATING_URI, |
|
new Object[]{href, base}); |
|
} |
|
} |
|
|
|
SAXSource source = new SAXSource(); |
|
source.setInputSource(new InputSource(result)); |
|
setEntityResolver(source); |
|
return source; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void setEntityResolver(SAXSource source) { |
|
XMLReader reader = source.getXMLReader(); |
|
if (reader == null) { |
|
SAXParserFactory spFactory = new SAXParserFactoryImpl(); |
|
spFactory.setNamespaceAware(true); |
|
try { |
|
reader = spFactory.newSAXParser().getXMLReader(); |
|
} catch (ParserConfigurationException | SAXException ex) { |
|
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex); |
|
} |
|
} |
|
if (entityResolver != null) { |
|
entityResolver = new CatalogResolverImpl(catalog); |
|
} |
|
reader.setEntityResolver(entityResolver); |
|
source.setXMLReader(reader); |
|
} |
|
|
|
@Override |
|
public InputStream resolveEntity(String publicId, String systemId, String baseUri, String namespace) { |
|
InputSource is = resolveEntity(publicId, systemId); |
|
|
|
if (is != null && !is.isEmpty()) { |
|
|
|
try { |
|
return new URL(is.getSystemId()).openStream(); |
|
} catch (IOException ex) { |
|
//considered as no mapping. |
|
} |
|
|
|
} |
|
|
|
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve(); |
|
switch (resolveType) { |
|
case IGNORE: |
|
return null; |
|
case STRICT: |
|
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH, |
|
new Object[]{publicId, systemId}); |
|
} |
|
|
|
|
|
return null; |
|
} |
|
|
|
@Override |
|
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { |
|
InputSource is = resolveEntity(publicId, systemId); |
|
|
|
if (is != null && !is.isEmpty()) { |
|
return new LSInputImpl(is.getSystemId()); |
|
} |
|
|
|
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve(); |
|
switch (resolveType) { |
|
case IGNORE: |
|
return null; |
|
case STRICT: |
|
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH, |
|
new Object[]{publicId, systemId}); |
|
} |
|
|
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
class LSInputImpl implements LSInput { |
|
|
|
private String systemId; |
|
|
|
public LSInputImpl(String systemId) { |
|
this.systemId = systemId; |
|
} |
|
|
|
@Override |
|
public Reader getCharacterStream() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setCharacterStream(Reader characterStream) { |
|
} |
|
|
|
@Override |
|
public InputStream getByteStream() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setByteStream(InputStream byteStream) { |
|
} |
|
|
|
@Override |
|
public String getStringData() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setStringData(String stringData) { |
|
} |
|
|
|
@Override |
|
public String getSystemId() { |
|
return systemId; |
|
} |
|
|
|
@Override |
|
public void setSystemId(String systemId) { |
|
this.systemId = systemId; |
|
} |
|
|
|
@Override |
|
public String getPublicId() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setPublicId(String publicId) { |
|
} |
|
|
|
@Override |
|
public String getBaseURI() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setBaseURI(String baseURI) { |
|
} |
|
|
|
@Override |
|
public String getEncoding() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void setEncoding(String encoding) { |
|
} |
|
|
|
@Override |
|
public boolean getCertifiedText() { |
|
return false; |
|
} |
|
|
|
@Override |
|
public void setCertifiedText(boolean certifiedText) { |
|
} |
|
} |
|
|
|
} |