下面列出了org.w3c.dom.ls.LSInput#setByteStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testLSInputParsingByteStream() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSInput src = impl.createLSInput();
try (InputStream is = new FileInputStream(ASTROCAT)) {
src.setByteStream(is);
assertNotNull(src.getByteStream());
// set certified accessor methods
boolean origCertified = src.getCertifiedText();
src.setCertifiedText(true);
assertTrue(src.getCertifiedText());
src.setCertifiedText(origCertified); // set back to orig
src.setSystemId(filenameToURL(ASTROCAT));
Document doc = domParser.parse(src);
Element result = doc.getDocumentElement();
assertEquals(result.getTagName(), "stardb");
}
}
@Override
public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
final LSInput input = lsImplementation.createLSInput();
final String path = URI.create(systemId).getPath();
input.setPublicId(publicId);
input.setBaseURI(baseURI);
input.setSystemId(path);
input.setByteStream(LocalResolver.class.getResourceAsStream("/xsd/" + path));
return input;
}
private LSInput getXmlSource(String xml1) {
LSInput src = implLS.createLSInput();
try {
if (xml1.endsWith(".xml"))
src.setByteStream(this.getClass().getResourceAsStream(XML_FILE_INTERNAL_DTD));
else
src.setStringData(xml1);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
return src;
}
private LSInput createLSInputEncoding() {
LSInput src = implLS.createLSInput();
Assert.assertFalse(src == null, "Could not create LSInput from DOMImplementationLS");
try {
src.setByteStream(new ByteArrayInputStream(encodingXML.getBytes("UTF-16")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
return src;
}
@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
String location = URL_MAP.get(systemId);
if (location == null) {
throw new IllegalStateException("Unable to find mapping for:" + systemId);
}
InputStream is = this.getClass().getResourceAsStream(location);
LSInput input = dls.createLSInput();
input.setByteStream(is);
input.setPublicId(publicId);
input.setSystemId(systemId);
return input;
}
public LSInput resolveResource( String type, String namespaceURI, String publicId, String systemId, String baseURI )
{
String resourceName = local.getProperty( systemId );
if( resourceName == null )
{
System.out.println( "type: " + type );
System.out.println( "namespaceURI: " + namespaceURI );
System.out.println( "publicId: " + publicId );
System.out.println( "systemId: " + systemId );
System.out.println( "baseURI: " + baseURI );
return null;
}
InputStream resource = getClass().getClassLoader().getResourceAsStream( resourceName );
LSInput input;
try
{
input = getLSInput();
}
catch( Exception e )
{
throw new UnsupportedOperationException( "Internal problem. Please report to [email protected] mailing list.", e );
}
input.setBaseURI( baseURI );
input.setByteStream( resource );
input.setPublicId( publicId );
input.setSystemId( systemId );
return input;
}
/**
* @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
if (!type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
return null;
}
LOG.error(type);
LOG.error(namespaceURI);
LOG.error(publicId);
LOG.error(systemId);
LOG.error(baseURI);
String path = resolveSystemId(systemId);
if (path == null) {
return null;
}
LOG.debug("Looking up resource '" + path + "' for system id '" + systemId + "'");
InputStream is = getClass().getClassLoader().getResourceAsStream(path);
if (is == null) {
String message = "Unable to find schema (" + path + ") for: " + systemId;
LOG.error(message);
throw new RuntimeException/*SAXException*/(message);
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
DOMImplementationLS dils = (DOMImplementationLS) domImpl;
LSInput input = dils.createLSInput();
input.setByteStream(is);
return input;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private LSInput loadLSInput(String ns) {
String path = ToolConstants.CXF_SCHEMAS_DIR_INJAR + NSFILEMAP.get(ns);
URL url = getClass().getClassLoader().getResource(path);
LSInput lsin = new LSInputImpl();
lsin.setSystemId(url.toString());
try {
lsin.setByteStream(url.openStream());
} catch (IOException e) {
return null;
}
return lsin;
}
/**
* Allow the application to resolve external resources.
*/
public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
// System.out.println("\n>> Resolving " + "\n"
// + "TYPE: " + type + "\n"
// + "NAMESPACE_URI: " + namespaceURI + "\n"
// + "PUBLIC_ID: " + publicId + "\n"
// + "SYSTEM_ID: " + systemId + "\n"
// + "BASE_URI: " + baseURI + "\n" );
final LSInput lsInput = new LSInputImpl();
// In all Java EE schema xsd files, the <xsd:include schemaLocation=../> always reference to a relative path.
// so the systemId here will be the xsd file name.
final URL schemaURL = JaxbJavaee.getSchemaURL(systemId);
InputStream is = null;
if (schemaURL != null) {
try {
is = schemaURL.openStream();
} catch (final IOException e) {
//should not happen
throw new RuntimeException(e);
}
}
lsInput.setSystemId(systemId);
lsInput.setByteStream(is);
return lsInput;
}