下面列出了怎么用org.w3c.dom.ls.LSInput的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
public final LSInput resolveResource (@Nonnull @Nonempty final String sType,
@Nullable final String sNamespaceURI,
@Nullable final String sPublicId,
@Nullable final String sSystemId,
@Nullable final String sBaseURI)
{
final LSInput ret = mainResolveResource (sType, sNamespaceURI, sPublicId, sSystemId, sBaseURI);
if (ret != null)
return ret;
// Pass to parent (if available)
if (m_aWrappedResourceResolver != null)
return m_aWrappedResourceResolver.resolveResource (sType, sNamespaceURI, sPublicId, sSystemId, sBaseURI);
// Not found
return null;
}
@Test
public void testExternalEncoding() {
try {
LSInput src = null;
LSParser dp = null;
src = createLSInputEncoding();
dp = createLSParser();
src.setEncoding("UTF-16");
Document doc = dp.parse(src);
Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
@Override
@Nullable
public LSInput mainResolveResource (@Nullable final String sType,
@Nullable final String sNamespaceURI,
@Nullable final String sPublicId,
@Nullable final String sSystemId,
@Nullable final String sBaseURI)
{
if (LOGGER.isInfoEnabled ())
LOGGER.info ("mainResolveResource (" +
sType +
", " +
sNamespaceURI +
", " +
sPublicId +
", " +
sSystemId +
", " +
sBaseURI +
")");
return null;
}
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
LSInput input = null;
Iterator<ValidatorSchemaFactory> it = mExtSchemaFactories.iterator();
while(it.hasNext()) {
ValidatorSchemaFactory fac = it.next();
LSResourceResolver resolver = fac.getLSResourceResolver();
if(resolver != null) {
input = resolver.resolveResource(type, namespaceURI, publicId, systemId, baseURI);
if(input != null) {
break;
}
}
}
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 final LSInput newLSInput( InputSource pSource )
{
final LSInputImpl lsInput = new LSInputImpl();
lsInput.setByteStream( pSource.getByteStream() );
lsInput.setCharacterStream( pSource.getCharacterStream() );
lsInput.setPublicId( lsInput.getPublicId() );
lsInput.setSystemId( pSource.getSystemId() );
lsInput.setEncoding( pSource.getEncoding() );
return lsInput;
}
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
public XSModel load(LSInput is) {
try {
Grammar g = loadGrammar(dom2xmlInputSource(is));
return ((XSGrammar) g).toXSModel();
} catch (Exception e) {
reportDOMFatalError(e);
return null;
}
}
/**
* Parse an XML Schema document from a resource identified by a
* <code>LSInput</code> .
* @param is The <code>LSInput</code> from which the source
* document is to be read.
* @return An XSModel representing this schema.
*/
public XSModel load(LSInput is) {
try {
fGrammarPool.clear();
return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel();
}
catch (Exception e) {
fSchemaLoader.reportDOMFatalError(e);
return null;
}
}
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
/**
* Parse an XML Schema document from a resource identified by a
* <code>LSInput</code> .
* @param is The <code>LSInput</code> from which the source
* document is to be read.
* @return An XSModel representing this schema.
*/
public XSModel load(LSInput is) {
try {
fGrammarPool.clear();
return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel();
}
catch (Exception e) {
fSchemaLoader.reportDOMFatalError(e);
return null;
}
}
/**
* 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;
}
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
public XSModel load(LSInput is) {
try {
Grammar g = loadGrammar(dom2xmlInputSource(is));
return ((XSGrammar) g).toXSModel();
} catch (Exception e) {
reportDOMFatalError(e);
return null;
}
}
@Override
public LSInput resolveResource(
String type, String namespaceURI, String publicId, String systemId, String baseURI) {
// give the entity resolver an opportunity (mostly to throw an exception)
try {
InputSource is = entityResolver.resolveEntity(publicId, systemId);
if (is != null) {
return new InputSourceToLSResource(is);
}
} catch (SAXException | IOException e) {
throw new RuntimeException(e);
}
// otherwise fall back on the default resolution path
return delegate.resolveResource(type, namespaceURI, publicId, systemId, baseURI);
}
@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId,
String systemId, String baseURI) {
for (int i = 0; i < systemIds.length; i++) {
if (systemId.endsWith(systemIds[i])) {
return returnValues[i];
}
}
return null;
}
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;
}
public XSModel load(LSInput is) {
try {
Grammar g = loadGrammar(dom2xmlInputSource(is));
return ((XSGrammar) g).toXSModel();
} catch (Exception e) {
reportDOMFatalError(e);
return null;
}
}
/**
* Parse an XML Schema document from a resource identified by a
* <code>LSInput</code> .
* @param is The <code>LSInput</code> from which the source
* document is to be read.
* @return An XSModel representing this schema.
*/
public XSModel load(LSInput is) {
try {
fGrammarPool.clear();
return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel();
}
catch (Exception e) {
fSchemaLoader.reportDOMFatalError(e);
return null;
}
}
@Nullable
public LSInput resolveResource (@Nullable final String sType,
@Nullable final String sNamespaceURI,
@Nullable final String sPublicId,
@Nullable final String sSystemId,
@Nullable final String sBaseURI)
{
return null;
}
/**
* Parse an XML Schema document from a resource identified by a
* <code>LSInput</code> .
* @param is The <code>LSInput</code> from which the source
* document is to be read.
* @return An XSModel representing this schema.
*/
public XSModel load(LSInput is) {
try {
fGrammarPool.clear();
return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel();
}
catch (Exception e) {
fSchemaLoader.reportDOMFatalError(e);
return null;
}
}
@Test
public void testResolveResource() throws Exception {
MetsLSResolver resolver = MetsLSResolver.getInstance();
LSInput input = resolver.resolveResource(null, null, null, "http://www.openarchives.org/OAI/2.0/oai_dc.xsd", null);
assertNotNull(input);
InputStream stream = input.getByteStream();
assertNotNull(stream);
stream.close();
}
public LSInput resolveResource(String type,
String namespaceURI,
String publicId,
String systemId,
String baseURI) {
LSInput lsi = mDelegate.resolveResource(type, namespaceURI, publicId, systemId, baseURI);
if (lsi == null) {
//if we can not get an input from catalog, then it could that
//there as a schema in types section which refer to schema compoment from other inline schema
//so we try to check in other inline schema.
WSDLSchema schema = findSchema(namespaceURI);
if(schema != null) {
Reader in = createInlineSchemaSource(mWsdlText, mWsdlPrefixes, mWsdlLinePositions, schema);
if(in != null) {
//create LSInput object
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "resolveResource", ex); //NOI18N
return null;
}
DOMImplementationLS dols = (DOMImplementationLS) domImpl.getFeature("LS","3.0");
lsi = dols.createLSInput();
lsi.setCharacterStream(in);
if(mWsdlSystemId != null) {
lsi.setSystemId(mWsdlSystemId);
}
return lsi;
}
}
}
return lsi;
}
@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 {
src.setStringData(xml1);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
return src;
}
@Test
public void test1() throws Exception {
String xsd1 = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + " xmlns:test='jaxp13_test1'\n"
+ " targetNamespace='jaxp13_test1'\n" + " elementFormDefault='qualified'>\n" + " <import namespace='jaxp13_test2'/>\n"
+ " <element name='test'/>\n" + " <element name='child1'/>\n" + "</schema>\n";
final NullPointerException EUREKA = new NullPointerException("NewSchema015");
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
StringReader reader = new StringReader(xsd1);
StreamSource source = new StreamSource(reader);
LSResourceResolver resolver = new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
LSInput input;
if (namespaceURI != null && namespaceURI.endsWith("jaxp13_test2")) {
throw EUREKA;
} else {
input = null;
}
return input;
}
};
schemaFactory.setResourceResolver(resolver);
try {
schemaFactory.newSchema(new Source[] { source });
Assert.fail("NullPointerException was not thrown.");
} catch (RuntimeException e) {
if (e != EUREKA)
throw e;
}
}
/**
* Resolves the given resource and adapts the <code>LSInput</code>
* returned into an <code>InputSource</code>.
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
if (fEntityResolver != null) {
LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
if (lsInput != null) {
final String pubId = lsInput.getPublicId();
final String sysId = lsInput.getSystemId();
final String baseSystemId = lsInput.getBaseURI();
final Reader charStream = lsInput.getCharacterStream();
final InputStream byteStream = lsInput.getByteStream();
final String data = lsInput.getStringData();
final String encoding = lsInput.getEncoding();
/**
* An LSParser looks at inputs specified in LSInput in
* the following order: characterStream, byteStream,
* stringData, systemId, publicId. For consistency
* with the DOM Level 3 Load and Save Recommendation
* use the same lookup order here.
*/
InputSource inputSource = new InputSource();
inputSource.setPublicId(pubId);
inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);
if (charStream != null) {
inputSource.setCharacterStream(charStream);
}
else if (byteStream != null) {
inputSource.setByteStream(byteStream);
}
else if (data != null && data.length() != 0) {
inputSource.setCharacterStream(new StringReader(data));
}
inputSource.setEncoding(encoding);
return inputSource;
}
}
return null;
}
public LSInput resolveResource(String type,
String namespaceURI,
String publicId,
String systemId,
String baseURI) {
InputStream is = null;
String resourceName = systemId;
int index = systemId.lastIndexOf('/');
if (index != -1) {
resourceName = systemId.substring(index+1);
}
String resourcePath = (ParserUtils.schemaResourcePrefix != null) ?
(ParserUtils.schemaResourcePrefix + resourceName) :
resourceName;
if (ParserUtils.isSchemaResourcePrefixFileUrl) {
try {
File f = new File(new URI(resourcePath));
if (f.exists()) {
is = new FileInputStream(f);
}
} catch (Exception e) {
}
} else {
is = this.getClass().getResourceAsStream(resourcePath);
}
MyLSInput ls = new MyLSInput();
ls.setByteStream(is);
ls.setSystemId(systemId); // See CR 6402288
return ls;
}
public XSModel load(LSInput is) {
try {
Grammar g = loadGrammar(dom2xmlInputSource(is));
return ((XSGrammar) g).toXSModel();
} catch (Exception e) {
reportDOMFatalError(e);
return null;
}
}
/**
* @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;
}
if (!systemId.startsWith(CONTENT_TYPE_PREFIX)) {
LOG.warn("Cannot resolve non-ContentType resources");
return null;
}
NotificationContentTypeBo notificationContentType = resolveContentType(systemId);
if (notificationContentType == null) {
LOG.error("Unable to resolve system id '" + systemId + "' locally...delegating to default resolution strategy.");
return null;
}
Reader reader = new StringReader(notificationContentType.getXsd());
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
DOMImplementationLS dils = (DOMImplementationLS) domImpl;
LSInput input = dils.createLSInput();
input.setCharacterStream(reader);
return input;
} catch (Exception e) {
throw new RuntimeException(e);
}
}