List<Node> nodes = XMLUtil.filter(nl, toFilter); List<Node> nodes2 = XMLUtil.filter(nl2, toFilter); Node n = nodes.get(i); Node n2 = nodes2.get(i); XMLUtil.assertEquivalent(n, n2);
if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = XMLUtil.elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); if (!hasValidChildren && (textContent = XMLUtil.getTextContent(n)) != null) { sb.append(textContent);
public static void validate(final Node node, final String schemaFile) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new URL(findResource(schemaFile).toURI().toASCIIString())); Validator validator = schema.newValidator(); // validate the DOM tree try { validator.validate(new DOMSource(node)); } catch (SAXException e) { ActiveMQClientLogger.LOGGER.errorOnXMLTransformInvalidConf(e); throw new IllegalStateException("Invalid configuration", e); } }
@Test public void testElementToString_1() throws Exception { String s = "<a b=\"something\">somethingelse</a>"; Element e = XMLUtil.stringToElement(s); String tostring = XMLUtil.elementToString(e); Element convertedAgain = XMLUtil.stringToElement(tostring); XMLUtil.assertEquivalent(e, convertedAgain); }
@Test public void testEquivalent_1() throws Exception { String s = "<a/>"; String s2 = "<a/>"; XMLUtil.assertEquivalent(XMLUtil.stringToElement(s), XMLUtil.stringToElement(s2)); }
public Configuration parseMainConfig(final InputStream input) throws Exception { Reader reader = new InputStreamReader(input); String xml = XMLUtil.readerToString(reader); xml = XMLUtil.replaceSystemProps(xml); Element e = XMLUtil.stringToElement(xml); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(XMLUtil.findResource("schema/artemis-server.xsd")); Validator validator = schema.newValidator(); try { validator.validate(new DOMSource(e)); } catch (Exception ex) { ActiveMQServerLogger.LOGGER.error(ex.getMessage()); } Configuration config = new ConfigurationImpl(); parseMainConfig(e, config); return config; }
@Test public void testGetTextContext_4() throws Exception { String document = "<blah someattribute=\"somevalue\"><a></a></blah>"; Element e = XMLUtil.stringToElement(document); String s = XMLUtil.getTextContent(e); Element subelement = XMLUtil.stringToElement(s); Assert.assertEquals("a", subelement.getNodeName()); }
public static Element stringToElement(final String s) throws Exception { return XMLUtil.readerToElement(new StringReader(s)); }
public void readConfiguration() throws Exception { URL url; url = Thread.currentThread().getContextClassLoader().getResource(configurationUrl); if (url == null) { // trying a different classloader now url = getClass().getClassLoader().getResource(configurationUrl); } if (url == null) { // The URL is outside of the classloader. Trying a pure url now url = new URL(configurationUrl); } // create a reader try (Reader reader = new InputStreamReader(url.openStream())) { String xml = XMLUtil.readerToString(reader); //replace any system props xml = XMLUtil.replaceSystemProps(xml); Element e = XMLUtil.stringToElement(xml); //iterate around all the deployables for (Deployable deployable : deployables.values()) { String root = deployable.getRootElement(); NodeList children = e.getElementsByTagName(root); //if the root element exists then parse it if (root != null && children.getLength() > 0) { Node item = children.item(0); XMLUtil.validate(item, deployable.getSchema()); deployable.parse((Element) item, url); } } } }
/** * Note: if the content is another element or set of elements, it returns a string representation * of the hierarchy. */ public static String getTextContent(final Node n) { if (n.hasChildNodes()) { StringBuffer sb = new StringBuffer(); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { sb.append(XMLUtil.elementToString(nl.item(i))); if (i < nl.getLength() - 1) { sb.append('\n'); } } String s = sb.toString(); if (s.length() != 0) { return s; } } return n.getTextContent(); }
public void parseConfiguration(final InputStream input) throws Exception { Reader reader = new InputStreamReader(input); String xml = XMLUtil.readerToString(reader); xml = XMLUtil.replaceSystemProps(xml); Element e = XMLUtil.stringToElement(xml); // only parse elements from <jms> NodeList children = e.getElementsByTagName(CONFIGURATION_SCHEMA_ROOT_ELEMENT); if (children.getLength() > 0) { parseConfiguration(children.item(0)); } }
String xml = XMLUtil.readerToString(reader); xml = XMLUtil.replaceSystemProps(xml); configuration = (MessageServiceConfiguration) jaxb.createUnmarshaller().unmarshal(new StringReader(xml));
/** * test does not pass in eclipse (because it can not find artemis-configuration.xsd). * It runs fine on the CLI with the proper env setting. */ @Test public void testMinimalConfiguration() throws Exception { String xml = "<core xmlns='urn:activemq:core'>" + "</core>"; Element element = XMLUtil.stringToElement(xml); Assert.assertNotNull(element); XMLUtil.validate(element, "schema/artemis-configuration.xsd"); }
@Test public void testReplaceSystemProperties() { String before = "<configuration>\n" + " <test name=\"${sysprop1}\">content1</test>\n" + " <test name=\"test2\">content2</test>\n" + " <test name=\"test3\">content3</test>\n" + " <test name=\"test4\">${sysprop2}</test>\n" + " <test name=\"test5\">content5</test>\n" + " <test name=\"test6\">content6</test>\n" + "</configuration>"; String after = "<configuration>\n" + " <test name=\"test1\">content1</test>\n" + " <test name=\"test2\">content2</test>\n" + " <test name=\"test3\">content3</test>\n" + " <test name=\"test4\">content4</test>\n" + " <test name=\"test5\">content5</test>\n" + " <test name=\"test6\">content6</test>\n" + "</configuration>"; System.setProperty("sysprop1", "test1"); System.setProperty("sysprop2", "content4"); String replaced = XMLUtil.replaceSystemProps(before); Assert.assertEquals(after, replaced); }
@Test public void testElementToString_4() throws Exception { String s = "<a><![CDATA[somedata]]></a>"; Element e = XMLUtil.stringToElement(s); String tostring = XMLUtil.elementToString(e); Element convertedAgain = XMLUtil.stringToElement(tostring); XMLUtil.assertEquivalent(e, convertedAgain); }
@Test public void testEquivalent_4() throws Exception { String s = "<a attr1=\"val1\" attr2=\"val2\"/>"; String s2 = "<a attr2=\"val2\" attr1=\"val1\"/>"; XMLUtil.assertEquivalent(XMLUtil.stringToElement(s), XMLUtil.stringToElement(s2)); }
@Test public void testGetTextContext_3() throws Exception { String document = "<blah someattribute=\"somevalue\"><a/></blah>"; Element e = XMLUtil.stringToElement(document); String s = XMLUtil.getTextContent(e); Element subelement = XMLUtil.stringToElement(s); Assert.assertEquals("a", subelement.getNodeName()); }
public static Element urlToElement(final URL url) throws Exception { return XMLUtil.readerToElement(new InputStreamReader(url.openStream())); }
url = configFile.toURI().toURL(); reader = new InputStreamReader(url.openStream()); String xml = XMLUtil.readerToString(reader); xml = XMLUtil.replaceSystemProps(xml); Element e = XMLUtil.stringToElement(xml); if (root != null && children.getLength() > 0) { Node item = children.item(0); XMLUtil.validate(item, ((FileConfiguration) configuration).getSchema()); ((FileConfiguration) configuration).parse((Element) item, url);
/** * Note: if the content is another element or set of elements, it returns a string representation * of the hierarchy. */ public static String getTextContent(final Node n) { if (n.hasChildNodes()) { StringBuffer sb = new StringBuffer(); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { sb.append(XMLUtil.elementToString(nl.item(i))); if (i < nl.getLength() - 1) { sb.append('\n'); } } String s = sb.toString(); if (s.length() != 0) { return s; } } return n.getTextContent(); }