List of usage examples for javax.xml.xpath XPath evaluate
public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException;
From source file:nz.govt.natlib.ndha.wctdpsdepositor.extractor.XPathWctMetsExtractor.java
private void populateFileValueObjectCollectionFrom(Document doc, XPath xpath, String nodeQuery, List<ArchiveFile> fileCollection, FileArchiveBuilder fileBuilder) throws XPathExpressionException { NodeList seedNodes = (NodeList) xpath.evaluate(nodeQuery, doc, XPathConstants.NODESET); for (int i = 0; i < seedNodes.getLength(); i++) { Node metsFileNode = seedNodes.item(i); ArchiveFile af = populateFileValueObjectFrom(xpath, metsFileNode, fileBuilder); fileCollection.add(af);/*ww w . ja va 2s.c o m*/ } }
From source file:nz.govt.natlib.ndha.wctdpsdepositor.extractor.XPathWctMetsExtractor.java
private ArchiveFile populateFileValueObjectFrom(XPath xpath, Node metsFileNode, FileArchiveBuilder fileBuilder) throws XPathExpressionException { String mimeType = (String) xpath.evaluate("@MIMETYPE", metsFileNode, XPathConstants.STRING); String checkSum = (String) xpath.evaluate("@CHECKSUM", metsFileNode, XPathConstants.STRING); String fileLocation = (String) xpath.evaluate("mets:FLocat/@xlink:href", metsFileNode, XPathConstants.STRING); String fileName = getFileName(fileLocation); return fileBuilder.createFileFrom(mimeType, checkSum, fileName); }
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 v a2s .co 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.ala.documentmapper.FlickrDocumentMapper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private void handleMachineTag(ParsedDocument parsedDoc, Document xmlDocument, String subject, String xpathString) throws Exception { //"/rsp/photo/tags/tag[@machine_tag=1]/@raw[starts-with(., 'taxonomy:binomial')]" // String tag = getXPathSingleValue(xmlDocument, xpath); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); NodeList nodes = (NodeList) xpath.evaluate(xpathString, xmlDocument, XPathConstants.NODESET); boolean gotSciName = false; String subspecies = null;//from w w w . ja va 2s . com String genus = null; String family = null; String order = null; String suborder = null; String kingdom = null; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String machineTag = node.getNodeValue(); int charIdx = machineTag.indexOf('='); if (charIdx > 0) { String scientificName = machineTag.substring(charIdx + 1); scientificName = scientificName.trim(); if (machineTag != null) { machineTag = machineTag.toLowerCase(); if (machineTag.contains("binomial")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.SPECIES.toString(), scientificName)); parsedDoc.getTriples() .add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), scientificName)); gotSciName = true; } else if (machineTag.contains("trinomial")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.SUBSPECIES.toString(), scientificName)); subspecies = scientificName; // } else if(machineTag.contains("common")){ // parsedDoc.getTriples().add(new Triple(subject, Predicates.COMMON_NAME.toString(), scientificName)); } else if (machineTag.contains("genus")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.GENUS.toString(), scientificName)); genus = scientificName; } else if (machineTag.contains("family")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.FAMILY.toString(), scientificName)); family = scientificName; } else if (machineTag.contains("order")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.ORDER.toString(), scientificName)); order = scientificName; } else if (machineTag.contains("suborder")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.SUBORDER.toString(), scientificName)); suborder = scientificName; } else if (machineTag.contains("kingdom")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.KINGDOM.toString(), scientificName)); kingdom = scientificName; } else if (machineTag.contains("scientific")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), scientificName)); gotSciName = true; } else if (machineTag.contains("country")) { parsedDoc.getTriples() .add(new Triple(subject, Predicates.COUNTRY.toString(), scientificName)); } } } } if (!gotSciName) { if (subspecies != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), subspecies)); } else if (genus != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), genus)); } else if (family != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), family)); } else if (order != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), order)); } else if (suborder != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), suborder)); } else if (kingdom != null) { parsedDoc.getTriples().add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), kingdom)); } } }
From source file:org.ala.documentmapper.XMLDocumentMapper.java
/** * Uses the supplied xpath to retrieve values * //from w ww . j av a2s . c om * @param document * @param xpathAsString * @return * @throws Exception */ protected List<String> getXPathValues(Document document, String xpathAsString) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); List<String> extractedValues = new ArrayList<String>(); NodeList nodes = (NodeList) xpath.evaluate(xpathAsString, document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { String value = extractValue(nodes.item(i)); value = StringUtils.trimToNull(value); if (value != null) { extractedValues.add(value); } } return extractedValues; }
From source file:org.ala.documentmapper.XMLDocumentMapper.java
/** * Uses the supplied xpath to retrieve values * // w w w . j av a 2 s . com * @param document * @param xpathAsString * @return * @throws Exception */ protected String getXPathSingleValue(Document document, String xpathAsString) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); NodeList nodes = null; try { nodes = (NodeList) xpath.evaluate(xpathAsString, document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { String value = extractValue(nodes.item(i)); value = StringUtils.trimToNull(value); if (value != null) { return value; } } } catch (XPathException e) { String value = (String) xpath.evaluate(xpathAsString, document, XPathConstants.STRING); return value; } return null; }
From source file:org.ala.documentmapper.XMLDocumentMapper.java
/** * Map the property using the xpath mapping supplied. * /*from ww w . j av a 2s.com*/ * @param document * @param parsedDoc * @param isDublinCore * @param xpath * @param mapping */ private void performXPathMapping(Document document, ParsedDocument parsedDoc, boolean isDublinCore, XPath xpath, Mapping mapping) { try { NodeList nodes = (NodeList) xpath.evaluate(mapping.getQueryString(), document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { String value = extractValue(nodes.item(i)); if (value != null) { if (mapping.isGuid) { parsedDoc.setGuid(value); } //create the triples createTriples(parsedDoc, isDublinCore, mapping, value); } } } catch (XPathExpressionException e) { //throw new Exception("Failed to extract XPath property: "+ e.getMessage(), e); // To handle the XPATH expressions which don't represent a nodelist. String value; try { value = trim(xpath.evaluate(mapping.getQueryString(), document)); if (value != null || !"".equals(value)) { value = value.replaceAll("[\\s&&[^ ]]{1,}", ""); value = value.replaceAll("[ ]{2,}", " "); //create the triples createTriples(parsedDoc, isDublinCore, mapping, value); } } catch (XPathExpressionException e1) { logger.warn(e.getMessage(), e); } } }
From source file:org.alfresco.po.share.dashlet.AbstractDashlet.java
/** * Parses the xml string of the original-title attribute element to get tooltip * data for report dashlets/*from ww w .ja v a2 s . c o m*/ * * @param xml String * @param element String * @return String */ protected String getElement(String xml, String element) throws Exception { String tooltipElement = " "; xml = xml.replaceAll("alt=\"avatar\">", "alt=\"avatar\"/>"); xml = xml.replaceAll("<br>", ""); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); InputSource source = new InputSource(new StringReader(xml)); try { tooltipElement = (String) xpath.evaluate(element, source, XPathConstants.STRING); } catch (XPathExpressionException ee) { logger.error("Cannot parse xml string " + ee); } return tooltipElement; }
From source file:org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBindingParser.java
private Node queryXPathNode(Node target, String expression) { NodeList nlst;//from w ww .j ava2s . c o m try { ContextImpl contextImpl = new ContextImpl(target); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(contextImpl); nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET); } catch (XPathExpressionException e) { Message msg = new Message("XPATH_ERROR", LOG, new Object[] { expression }); throw new ToolException(msg, e); } if (nlst.getLength() != 1) { Message msg = new Message("ERROR_TARGETNODE_WITH_XPATH", LOG, new Object[] { expression }); throw new ToolException(msg); } Node rnode = nlst.item(0); if (!(rnode instanceof Element)) { return null; } return rnode; }
From source file:org.apache.geode.management.internal.configuration.utils.XmlUtils.java
public static NodeList query(Node node, String searchString) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); return (NodeList) xpath.evaluate(searchString, node, XPathConstants.NODESET); }