/** * Searches XML element with XPath and returns list of nodes found * * @param xml input stream with the XML in which the element is being searched * @param expression XPath expression used in search * @return {@link NodeList} of elements matching the XPath in the XML * @throws XPathExpressionException if there is an error in the XPath expression * @throws IOException if the XML at the specified path is missing * @throws SAXException if the XML cannot be parsed * @throws ParserConfigurationException */ public static NodeList findWithXPath(InputStream xml, String expression) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xml); doc.getDocumentElement().normalize(); XPath xPath = XPathFactory.newInstance().newXPath(); try { return (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new InvalidXPathException(expression, e); } }
public IAttributeValue getAttribute(IInstance row) { if (row instanceof XMLRow) { Node node = ((XMLRow)row).getNode(); Node selectedNode = null; try { selectedNode = node.selectSingleNode(getXpath()); } catch (InvalidXPathException e) { throw new IllegalArgumentException("Invalid XPath '" + getXpath() + "' in attribute '" + getName() + "'", e); } if (selectedNode == null) { return(new EmptyAttributeValue(this)); //throw new IllegalArgumentException("Column '" + getXPath() + "' not found in row '" + node.getPath() +"'"); } TextAttributeValue col = new TextAttributeValue(this, selectedNode.getText()); return(col); } return(null); }
/** * Validates an XPath expression. * * @return {@link XPathActivityConfigurationBean#XPATH_VALID XPATH_VALID} - * if the expression is valid;<br/> * {@link XPathActivityConfigurationBean#XPATH_EMPTY XPATH_EMPTY} - * if expression is empty;<br/> * {@link XPathActivityConfigurationBean#XPATH_INVALID * XPATH_INVALID} - if the expression is invalid / ill-formed.<br/> */ public static int validateXPath(String xpathExpressionToValidate) { // no XPath expression if (xpathExpressionToValidate == null || xpathExpressionToValidate.trim().isEmpty()) { return XPATH_EMPTY; } try { // try to parse the XPath expression... createXPath(xpathExpressionToValidate.trim()); // ...success return XPATH_VALID; } catch (InvalidXPathException e) { // ...failed to parse the XPath expression: notify of the error return XPATH_INVALID; } }
/** * Tests that the {@link InvalidXPathException} exception is thrown when the SIP profile contains an invalid xpath */ @Test public void generateArclibInvalidXPath() throws IOException { SipProfile profile = new SipProfile(); String sipProfileXml = Resources.toString(this.getClass().getResource( "/arclibxmlgeneration/sipProfiles/sipProfileInvalidXPath.xml"), StandardCharsets.UTF_8); profile.setXml(sipProfileXml); store.save(profile); assertThrown(() -> generator.generateArclibXml(SIP_PATH, profile.getId())).isInstanceOf(InvalidXPathException.class); }
private String getXPathValidationErrorMessage() { try { // try to parse the XPath expression... DocumentHelper.createXPath(tfXPathExpression.getText().trim()); // ...success return (""); } catch (InvalidXPathException e) { // ...failed to parse the XPath expression: notify of the error return (e.getMessage()); } }
/** * Apply the given XPath expression to the given node. * * @param prefixes mapping of prefixes to namespace URIs for prefixes used in expr * @return Iterator over org.w3c.dom.Node objects, never null */ public static Iterator selectIterator(org.dom4j.Node node, String expr, Map prefixes) { try { org.dom4j.XPath path = node.createXPath(expr); path.setNamespaceContext(new SimpleNamespaceContext(prefixes)); return new IteratorFilter(path.selectNodes(node).iterator(), org.dom4j.Namespace.class); } catch (InvalidXPathException e) { throw new OXFException(e); } }
public static Iterator selectIterator(org.dom4j.Node node, String expr, Map prefixes, VariableContext variableContext, FunctionContext functionContext) { try { org.dom4j.XPath path = node.createXPath(expr); hookupPath(path, prefixes, variableContext, functionContext); return new IteratorFilter(path.selectNodes(node).iterator(), org.dom4j.Namespace.class); } catch (InvalidXPathException e) { throw new OXFException(e); } }
/** * Apply the given XPath expression to the given node. * * @param prefixes mapping of prefixes to namespace URIs for prefixes used in expr */ public static org.dom4j.Node selectSingleNode(org.dom4j.Node node, String expr, Map prefixes) { try { org.dom4j.XPath path = node.createXPath(expr); path.setNamespaceContext(new SimpleNamespaceContext(prefixes)); return path.selectSingleNode(node); } catch (InvalidXPathException e) { throw new OXFException(e); } }
public static Object selectObjectValue(org.dom4j.Node node, String expr, Map prefixes, VariableContext variableContext, FunctionContext functionContext) { try { org.dom4j.XPath path = node.createXPath(expr); hookupPath(path, prefixes, variableContext, functionContext); return path.evaluate(node); } catch (InvalidXPathException e) { throw new OXFException(e); } }
public static String selectStringValue(org.dom4j.Node node, String expr, Map prefixes, VariableContext variableContext, FunctionContext functionContext) { try { org.dom4j.XPath path = node.createXPath(expr); hookupPath(path, prefixes, variableContext, functionContext); Object result = path.evaluate(node); // Test for empty node-set if (result == null || (result instanceof List && ((List) result).size() == 0)) return null; // Otherwise return a String return (result instanceof String) ? (String) result : node.createXPath(".").valueOf(result); } catch (InvalidXPathException e) { throw new OXFException(e); } }
public static Boolean selectBooleanValue(org.dom4j.Node node, String expr, Map prefixes, VariableContext variableContext, FunctionContext functionContext, boolean allowNull) { try { org.dom4j.XPath path = node.createXPath(expr); hookupPath(path, prefixes, variableContext, functionContext); Object result = path.evaluate(node); if (allowNull && (result == null || (result instanceof List && ((List) result).size() == 0))) return null; else return new Boolean(node.createXPath("boolean(.)").valueOf(result)); } catch (InvalidXPathException e) { throw new OXFException(e); } }
@Test public void findWithXPathInvalidXPathTest() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { assertThrown(() -> XPathUtils.findWithXPath(new FileInputStream(SIP_PATH + "/info.xml"), "///")).isInstanceOf(InvalidXPathException.class); }
public XPath createXPath(String xpath) throws InvalidXPathException { return element.createXPath( xpath ); }
public XPath createXPath(String xpathExpression) throws InvalidXPathException { return getWrapped().createXPath(xpathExpression); }
public XPath createXPath(String xpath) throws InvalidXPathException { return target().createXPath( xpath ); }