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.eclipse.lyo.testsuite.server.oslcv1tests.ServiceDescriptionTests.java
@Test public void changeManagementServiceDescriptionHasValidFactory() throws XPathException { //If ServiceDescription is oslc_cm, make sure it has a valid factory child element Node cmRequest = (Node) OSLCUtils.getXPath().evaluate("//oslc_cm:changeRequests", doc, XPathConstants.NODE); if (cmRequest != null) { Node fac = (Node) OSLCUtils.getXPath().evaluate("//oslc_cm:changeRequests/oslc_cm:factory", doc, XPathConstants.NODE); assertNotNull(fac);//from w ww .j a v a 2 s.c om Node facUrl = (Node) OSLCUtils.getXPath() .evaluate("//oslc_cm:changeRequests/oslc_cm:factory/oslc_cm:url", doc, XPathConstants.NODE); assertNotNull(facUrl); Node facTitle = (Node) OSLCUtils.getXPath() .evaluate("//oslc_cm:changeRequests/oslc_cm:factory/dc:title", doc, XPathConstants.NODE); assertNotNull(facTitle); assertFalse(facTitle.getTextContent().isEmpty()); } }
From source file:de.ingrid.iplug.opensearch.converter.IngridRSSConverter.java
/** * Calculate a document id so that it can be identified later when looking * for the detail./*from www .j a v a 2s . co m*/ * * @param item * @return * @throws XPathExpressionException */ private String getDocumentId(Node item) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); Node node = (Node) xpath.evaluate("docid", item, XPathConstants.NODE); if (node == null) { return String.valueOf(customDocId++); } else { return node.getTextContent(); } }
From source file:org.eclipse.lyo.testsuite.oslcv2.ServiceProviderXmlTests.java
@Test public void serviceProviderHasService() throws XPathException { //Verify the ServiceProvider has at least one rdf:service child element Node service = (Node) OSLCUtils.getXPath().evaluate("//oslc_v2:ServiceProvider/oslc_v2:service", doc, XPathConstants.NODE); assertNotNull(service);/* w w w . j av a 2 s . c o m*/ }
From source file:org.ambraproject.article.service.FetchArticleServiceImpl.java
/** * Get the author affiliations for a given article * @param doc article xml/*from www.ja v a2 s .c om*/ * @param doc article xml * @return author affiliations */ public ArrayList<AuthorExtra> getAuthorAffiliations(Document doc) { ArrayList<AuthorExtra> list = new ArrayList<AuthorExtra>(); Map<String, String> affiliateMap = new HashMap<String, String>(); if (doc == null) { return list; } try { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression affiliationListExpr = xpath.compile("//aff"); XPathExpression affiliationAddrExpr = xpath.compile("//addr-line"); NodeList affiliationNodeList = (NodeList) affiliationListExpr.evaluate(doc, XPathConstants.NODESET); // Map all affiliation id's to their affiliation strings for (int i = 0; i < affiliationNodeList.getLength(); i++) { Node node = affiliationNodeList.item(i); // Not all <aff>'s have the 'id' attribute. String id = (node.getAttributes().getNamedItem("id") == null) ? "" : node.getAttributes().getNamedItem("id").getTextContent(); // Not all <aff> id's are affiliations. if (id.startsWith("aff")) { DocumentFragment df = doc.createDocumentFragment(); df.appendChild(node); String address = ((Node) affiliationAddrExpr.evaluate(df, XPathConstants.NODE)) .getTextContent(); affiliateMap.put(id, address); } } XPathExpression authorExpr = xpath.compile("//contrib-group/contrib[@contrib-type='author']"); XPathExpression surNameExpr = xpath.compile("//name/surname"); XPathExpression givenNameExpr = xpath.compile("//name/given-names"); XPathExpression affExpr = xpath.compile("//xref[@ref-type='aff']"); NodeList authorList = (NodeList) authorExpr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < authorList.getLength(); i++) { Node cnode = authorList.item(i); DocumentFragment df = doc.createDocumentFragment(); df.appendChild(cnode); Node sNode = (Node) surNameExpr.evaluate(df, XPathConstants.NODE); Node gNode = (Node) givenNameExpr.evaluate(df, XPathConstants.NODE); // Either surname or givenName can be blank String surname = (sNode == null) ? "" : sNode.getTextContent(); String givenName = (gNode == null) ? "" : gNode.getTextContent(); // If both are null then don't bother to add if ((sNode != null) || (gNode != null)) { NodeList affList = (NodeList) affExpr.evaluate(df, XPathConstants.NODESET); ArrayList<String> affiliations = new ArrayList<String>(); // Build a list of affiliations for this author for (int j = 0; j < affList.getLength(); j++) { Node anode = affList.item(j); String affId = anode.getAttributes().getNamedItem("rid").getTextContent(); affiliations.add(affiliateMap.get(affId)); } AuthorExtra authorEx = new AuthorExtra(); authorEx.setAuthorName(surname, givenName); authorEx.setAffiliations(affiliations); list.add(authorEx); } } } catch (Exception e) { log.error("Error occurred while gathering the author affiliations.", e); } return list; }
From source file:org.eclipse.lyo.testsuite.oslcv2.FetchResourceTests.java
/** * Assume that nodeList.getLength()==1/* w w w. ja v a 2 s .c om*/ */ protected void validateCompactPreview(NodeList nodeList) throws IOException, XPathExpressionException { Node node = (Node) OSLCUtils.getXPath().evaluate("./oslc_v2:Preview/oslc_v2:document/@rdf:resource", nodeList.item(0), XPathConstants.NODE); assertNotNull("Expected number of oslc:Preview/oslc:document/@rdf:resource", node); String previewUrl = node.getTextContent(); HttpResponse response = OSLCUtils.getResponseFromUrl(previewUrl, previewUrl, creds, "*/*", headers); int statusCode = response.getStatusLine().getStatusCode(); String contentType = response.getEntity().getContentType().getValue(); EntityUtils.consume(response.getEntity()); assertTrue("Fetching document preview from " + previewUrl + " did not respond with expected code, received " + statusCode, 200 <= statusCode && statusCode < 400); assertTrue("Expected HTML content type from preview document but received " + contentType, contentType.startsWith("text/html")); }
From source file:gov.nij.bundles.intermediaries.ers.EntityResolutionMessageHandler.java
Set<AttributeParametersXpathSupport> getAttributeParameters(Node attributeParametersNode) throws Exception { Set<AttributeParametersXpathSupport> ret = new HashSet<AttributeParametersXpathSupport>(); NodeList parameterNodes = null; if (attributeParametersNode == null) { parameterNodes = (NodeList) xpath.evaluate("er-ext:AttributeParameter", attributeParametersDocument.getDocumentElement(), XPathConstants.NODESET); } else {/*from www . j a v a2s.co m*/ parameterNodes = (NodeList) xpath.evaluate("er-ext:AttributeParameter", attributeParametersNode, XPathConstants.NODESET); } // XmlConverter converter = new XmlConverter(); // converter.getDocumentBuilderFactory().setNamespaceAware(true); // LOG.info(converter.toString(attributeParametersDocument)); for (int i = 0; i < parameterNodes.getLength(); i++) { Node node = parameterNodes.item(i); // From the attribute parameter element, extract the attribute xpath value // The namespace prefixes will need to be processed and added to the ER namespace context String attributeXpathValue = xpath.evaluate("er-ext:AttributeXPath", node); LOG.debug("Attribute parameter xpath value: " + attributeXpathValue); AttributeParametersXpathSupport parameter = new AttributeParametersXpathSupport(attributeXpathValue, node); String algorithmURI = xpath.evaluate("er-ext:AttributeMatchAlgorithmSimmetricsURICode", node); String botchedClassName = algorithmURI.replace("urn:org:search:ers:algorithms:", ""); String[] splitClassName = botchedClassName.split("\\."); StringBuffer reversedClassName = new StringBuffer(64); for (int ii = splitClassName.length - 2; ii >= 0; ii--) { reversedClassName.append(splitClassName[ii]).append("."); } reversedClassName.append(splitClassName[splitClassName.length - 1]); parameter.setAlgorithmClassName(reversedClassName.toString()); String isDeterm = xpath.evaluate("er-ext:AttributeIsDeterminativeIndicator", node); // LOG.info("$#$#$!!! isDeterm=" + isDeterm); parameter.setDeterminative("true".equals(isDeterm)); parameter.setThreshold(Double.parseDouble(xpath.evaluate("er-ext:AttributeThresholdValue", node))); Node sortNode = (Node) xpath.evaluate("er-ext:AttributeSortSpecification", node, XPathConstants.NODE); if (sortNode != null) { SortOrderSpecification sos = new SortOrderSpecification(); String sortOrder = xpath.evaluate("er-ext:AttributeSortOrder", sortNode); String sortOrderRankS = xpath.evaluate("er-ext:AttributeSortOrderRank", sortNode); if (sortOrder == null || sortOrderRankS == null) { throw new IllegalArgumentException( "If the AttributeSortSpecification element is specified, both sort order and rank must be specified."); } int sortOrderRank = Integer.parseInt(sortOrderRankS); sos.setSortOrder(sortOrder); sos.setSortOrderRank(sortOrderRank); parameter.setSortOrder(sos); } ret.add(parameter); } return ret; }
From source file:org.opencastproject.remotetest.server.ComposerRestEndpointTest.java
/** * Gets the mediapackage element from a job polling response * //from w w w .ja v a 2s. c o m * @param jobXml * the job as xml * @return the mediapackage elemet as an xml string */ protected long getDurationFromJob(String jobXml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(IOUtils.toInputStream(jobXml, "UTF-8")); String payload = (String) XPathFactory.newInstance().newXPath().compile("//*[local-name() = 'payload']") .evaluate(doc, XPathConstants.STRING); Document payloadDoc = builder.parse(IOUtils.toInputStream(payload, "UTF-8")); Element element = ((Element) XPathFactory.newInstance().newXPath() .compile("//*[local-name() = 'duration'][1]").evaluate(payloadDoc, XPathConstants.NODE)); if (element == null) throw new IllegalStateException("Track doesn't contain a duration"); return Long.parseLong(element.getFirstChild().getNodeValue()); }
From source file:de.ingrid.iplug.opensearch.converter.IngridRSSConverter.java
/** * Get the description of the entry./*from w ww.j a v a 2 s . c om*/ * * @param item * @return * @throws XPathExpressionException */ private Object getAbstract(Node item) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); Node node = (Node) xpath.evaluate("description", item, XPathConstants.NODE); if (node != null) { return node.getTextContent(); } else { return ""; } }
From source file:org.eclipse.lyo.testsuite.oslcv2.ServiceProviderXmlTests.java
@Test public void serviceProviderHasValidDetails() throws XPathException, DOMException, IOException { //Verify the ServiceProvider has a valid oslc:details attribute Node details = (Node) OSLCUtils.getXPath().evaluate("//oslc_v2:ServiceProvider/oslc_v2:details", doc, XPathConstants.NODE); assertNotNull("oslc:details element is required for oslc:ServiceProfile", details); Node node = details.getAttributes().getNamedItemNS(OSLCConstants.RDF, "resource"); assertNotNull(node.getNodeValue());//from w w w . ja va2 s .c o m HttpResponse resp = OSLCUtils.getResponseFromUrl(setupBaseUrl, node.getNodeValue(), basicCreds, ""); resp.getEntity().consumeContent(); assertFalse(resp.getStatusLine().getStatusCode() == 404); }
From source file:hudson.plugins.plot.XMLSeries.java
/** * Convert a given object into a String. * //from w w w . j ava 2 s .co m * @param obj * Xpath Object * @return String representation of the node */ private String nodeToString(Object obj) { String ret = null; if (nodeType == XPathConstants.BOOLEAN) { return (((Boolean) obj)) ? "1" : "0"; } if (nodeType == XPathConstants.NUMBER) return ((Double) obj).toString().trim(); if (nodeType == XPathConstants.NODE || nodeType == XPathConstants.NODESET) { if (obj instanceof String) { ret = ((String) obj).trim(); } else { if (null == obj) { return null; } Node node = (Node) obj; NamedNodeMap nodeMap = node.getAttributes(); if ((null != nodeMap) && (null != nodeMap.getNamedItem("time"))) { ret = nodeMap.getNamedItem("time").getTextContent(); } if (null == ret) { ret = node.getTextContent().trim(); } } } if (nodeType == XPathConstants.STRING) ret = ((String) obj).trim(); // for Node/String/NodeSet, try and parse it as a double. // we don't store a double, so just throw away the result. Scanner scanner = new Scanner(ret); if (scanner.hasNextDouble()) { return String.valueOf(scanner.nextDouble()); } return null; }