/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
public void testHandleServiceExceptionEncoding() throws Exception { String message = "foo & <foo> \"foo's\""; ServiceException exception = new ServiceException(message); exception.setLocator("test-locator"); handler.handleServiceException(exception, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionText = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionText); assertEquals( "round-tripped through character entities", message, exceptionText.getTextContent()); }
@SuppressWarnings("unchecked") public void testHandleServiceExceptionEncodingMore() throws Exception { String message1 = "foo & <foo> \"foo's\""; String message2 = "a \"different\" <message>"; ServiceException exception = new ServiceException(message1); exception.setLocator("test-locator"); exception.getExceptionText().add(message2); handler.handleServiceException(exception, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionText = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionText); String message = message1 + "\n" + message2; assertEquals( "round-tripped through character entities", message, exceptionText.getTextContent()); }
public void testHandleServiceExceptionCauses() throws Exception { // create a stack of three exceptions IllegalArgumentException illegalArgument = new IllegalArgumentException("Illegal argument here"); IOException ioException = new IOException("I/O exception here"); ioException.initCause(illegalArgument); ServiceException serviceException = new ServiceException("hello service exception"); serviceException.setCode("helloCode"); serviceException.setLocator("helloLocator"); serviceException.getExceptionText().add("helloText"); serviceException.initCause(ioException); handler.handleServiceException(serviceException, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionTextNode = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionTextNode); // normalise whitespace String exceptionText = exceptionTextNode.getNodeValue().replaceAll("\\s+", " "); assertTrue(exceptionText.indexOf(illegalArgument.getMessage()) != -1); assertTrue(exceptionText.indexOf(ioException.getMessage()) != -1); assertTrue(exceptionText.indexOf(serviceException.getMessage()) != -1); }
public void testHandleServiceExceptionNullMessages() throws Exception { // create a stack of three exceptions NullPointerException npe = new NullPointerException(); ServiceException serviceException = new ServiceException("hello service exception"); serviceException.setCode("helloCode"); serviceException.setLocator("helloLocator"); serviceException.getExceptionText().add("NullPointerException"); serviceException.initCause(npe); handler.handleServiceException(serviceException, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionTextNode = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionTextNode); // normalise whitespace String exceptionText = exceptionTextNode.getNodeValue().replaceAll("\\s+", " "); // used to contain an extra " null" at the end assertEquals("hello service exception NullPointerException", exceptionText); } }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
/** * Use an XPath string to select a single node. XPath namespace * prefixes are resolved from the context node, which may not * be what you want (see the next method). * * @param contextNode The node to start searching from. * @param str A valid XPath string. * @return The first node found that matches the XPath, or null. * * @throws TransformerException */ public static Node selectSingleNode(Node contextNode, String str) throws TransformerException { return selectSingleNode(contextNode, str, contextNode); }
private String getPropertyValue(Element part, String property) throws Exception { Element element = (Element)XPathAPI. selectSingleNode(part, "Property[@Name='" + property + "']"); if (element == null) { return null; } else { return element.getAttribute("Value"); } }
/** * @param xml The starting context (a Node or a Document, for example). * @param NodeListXPath xpath * @return A Node matches the NodeListXPath * null if nothing matches the NodeListXPath * @throws XPathExpressionException if xpath error */ public static Node getNode(Node xml, String NodeListXPath) throws XPathExpressionException { Node result = null; try { result = XPathAPI.selectSingleNode(xml, NodeListXPath); } catch (TransformerException e) { log.error("Error", e); } return result; }
public static Node findSingleNode(Node baseNode, String xpathExpression, Element nsElement) { try { Node node = XPathAPI.selectSingleNode(baseNode, xpathExpression, nsElement); return node; } catch (TransformerException e) { throw new RuntimeException("XPath error: " + e.getMessage(), e); } }
public Node getNode(Node base, String xpath) throws ProviderInvocationException { try { return XPathAPI.selectSingleNode(base, xpath); } catch (TransformerException e) { throw new ProviderInvocationException(e); } }
public Node getNode(Node base, String xpath) throws ProviderInvocationException { try { return XPathAPI.selectSingleNode(base, xpath); } catch (TransformerException e) { throw new ProviderInvocationException(e); } }
public static File writeBase64XMLToFile(InputStream stream, String extension) throws SAXException, IOException, ParserConfigurationException, DOMException, TransformerException { // ToDo: look at StAX to stream XML parsing instead of in memory DOM Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(stream); String binaryContent = XPathAPI.selectSingleNode( document.getFirstChild(), "text()").getTextContent(); InputStream byteStream = null; try { byteStream = new ByteArrayInputStream(binaryContent.getBytes()); return writeBase64ToFile(byteStream, extension); } finally { closeQuietly(byteStream); } }