public static Document documentFrom(URL url) throws SAXException, IOException, ParserConfigurationException { return documentFrom(url.openStream()); }
private Document parseXml(String xml, String idString) throws Exception { try { return XpathUtils.documentFrom(xml); } catch (Exception e) { log.debug(String.format("Unable to parse HTTP response (%s) content to XML document '%s' ", idString, xml), e); throw e; } }
public static Document documentFrom(String xml) throws SAXException, IOException, ParserConfigurationException { return documentFrom(new ByteArrayInputStream(xml.getBytes(StringUtils.UTF8))); }
private Document documentFromContent(InputStream content, String idString) throws ParserConfigurationException, SAXException, IOException { try { return parseXml(contentToString(content, idString), idString); } catch (Exception e) { // Generate an empty document to make the unmarshallers happy. Ultimately the default // unmarshaller will be called to unmarshall into the service base exception. return XpathUtils.documentFrom("<empty/>"); } }
/** * URL to Document, * @param url the URL. * @return the Document. * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ public static Document documentFrom(URL url) throws SAXException, IOException, ParserConfigurationException { return documentFrom(url.openStream()); }
public static Document documentFrom(URL url) throws SAXException, IOException, ParserConfigurationException { return documentFrom(url.openStream()); }
private Document parseXml(String xml, String idString) throws Exception { try { return XpathUtils.documentFrom(xml); } catch (Exception e) { log.debug(String.format("Unable to parse HTTP response (%s) content to XML document '%s' ", idString, xml), e); throw e; } }
/** * Xml string to Document. * @param xml the xml string. * @return the Document. * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ public static Document documentFrom(String xml) throws SAXException, IOException, ParserConfigurationException { return documentFrom(new ByteArrayInputStream(xml.getBytes(StringUtils.UTF8))); }
public static Document documentFrom(String xml) throws SAXException, IOException, ParserConfigurationException { return documentFrom(new ByteArrayInputStream(xml.getBytes(StringUtils.UTF8))); }
private Document documentFromContent(InputStream content, String idString) throws ParserConfigurationException, SAXException, IOException { try { return parseXml(contentToString(content, idString), idString); } catch (Exception e) { // Generate an empty document to make the unmarshallers happy. Ultimately the default // unmarshaller will be called to unmarshall into the service base exception. return XpathUtils.documentFrom("<empty/>"); } }
@Test public void testGetRoot() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node childTitleNode = XpathUtils.asNode("Foo/Title", document); Node childAsRoot = XpathUtils.asNode("/", childTitleNode); assertEquals("#document", childAsRoot.getNodeName()); }
/** * Tests that {@link XpathUtils#asByteBuffer(String, Node)} correctly base64 * decodes the XML text data and transforms it into a ByteBuffer. */ @Test public void testAsByteBuffer() throws Exception { String expectedData = "hello world"; Document document = XpathUtils.documentFrom(DOCUMENT); ByteBuffer byteBuffer = XpathUtils.asByteBuffer("Foo/Blob", document); assertEquals(expectedData.length(), byteBuffer.limit()); String data = new String(byteBuffer.array(), StringUtils.UTF8); assertEquals(expectedData, data); assertEquals(null, XpathUtils.asByteBuffer("Foo/Empty", document)); }
@Test public void testAsNode() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node node = XpathUtils.asNode("Foo/Title", document); assertNotNull(node); assertEquals("Title", node.getNodeName()); }
/** * Tests that we can correctly pull a Byte out of an XML document. */ @Test public void testAsByte() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals(new Byte((byte) 123), XpathUtils.asByte("Foo/PositiveByte", document)); assertEquals(new Byte((byte) -99), XpathUtils.asByte("Foo/NegativeByte", document)); assertEquals(null, XpathUtils.asByte("Foo/Empty", document)); }
@Test public void testAsString() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals("Boo", XpathUtils.asString("Foo/Title", document)); assertEquals("", XpathUtils.asString("Foo/Empty", document)); assertEquals("Bar", XpathUtils.asString("Foo/Count/@Foo", document)); Document namespaceDocument = XpathUtils.documentFrom(DOCUMENT_WITH_NAMESPACE); assertEquals("174.129.242.223", XpathUtils.asString("/AllocateAddressResponse/publicIp", namespaceDocument)); }
@Test public void testAsBoolean() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals(true, XpathUtils.asBoolean("Foo/Enabled", document)); assertEquals(null, XpathUtils.asBoolean("Foo/Empty", document)); }
@Test public void testAsFloat() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals((Float) 0.0000071759f, XpathUtils.asFloat("Foo/Usage", document)); assertEquals(null, XpathUtils.asFloat("Foo/Empty", document)); }
@Test public void testXmlDocumentWithNamespace() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT_WITH_NAMESPACE); Node root = XpathUtils.asNode("/", document); assertNotNull(root); Node node = XpathUtils.asNode("AllocateAddressResponse", document); assertNotNull(node); }
@Test public void testAsInteger() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals((Integer) 1, XpathUtils.asInteger("Foo/Count", document)); assertEquals(null, XpathUtils.asInteger("Foo/Empty", document)); }
@Test public void testIsEmpty() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node emptyNode = XpathUtils.asNode("Foo/Fake", document); Node realNode = XpathUtils.asNode("Foo/Count", document); assertTrue(XpathUtils.isEmpty(emptyNode)); assertFalse(XpathUtils.isEmpty(realNode)); }