com.okworx.ilcd.validation.util.ResourceResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ilcd-validation Show documentation
Show all versions of ilcd-validation Show documentation
A Java library for performing technical validation of data in ILCD data format.
package com.okworx.ilcd.validation.util;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
/**
* ResourceResolver class.
*
* @author oliver.kusche
* @version $Id: $Id
*/
public class ResourceResolver implements LSResourceResolver {
protected final Logger log = org.apache.logging.log4j.LogManager.getLogger(this.getClass());
private String prefix;
/**
* Constructor for ResourceResolver.
*
* @param prefix a {@link java.lang.String} object.
*/
public ResourceResolver(String prefix) {
log.debug("Setting up resolver, using URL prefix " + prefix);
this.prefix = prefix;
}
/** {@inheritDoc} */
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
if (log.isTraceEnabled())
log.trace(systemId + " ### " + baseURI);
String newSysId = prefix + systemId;
if (systemId.equals("http://www.w3.org/2001/xml.xsd"))
newSysId = prefix + "ext/xml.xsd";
InputStream resourceAsStream;
try {
resourceAsStream = new URL(newSysId).openStream();
} catch (MalformedURLException e) {
log.error(e);
return null;
} catch (IOException e) {
log.error(e);
return null;
}
return new LSInputImpl(publicId, systemId, resourceAsStream);
}
protected class LSInputImpl implements LSInput {
private String publicId;
private String systemId;
public String getPublicId() {
return publicId;
}
public void setPublicId(String publicId) {
this.publicId = publicId;
}
public String getBaseURI() {
return null;
}
public InputStream getByteStream() {
return null;
}
public boolean getCertifiedText() {
return false;
}
public Reader getCharacterStream() {
return null;
}
public String getEncoding() {
return null;
}
public String getStringData() {
synchronized (inputStream) {
try {
byte[] input = new byte[inputStream.available()];
inputStream.read(input);
String contents = new String(input);
return contents;
} catch (IOException e) {
log.error(e.getClass().getName() + ": " + e.getMessage());
log.error("When resolving " + systemId);
e.printStackTrace();
return null;
}
}
}
public void setBaseURI(String baseURI) {
}
public void setByteStream(InputStream byteStream) {
}
public void setCertifiedText(boolean certifiedText) {
}
public void setCharacterStream(Reader characterStream) {
}
public void setEncoding(String encoding) {
}
public void setStringData(String stringData) {
}
public String getSystemId() {
return systemId;
}
public void setSystemId(String systemId) {
this.systemId = systemId;
}
public BufferedInputStream getInputStream() {
return inputStream;
}
public void setInputStream(BufferedInputStream inputStream) {
this.inputStream = inputStream;
}
private BufferedInputStream inputStream;
public LSInputImpl(String publicId, String sysId, InputStream input) {
this.publicId = publicId;
this.systemId = sysId;
this.inputStream = new BufferedInputStream(input);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy