Example usage for javax.xml.xpath XPathExpressionException printStackTrace

List of usage examples for javax.xml.xpath XPathExpressionException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpressionException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Print stack trace to System.err .

Usage

From source file:android.databinding.tool.MakeCopy.java

public static void main(String[] args) {
    if (args.length < 5) {
        System.out.println("required parameters: [-l] manifest adk-dir src-out-dir xml-out-dir "
                + "res-out-dir res-in-dir...");
        System.out.println("Creates an android data binding class and copies resources from");
        System.out.println("res-source to res-target and modifies binding layout files");
        System.out.println("in res-target. Binding data is extracted into XML files");
        System.out.println("and placed in xml-out-dir.");
        System.out.println("  -l          indicates that this is a library");
        System.out.println("  manifest    path to AndroidManifest.xml file");
        System.out.println("  src-out-dir path to where generated source goes");
        System.out.println("  xml-out-dir path to where generated binding XML goes");
        System.out.println("  res-out-dir path to the where modified resources should go");
        System.out.println(//  w  w w .j  a  v  a2 s .c om
                "  res-in-dir  path to source resources \"res\" directory. One" + " or more are allowed.");
        System.exit(1);
    }
    final boolean isLibrary = args[0].equals("-l");
    final int indexOffset = isLibrary ? 1 : 0;
    final String applicationPackage;
    final int minSdk;
    final Document androidManifest = readAndroidManifest(new File(args[MANIFEST_INDEX + indexOffset]));
    try {
        final XPathFactory xPathFactory = XPathFactory.newInstance();
        final XPath xPath = xPathFactory.newXPath();
        applicationPackage = xPath.evaluate("string(/manifest/@package)", androidManifest);
        final Double minSdkNumber = (Double) xPath.evaluate("number(/manifest/uses-sdk/@android:minSdkVersion)",
                androidManifest, XPathConstants.NUMBER);
        minSdk = minSdkNumber == null ? 1 : minSdkNumber.intValue();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        System.exit(6);
        return;
    }
    final File srcDir = new File(args[SRC_INDEX + indexOffset], APP_SUBPATH);
    if (!makeTargetDir(srcDir)) {
        System.err.println("Could not create source directory " + srcDir);
        System.exit(2);
    }
    final File resTarget = new File(args[RES_OUT_INDEX + indexOffset]);
    if (!makeTargetDir(resTarget)) {
        System.err.println("Could not create resource directory: " + resTarget);
        System.exit(4);
    }
    final File xmlDir = new File(args[XML_INDEX + indexOffset]);
    if (!makeTargetDir(xmlDir)) {
        System.err.println("Could not create xml output directory: " + xmlDir);
        System.exit(5);
    }
    System.out.println("Application Package: " + applicationPackage);
    System.out.println("Minimum SDK: " + minSdk);
    System.out.println("Target Resources: " + resTarget.getAbsolutePath());
    System.out.println("Target Source Dir: " + srcDir.getAbsolutePath());
    System.out.println("Target XML Dir: " + xmlDir.getAbsolutePath());
    System.out.println("Library? " + isLibrary);

    boolean foundSomeResources = false;
    for (int i = RES_IN_INDEX + indexOffset; i < args.length; i++) {
        final File resDir = new File(args[i]);
        if (!resDir.exists()) {
            System.out.println("Could not find resource directory: " + resDir);
        } else {
            System.out.println("Source Resources: " + resDir.getAbsolutePath());
            try {
                FileUtils.copyDirectory(resDir, resTarget);
                addFromFile(resDir, resTarget);
                foundSomeResources = true;
            } catch (IOException e) {
                System.err.println("Could not copy resources from " + resDir + " to " + resTarget + ": "
                        + e.getLocalizedMessage());
                System.exit(3);
            }
        }
    }

    if (!foundSomeResources) {
        System.err.println("No resource directories were found.");
        System.exit(7);
    }
    processLayoutFiles(applicationPackage, resTarget, srcDir, xmlDir, minSdk, isLibrary);
}

From source file:com.semsaas.jsonxml.tools.JsonXpath.java

