Example usage for javax.xml.xpath XPathFactory newInstance

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

Introduction

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

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:com.dianping.zebra.shard.jdbc.base.SingleDBBaseTestCase.java

protected void parseCreateTableScriptFile() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document configDoc = builder/*from www  .j  a v a  2  s .  c  om*/
            .parse(SingleDBBaseTestCase.class.getClassLoader().getResourceAsStream(getCreateTableScriptPath()));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NodeList tableScriptList = (NodeList) xpath.compile("/tables/table").evaluate(configDoc,
            XPathConstants.NODESET);
    for (int i = 0; i < tableScriptList.getLength(); i++) {
        SingleCreateTableScriptEntry entry = new SingleCreateTableScriptEntry();
        Element ele = (Element) tableScriptList.item(i);
        entry.setTableName(ele.getAttribute("name"));
        entry.setCreateTableScript(ele.getTextContent());
        createdTableList.add(entry);
    }
}

From source file:jGPIO.DTO.java

/**
 * Tries to use lshw to detect the physical system in use.
 * /*from  w  ww  . j  a v  a 2s  . com*/
 * @return The filename of the GPIO Definitions file.
 */
static private String autoDetectSystemFile() {
    String definitions = System.getProperty("definitions.lookup");
    if (definitions == null) {
        definitions = DEFAULT_DEFINITIONS;
    }

    File capabilitiesFile = new File(definitions);

    // If it doesn't exist, fall back to the default
    if (!capabilitiesFile.exists() && !definitions.equals(DEFAULT_DEFINITIONS)) {
        System.out.println("Could not find definitions lookup file at: " + definitions);
        System.out.println("Trying default definitions file at: " + definitions);
        capabilitiesFile = new File(DEFAULT_DEFINITIONS);
    }

    if (!capabilitiesFile.exists()) {
        System.out.println("Could not find definitions file at: " + definitions);
        return null;
    }

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = null;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
    }

    // Generate the lshw output if available
    Process lshw;
    try {
        lshw = Runtime.getRuntime().exec("lshw -c bus -disable dmi -xml");
        lshw.waitFor();
    } catch (Exception e1) {
        System.out.println("Couldn't execute lshw to identify board");
        e1.printStackTrace();
        return null;
    }
    Document lshwXML = null;
    try {
        lshwXML = dBuilder.parse(lshw.getInputStream());
    } catch (IOException e1) {
        System.out.println("IO Exception running lshw");
        e1.printStackTrace();
        return null;
    } catch (SAXException e1) {
        System.out.println("Could not parse lshw output");
        e1.printStackTrace();
        return null;
    }

    XPath xp = XPathFactory.newInstance().newXPath();
    NodeList capabilities;
    try {
        capabilities = (NodeList) xp.evaluate("/list/node[@id=\"core\"]/capabilities/capability", lshwXML,
                XPathConstants.NODESET);
    } catch (XPathExpressionException e1) {
        System.out.println("Couldn't run Caoability lookup");
        e1.printStackTrace();
        return null;
    }

    Document lookupDocument = null;
    try {
        lookupDocument = dBuilder.parse(capabilitiesFile);
        String lookupID = null;

        for (int i = 0; i < capabilities.getLength(); i++) {
            Node c = capabilities.item(i);
            lookupID = c.getAttributes().getNamedItem("id").getNodeValue();
            System.out.println("Looking for: " + lookupID);
            NodeList nl = (NodeList) xp.evaluate("/lookup/capability[@id=\"" + lookupID + "\"]", lookupDocument,
                    XPathConstants.NODESET);

            if (nl.getLength() == 1) {
                definitionFile = nl.item(0).getAttributes().getNamedItem("file").getNodeValue();
                pinDefinitions = (JSONArray) new JSONParser().parse(new FileReader(definitionFile));
                return definitionFile;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.vmware.o11n.plugin.powershell.model.RemotePsType.java

private void initDomDocument() {
    if (doc == null && xml != null) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        //            domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder;/*from  ww  w.  j  av  a  2s.c  o m*/
        try {
            builder = domFactory.newDocumentBuilder();
            doc = builder.parse(IOUtils.toInputStream(xml));
        } catch (ParserConfigurationException e) {
            throw new PowerShellException(e.getMessage(), e);
        } catch (SAXException e) {
            throw new PowerShellException(e.getMessage(), e);
        } catch (IOException e) {
            throw new PowerShellException(e.getMessage(), e);
        }

        xpathFactory = XPathFactory.newInstance();
    }

}

From source file:de.tudarmstadt.ukp.dkpro.core.io.xml.XmlReaderXPath.java

@Override
public void initialize(UimaContext arg0) throws ResourceInitializationException {
    super.initialize(arg0);

    fileIterator = getFileSetIterator();
    XPath xpath = XPathFactory.newInstance().newXPath();
    nodes = new ArrayDeque<Node>();

    if (StringUtils.isWhitespace(rootXPath)) {
        throw new IllegalArgumentException("Illegal root XPath expression. Please provide a valid one.");
    }/*from  w  w  w.  j  a  va 2 s  . c  o m*/
    try {
        compiledRootXPath = xpath.compile(rootXPath);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("Illegal root XPath expression. Please provide a valid one.");
    }

    if (docIdTag != null) {
        if (StringUtils.isWhitespace(docIdTag)) {
            throw new IllegalArgumentException("Illegal ID XPath expression. Please provide a valid one.");
        }
        try {
            compiledIdXPath = xpath.compile(docIdTag);
        } catch (XPathExpressionException e) {
            throw new IllegalArgumentException("Illegal ID XPath expression. Please provide a valid one.");
        }
    }

    // Substitution
    if (substituteTags != null && substituteTags.length > 0) {
        if (substituteTags.length % 2 != 0) {
            throw new IllegalArgumentException("Parameter substitute tags must "
                    + "be given in an array of even number of elements, in 'before, after' order");
        }

        useSubstitution = true;
        substitution = new HashMap<String, String>(substituteTags.length);
        for (int i = 0; i < substituteTags.length; i += 2) {
            substitution.put(substituteTags[i], substituteTags[i + 1]);
        }
    }

    processNextFile();
}