本文整理汇总了Java中org.w3c.dom.ls.LSParser类的典型用法代码示例。如果您正苦于以下问题:Java LSParser类的具体用法?Java LSParser怎么用?Java LSParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LSParser类属于org.w3c.dom.ls包,在下文中一共展示了LSParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExternalEncoding
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
@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());
}
}
示例2: testLSInputParsingByteStream
import org.w3c.dom.ls.LSParser; //导入依赖的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");
}
}
示例3: testLSInputParsingString
import org.w3c.dom.ls.LSParser; //导入依赖的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>");
}
示例4: getBeanDefinition
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Finds bean definition element by id and type in Spring application context and
* performs unmarshalling in order to return JaxB object.
* @param project
* @param id
* @param type
* @return
*/
public <T> T getBeanDefinition(File configFile, Project project, String id, Class<T> type) {
LSParser parser = XMLUtils.createLSParser();
GetSpringBeanFilter filter = new GetSpringBeanFilter(id, type);
parser.setFilter(filter);
List<File> configFiles = new ArrayList<>();
configFiles.add(configFile);
configFiles.addAll(getConfigImports(configFile, project));
for (File file : configFiles) {
parser.parseURI(file.toURI().toString());
if (filter.getBeanDefinition() != null) {
return createJaxbObjectFromElement(filter.getBeanDefinition());
}
}
return null;
}
示例5: getBeanDefinitions
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Finds all bean definition elements by type and attribute values in Spring application context and
* performs unmarshalling in order to return a list of JaxB object.
* @param project
* @param type
* @param attributes
* @return
*/
public <T> List<T> getBeanDefinitions(File configFile, Project project, Class<T> type, Map<String, String> attributes) {
List<T> beanDefinitions = new ArrayList<T>();
List<File> importedFiles = getConfigImports(configFile, project);
for (File importLocation : importedFiles) {
beanDefinitions.addAll(getBeanDefinitions(importLocation, project, type, attributes));
}
LSParser parser = XMLUtils.createLSParser();
GetSpringBeansFilter filter = new GetSpringBeansFilter(type, attributes);
parser.setFilter(filter);
parser.parseURI(configFile.toURI().toString());
for (Element element : filter.getBeanDefinitions()) {
beanDefinitions.add(createJaxbObjectFromElement(element));
}
return beanDefinitions;
}
示例6: loadContext
import org.w3c.dom.ls.LSParser; //导入依赖的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);
}
}
示例7: normalizeXML
import org.w3c.dom.ls.LSParser; //导入依赖的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);
}
示例8: normalizeXML
import org.w3c.dom.ls.LSParser; //导入依赖的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);
}
示例9: testfilter0001
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning
* with state, input and output values orientation
* for public Document parse(LSInput is),
* <br><b>pre-conditions</b>: set filter that REJECTs any CHILD* node,
* <br><b>is</b>: xml1
* <br><b>output</b>: XML document with ELEMNENT1 and ELEMENT2 only.
*/
@Test
public void testfilter0001() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("CHILD")) {
return FILTER_REJECT;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例10: match
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
public boolean match(String template, Node source) {
LSParser dp = createLSParser();
if (dp == null) {
System.out.println("Can not create LSParser.");
return false;
}
LSInput src = getXmlSource(template);
Document doc = dp.parse(src);
return checkXMLs(doc, source);
}
示例11: testFilter0002
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
*/
@Test
public void testFilter0002() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("ELEMENT1")) {
return FILTER_SKIP;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例12: testFilter0003
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with ELEMENT1 only.
*/
@Test
public void testFilter0003() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("ELEMENT2")) {
return FILTER_INTERRUPT;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例13: testFilter0004
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that accepts all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: full XML document.
*/
@Test
public void testFilter0004() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例14: testFilter0005
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that REJECTs all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: empty XML document.
*/
@Test
public void testFilter0005() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_REJECT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
Document doc = parser.parse(getXmlSource(xml1));
NodeList children = doc.getDocumentElement().getChildNodes();
if (children.getLength() != 0) {
Assert.fail("Not all children skipped");
}
System.out.println("OKAY");
}
示例15: testFilter0006
import org.w3c.dom.ls.LSParser; //导入依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: empty XML document.
*/
@Test
public void testFilter0006() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_SKIP;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
Document doc = parser.parse(getXmlSource(xml1));
NodeList children = doc.getDocumentElement().getChildNodes();
if (children.getLength() != 0) {
Assert.fail("Not all children skipped");
}
System.out.println("OKAY");
}