public static void main(String[] args) {
    /*//from www.j  a v  a 2s .  c  o m
     * Process options
     */
    LinkedList<String> files = new LinkedList<String>();
    LinkedList<String> expr = new LinkedList<String>();
    boolean help = false;
    String activeOption = null;
    String error = null;

    for (int i = 0; i < args.length && error == null && !help; i++) {
        if (activeOption != null) {
            if (activeOption.equals("-e")) {
                expr.push(args[i]);
            } else if (activeOption.equals("-h")) {
                help = true;
            } else {
                error = "Unknown option " + activeOption;
            }
            activeOption = null;
        } else {
            if (args[i].startsWith("-")) {
                activeOption = args[i];
            } else {
                files.push(args[i]);
            }
        }
    }

    if (error != null) {
        System.err.println(error);
        showHelp();
    } else if (help) {
        showHelp();
    } else {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            for (String f : files) {
                System.out.println("*** " + f + " ***");
                try {
                    // Create a JSON XML reader
                    XMLReader reader = XMLReaderFactory.createXMLReader("com.semsaas.jsonxml.JsonXMLReader");

                    // Prepare a reader with the JSON file as input
                    InputStreamReader stringReader = new InputStreamReader(new FileInputStream(f));
                    SAXSource saxSource = new SAXSource(reader, new InputSource(stringReader));

                    // Prepare a DOMResult which will hold the DOM of the xjson
                    DOMResult domResult = new DOMResult();

                    // Run SAX processing through a transformer
                    // (This could be done more simply, but we have here the opportunity to pass our xjson through
                    // an XSLT and get a legacy XML output ;) )
                    transformer.transform(saxSource, domResult);
                    Node dom = domResult.getNode();

                    XPathFactory xpathFactory = XPathFactory.newInstance();
                    for (String x : expr) {
                        try {
                            XPath xpath = xpathFactory.newXPath();
                            xpath.setNamespaceContext(new NamespaceContext() {
                                public Iterator getPrefixes(String namespaceURI) {
                                    return null;
                                }

                                public String getPrefix(String namespaceURI) {
                                    return null;
                                }

                                public String getNamespaceURI(String prefix) {
                                    if (prefix == null) {
                                        return XJSON.XMLNS;
                                    } else if ("j".equals(prefix)) {
                                        return XJSON.XMLNS;
                                    } else {
                                        return null;
                                    }
                                }
                            });
                            NodeList nl = (NodeList) xpath.evaluate(x, dom, XPathConstants.NODESET);
                            System.out.println("-- Found " + nl.getLength() + " nodes for xpath '" + x
                                    + "' in file '" + f + "'");
                            for (int i = 0; i < nl.getLength(); i++) {
                                System.out.println(" +(" + i + ")+ ");
                                XMLJsonGenerator handler = new XMLJsonGenerator();
                                StringWriter buffer = new StringWriter();
                                handler.setOutputWriter(buffer);

                                SAXResult result = new SAXResult(handler);
                                transformer.transform(new DOMSource(nl.item(i)), result);

                                System.out.println(buffer.toString());
                            }
                        } catch (XPathExpressionException e) {
                            System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'");
                            e.printStackTrace();
                        } catch (TransformerException e) {
                            System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'");
                            e.printStackTrace();
                        }
                    }
                } catch (FileNotFoundException e) {
                    System.err.println("File '" + f + "' was not found");
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TransformerException e) {
                    e.printStackTrace();
                }
            }
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String getValueAsString(Document doc, XPath xpath, String value) {
    String id = null;//  ww w.  ja  va2 s  . c  om
    try {
        XPathExpression expr = xpath.compile(value);
        id = (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return id;
}

From source file:Main.java

static public Element element(Node context, String expression) {
    try {//from ww  w  . ja v  a 2 s .  c  om
        return (Element) xpath.evaluate(expression, context, XPathConstants.NODE);
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        throw new RuntimeException("invalid xpath expresion used");
    }
}

From source file:Main.java

public static Node findSchemaPropertyByDimensionName(Document xmlDoc, String xPathExpression) {
    XPath xPath = XPathFactory.newInstance().newXPath();

    Node nOde = null;//  w  ww.  ja  va 2s .  co  m
    try {
        nOde = (Node) xPath.compile(xPathExpression).evaluate(xmlDoc, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return nOde;
}

From source file:Main.java

/**
 * Use the supplied XPath statement find the data.
 * /*from w  w w. j  av a  2 s .  c om*/
 */
public static String findString(Document document, String xpath) {
    try {
        return (String) _xpath.evaluate(xpath, document, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static NodeList getNodesByXPath(Node node, String xpathStr) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr;/* w  w  w  .j ava2  s  .  c  om*/
    try {
        expr = xpath.compile(xpathStr);
        NodeList favoris = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
        return favoris;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Evaluate xpath.//from  w  w w . j  a  v a  2  s.  c  o  m
 * 
 * @param doc
 *            the doc
 * @param expr
 *            the expr
 * @return the object
 */
public static NodeList evaluateXpath(Document doc, XPathExpression expr) {
    NodeList XmlResult = null;
    try {
        XmlResult = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return XmlResult;
}

From source file:Main.java

public static String getNodeCount(String nodeName, String xmlString) {
    String response = "";
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {/*from  ww w .j ava  2  s  .c om*/
        XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")");
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlString));
        response = xPathExpression.evaluate(is);

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return response;
}

From source file:Main.java

public static synchronized boolean deleteNode(String charCode, Node root) {
    try {//  ww w. j a va2 s  .c  om
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node node = (Node) xpath.evaluate(String.format(XPATH_EVAL_ID, charCode), root, XPathConstants.NODE);
        if (node != null) { // remove existing node
            Node parent = node.getParentNode();
            parent.removeChild(node);
            return true;
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return false;
}