Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:gov.nih.nci.cacis.common.util.ExtractSchematron.java

/**
 * Creates new instance of the schematron extractor.
 * /*ww w  .ja  va 2  s  .  co  m*/
 * @throws ClassCastException if there is an error getting a SchemaLoader
 * @throws ClassNotFoundException if there is an error getting a SchemaLoader
 * @throws InstantiationException if there is an error getting a SchemaLoader
 * @throws IllegalAccessException if there is an error getting a SchemaLoader
 * @throws TransformerFactoryConfigurationError if there is an error getting a serializer
 * @throws TransformerConfigurationException if there is an error getting a serializer
 * @throws XPathExpressionException if there is an error compiling expressions
 */
public ExtractSchematron()
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        TransformerConfigurationException, TransformerFactoryConfigurationError, XPathExpressionException {
    final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");

    this.dfactory = DocumentBuilderFactory.newInstance();
    this.dfactory.setNamespaceAware(true);

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new SchNamespaceContext());

    this.serializer = TransformerFactory.newInstance().newTransformer();
    this.serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    this.patternExpr = xpath.compile("//sch:pattern");

    this.schemaLoader = impl.createXSLoader(null);
}

From source file:org.ambraproject.article.service.FetchArticleServiceImpl.java

/**
 * Get references for a given article//from   w w w.j  av  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 {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//back/ref-list[title='References']/ref");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList refList = (NodeList) result;

        if (refList.getLength() == 0) {
            expr = xpath.compile("//back/ref-list/ref");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            refList = (NodeList) result;
        }

        XPathExpression typeExpr = xpath.compile("//citation | //nlm-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) {
                NamedNodeMap nnm = resultNode.getAttributes();
                Node nnmNode = nnm.getNamedItem("citation-type");
                // some old articles do not have this attribute
                if (nnmNode != null) {
                    citation.setCitationType(nnmNode.getTextContent());
                }
            }

            // 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:betullam.xmlmodifier.XMLmodifier.java

private boolean checkCondition(String condStructureType, String condMdName, String condMdValue, Document xmlDoc)
        throws XPathExpressionException {
    String textContent;//from  w  w w  .j av a 2 s.c o m

    // Get the IDs for the requestet structure type, e. g. for "Monograph", "JournalVolume", etc.
    List<String> dmdIDs = getDMDIDs(condStructureType, xmlDoc);

    // Loop over the structure types and check if the metadata and it's value matches the requestet metadata and value.
    // E. g.: Only change a file if "TitleDocMain" (=condMdName) of a "Monograph" (=condMdValue) has the value "Title of the Monograph" (=condMdValue)
    for (String dmdID : dmdIDs) {

        // If we do not check for the value of the "name" attribute, we always have a match, so return true
        if (condMdName.equals("null")) {
            return true;
        } else {
            String xPathString = "//dmdSec[@ID=\"" + dmdID + "\"]//goobi/metadata[@name=\"" + condMdName
                    + "\"]";
            XPathFactory xPathFactory = XPathFactory.newInstance();
            XPath xPath = xPathFactory.newXPath();
            XPathExpression xPathExpr = xPath.compile(xPathString);
            NodeList nodeList = (NodeList) xPathExpr.evaluate(xmlDoc, XPathConstants.NODESET);

            // Check if there is at least 1 element with the given value (condMdName)
            if (nodeList.getLength() > 0) {
                if (condMdValue.equals("null")) { // If we do not check for the value of the element, we always have a match, so return true
                    return true;
                } else {
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        Element xmlElement = (Element) nodeList.item(i);
                        textContent = xmlElement.getTextContent();
                        if (textContent.equals(condMdValue)) {
                            // Return true immediatly if the condition is met, because there could be other values for a duplicate metadata, which could return false.
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

From source file:org.ala.harvester.FlickrHarvester.java

/**
 * Process a single image, do the document mapping etc
 * /* w  ww .ja  v  a 2s  .  c  om*/
 * @param infosourceId
 * @param imageIndex
 * @param currentResDom
 * @throws Exception
 */
