本文整理汇总了Java中org.w3c.dom.ls.DOMImplementationLS.createLSParser方法的典型用法代码示例。如果您正苦于以下问题:Java DOMImplementationLS.createLSParser方法的具体用法?Java DOMImplementationLS.createLSParser怎么用?Java DOMImplementationLS.createLSParser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.ls.DOMImplementationLS
的用法示例。
在下文中一共展示了DOMImplementationLS.createLSParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLSInputParsingByteStream
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
@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");
}
}
示例2: testLSInputParsingString
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
@Test
public void testLSInputParsingString() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>";
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSSerializer domSerializer = impl.createLSSerializer();
// turn off xml decl in serialized string for comparison
domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
LSInput src = impl.createLSInput();
src.setStringData(xml);
assertEquals(src.getStringData(), xml);
Document doc = domParser.parse(src);
String result = domSerializer.writeToString(doc);
assertEquals(result, "<test>runDocumentLS_Q6</test>");
}
示例3: loadContext
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
private void loadContext(final Collection<String> routes) {
try {
DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
JAXBContext jaxbContext = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
List<RouteDefinition> routeDefs = new ArrayList<>();
for (String route : routes) {
try (InputStream input = IOUtils.toInputStream(route, StandardCharsets.UTF_8)) {
LSInput lsinput = domImpl.createLSInput();
lsinput.setByteStream(input);
Node routeElement = parser.parse(lsinput).getDocumentElement();
routeDefs.add(unmarshaller.unmarshal(routeElement, RouteDefinition.class).getValue());
}
}
camelContext.addRouteDefinitions(routeDefs);
} catch (Exception e) {
LOG.error("While loading Camel context {}", e);
throw new CamelException(e);
}
}
示例4: normalizeXML
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
private static String normalizeXML(String xml) throws Exception {
// Remove all white space adjoining tags ("trim all elements")
xml = xml.replaceAll("\\s*<", "<");
xml = xml.replaceAll(">\\s*", ">");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input = domLS.createLSInput();
input.setStringData(xml);
Document document = lsParser.parse(input);
LSSerializer lsSerializer = domLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
return lsSerializer.writeToString(document);
}
示例5: normalizeXML
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Normalize and pretty-print XML so that it can be compared using string
* compare. The following code does the following: - Removes comments -
* Makes sure attributes are ordered consistently - Trims every element -
* Pretty print the document
*
* @param xml The XML to be normalized
* @return The equivalent XML, but now normalized
*/
public static String normalizeXML(String xml) throws Exception {
// Remove all white space adjoining tags ("trim all elements")
xml = xml.replaceAll("\\s*<", "<");
xml = xml.replaceAll(">\\s*", ">");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input = domLS.createLSInput();
input.setStringData(xml);
Document document = lsParser.parse(input);
LSSerializer lsSerializer = domLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
return lsSerializer.writeToString(document);
}
示例6: testCreateNewUser
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Checking when creating an XML document using DOM Level 2 validating
* it without having a schema source or a schema location It must throw a
* sax parse exception.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCreateNewUser() throws Exception {
String resultFile = USER_DIR + "accountInfoOut.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.newDocument();
Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
account.setAttributeNode(accountID);
account.appendChild(document.createElement("FirstName"));
account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
account.appendChild(document.createElement("UserID"));
DOMImplementationLS impl
= (DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
try(FileOutputStream output = new FileOutputStream(resultFile)) {
MyDOMOutput domOutput = new MyDOMOutput();
domOutput.setByteStream(output);
writer.write(account, domOutput);
docBuilder.parse(resultFile);
}
assertTrue(eh.isAnyError());
}
示例7: testCheckScreenNameExists
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Checking for namespace normalization.
* @see <a href="content/screenName.xml">screenName.xml</a> has prefix of
* userName is bound to "http://hibid.com/user" namespace normalization
* will create a namespace of prefix us and attach userEmail.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCheckScreenNameExists() throws Exception {
String resultFile = USER_DIR + "screenName.out";
String xmlFile = XML_DIR + "screenName.xml";
String goldFile = GOLDEN_DIR + "screenNameGold.xml";
String nsTagName = "http://hibid.com/screenName";
String userNs = "http://hibid.com/user";
try (FileOutputStream output = new FileOutputStream(resultFile)) {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
Document document = builder.parseURI(xmlFile);
NodeList nl = document.getElementsByTagNameNS(nsTagName, "screen-name");
assertEquals(nl.getLength(), 1);
Element screenName = (Element)nl.item(0);
Element userEmail = document.createElementNS(userNs, "userEmail");
assertTrue(userEmail.isDefaultNamespace(userNs));
Text email = document.createTextNode("[email protected]");
userEmail.appendChild(email);
screenName.appendChild(userEmail);
document.normalizeDocument();
MyDOMOutput domoutput = new MyDOMOutput();
domoutput.setByteStream(output);
writer.write(document, domoutput);
}
assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
示例8: parse
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
public static Document parse(InputStream stream)
{
DOMImplementationLS impl = (DOMImplementationLS)REGISTRY.getDOMImplementation("LS");
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input=impl.createLSInput();
input.setByteStream(stream);
Document document = builder.parse(input);
LinkedList<Pair<Node, Node>> removed=new LinkedList<>();
removeBlankText(document, removed);
for(Pair<Node, Node> r: removed) r.get0().removeChild(r.get1());
return document;
}
示例9: parse
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
private static Document parse(LSInput input, boolean validateIfSchema) {
DOMImplementationLS impl = getDOMImpl();
LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = parser.getDomConfig();
config.setParameter("element-content-whitespace", false);
config.setParameter("namespaces", true);
config.setParameter("validate-if-schema", validateIfSchema);
return parser.parse(input);
}
示例10: parse
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
@Override
public Element parse(final InputStream input) {
try {
final DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
final LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
final LSInput lsinput = impl.createLSInput();
lsinput.setByteStream(input);
return parser.parse(lsinput).getDocumentElement();
} catch (Exception e) {
throw new IllegalArgumentException("Could not parse DOM", e);
}
}
示例11: getDocument
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
private Document getDocument(InputStream is) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// we need to ignore whitespace here so the nodeToString() method will be able to indent it properly:
parser.setFilter(new IgnoreWhitespaceFilter());
LSInput domInput = impl.createLSInput();
domInput.setByteStream(is);
return parser.parse(domInput);
}
示例12: testDOMConfiguration
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
@Test
public void testDOMConfiguration() {
final DOMErrorHandler handler = new DOMErrorHandler() {
public boolean handleError(final DOMError error) {
return false;
}
};
final LSResourceResolver resolver = new LSResourceResolver() {
public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
return null;
}
};
final Object[][] values = {
// parameter, value
{ "canonical-form", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, { "cdata-sections", Boolean.TRUE },
{ "check-character-normalization", Boolean.FALSE }, { "comments", Boolean.FALSE }, { "comments", Boolean.TRUE },
{ "datatype-normalization", Boolean.FALSE }, { "entities", Boolean.FALSE }, { "entities", Boolean.TRUE }, { "error-handler", handler },
{ "infoset", Boolean.TRUE }, { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE },
{ "namespace-declarations", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "split-cdata-sections", Boolean.TRUE },
{ "split-cdata-sections", Boolean.FALSE }, { "validate", Boolean.FALSE }, { "validate-if-schema", Boolean.FALSE },
{ "well-formed", Boolean.TRUE }, { "element-content-whitespace", Boolean.TRUE },
{ "charset-overrides-xml-encoding", Boolean.TRUE }, { "charset-overrides-xml-encoding", Boolean.FALSE }, { "disallow-doctype", Boolean.FALSE },
{ "ignore-unknown-character-denormalizations", Boolean.TRUE }, { "resource-resolver", resolver }, { "resource-resolver", null },
{ "supported-media-types-only", Boolean.FALSE }, };
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException parserConfigurationException) {
parserConfigurationException.printStackTrace();
Assert.fail(parserConfigurationException.toString());
}
DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = lsParser.getDomConfig();
for (int i = values.length; --i >= 0;) {
Object val = values[i][1];
String param = (String) values[i][0];
try {
config.setParameter(param, val);
Object returned = config.getParameter(param);
Assert.assertEquals(val, returned, "'" + param + "' is set to " + returned + ", but expected " + val);
System.out.println("set '" + param + "'" + " to '" + val + "'" + " and returned '" + returned + "'");
} catch (DOMException e) {
String settings = "setting '" + param + "' to " + val;
System.err.println(settings);
e.printStackTrace();
Assert.fail(settings + ", " + e.toString());
}
}
}
示例13: testCheckCharNorm001
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has one Text node with not fully
* normalized characters, the 'check-character-normalization' parameter set
* to true, <br>
* <b>name</b>: error-handler <br>
* <b>value</b>: DOMErrorHandler. <br>
* <b>Expected results</b>: LSParser calls the specified error handler
*/
@Test
public void testCheckCharNorm001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
if (lsImpl == null) {
System.out.println("OK, the DOM implementation does not support the LS 3.0");
return;
}
LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = lsParser.getDomConfig();
if (!config.canSetParameter("check-character-normalization", Boolean.TRUE)) {
System.out.println("OK, setting 'check-character-normalization' to true is not supported");
return;
}
config.setParameter("check-character-normalization", Boolean.TRUE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData("<root>\u0073\u0075\u0063\u0327\u006F\u006E</root>");
Document doc = lsParser.parse(lsInput);
if (null == testHandler.getError()) {
Assert.fail("no error is reported, expected 'check-character-normalization-failure'");
}
return; // Status.passed("OK");
}
示例14: testCheckCharNorm002
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element contains a fully-normalized text, <br>
* <b>name</b>: check-character-normalization <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: LSParser reports no errors
*/
@Test
public void testCheckCharNorm002() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
if (lsImpl == null) {
System.out.println("OK, the DOM implementation does not support the LS 3.0");
return;
}
LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = lsParser.getDomConfig();
if (!config.canSetParameter("check-character-normalization", Boolean.FALSE)) {
Assert.fail("setting 'check-character-normalization' to false is not supported");
}
config.setParameter("check-character-normalization", Boolean.FALSE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData("<root>fully-normalized</root>");
Document doc = lsParser.parse(lsInput);
if (null != testHandler.getError()) {
Assert.fail("no error is expected, but reported: " + testHandler.getError());
}
return; // Status.passed("OK");
}
示例15: newInputSource
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
public InputSource newInputSource(String filename) throws Exception {
// Create DOMImplementationLS, and DOM L3 LSParser
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder bldr = fact.newDocumentBuilder();
DOMImplementationLS impl = (DOMImplementationLS) bldr.getDOMImplementation();
LSParser domparser = impl.createLSParser(MODE_SYNCHRONOUS, null);
domparser.setFilter(new MyDOMBuilderFilter());
// Parse the xml document to create the DOM Document using
// the DOM L3 LSParser and a LSInput (formerly LSInputSource)
Document doc = null;
LSInput src = impl.createLSInput();
// register the input file with the input source...
String systemId = filenameToURL(filename);
src.setSystemId(systemId);
try (Reader reader = new FileReader(filename)) {
src.setCharacterStream(reader);
src.setEncoding("UTF-8");
doc = domparser.parse(src);
}
// Use DOM L3 LSSerializer (previously called a DOMWriter)
// to serialize the xml doc DOM to a file stream.
String tmpCatalog = Files.createTempFile(Paths.get(USER_DIR), "catalog.xml", null).toString();
LSSerializer domserializer = impl.createLSSerializer();
domserializer.setFilter(new MyDOMWriterFilter());
domserializer.getNewLine();
DOMConfiguration config = domserializer.getDomConfig();
config.setParameter("xml-declaration", Boolean.TRUE);
String result = domserializer.writeToString(doc);
try (FileWriter os = new FileWriter(tmpCatalog, false)) {
os.write(result);
os.flush();
}
// Return the Input Source created from the Serialized DOM L3 Document.
InputSource catsrc = new InputSource(new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmpCatalog)))));
catsrc.setSystemId(systemId);
return catsrc;
}