List of usage examples for javax.xml.xpath XPathConstants NODE
QName NODE
To view the source code for javax.xml.xpath XPathConstants NODE.
Click Source Link
The XPath 1.0 NodeSet data type.
From source file:org.adl.sequencer.ADLSeqUtilities.java
/** * Initializes one activity (<code>SeqActivity</code>) that will be added to * an activity tree.//from ww w . j a va 2s .c o m * * @param iNode A node from the DOM tree of an element containing * sequencing information. * * @param iColl The collection of reusable sequencing information. * * @return An initialized activity (<code>SeqActivity</code>), or <code> * null</code> if there was an error initializing the activity. */ private static SeqActivity buildActivityNode(Node iNode, Node iColl) { if (_Debug) { System.out.println(" :: ADLSeqUtilities --> BEGIN - " + "buildActivityNode"); } SeqActivity act = new SeqActivity(); boolean error = false; String tempVal = null; // Set the activity's ID -- this is a required attribute act.setID(ADLSeqUtilities.getAttribute(iNode, "identifier")); // Get the activity's resource ID -- if it exsits tempVal = ADLSeqUtilities.getAttribute(iNode, "identifierref"); if (tempVal != null) { if (!isEmpty(tempVal)) { act.setResourceID(tempVal); } } // Check if the activity is visible tempVal = ADLSeqUtilities.getAttribute(iNode, "isvisible"); if (tempVal != null) { if (!isEmpty(tempVal)) { act.setIsVisible((Boolean.valueOf(tempVal)).booleanValue()); } } // Get the children elements of this activity NodeList children = iNode.getChildNodes(); // Initalize this activity from the information in the DOM for (int i = 0; i < children.getLength(); i++) { Node curNode = children.item(i); // Check to see if this is an element node. if (curNode.getNodeType() == Node.ELEMENT_NODE) { if (curNode.getLocalName().equals("item")) { if (_Debug) { System.out.println(" ::--> Found an <item> element"); } // Initialize the nested activity SeqActivity nestedAct = ADLSeqUtilities.buildActivityNode(curNode, iColl); // Make sure this activity was created successfully if (nestedAct != null) { if (_Debug) { System.out.println(" ::--> Adding child"); } act.addChild(nestedAct); } else { error = true; } } else if (curNode.getLocalName().equals("title")) { if (_Debug) { System.out.println(" ::--> Found the <title> element"); } act.setTitle(ADLSeqUtilities.getElementText(curNode, null)); } else if (curNode.getLocalName().equals("sequencing")) { if (_Debug) { System.out.println(" ::--> Found the <sequencing> element"); } Node seqInfo = curNode; // Check to see if the sequencing information is referenced in // the <sequencingCollection> tempVal = ADLSeqUtilities.getAttribute(curNode, "IDRef"); if (tempVal != null) { // Combine local and global sequencing information // Get the referenced Global sequencing information String search = "imsss:sequencing[@ID='" + tempVal + "']"; if (_Debug) { System.out.println(" ::--> Looking for XPATH --> " + search); } // Use the referenced set of sequencing information Node seqGlobal = null; XPathFactory pathFactory = XPathFactory.newInstance(); XPath path = pathFactory.newXPath(); try { seqGlobal = (Node) path.evaluate(search, iColl, XPathConstants.NODE); //XPathAPI.selectSingleNode(iColl, search); } catch (Exception e) { if (_Debug) { System.out.println(" ::--> ERROR : In transform"); e.printStackTrace(); } } if (seqGlobal != null) { if (_Debug) { System.out.println(" ::--> FOUND"); } } else { if (_Debug) { System.out.println(" ::--> ERROR: Not Found"); } seqInfo = null; error = true; } if (!error) { // Clone the global node seqInfo = seqGlobal.cloneNode(true); // Loop through the local sequencing element NodeList seqChildren = curNode.getChildNodes(); for (int j = 0; j < seqChildren.getLength(); j++) { Node curChild = seqChildren.item(j); // Check to see if this is an element node. if (curChild.getNodeType() == Node.ELEMENT_NODE) { if (_Debug) { System.out.println(" ::--> Local definition"); System.out.println(" ::--> " + j); System.out.println(" ::--> <" + curChild.getLocalName() + ">"); } // Add this to the global sequencing info try { seqInfo.appendChild(curChild); } catch (org.w3c.dom.DOMException e) { if (_Debug) { System.out.println(" ::--> ERROR: "); e.printStackTrace(); } error = true; seqInfo = null; } } } } } // If we have an node to look at, extract its sequencing info if (seqInfo != null) { // Record this activity's sequencing XML fragment // XMLSerializer serializer = new XMLSerializer(); // -+- TODO -+- // serializer.setNewLine("CR-LF"); // act.setXMLFragment(serializer.writeToString(seqInfo)); // Extract the sequencing information for this activity error = !ADLSeqUtilities.extractSeqInfo(seqInfo, act); if (_Debug) { System.out.println(" ::--> Extracted Sequencing Info"); } } } } } // Make sure this activity either has an associated resource or children if (act.getResourceID() == null && !act.hasChildren(true)) { // This is not a vaild activity -- ignore it error = true; } // If the activity failed to initialize, clear the variable if (error) { act = null; } if (_Debug) { System.out.println(" ::--> error == " + error); System.out.println(" :: ADLSeqUtilities --> END - " + "buildActivityNode"); } return act; }
From source file:org.alfresco.repo.content.selector.XPathContentWorkerSelector.java
/** * Check the given document against the list of XPath statements provided. * //from w ww. j a v a2 s .c o m * @param doc the XML document * @return Returns a content worker that was matched or <tt>null</tt> */ private W processDocument(Document doc) { for (Map.Entry<String, W> entry : workersByXPath.entrySet()) { try { String xpath = entry.getKey(); W worker = entry.getValue(); // Execute the statement Object ret = xpathFactory.newXPath().evaluate(xpath, doc, XPathConstants.NODE); if (ret != null) { // We found one return worker; } } catch (XPathExpressionException e) { // We accept this and move on } } // Nothing found return null; }
From source file:org.alfresco.repo.dictionary.CustomModelServiceImpl.java
/** * Finds the {@code module} element within the Share persisted-extension * XML file and then writes the XML fragment as the content of a newly created node. * * @param modelName the model name/*from www .ja va 2 s . com*/ * @return the created nodeRef */ protected NodeRef createCustomModelShareExtModuleRef(final String modelName) { final String moduleId = "CMM_" + modelName; final NodeRef formNodeRef = getShareExtModule(); ContentReader reader = contentService.getReader(formNodeRef, ContentModel.PROP_CONTENT); if (reader == null) { throw new CustomModelException("cmm.service.download.share_ext_node_read_err"); } InputStream in = reader.getContentInputStream(); Node moduleIdXmlNode = null; try { Document document = XMLUtil.parse(in); // the stream will be closed final String xpathQuery = "/extension//modules//module//id[.= '" + moduleId + "']"; XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expression = xPath.compile(xpathQuery); moduleIdXmlNode = (Node) expression.evaluate(document, XPathConstants.NODE); } catch (Exception ex) { throw new CustomModelException("cmm.service.download.share_ext_file_parse_err", ex); } if (moduleIdXmlNode == null) { throw new CustomModelException("cmm.service.download.share_ext_module_not_found", new Object[] { moduleId }); } final File moduleFile = TempFileProvider.createTempFile(moduleId, ".xml"); try { XMLUtil.print(moduleIdXmlNode.getParentNode(), moduleFile); } catch (IOException error) { throw new CustomModelException("cmm.service.download.share_ext_write_err", new Object[] { moduleId }, error); } return doInTransaction(MSG_DOWNLOAD_CREATE_SHARE_EXT_ERR, true, new RetryingTransactionCallback<NodeRef>() { @Override public NodeRef execute() throws Exception { final NodeRef nodeRef = createDownloadTypeNode(moduleId + SHARE_EXT_MODULE_SUFFIX); ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); writer.setMimetype(MimetypeMap.MIMETYPE_XML); writer.setEncoding("UTF-8"); writer.putContent(moduleFile); return nodeRef; } }); }
From source file:org.ambraproject.rhino.content.xml.AbstractXpathReader.java
protected Node readNode(String query, Node node) { try {/*from w w w . j a va 2 s . c om*/ return (Node) xPath.evaluate(query, node, XPathConstants.NODE); } catch (XPathExpressionException e) { throw new InvalidXPathException(query, e); } }
From source file:org.ambraproject.service.article.FetchArticleServiceImpl.java
/** * Get references for a given article//from w ww. j a v a 2 s. co m * * @param doc article xml * @return references */ public ArrayList<CitationReference> getReferences(Document doc) { ArrayList<CitationReference> list = new ArrayList<CitationReference>(); if (doc == null) { return list; } try { NodeList refList = getReferenceNodes(doc); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression typeExpr = xpath.compile("//citation | //nlm-citation | //element-citation"); XPathExpression titleExpr = xpath.compile("//article-title"); XPathExpression authorsExpr = xpath.compile("//person-group[@person-group-type='author']/name"); XPathExpression journalExpr = xpath.compile("//source"); XPathExpression volumeExpr = xpath.compile("//volume"); XPathExpression numberExpr = xpath.compile("//label"); XPathExpression fPageExpr = xpath.compile("//fpage"); XPathExpression lPageExpr = xpath.compile("//lpage"); XPathExpression yearExpr = xpath.compile("//year"); XPathExpression publisherExpr = xpath.compile("//publisher-name"); for (int i = 0; i < refList.getLength(); i++) { Node refNode = refList.item(i); CitationReference citation = new CitationReference(); DocumentFragment df = doc.createDocumentFragment(); df.appendChild(refNode); // citation type Object resultObj = typeExpr.evaluate(df, XPathConstants.NODE); Node resultNode = (Node) resultObj; if (resultNode != null) { String citationType = getCitationType(resultNode); if (citationType != null) { citation.setCitationType(citationType); } } // title resultObj = titleExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setTitle(resultNode.getTextContent()); } // authors resultObj = authorsExpr.evaluate(df, XPathConstants.NODESET); NodeList resultNodeList = (NodeList) resultObj; ArrayList<String> authors = new ArrayList<String>(); for (int j = 0; j < resultNodeList.getLength(); j++) { Node nameNode = resultNodeList.item(j); NodeList namePartList = nameNode.getChildNodes(); String surName = ""; String givenName = ""; for (int k = 0; k < namePartList.getLength(); k++) { Node namePartNode = namePartList.item(k); if (namePartNode.getNodeName().equals("surname")) { surName = namePartNode.getTextContent(); } else if (namePartNode.getNodeName().equals("given-names")) { givenName = namePartNode.getTextContent(); } } authors.add(givenName + " " + surName); } citation.setAuthors(authors); // journal title resultObj = journalExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setJournalTitle(resultNode.getTextContent()); } // volume resultObj = volumeExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setVolume(resultNode.getTextContent()); } // citation number resultObj = numberExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setNumber(resultNode.getTextContent()); } // citation pages String firstPage = null; String lastPage = null; resultObj = fPageExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { firstPage = resultNode.getTextContent(); } resultObj = lPageExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { lastPage = resultNode.getTextContent(); } if (firstPage != null) { if (lastPage != null) { citation.setPages(firstPage + "-" + lastPage); } else { citation.setPages(firstPage); } } // citation year resultObj = yearExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setYear(resultNode.getTextContent()); } // citation publisher resultObj = publisherExpr.evaluate(df, XPathConstants.NODE); resultNode = (Node) resultObj; if (resultNode != null) { citation.setPublisher(resultNode.getTextContent()); } list.add(citation); } } catch (Exception e) { log.error("Error occurred while gathering the citation references.", e); } return list; }
From source file:org.ambraproject.service.article.FetchArticleServiceImpl.java
/** * Decorates the citation elements of the XML DOM with extra information from the citedArticle table in the DB. An * extraCitationInfo element is appended to each citation element. It will contain between one and two attributes * with the extra info: citedArticleID, the DB primary key, and doi, the DOI string, if it exists. * * @param doc DOM of the XML/*w w w . j a v a 2s. co m*/ * @param citedArticles List of CitedArticle persistent objects * @return modified DOM * @throws ApplicationException */ private Document addExtraCitationInfo(Document doc, List<CitedArticle> citedArticles) throws ApplicationException { if (citedArticles.isEmpty()) { return doc; // This happens in some unit tests. } try { NodeList referenceList = getReferenceNodes(doc); // If sortOrder on citedArticle has duplicate value, you will get below error.Ideally it should not happen // but since sortOrder is not unique it may be possible to update that field from backend to have duplicate value // Now index is on sortOrder(article.hbm.xml), index will be only on one of those of duplicate value and // hence citedArticle will have less count then the xml. if (referenceList.getLength() != citedArticles.size()) { throw new ApplicationException(String.format("Article has %d citedArticles but %d references", citedArticles.size(), referenceList.getLength())); } XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression citationExpr = xpath .compile("./citation|./nlm-citation|./element-citation|./mixed-citation"); for (int i = 0; i < referenceList.getLength(); i++) { Node referenceNode = referenceList.item(i); Node citationNode = (Node) citationExpr.evaluate(referenceNode, XPathConstants.NODE); CitedArticle citedArticle = citedArticles.get(i); if (citationNode != null && "journal".equals(getCitationType(citationNode)) && citedArticleIsValid(citedArticle)) { Element extraInfo = doc.createElement("extraCitationInfo"); setExtraCitationInfo(extraInfo, citedArticle); citationNode.appendChild(extraInfo); } } } catch (XPathExpressionException xpee) { throw new ApplicationException(xpee); } return doc; }
From source file:org.apache.aries.blueprint.plugin.GeneratorTest.java
private static Node getBeanById(String id) throws XPathExpressionException { return (Node) xpath.evaluate("/blueprint/bean[@id='" + id + "']", document, XPathConstants.NODE); }
From source file:org.apache.aries.blueprint.plugin.GeneratorTest.java
private static Node getServiceByRef(String id) throws XPathExpressionException { return (Node) xpath.evaluate("/blueprint/service[@ref='" + id + "']", document, XPathConstants.NODE); }
From source file:org.apache.aries.blueprint.plugin.GeneratorTest.java
private static Node getReferenceById(String id) throws XPathExpressionException { return (Node) xpath.evaluate("/blueprint/reference[@id='" + id + "']", document, XPathConstants.NODE); }
From source file:org.apache.camel.builder.xml.XPathBuilder.java
/** * Sets the expression result type to boolean * * @return the current builder// ww w. ja v a 2 s .c o m */ public XPathBuilder nodeResult() { resultQName = XPathConstants.NODE; return this; }