List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
From source file:org.apache.openaz.xacml.std.StdRequestAttributes.java
@Override public NodeList getContentNodeListByXpathExpression(XPathExpression xpathExpression) { if (xpathExpression == null) { throw new NullPointerException("Null XPathExpression"); }//from ww w . j a va2 s. co m Node nodeRootThis = this.getContentRoot(); if (nodeRootThis == null) { return null; } NodeList matchingNodeList = null; try { matchingNodeList = (NodeList) xpathExpression.evaluate(nodeRootThis, XPathConstants.NODESET); } catch (XPathExpressionException ex) { this.logger.warn("Failed to retrieve nodelist for \"" + xpathExpression.toString() + "\"", ex); } return matchingNodeList; }
From source file:org.apache.openmeetings.cli.ConnectionPropertiesPatcher.java
private static Attr getConnectionProperties(Document doc) throws Exception { XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath .compile("/persistence/persistence-unit/properties/property[@name='openjpa.ConnectionProperties']"); Element element = (Element) expr.evaluate(doc, XPathConstants.NODE); return element.getAttributeNode("value"); }
From source file:org.apache.syncope.console.commons.XMLRolesReader.java
/** * Get all roles allowed for specific page and action requested. * * @param pageId//from w w w. ja v a2 s . c o m * @param actionId * @return roles list comma separated */ public String getAllAllowedRoles(final String pageId, final String actionId) { if (doc == null) { return StringUtils.EMPTY; } final StringBuilder roles = new StringBuilder(); try { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile( "//page[@id='" + pageId + "']/" + "action[@id='" + actionId + "']/" + "entitlement/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { if (i > 0) { roles.append(","); } roles.append(nodes.item(i).getNodeValue()); } } catch (XPathExpressionException e) { LOG.error("While parsing authorizations file", e); } LOG.debug("Authorizations found: {}", roles); return roles.toString(); }
From source file:org.apereo.portal.layout.dlm.PositionManager.java
/** * Return true if the passed in node or any downstream (higher index) * siblings <strong>relative to its destination location</strong> have * moveAllowed="false"./* w w w . j a v a 2 s.com*/ */ private static boolean isNotReparentable(NodeInfo ni, Element compViewParent, Element positionSet) { // This one is easy -- can't re-parent a node with dlm:moveAllowed=false if (ni.getNode().getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false")) { return true; } try { /* * Annoying to do in Java, but we need to find our own placeholder * element in the positionSet */ final XPathFactory xpathFactory = XPathFactory.newInstance(); final XPath xpath = xpathFactory.newXPath(); final String findPlaceholderXpath = ".//*[local-name()='position' and @name='" + ni.getId() + "']"; final XPathExpression findPlaceholder = xpath.compile(findPlaceholderXpath); final NodeList findPlaceholderList = (NodeList) findPlaceholder.evaluate(positionSet, XPathConstants.NODESET); switch (findPlaceholderList.getLength()) { case 0: LOG.warn("Node not found for XPathExpression=\"" + findPlaceholderXpath + "\" in positionSet=" + XmlUtilitiesImpl.toString(positionSet)); return true; case 1: // This is healthy break; default: LOG.warn("More than one node found for XPathExpression=\"" + findPlaceholderXpath + "\" in positionSet=" + XmlUtilitiesImpl.toString(positionSet)); return true; } final Element placeholder = (Element) findPlaceholderList.item(0); // At last for (Element nextPlaceholder = (Element) placeholder.getNextSibling(); // Start with the next dlm:position element after placeholder nextPlaceholder != null; // As long as we have a placeholder to look at nextPlaceholder = (Element) nextPlaceholder.getNextSibling()) { // Advance to the next placeholder if (LOG.isDebugEnabled()) { LOG.debug("Considering whether node ''" + ni.getId() + "' is Reparentable; subsequent sibling is: " + nextPlaceholder.getAttribute("name")); } /* * Next task: we have to find the non-placeholder representation of * the nextSiblingPlaceholder within the compViewParent */ final String unmaskPlaceholderXpath = ".//*[@ID='" + nextPlaceholder.getAttribute("name") + "']"; final XPathExpression unmaskPlaceholder = xpath.compile(unmaskPlaceholderXpath); final NodeList unmaskPlaceholderList = (NodeList) unmaskPlaceholder.evaluate(compViewParent, XPathConstants.NODESET); switch (unmaskPlaceholderList.getLength()) { case 0: // Not a problem; the nextSiblingPlaceholder also refers // to a node that has been moved to this context (afaik) continue; case 1: final Element nextSibling = (Element) unmaskPlaceholderList.item(0); if (LOG.isDebugEnabled()) { LOG.debug("Considering whether node ''" + ni.getId() + "' is Reparentable; subsequent sibling '" + nextSibling.getAttribute("ID") + "' has dlm:moveAllowed=" + !nextSibling.getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false")); } // Need to perform some checks... if (nextSibling.getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false")) { /* * The following check is a bit strange; it seems to verify * that the current NodeInfo and the nextSibling come from the * same fragment. If they don't, the re-parenting is allowable. * I believe this check could only be unsatisfied in the case * of tabs. */ Precedence p = Precedence.newInstance(nextSibling.getAttribute(Constants.ATT_FRAGMENT)); if (ni.getPrecedence().isEqualTo(p)) { return true; } } break; default: LOG.warn("More than one node found for XPathExpression=\"" + unmaskPlaceholderXpath + "\" in compViewParent"); return true; } } } catch (XPathExpressionException xpe) { throw new RuntimeException("Failed to evaluate XPATH", xpe); } return false; // Re-parenting is "not disallowed" (double-negative less readable) }
From source file:org.apereo.portal.portlets.marketplace.PortletMarketplaceController.java
private List<PortletTab> getPortletTabInfo(DistributedUserLayout layout, String fname) { final String XPATH_TAB = "/layout/folder/folder[@hidden = 'false' and @type = 'regular']"; final String XPATH_COUNT_COLUMNS = "count(./folder[@hidden = \"false\"])"; final String XPATH_COUNT_NON_EDITABLE_COLUMNS = "count(./folder[@hidden = \"false\" and @*[local-name() = \"editAllowed\"] = \"false\"])"; final String XPATH_GET_TAB_PORTLET_FMT = ".//channel[@hidden = \"false\" and @fname = \"%s\"]"; Document doc = layout.getLayout(); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try {/*from w ww. ja v a2 s . com*/ XPathExpression tabExpr = xpath.compile(XPATH_TAB); NodeList list = (NodeList) tabExpr.evaluate(doc, XPathConstants.NODESET); // Count columns and non-editable columns... XPathExpression columnCountExpr = xpath.compile(XPATH_COUNT_COLUMNS); XPathExpression nonEditableCountExpr = xpath.compile(XPATH_COUNT_NON_EDITABLE_COLUMNS); // get the list of tabs... String xpathStr = format(XPATH_GET_TAB_PORTLET_FMT, fname); XPathExpression portletExpr = xpath.compile(xpathStr); List<PortletTab> tabs = new ArrayList<>(); for (int i = 0; i < list.getLength(); i++) { Node tab = list.item(i); String tabName = ((Element) tab).getAttribute("name"); String tabId = ((Element) tab).getAttribute("ID"); // check if tab is editable... Number columns = (Number) columnCountExpr.evaluate(tab, XPathConstants.NUMBER); Number nonEditColumns = (Number) nonEditableCountExpr.evaluate(tab, XPathConstants.NUMBER); // tab is not editable... skip it... if (columns.intValue() > 0 && columns.intValue() == nonEditColumns.intValue()) { continue; } // get all instances of this portlet on this tab... List<String> layoutIds = new ArrayList<>(); NodeList fnameListPerTab = (NodeList) portletExpr.evaluate(tab, XPathConstants.NODESET); for (int j = 0; j < fnameListPerTab.getLength(); j++) { Node channel = fnameListPerTab.item(j); String layoutId = ((Element) channel).getAttribute("ID"); layoutIds.add(layoutId); } PortletTab tabInfo = new PortletTab(tabName, tabId, layoutIds); tabs.add(tabInfo); } return tabs; } catch (XPathExpressionException e) { logger.error("Error evaluating xpath", e); } return null; }
From source file:org.apereo.portal.xml.XmlUtilitiesImplTest.java
@Test public void testGetUniqueXPath() throws Exception { final Document testDoc = loadTestDocument(); final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); final XPathExpression xPathExpression = xPath.compile("//*[@ID='11']"); final Node node = (Node) xPathExpression.evaluate(testDoc, XPathConstants.NODE); final XmlUtilitiesImpl xmlUtilities = new XmlUtilitiesImpl(); final String nodePath = xmlUtilities.getUniqueXPath(node); assertEquals("/layout/folder[2]/folder[3]", nodePath); }
From source file:org.apereo.portal.xml.xpath.XPathPoolImpl.java
@Override public <T> T evaluate(String expression, Map<String, ?> variables, final Object item, final QName returnType) { return this.doWithExpression(expression, variables, new Function<XPathExpression, T>() { /* (non-Javadoc) * @see com.google.common.base.Function#apply(java.lang.Object) */// ww w . j av a2 s. c om @SuppressWarnings("unchecked") @Override public T apply(XPathExpression xpathExpression) { try { return (T) xpathExpression.evaluate(item, returnType); } catch (XPathExpressionException e) { throw new RuntimeException("Failed to execute XPathExpression '" + xpathExpression + "'", e); } } }); }
From source file:org.archive.crawler.migrate.MigrateH1to3Tool.java
/** * Given a Document, return a Map of all non-blank simple text * nodes, keyed by the pseudo-XPath to their parent element. * /*w w w. jav a 2 s . c o m*/ * @param h1order Document to extract Map * @return Map<String,String> Xpath-like-String -> non-blank text content * @throws XPathExpressionException */ public static Map<String, String> flattenH1Order(Document h1order) throws XPathExpressionException { Map<String, String> flattened = new LinkedHashMap<String, String>(); XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()"); NodeList nodes = (NodeList) xpath.evaluate(h1order, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (StringUtils.isNotBlank(node.getTextContent())) { String pseudoXPath = getPseudoXpath(node.getParentNode()); pseudoXPath = pseudoXPath.replaceFirst("/crawl-order", "/"); // System.out.println( // pseudoXPath // +" "+node.getTextContent()); flattened.put(pseudoXPath, node.getTextContent()); } } // System.err.println(flattened.size()); // System.err.println(flattened); return flattened; }
From source file:org.artifactory.webapp.wicket.util.DescriptionExtractor.java
private String executeQuery(String query) { try {//www. ja va 2s . c o m XPathFactory xFactory = XPathFactory.newInstance(); XPath xpath = xFactory.newXPath(); xpath.setNamespaceContext(new SchemaNamespaceContext()); XPathExpression expr = xpath.compile(query); Object description = expr.evaluate(doc, XPathConstants.STRING); return description.toString().trim(); } catch (XPathExpressionException e) { throw new RuntimeException("Failed to execute xpath query: " + query, e); } }
From source file:org.artificer.integration.artifactbuilder.XmlArtifactBuilder.java
protected Object query(Element element, String query, QName returnType) throws XPathExpressionException { XPathExpression expr = xpath.compile(query); return expr.evaluate(element, returnType); }