Example usage for javax.xml.xpath XPathConstants NODESET

List of usage examples for javax.xml.xpath XPathConstants NODESET

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODESET.

Prototype

QName NODESET

To view the source code for javax.xml.xpath XPathConstants NODESET.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Maps to Java org.w3c.dom.NodeList .

Usage

From source file:org.eclipse.lyo.testsuite.oslcv2.CoreResourceXmlTests.java

@Test
public void CoreResourceHasOneTitle() throws XPathExpressionException {
    String eval = "//" + getNode() + "/" + "dc:title";

    NodeList titles = (NodeList) OSLCUtils.getXPath().evaluate(eval, doc, XPathConstants.NODESET);

    assertEquals("dc:title" + getFailureMessage(), 1, titles.getLength());
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Gets the rdf pids./*  w  w w  .  j  av  a2  s  . c  om*/
 * 
 * @param pid
 *        the pid
 * @param relation
 *        the relation
 * @return the rdf pids
 */
public static ArrayList<String> getRdfPids(String pid, String relation) {
    ArrayList<String> pids = new ArrayList<String>();
    try {

        String command = configuration.getFedoraHost() + "/get/" + pid + "/" + RELS_EXT_STREAM;
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        String xPathStr = "/RDF/Description/" + relation;
        XPathExpression expr = xpath.compile(xPathStr);
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            if (!childnode.getNodeName().contains("hasModel")) {
                pids.add(childnode.getNodeName() + " "
                        + childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return pids;
}

From source file:com.esri.gpt.server.openls.provider.services.poi.DirectoryProvider.java

/**
 * Parse directory request//from   w ww  .  ja  v  a2  s. co m
 * @param context
 * @param ndReq
 * @param xpath
 * @throws XPathExpressionException
 */
private void parseRequest(OperationContext context, Node ndReq, XPath xpath) throws XPathExpressionException {
    DirectoryParams params = context.getRequestOptions().getDirectoryOptions();
    HashMap<String, String> poiProperties = null;
    HashMap<String, Object> poiLocations = null;

    Node ndPoiLoc = (Node) xpath.evaluate("xls:POILocation", ndReq, XPathConstants.NODE);
    if (ndPoiLoc != null) {
        poiLocations = new HashMap<String, Object>();
        Node ndPos = (Node) xpath.evaluate("xls:Nearest/xls:Position/gml:Point/gml:pos", ndPoiLoc,
                XPathConstants.NODE);
        if (ndPos != null) {
            String[] xy = ndPos.getTextContent().split(" ");
            Point loc = new Point(xy[0], xy[1]);
            poiLocations.put("nearest", loc);
        }
        @SuppressWarnings("unused")
        Node ndWDAL = (Node) xpath.evaluate("xls:WithinDistance/xls:POI/xls:POIAttributeList/xls:POIInfoList",
                ndPoiLoc, XPathConstants.NODE);
        String maxDist = (String) xpath.evaluate("xls:WithinDistance/xls:MaximumDistance/@value", ndPoiLoc,
                XPathConstants.STRING);
        if (maxDist != null) {
            poiLocations.put("withinDistance", maxDist);
        }

    }
    Node ndPoiProp = (Node) xpath.evaluate("xls:POIProperties", ndReq, XPathConstants.NODE);
    if (ndPoiProp != null) {
        NodeList nlProp = (NodeList) xpath.evaluate("xls:POIProperty", ndPoiProp, XPathConstants.NODESET);
        if (nlProp != null) {
            for (int j = 0; j < nlProp.getLength(); j++) {
                Node ndProp = nlProp.item(j);
                poiProperties = new HashMap<String, String>();
                String name = (String) xpath.evaluate("@name", ndProp, XPathConstants.STRING);
                String param = context.getRequestContext().getApplicationConfiguration()
                        .getCatalogConfiguration().getParameters().getValue(name);
                String value = (String) xpath.evaluate("@value", ndProp, XPathConstants.STRING);
                poiProperties.put(param, value);
            }
        }
    }
    params.setPoiLocations(poiLocations);
    params.setPoiProperties(poiProperties);
}

From source file:net.sourceforge.buildmonitor.monitors.BambooMonitor.java

private List<BuildPlan> getProjects(String bambooServerBaseUrl) throws MonitoringException {
    List returnList = new ArrayList<BuildReport>();
    try {// w  w w  .j av a  2 s  .c  o m
        String methodURL = bambooServerBaseUrl + "/rest/api/latest/plan" + "?os_authType=basic";
        if (bambooProperties.getFavouriteProjectsOnly()) {
            methodURL += "&favourite";
        }
        String serverResponse = callBambooApi(new URL(methodURL));
        InputSource serverResponseIS = new InputSource(new StringReader(serverResponse));

        NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().evaluate("/plans/plans/plan",
                serverResponseIS, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Element e = (Element) nodes.item(i);
            BuildPlan plan = new BuildPlan();
            plan.key = e.getAttribute("key");
            plan.name = e.getAttribute("name");
            returnList.add(plan);
        }
    } catch (Throwable t) {
        throw new MonitoringException(t, null);
    }
    return returnList;
}

From source file:com.twentyn.patentExtractor.PatentDocumentFeatures.java

private static Map<String, Integer> extractMoleculeCounts(Map<String, Integer> moleculeCounts, Document doc)
        throws ParserConfigurationException, XPathExpressionException {
    if (doc != null) {
        /* This uses //MOLECULE instead of //MOLECULE//text(), as the latter finds all text for all molecules
         * instead of text for each molecule.  We could also do a secondary traversal of each MOLECULE fragment,
         * but running XPath queries over XPath results is a major pain.  Instead, we'll grab the MOLECULE nodes
         * and recursively extract the text content one molecule at a time. */
        XPath xpath = Util.getXPathFactory().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(MOLECULE_PATH, doc, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            List<String> nameList = appendTextContent(new LinkedList<String>(), nodes.item(i));
            String moleculeName = StringUtils.join(nameList, " ");
            Integer count = moleculeCounts.get(moleculeName);
            if (count == null) {
                count = 0;//  ww  w.ja va 2  s.c  o  m
            }
            moleculeCounts.put(moleculeName, count + 1);
        }
    }
    return moleculeCounts;
}

From source file:au.csiro.casda.sodalint.ValidateServiceDescriptor.java

private Node getSodaServiceResource(Reporter reporter, Document document) throws XPathExpressionException {
    // logDocumentContent(document);

    XPath xpath = XPathFactory.newInstance().newXPath();
    String expression = "*[local-name()= 'RESOURCE' and "
            + "@utype='adhoc:service' and ./*/@value='ivo://ivoa.net/std/SODA#sync-1.0']";
    NodeList svcResList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(),
            XPathConstants.NODESET);

    if (svcResList != null && svcResList.getLength() > 0) {
        return svcResList.item(0);
    }//from w ww  .ja va 2s. co  m
    return null;
}

From source file:org.opencastproject.loadtest.impl.IngestJob.java

/**
 * Parse and change the manifest.xml's id to match the mediapackage id we will be ingesting.
 * /*  ww w.  jav a  2 s .c  o m*/
 * @param filepath
 *          The path to the manifest.xml file.
 */
private void changeManifestID(String filepath) {
    try {
        logger.info("Filepath for changing the manifest id is " + filepath);
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);

        // Get the mediapackage
        XPath xPath = XPathFactory.newInstance().newXPath();
        Node mediapackage = ((NodeList) xPath.evaluate("//*[local-name() = 'mediapackage']", doc,
                XPathConstants.NODESET)).item(0);

        // update mediapackage attribute
        NamedNodeMap attr = mediapackage.getAttributes();
        Node nodeAttr = attr.getNamedItem("id");
        nodeAttr.setTextContent(id);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SAXException sae) {
        sae.printStackTrace();
    } catch (XPathExpressionException xpe) {
        xpe.printStackTrace();
    }
}

From source file:aurelienribon.gdxsetupui.ProjectUpdate.java

private void writeClasspath(File classpathFile, List<ClasspathEntry> classpath) {
    try {//from ww w  . j  av a2  s . co m
        Document doc = XmlUtils.createParser().parse(classpathFile);
        Node root = (Node) XmlUtils.xpath("classpath", doc, XPathConstants.NODE);
        NodeList libsNodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc,
                XPathConstants.NODESET);

        for (int i = 0; i < libsNodes.getLength(); i++) {
            root.removeChild(libsNodes.item(i));
        }

        for (ClasspathEntry entry : classpath) {
            Element elem = doc.createElement("classpathentry");
            root.appendChild(elem);

            elem.setAttribute("kind", "lib");
            if (entry.exported)
                elem.setAttribute("exported", "true");
            elem.setAttribute("path", entry.path);
            if (entry.sourcepath != null)
                elem.setAttribute("sourcepath", entry.sourcepath);
        }

        XmlUtils.clean(doc);
        String str = XmlUtils.transform(doc);
        FileUtils.writeStringToFile(classpathFile, str);

    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.eclipse.lyo.testsuite.oslcv2.CoreResourceXmlTests.java

@Test
public void CoreResourceHasAtMostOneDescription() throws XPathExpressionException {
    String eval = "//" + getNode() + "/" + "dc:description";

    NodeList descriptions = (NodeList) OSLCUtils.getXPath().evaluate(eval, doc, XPathConstants.NODESET);

    assertTrue("dc:description" + getFailureMessage(), descriptions.getLength() <= 1);
}

From source file:be.fedict.eid.applet.service.signer.odf.ODFUtil.java

/**
 * Check if an ODF package is self-contained, i.e. content files don't have
 * OLE objects linked to external files//from  ww  w .  jav  a 2  s. c o m
 * 
 * @param odfUrl
 * @return
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws XPathExpressionException
 */
public static boolean isSelfContained(URL odfUrl)
        throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
    InputStream odfInputStream = odfUrl.openStream();
    List zipEntries = getZipEntriesAsList(odfInputStream);

    odfInputStream = odfUrl.openStream();
    ZipInputStream odfZipInputStream = new ZipInputStream(odfInputStream);
    ZipEntry zipEntry;

    XPathFactory factory = XPathFactory.newInstance();
    /* Maybe a bit overkill, but implementations can use other prefixes */
    ODFNamespaceContext namespaceContext = new ODFNamespaceContext();

    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(namespaceContext);
    XPathExpression expression = xpath.compile("//draw:object/@xlink:href|" + "//draw:object-ole/@xlink:href|"
            + "//draw:image/@xlink:href|" + "//draw:floating-frame/@xlink:href");

    while (null != (zipEntry = odfZipInputStream.getNextEntry())) {
        if (isContentFile(zipEntry)) {
            /* TODO: pure SAX is probably more memory-efficient */
            Document content = ODFUtil.loadDocument(odfZipInputStream);
            NodeList nodes = (NodeList) expression.evaluate(content, XPathConstants.NODESET);
            return checkNodes(nodes, zipEntries);
        }
    }
    return true;
}