private boolean processSingleImage(int infosourceId, int imageIndex, Document currentResDom) throws Exception {

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    String xPathToPhotoId = "/rsp/photos/photo[" + imageIndex + "]/@id";
    String photoId = (String) xpath.evaluate(xPathToPhotoId, currentResDom, XPathConstants.STRING);
    logger.info("Handling photo ID: " + photoId);

    final String flickrMethod = "flickr.photos.getInfo";

    // Calls the Flickr's Photo Info API to determine whether the photo
    // comes from Australia or not.
    String photoInfoFlickrUrl = this.flickrRestBaseUrl + "/" + "?method=" + flickrMethod + "&" + "api_key="
            + this.flickrApiKey + "&" + "photo_id=" + photoId;

    System.out.println("PHOTO URL:" + photoInfoFlickrUrl);

    org.w3c.dom.Document photoInfoDom = null;

    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();
    // Create a method instance.
    GetMethod method = new GetMethod(photoInfoFlickrUrl);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    method.getParams().setParameter(HttpMethodParams.HTTP_ELEMENT_CHARSET, "UTF-8");
    method.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, "UTF-8");

    logger.debug(
            "Fetching info. for photo with ID " + photoId + " from " + "`" + photoInfoFlickrUrl + "`" + "\n");

    try {
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            String errMsg = "HTTP GET to " + "`" + photoInfoFlickrUrl + "`" + " returned non HTTP OK code.  "
                    + "Returned code " + statusCode + " and message " + method.getStatusLine() + "\n";
            method.releaseConnection();
            logger.error(errMsg);
            throw new Exception(errMsg);
        }

        InputStream responseStream = method.getResponseBodyAsStream();

        // Instantiates a DOM builder to create a DOM of the response.
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

        photoInfoDom = domBuilder.parse(responseStream);

    } catch (Exception domCreationErr) {
        throw new Exception("Failed to create DOM representation of GET response.", domCreationErr);

    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    // Check for Flickr's error.
    if (!isDocExtractionSuccessful(photoInfoDom)) {
        throw new Exception("Flickr error response for fetching single image information.");
    }

    if (System.getProperty("overwrite") != null && "false".equals(System.getProperty("overwrite"))) {
        String photoPageUrl = (String) xpath.evaluate("/rsp/photo/urls/url[@type=\"photopage\"]/text()",
                photoInfoDom, XPathConstants.STRING);

        logger.debug("photo page URL: " + photoPageUrl);
        org.ala.model.Document doc = this.repository.getDocumentByGuid(photoPageUrl);
        if (doc != null) {
            logger.debug("Document with URI already harvested. Skipping: " + photoPageUrl);
            return true;
        }
    }

    // Determines whether photo has geo-coded tag from Australia.
    // If so, pass onto DocumentMapper.
    if (isPhotoFromAustralia(photoInfoDom)) {

        try {
            String document = (DOMUtils.domToString(photoInfoDom));
            // FIXME flickr GUID ???
            List<ParsedDocument> parsedDocs = documentMapper.map("", document.getBytes());
            for (ParsedDocument parsedDoc : parsedDocs) {
                this.repository.storeDocument(infosourceId, parsedDoc);
                debugParsedDoc(parsedDoc);
            }
            return false;
        } catch (Exception docMapperErr) {
            // Skipping over errors here and proceeding to next document.
            logger.error("Problem processing image. " + docMapperErr.toString() + ", Problem processing: "
                    + photoInfoFlickrUrl, docMapperErr);
        }
    } else {
        logger.debug("Photo is unAustralian: " + photoInfoFlickrUrl);
    }

    return false;
}

From source file:betullam.xmlmodifier.XMLmodifier.java

