List of usage examples for javax.xml.xpath XPathFactory newXPath
public abstract XPath newXPath();
Return a new XPath
using the underlying object model determined when the XPathFactory was instantiated.
From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java
protected List<Node> evaluateXPath(String xpathExpression, Node node) throws XPathExpressionException { final XPathFactory factory = XPathFactory.newInstance(); final XPath xpath = factory.newXPath(); final XPathExpression pathExpression = xpath.compile(xpathExpression); return wrapNodeList((NodeList) pathExpression.evaluate(node, XPathConstants.NODESET)); }
From source file:com.photon.phresco.impl.ConfigManagerImpl.java
private Node getNode(String xpath) throws ConfigurationException { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath newXPath = xPathFactory.newXPath(); XPathExpression xPathExpression; Node xpathNode = null;/*from www.j a va 2 s .c o m*/ try { xPathExpression = newXPath.compile(xpath); xpathNode = (Node) xPathExpression.evaluate(document, XPathConstants.NODE); } catch (XPathExpressionException e) { throw new ConfigurationException(e); } return xpathNode; }
From source file:com.photon.phresco.impl.ConfigManagerImpl.java
private NodeList getNodeList(String xpath) throws ConfigurationException { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath newXPath = xPathFactory.newXPath(); XPathExpression xPathExpression; NodeList xpathNodes = null;//from www . j ava 2s . co m try { xPathExpression = newXPath.compile(xpath); xpathNodes = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new ConfigurationException(e); } return xpathNodes; }
From source file:org.ambraproject.article.service.FetchArticleServiceImpl.java
/** * Returns abbreviated journal name/*w w w . ja v a 2 s.c o m*/ * @param doc article xml * @return abbreviated journal name */ public String getJournalAbbreviation(Document doc) { String journalAbbrev = ""; if (doc == null) { return journalAbbrev; } try { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//journal-meta/journal-id[@journal-id-type='nlm-ta']"); Object resultObj = expr.evaluate(doc, XPathConstants.NODE); Node resultNode = (Node) resultObj; if (resultNode != null) { journalAbbrev = resultNode.getTextContent(); } } catch (Exception e) { log.error("Error occurred while getting abbreviated journal name.", e); } return journalAbbrev; }
From source file:org.ala.harvester.MorphbankHarvester.java
/** * Process a single image, do the document mapping etc * // ww w . j a va 2s .c om * @param infosourceId * @param imageIndex * @param currentResDom * @throws Exception */ private void processSingleImage(int infosourceId, int imageIndex, Document currentResDom) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); ParsedDocument pd = new ParsedDocument(); ParsedDocument imageDoc = new ParsedDocument(); String subject = MappingUtils.getSubject(); String xPathToIdentifier = "/response/object[" + imageIndex + "]/detailPageUrl/text()"; String xPathToScientificName = "/response/object[" + imageIndex + "]/ScientificName/text()"; String xPathToLatitude = "/response/object[" + imageIndex + "]/DecimalLatitude/text()"; String xPathToLongitude = "/response/object[" + imageIndex + "]/DecimalLongitude/text()"; String xPathToImageUrl = "/response/object[" + imageIndex + "]/thumbUrl/text()"; String xPathToLicense = "/response/object[" + imageIndex + "]/copyrightText/text()"; String xPathToKingdom = "/response/object[" + imageIndex + "]/Kingdom/text()"; String xPathToPhylum = "/response/object[" + imageIndex + "]/Phylum/text()"; String xPathToClass = "/response/object[" + imageIndex + "]/Class/text()"; String xPathToOrder = "/response/object[" + imageIndex + "]/Order/text()"; String xPathToFamily = "/response/object[" + imageIndex + "]/Family/text()"; String xPathToGenus = "/response/object[" + imageIndex + "]/Genus/text()"; String xPathToSpecificEpithet = "/response/object[" + imageIndex + "]/SpecificEpithet/text()"; String xPathToCountry = "/response/object[" + imageIndex + "]/Country/text()"; String xPathToLocality = "/response/object[" + imageIndex + "]/Locality/text()"; String identifier = null; String scientificName = null; String latitude = null; String longitude = null; String imageUrl = null; String license = null; String kingdom = null; String phylum = null; String klass = null; String order = null; String family = null; String genus = null; String specificEpithet = null; String country = null; String locality = null; try { identifier = (String) xpath.evaluate(xPathToIdentifier, currentResDom, XPathConstants.STRING); scientificName = (String) xpath.evaluate(xPathToScientificName, currentResDom, XPathConstants.STRING); latitude = (String) xpath.evaluate(xPathToLatitude, currentResDom, XPathConstants.STRING); longitude = (String) xpath.evaluate(xPathToLongitude, currentResDom, XPathConstants.STRING); imageUrl = (String) xpath.evaluate(xPathToImageUrl, currentResDom, XPathConstants.STRING); license = (String) xpath.evaluate(xPathToLicense, currentResDom, XPathConstants.STRING); kingdom = (String) xpath.evaluate(xPathToKingdom, currentResDom, XPathConstants.STRING); phylum = (String) xpath.evaluate(xPathToPhylum, currentResDom, XPathConstants.STRING); klass = (String) xpath.evaluate(xPathToClass, currentResDom, XPathConstants.STRING); order = (String) xpath.evaluate(xPathToOrder, currentResDom, XPathConstants.STRING); family = (String) xpath.evaluate(xPathToFamily, currentResDom, XPathConstants.STRING); genus = (String) xpath.evaluate(xPathToGenus, currentResDom, XPathConstants.STRING); specificEpithet = (String) xpath.evaluate(xPathToSpecificEpithet, currentResDom, XPathConstants.STRING); country = (String) xpath.evaluate(xPathToCountry, currentResDom, XPathConstants.STRING); locality = (String) xpath.evaluate(xPathToLocality, currentResDom, XPathConstants.STRING); } catch (XPathExpressionException getPageFragmentationError) { String errMsg = "Failed to obtain Morphbank's Detail Page Url"; logger.error(errMsg); throw new Exception(errMsg, getPageFragmentationError); } // System.out.println("Index: " + imageIndex); identifier = identifier.replaceAll("\\-svc", ""); System.out.println(imageIndex + ", PHOTO URL:" + identifier); List<Triple<String, String, String>> triples = pd.getTriples(); Map<String, String> dcs = pd.getDublinCore(); pd.setGuid(identifier); pd.setContent(getContent(identifier)); pd.setContentType(contentType); dcs.put(Predicates.DC_TITLE.toString(), scientificName); dcs.put(Predicates.DC_IDENTIFIER.toString(), identifier); dcs.put(Predicates.LATITUDE.toString(), latitude); dcs.put(Predicates.LONGITUDE.toString(), longitude); dcs.put(Predicates.DC_LICENSE.toString(), "Creative Commons Attribution-Non Commercial 3.0 Australia License, http://creativecommons.org/licenses/by-nc/3.0/au/deed.en"); dcs.put(Predicates.DC_CREATOR.toString(), license); if (isCoral) { dcs.put(Predicates.DC_RIGHTS.toString(), "J. Veron Coral Reef Research"); } else { if (license != null && !"".equals(license)) { dcs.put(Predicates.DC_RIGHTS.toString(), license); } else { dcs.put(Predicates.DC_RIGHTS.toString(), "Copyright by Morphbank"); } } dcs.put(Predicates.COUNTRY.toString(), country); dcs.put(Predicates.LOCALITY.toString(), locality); triples.add(new Triple(subject, Predicates.SCIENTIFIC_NAME.toString(), scientificName)); triples.add(new Triple(subject, Predicates.KINGDOM.toString(), kingdom)); triples.add(new Triple(subject, Predicates.PHYLUM.toString(), phylum)); triples.add(new Triple(subject, Predicates.CLASS.toString(), klass)); triples.add(new Triple(subject, Predicates.ORDER.toString(), order)); triples.add(new Triple(subject, Predicates.FAMILY.toString(), family)); triples.add(new Triple(subject, Predicates.GENUS.toString(), genus)); triples.add(new Triple(subject, Predicates.SPECIFIC_EPITHET.toString(), specificEpithet)); if (imageUrl != null && !"".equals(imageUrl)) { imageUrl = imageUrl.replaceAll("thumb", "jpg"); imageUrl = imageUrl.replaceAll("images\\.morphbank\\.net", "morphbank-images.ala.org.au"); imageDoc = MappingUtils.retrieveImageDocument(pd, imageUrl); // debugParsedDoc(imageDoc); } // debugParsedDoc(pd); if (pd != null) { this.repository.storeDocument(infosourceId, pd); } if (imageDoc != null) { this.repository.storeDocument(infosourceId, imageDoc); } }
From source file:com.rest4j.generator.Generator.java
List<ModelNode> computeModelGraph(Document xml) throws Exception { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); xpath.setNamespaceContext(new APINamespaceContext()); XPathExpression refsExpr = xpath.compile(".//api:complex"); HashMap<String, ModelNode> graph = new HashMap<String, ModelNode>(); for (Node model : Util.it(xml.getDocumentElement().getElementsByTagName("model"))) { String name = ((Attr) model.getAttributes().getNamedItem("name")).getValue(); graph.put(name, new ModelNode(model)); }/*w ww. j a va2 s .c o m*/ for (ModelNode node : graph.values()) { for (Node complex : Util.it((NodeList) refsExpr.evaluate(node.model, XPathConstants.NODESET))) { Ref ref = new Ref(); String type = complex.getAttributes().getNamedItem("type").getTextContent(); ModelNode referenced = graph.get(type); if (referenced == null) throw new IllegalArgumentException("Wrong reference from " + node.model.getAttributes().getNamedItem("name").getTextContent() + "." + complex.getAttributes().getNamedItem("name").getTextContent() + " to " + type); ref.referencedModel = referenced; if (complex.getAttributes().getNamedItem("collection").getTextContent().equals("array")) { ref.array = true; } node.references.add(ref); } } return new ArrayList<ModelNode>(graph.values()); }
From source file:org.powertac.server.CompetitionSetupService.java
private ArrayList<Object> processBootDataset(URL bootDataset) { // Read and convert the bootstrap dataset ArrayList<Object> result = new ArrayList<Object>(); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); try {//from w w w . j a v a 2s. c om InputSource source = new InputSource(bootDataset.openStream()); // we want all the children of the bootstrap node XPathExpression exp = xPath.compile("/powertac-bootstrap-data/bootstrap/*"); NodeList nodes = (NodeList) exp.evaluate(source, XPathConstants.NODESET); log.info("Found " + nodes.getLength() + " bootstrap nodes"); // Each node is a bootstrap data item for (int i = 0; i < nodes.getLength(); i++) { String xml = nodeToString(nodes.item(i)); Object msg = messageConverter.fromXML(xml); result.add(msg); } } catch (XPathExpressionException xee) { log.error("runOnce: Error reading config file: " + xee.toString()); } catch (IOException ioe) { log.error("runOnce: reset fault: " + ioe.toString()); } return result; }
From source file:org.powertac.server.CompetitionSetupService.java
private String getBaseTimeXML(String weatherData) { try {// w ww . j av a 2 s .c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(weatherData); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression expr = xPath.compile("/data/weatherReports/weatherReport/@date"); String earliest = "ZZZZ-ZZ-ZZ"; NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { String date = nodes.item(i).toString().split(" ")[0].split("\"")[1]; earliest = date.compareTo(earliest) < 0 ? date : earliest; } return earliest; } catch (Exception e) { log.error("Error extracting BaseTime from : " + weatherData); e.printStackTrace(); } return null; }
From source file:org.ambraproject.article.service.FetchArticleServiceImpl.java
/** * Get the author affiliations for a given article * @param doc article xml//from w w w .ja v a2 s . c o m * @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.ala.harvester.FlickrHarvester.java
/** * Parses the XML listing of images to obtain data necessary for future data * extraction. Specifically, the current page number, current images per * page and total number of pages.// w ww . ja v a 2 s .c om * * @since v0.4 */ private int[] parseDataFragmentationInfo(Document currentResDom) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); try { int currentPageNum = Integer .parseInt((String) xpath.evaluate("/rsp/photos/@page", currentResDom, XPathConstants.STRING)); int totalPages = Integer .parseInt((String) xpath.evaluate("/rsp/photos/@pages", currentResDom, XPathConstants.STRING)); int actualRecordsPerPage = Integer.parseInt( (String) xpath.evaluate("count(/rsp/photos/photo)", currentResDom, XPathConstants.STRING)); logger.debug("Extracted and set current page number to " + currentPageNum); logger.debug("Extracted and set total page number to " + totalPages); logger.debug("Actual number of records returned is " + actualRecordsPerPage); return new int[] { currentPageNum, totalPages, actualRecordsPerPage }; } catch (XPathExpressionException getPageFragmentationError) { String errMsg = "Failed to obtain data fragmentation information from Flickr's REST response."; throw new Exception(errMsg, getPageFragmentationError); } }