List of usage examples for org.dom4j XPath setNamespaceURIs
void setNamespaceURIs(Map<String, String> map);
From source file:net.sf.jguard.ext.authorization.manager.XmlAuthorizationManager.java
License:Open Source License
private List getElements(String xpath) { XPath xp2 = DocumentHelper.createXPath(xpath); Map<String, String> uris = new HashMap<String, String>(); uris.put(STRING_NAMESPACE_PREFIX, HTTP_JGUARD_SOURCEFORGE_NET_XSD_J_GUARD_PRINCIPALS_PERMISSIONS_2_0_0); xp2.setNamespaceURIs(uris); return xp2.selectNodes(root); }
From source file:org.apache.archiva.xml.XMLReader.java
License:Apache License
private XPath createXPath(String xpathExpr) { XPath xpath = document.createXPath(xpathExpr); if (!this.namespaceMap.isEmpty()) { xpath.setNamespaceURIs(this.namespaceMap); }//from www . ja v a2 s . c o m return xpath; }
From source file:org.apache.taverna.activities.xpath.XPathActivity.java
License:Apache License
/** * This method executes pre-configured instance of XPath activity. *///from www . j av a 2 s . co m @Override public void executeAsynch(final Map<String, T2Reference> inputs, final AsynchronousActivityCallback callback) { // Don't execute service directly now, request to be run asynchronously callback.requestRun(new Runnable() { @Override @SuppressWarnings("unchecked") public void run() { InvocationContext context = callback.getContext(); ReferenceService referenceService = context.getReferenceService(); // ---- RESOLVE INPUT ---- String xmlInput = (String) referenceService.renderIdentifier(inputs.get(IN_XML), String.class, context); // ---- DO THE ACTUAL SERVICE INVOCATION ---- List<Node> matchingNodes = new ArrayList<Node>(); // only attempt to execute XPath expression if there is some input data if (xmlInput != null && xmlInput.length() > 0) { // XPath configuration is taken from the config bean try { XPath expr = DocumentHelper.createXPath(json.get("xpathExpression").textValue()); Map<String, String> xpathNamespaceMap = new HashMap<>(); for (JsonNode namespaceMapping : json.get("xpathNamespaceMap")) { xpathNamespaceMap.put(namespaceMapping.get("prefix").textValue(), namespaceMapping.get("uri").textValue()); } expr.setNamespaceURIs(xpathNamespaceMap); Document doc = DocumentHelper.parseText(xmlInput); matchingNodes = expr.selectNodes(doc); } catch (InvalidXPathException e) { callback.fail("Incorrect XPath Expression -- XPath processing library " + "reported the following error: " + e.getMessage(), e); // make sure we don't call callback.receiveResult later return; } catch (DocumentException e) { callback.fail("XML document was not valid -- XPath processing library " + "reported the following error: " + e.getMessage(), e); // make sure we don't call callback.receiveResult later return; } catch (XPathException e) { callback.fail("Unexpected error has occurred while executing the XPath expression. " + "-- XPath processing library reported the following error:\n" + e.getMessage(), e); // make sure we don't call callback.receiveResult later return; } } // --- PREPARE OUTPUTS --- List<String> outNodesText = new ArrayList<String>(); List<String> outNodesXML = new ArrayList<String>(); Object textValue = null; Object xmlValue = null; for (Object o : matchingNodes) { if (o instanceof Node) { Node n = (Node) o; if (n.getStringValue() != null && n.getStringValue().length() > 0) { outNodesText.add(n.getStringValue()); if (textValue == null) textValue = n.getStringValue(); } outNodesXML.add(n.asXML()); if (xmlValue == null) xmlValue = n.asXML(); } else { outNodesText.add(o.toString()); if (textValue == null) textValue = o.toString(); } } // ---- REGISTER OUTPUTS ---- Map<String, T2Reference> outputs = new HashMap<String, T2Reference>(); if (textValue == null) { ErrorDocumentService errorDocService = referenceService.getErrorDocumentService(); textValue = errorDocService.registerError("No value produced", 0, callback.getContext()); } if (xmlValue == null) { ErrorDocumentService errorDocService = referenceService.getErrorDocumentService(); xmlValue = errorDocService.registerError("No value produced", 0, callback.getContext()); } T2Reference firstNodeAsText = referenceService.register(textValue, 0, true, context); outputs.put(SINGLE_VALUE_TEXT, firstNodeAsText); T2Reference firstNodeAsXml = referenceService.register(xmlValue, 0, true, context); outputs.put(SINGLE_VALUE_XML, firstNodeAsXml); T2Reference outNodesAsText = referenceService.register(outNodesText, 1, true, context); outputs.put(OUT_TEXT, outNodesAsText); T2Reference outNodesAsXML = referenceService.register(outNodesXML, 1, true, context); outputs.put(OUT_XML, outNodesAsXML); // return map of output data, with empty index array as this is // the only and final result (this index parameter is used if // pipelining output) callback.receiveResult(outputs, new int[0]); } }); }
From source file:org.codehaus.cargo.util.Dom4JUtil.java
License:Apache License
/** * The following will search the given element for the specified xpath and return a list of * nodes that match./* ww w .j ava 2 s.co m*/ * * @param xpath - selection criteria * @param toSearch - element to start the search at * @return List of matching elements */ public List<Element> selectElementsMatchingXPath(String xpath, Element toSearch) { XPath xpathSelector = DocumentHelper.createXPath(xpath); xpathSelector.setNamespaceURIs(getNamespaces()); List<Element> results = xpathSelector.selectNodes(toSearch); return results; }
From source file:org.craftercms.core.util.XmlUtils.java
License:Open Source License
/** * Executes the specified namespace aware XPath query as a single node query, returning the resulting single node. *///w ww.ja v a2s. c o m public static Node selectSingleNode(Node node, String xpathQuery, Map<String, String> namespaceUris) { XPath xpath = DocumentHelper.createXPath(xpathQuery); xpath.setNamespaceURIs(namespaceUris); return xpath.selectSingleNode(node); }
From source file:org.craftercms.core.util.XmlUtils.java
License:Open Source License
/** * Executes the specified namespace aware XPath query as a multiple node query, returning the resulting list of nodes. *///from w w w . j a va 2 s. co m @SuppressWarnings("unchecked") public static List<Node> selectNodes(Node node, String xpathQuery, Map<String, String> namespaceUris) { XPath xpath = DocumentHelper.createXPath(xpathQuery); xpath.setNamespaceURIs(namespaceUris); return xpath.selectNodes(node); }
From source file:org.esupportail.lecture.domain.model.Source.java
/** * Make Items objects in fonction of itemXPath, xsltURL, xmlStream. * @throws ComputeItemsException // ww w . ja v a 2 s . c o m */ @SuppressWarnings("unchecked") synchronized private void computeItems() throws ComputeItemsException { if (LOG.isDebugEnabled()) { LOG.debug("id=" + this.profileId + " - computeItems()"); } if (!itemComputed) { try { //create dom4j document Document document = DocumentHelper.parseText(xmlStream); // get encoding String encoding = document.getXMLEncoding(); //lauch Xpath find String x = getItemXPath(); XPath xpath = document.createXPath(x); xpath.setNamespaceURIs(getXPathNameSpaces()); List<Node> list = xpath.selectNodes(document); //List<Node> list = document.selectNodes(getItemXPath()); Iterator<Node> iter = list.iterator(); while (iter.hasNext()) { Node node = iter.next(); Item item = new Item(this); StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\""); xml.append(encoding); xml.append("\" ?>"); xml.append(node.asXML()); String xmlAsString = xml.toString(); String htmlContent = xml2html(xmlAsString, getXsltURL(), encoding); item.setHtmlContent(htmlContent); String MobileHtmlContent = xml2html(xmlAsString, getMobileXsltURL(), encoding); item.setMobileHtmlContent(MobileHtmlContent); //find MD5 of item content for his ID byte[] hash = MessageDigest.getInstance("MD5").digest(xmlAsString.getBytes()); StringBuffer hashString = new StringBuffer(); for (int i = 0; i < hash.length; ++i) { String hex = Integer.toHexString(hash[i]); if (hex.length() == 1) { hashString.append('0'); hashString.append(hex.charAt(hex.length() - 1)); } else { hashString.append(hex.substring(hex.length() - 2)); } } item.setId(hashString.toString()); items.add(item); } } catch (DocumentException e) { String errorMsg = "Error parsing XML content of the source"; LOG.error(errorMsg, e); throw new ComputeItemsException(errorMsg, e); } catch (NoSuchAlgorithmException e) { String errorMsg = "MD5 algorithm not supported"; LOG.error(errorMsg, e); throw new ComputeItemsException(errorMsg, e); } catch (XPathException e) { String errorMsg = "Xpath with NameSpace not specified in mappings.xml"; LOG.error(errorMsg, e); throw new ComputeItemsException(errorMsg, e); } catch (MappingNotFoundException e) { String errorMsg = "Impossible to get itemXPath,XPathNameSpaces and xsltURL"; LOG.error(errorMsg, e); throw new ComputeItemsException(errorMsg, e); } catch (Xml2HtmlException e) { String errorMsg = "Impossible to make html content"; LOG.error(errorMsg, e); throw new ComputeItemsException(errorMsg, e); } itemComputed = true; } }
From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java
License:Apache License
/** * Parses the manifest and build modules * * @param document document/* w ww .j a va 2 s. c om*/ * @param unZippedDirPath unZipped fiels Directory Path * @exception throws exception */ public void parseAndBuildModules(Document document, String unZippedDirPath) throws Exception { if (logger.isDebugEnabled()) logger.debug("Entering parseAndBuildModules"); Map uris = new HashMap(); uris.put("imscp", DEFAULT_NAMESPACE_URI); uris.put("imsmd", IMSMD_NAMESPACE_URI); // organizations XPath xpath = document.createXPath("/imscp:manifest/imscp:organizations/imscp:organization"); xpath.setNamespaceURIs(uris); Element eleOrg = (Element) xpath.selectSingleNode(document); // build module // loop thru organization elements - item elements if (eleOrg != null) { importThreadLocal.set("MeleteIMSImportFiles", new HashSet<String>()); List elements = eleOrg.elements(); for (Iterator iter = eleOrg.elementIterator("item"); iter.hasNext();) { Element element = (Element) iter.next(); // for moodle packages which don't have parent item tag for modules. Element blankElement = checkModuleItem(element, eleOrg); if (blankElement != null) { buildModule(blankElement, document, unZippedDirPath, ToolManager.getCurrentPlacement().getContext()); break; } else buildModule(element, document, unZippedDirPath, ToolManager.getCurrentPlacement().getContext()); } } xpath = document.createXPath("/imscp:manifest/imscp:resources"); xpath.setNamespaceURIs(uris); Element eleResource = (Element) xpath.selectSingleNode(document); if (eleResource != null) processManageItems(eleResource, unZippedDirPath, ToolManager.getCurrentPlacement().getContext(), document); if (logger.isDebugEnabled()) logger.debug("Exiting parseAndBuildModules"); }
From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java
License:Apache License
private Element getResource(String resName, Document document) throws Exception { if (logger.isDebugEnabled()) logger.debug("Entering getResource..."); Map uris = new HashMap(); uris.put("imscp", DEFAULT_NAMESPACE_URI); uris.put("imsmd", IMSMD_NAMESPACE_URI); //resource/*from ww w . j a v a 2s. c o m*/ XPath xpath = document .createXPath("/imscp:manifest/imscp:resources/imscp:resource[@identifier = '" + resName + "']"); xpath.setNamespaceURIs(uris); Element eleRes = (Element) xpath.selectSingleNode(document); if (logger.isDebugEnabled()) logger.debug("Exiting getResource..."); return eleRes; }
From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java
License:Apache License
private Element getMergeResource(String resName, Document document) throws Exception { if (logger.isDebugEnabled()) logger.debug("Entering getResource..."); Map uris = new HashMap(); uris.put("imscp", DEFAULT_NAMESPACE_URI); uris.put("imsmd", IMSMD_NAMESPACE_URI); // resource// w ww . j av a 2 s . c om XPath xpath = document.createXPath("//resource[@identifier = '" + resName + "']"); xpath.setNamespaceURIs(uris); Element eleRes = (Element) xpath.selectSingleNode(document); if (logger.isDebugEnabled()) logger.debug("Exiting getResource..."); return eleRes; }