private Set<File> getFilesForInsertion(String condStructureElements, String mdDirectory) {
    Set<File> filesForInsertion = new HashSet<File>();
    List<String> lstStructureElements = Arrays.asList(condStructureElements.split("\\s*,\\s*"));
    // Iterate over all files in the given directory and it's subdirectories. Works with "FileUtils" in Apache "commons-io" library.

    //for (File mdFile : FileUtils.listFiles(new File(mdDirectory), new WildcardFileFilter(new String[]{"meta.xml", "meta_anchor.xml"}, IOCase.INSENSITIVE), TrueFileFilter.INSTANCE)) {
    for (File mdFile : FileUtils.listFiles(new File(mdDirectory),
            new WildcardFileFilter("*.xml", IOCase.INSENSITIVE), TrueFileFilter.INSTANCE)) {
        // DOM Parser:
        String filePath = mdFile.getAbsolutePath();
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = null;
        Document xmlDoc = null;/*from  w ww.ja  v  a2 s .co m*/
        try {
            documentBuilder = documentFactory.newDocumentBuilder();
            xmlDoc = documentBuilder.parse(filePath);

            // Only get files with a structure element that is listed in condStructureElements
            for (String structureElement : lstStructureElements) {

                String xPathString = "/mets/structMap[@TYPE='LOGICAL']//div[@TYPE='" + structureElement + "']";
                XPathFactory xPathFactory = XPathFactory.newInstance();
                XPath xPath = xPathFactory.newXPath();
                XPathExpression xPathExpr = xPath.compile(xPathString);
                NodeList nodeList = (NodeList) xPathExpr.evaluate(xmlDoc, XPathConstants.NODESET);

                if (nodeList.getLength() > 0) {
                    filesForInsertion.add(mdFile);
                } else {
                }
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }
    return filesForInsertion;

}

From source file:com.meltmedia.cadmium.servlets.guice.CadmiumListener.java

public String getVHostName(ServletContext context) {
    String jbossWebXml = context.getRealPath("/WEB-INF/jboss-web.xml");
    try {/*from  w w w.j  a v  a2 s .co  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(jbossWebXml);

        XPathFactory xpFactory = XPathFactory.newInstance();
        XPath xpath = xpFactory.newXPath();

        XPathExpression expr = xpath.compile("/jboss-web/virtual-host/text()");

        NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

        if (result.getLength() > 0) {
            return result.item(0).getNodeValue();
        }

    } catch (Exception e) {
        log.warn("Failed to read/parse file.", e);
    }
    return WarUtils.getWarName(context);
}

From source file:cz.cas.lib.proarc.common.export.cejsh.CejshBuilder.java

public CejshBuilder(CejshConfig config)
        throws TransformerConfigurationException, ParserConfigurationException, XPathExpressionException {
    this.gcalendar = new GregorianCalendar(UTC);
    this.logLevel = config.getLogLevel();
    TransformerFactory xslFactory = TransformerFactory.newInstance();
    tranformationErrorHandler = new TransformErrorListener();
    bwmetaXsl = xslFactory.newTransformer(new StreamSource(config.getCejshXslUrl()));
    if (bwmetaXsl == null) {
        throw new TransformerConfigurationException("Cannot load XSL: " + config.getCejshXslUrl());
    }//from   ww w.  j  av a2 s.  c o  m
    bwmetaXsl.setOutputProperty(OutputKeys.INDENT, "yes");
    bwmetaXsl.setErrorListener(tranformationErrorHandler);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    db = dbf.newDocumentBuilder();
    XPathFactory xPathFactory = ProarcXmlUtils.defaultXPathFactory();
    XPath xpath = xPathFactory.newXPath();
    xpath.setNamespaceContext(new SimpleNamespaceContext().add("m", ModsConstants.NS));
    issnPath = xpath.compile("m:mods/m:identifier[@type='issn' and not(@invalid)]");
    partNumberPath = xpath.compile("m:mods/m:titleInfo/m:partNumber");
    dateIssuedPath = xpath.compile("m:mods/m:originInfo/m:dateIssued");
    reviewedArticlePath = xpath.compile("m:mods/m:genre[text()='article' and @type='peer-reviewed']");
}

From source file:com.baracuda.piepet.nexusrelease.nexus.StageClient.java

/**
 * Create a new StageClient to handle communicating to a Nexus Pro server Staging suite.
 * //from ww  w.j  a  v  a 2  s . c o  m
 * @param nexusURL the base URL for the Nexus server.
 * @param username user name to use with staging privileges.
 * @param password password for the user.
 */
public StageClient(URL nexusURL, String username, String password) {
    this.nexusURL = nexusURL;
    this.username = username;
    this.password = password;
    // XPathFactory is not thread safe.
    XPathFactory factory;
    synchronized (XPathFactory.class) {
        factory = XPathFactory.newInstance();
    }
    synchronized (factory) {
        xpath = factory.newXPath();
    }
}

From source file:org.apache.zeppelin.sap.universe.UniverseClient.java

private String getValue(String response, String xPathString)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    try (InputStream xmlStream = new ByteArrayInputStream(response.getBytes())) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlStream);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile(xPathString);
        Node tokenNode = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (tokenNode != null) {
            return tokenNode.getTextContent();
        }//from w  w w.j  ava 2s  .c  om
    }
    return null;
}

From source file:org.apache.zeppelin.sap.universe.UniverseClient.java

public List<List<String>> getResults(String token, String queryId) throws UniverseException {
    HttpGet httpGet = new HttpGet(
            String.format("%s%s%s%s", apiUrl, "/sl/v1/queries/", queryId, "/data.svc/Flows0"));
    setHeaders(httpGet, token);/*from w w w .ja  v  a  2 s.  co  m*/
    HttpResponse response = null;
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new UniverseException(
                    String.format(errorMessageTemplate, "UniverseClient " + "(get results): Request failed\n",
                            EntityUtils.toString(response.getEntity())));
        }
    } catch (IOException e) {
        throw new UniverseException(String.format(errorMessageTemplate,
                "UniverseClient " + "(get results): Request failed", ExceptionUtils.getStackTrace(e)));
    }

    try (InputStream xmlStream = response.getEntity().getContent()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlStream);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//feed/entry/content/properties");
        NodeList resultsNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        if (resultsNodes != null) {
            return parseResults(resultsNodes);
        } else {
            throw new UniverseException(String.format(errorMessageTemplate,
                    "UniverseClient " + "(get results): Response processing failed"));
        }
    } catch (IOException e) {
        throw new UniverseException(String.format(errorMessageTemplate,
                "UniverseClient " + "(get results): Request failed", ExceptionUtils.getStackTrace(e)));
    } catch (ParserConfigurationException | SAXException | XPathExpressionException e) {
        throw new UniverseException(String.format(errorMessageTemplate,
                "UniverseClient " + "(get results): Response processing failed",
                ExceptionUtils.getStackTrace(e)));
    }
}