@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()); } }
@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"); } }
@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>"); }
/** * 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; }
/** * 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; }
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); } }
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); }
/** * 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); }
/** * 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"); }
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); }
/** * 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"); }
/** * 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"); }
/** * 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"); }
/** * 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"); }
/** * 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"); }
/** * 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* start element, <br> * <b>is</b>: xml1 <br> * <b>output</b>: XML document with ELEMENT1 and ELEMENT2 only. */ @Test public void testFilter0007() { LSParser parser = createLSParser(); if (parser == null) { Assert.fail("Unable to create LSParser!"); } // set filter parser.setFilter(new LSParserFilter() { public short startElement(Element elt) { if (elt.getTagName().startsWith("CHILD")) { return FILTER_REJECT; } return FILTER_ACCEPT; } public short acceptNode(Node enode) { 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"); }
/** * 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 start element, <br> * <b>is</b>: xml1 <br> * <b>output</b>: XML document with CHILD1 and ELEMENT2 only. */ @Test public void testFilter0008() { LSParser parser = createLSParser(); if (parser == null) { Assert.fail("Unable to create LSParser!"); } // set filter parser.setFilter(new LSParserFilter() { public short startElement(Element elt) { if (elt.getTagName().equals("ELEMENT1")) { return FILTER_SKIP; } return FILTER_ACCEPT; } public short acceptNode(Node enode) { 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"); }
/** * 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 start element, <br> * <b>is</b>: xml1 <br> * <b>output</b>: XML document with ELEMENT1 only. */ @Test public void testFilter0009() { LSParser parser = createLSParser(); if (parser == null) { Assert.fail("Unable to create LSParser!"); } // set filter parser.setFilter(new LSParserFilter() { public short startElement(Element elt) { if (elt.getTagName().startsWith("ELEMENT2")) { return FILTER_INTERRUPT; } 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></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"); }
/** * 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 start element, <br> * <b>is</b>: xml1 <br> * <b>output</b>: empty XML document. */ @Test public void testFilter0010() { 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_REJECT; } public short acceptNode(Node enode) { return FILTER_ACCEPT; } 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"); }
/** * 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 testFilter0011() { 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_SKIP; } public short acceptNode(Node enode) { return FILTER_ACCEPT; } public int getWhatToShow() { return NodeFilter.SHOW_ALL; } }); Document doc = parser.parse(getXmlSource(xml1)); NodeList children = doc.getDocumentElement().getChildNodes(); if (children.getLength() != 1) { Assert.fail("Not all Element nodes skipped"); } System.out.println("OKAY"); }
/** * 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()); }