Example usage for org.jdom2.xpath XPathExpression evaluateFirst

List of usage examples for org.jdom2.xpath XPathExpression evaluateFirst

Introduction

In this page you can find the example usage for org.jdom2.xpath XPathExpression evaluateFirst.

Prototype

public T evaluateFirst(Object context);

Source Link

Document

Return the first value in the XPath query result set type-cast to the return type of this XPathExpression.

Usage

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public Element selectSingleNode(String xpath, XmlNamespaceManager nsmgr) {
    XPathFactory factory = XPathFactory.instance();
    XPathExpression<Element> expr = factory.compile(xpath, Filters.element(), null, nsmgr.getNamespaceList());
    return expr.evaluateFirst(_element);
}

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public Element selectSingleNode(String xpath) {
    XPathFactory factory = XPathFactory.instance();
    XPathExpression<Element> expr = factory.compile(xpath, Filters.element());
    return expr.evaluateFirst(_element);
}

From source file:ataraxis.passwordmanager.XMLHandler.java

License:Open Source License

/**
 * Delete the Element with the ID ElementID and all sub-Elements
 *
 * @param ElementID ID of Element //ww  w  . jav  a2  s  . co m
 * @throws JDOMException if Element does not exist
 */
private void deleteElement(String ElementID) throws JDOMException {
    XPathExpression<Element> xpath = XPathFactory.instance().compile("//*[@id='" + ElementID + "']",
            Filters.element());
    Element el = xpath.evaluateFirst(s_doc);

    if (el != null) {
        el.detach();
    } else {
        throw new JDOMException("Element not Found");
    }
}

From source file:ataraxis.passwordmanager.XMLHandler.java

License:Open Source License

/**
 * Returns true if the Element exist, false otherwise
 * //from ww w  .  ja va2s .  com
 * @param ElementID the ID of the Element
 * @return true if exist, false otherwise
 */
public boolean existID(String EntryID) {
    boolean exist = false;

    try {
        XPathExpression<Element> xpath = XPathFactory.instance().compile("//*[@id='" + EntryID + "']",
                Filters.element());
        Element tempElement = xpath.evaluateFirst(s_doc);

        if (tempElement != null)
            exist = true;
    } catch (Exception e) {
        logger.debug("Element " + EntryID + " NOT found");
    }

    return exist;
}

From source file:ataraxis.passwordmanager.XMLHandler.java

License:Open Source License

/**
 * Check if ElementID is a Element of type group
 * /*from w w w  .j  a  v a2 s  .co  m*/
 * @param ElementID the ID of the Element
 * @return true if it is a group, false otherwise
 */
public boolean isGroupElement(String ElementID) {
    boolean isGroupElement = false;

    try {
        XPathExpression<Element> xpath = XPathFactory.instance().compile("//group[@id='" + ElementID + "']",
                Filters.element());
        Element tempElement = xpath.evaluateFirst(s_doc);

        if (tempElement != null) {
            isGroupElement = true;
        }
    } catch (Exception e) {
        logger.debug("Element " + ElementID + " NOT found");
    }

    return isGroupElement;
}

From source file:ataraxis.passwordmanager.XMLHandler.java

License:Open Source License

/**
 * Check if the ElementId has Child-Elements.
 * //w  w w  .ja  v  a2s  . c  o  m
 * @param ElementID
 * @return true if Child's exist, false otherwise
 */
public boolean hasChilds(String ElementID) {
    boolean hasChilds = false;

    if (ElementID == null || ElementID.equals("")) {
        hasChilds = false;
    } else {
        XPathExpression<Element> xpath = XPathFactory.instance().compile("//group[@id='" + ElementID + "']",
                Filters.element());
        Element tempElement = xpath.evaluateFirst(s_doc);

        if (tempElement == null) {
            hasChilds = false;
        } else {
            List<Element> childList = tempElement.getChildren();
            hasChilds = (childList.size() > 0);
        }
    }

    return hasChilds;
}

From source file:ataraxis.passwordmanager.XMLHandler.java

License:Open Source License

/**
 * Return the Element with the ID ElementID
 *
 * @param ElementID the ID of the Element (group or account)
 * @throws JDOMException by problems with jdom
 *//*  w ww  . j ava 2  s.c  o  m*/
private Element getElement(String ElementID) throws JDOMException {
    Element returnElement;

    XPathExpression<Element> xpath = XPathFactory.instance().compile("//*[@id='" + ElementID + "']",
            Filters.element());
    Element tempElement = xpath.evaluateFirst(s_doc);

    if (tempElement != null) {
        returnElement = tempElement;
    } else {
        throw new JDOMException("Element not Found");
    }
    return returnElement;
}

From source file:ca.nrc.cadc.vosi.avail.CheckWebService.java

License:Open Source License

void checkReturnedXml(String strXml) throws CheckException {
    Document doc;//from w  w w. j av  a2  s .  com
    String xpathStr;

    try {
        StringReader reader = new StringReader(strXml);
        doc = XmlUtil.buildDocument(reader, schemaMap);

        //get namespace and/or prefix from Document, then create xpath based on the prefix
        String nsp = doc.getRootElement().getNamespacePrefix(); //Namespace Prefix
        if (nsp != null && nsp.length() > 0)
            nsp = nsp + ":";
        else
            nsp = "";
        xpathStr = "/" + nsp + "availability/" + nsp + "available";
        XPathBuilder<Element> builder = new XPathBuilder<Element>(xpathStr, Filters.element());
        Namespace ns = Namespace.getNamespace(VOSI.NS_PREFIX, VOSI.AVAILABILITY_NS_URI);
        builder.setNamespace(ns);
        XPathExpression<Element> xpath = builder.compileWith(XPathFactory.instance());
        Element eleAvail = xpath.evaluateFirst(doc);
        log.debug(eleAvail);
        String textAvail = eleAvail.getText();

        // TODO: is this is actually valid? is the content not constrained by the schema?
        if (textAvail == null)
            throw new CheckException(wsURL + " output is invalid: no content in <available> element", null);

        if (!textAvail.equalsIgnoreCase("true")) {
            xpathStr = "/" + nsp + "availability/" + nsp + "note";
            builder = new XPathBuilder<Element>(xpathStr, Filters.element());
            builder.setNamespace(ns);
            xpath = builder.compileWith(XPathFactory.instance());
            Element eleNotes = xpath.evaluateFirst(doc);

            String textNotes = eleNotes.getText();
            throw new CheckException("service " + wsURL + " is not available, reported reason: " + textNotes,
                    null);
        }
    } catch (IOException e) {
        // probably an incorrect setup or bug in the checks
        throw new RuntimeException("failed to test " + wsURL, e);
    } catch (JDOMException e) {
        throw new CheckException(wsURL + " output is invalid", e);
    }
}

From source file:cager.parser.test.SimpleTest2.java

License:Open Source License

private void XMLtoJavaParser() {

    // Creamos el builder basado en SAX  
    SAXBuilder builder = new SAXBuilder();

    try {//ww  w.ja v a  2s.  c  o m

        String nombreMetodo = null;
        List<String> atributos = new ArrayList<String>();
        String tipoRetorno = null;
        String nombreArchivo = null;
        String exportValue = "";
        String codigoFuente = "";
        HashMap<String, String> tipos = new HashMap<String, String>();

        // Construimos el arbol DOM a partir del fichero xml  
        Document doc = builder.build(new FileInputStream(ruta + "VBParser\\ejemplosKDM\\archivo.kdm"));
        //Document doc = builder.build(new FileInputStream("E:\\WorkspaceParser\\VBParser\\ejemplosKDM\\archivo.kdm"));    

        Namespace xmi = Namespace.getNamespace("xmi", "http://www.omg.org/XMI");

        XPathExpression<Element> xpath = XPathFactory.instance().compile("//codeElement", Filters.element());

        List<Element> elements = xpath.evaluate(doc);

        for (Element emt : elements) {

            if (emt.getAttribute("type", xmi).getValue().compareTo("code:CompilationUnit") == 0) {

                nombreArchivo = emt.getAttributeValue("name").substring(0,
                        emt.getAttributeValue("name").indexOf('.'));

            }

            if (emt.getAttribute("type", xmi).getValue().compareTo("code:LanguageUnit") == 0) {

                List<Element> hijos = emt.getChildren();

                for (Element hijo : hijos) {

                    tipos.put(hijo.getAttributeValue("id", xmi), hijo.getAttributeValue("name"));

                }

            }
        }

        FileOutputStream fout;

        fout = new FileOutputStream(ruta + "VBParser\\src\\cager\\parser\\test\\" + nombreArchivo + ".java");
        //fout = new FileOutputStream("E:\\WorkspaceParser\\VBParser\\src\\cager\\parser\\test\\"+nombreArchivo+".java");           
        // get the content in bytes
        byte[] contentInBytes = null;

        contentInBytes = ("package cager.parser.test;\n\n").getBytes();

        fout.write(contentInBytes);
        fout.flush();

        contentInBytes = ("public class " + nombreArchivo + "{\n\n").getBytes();

        fout.write(contentInBytes);
        fout.flush();

        for (Element emt : elements) {
            // System.out.println("XPath has result: " + emt.getName()+" "+emt.getAttribute("type",xmi));
            if (emt.getAttribute("type", xmi).getValue().compareTo("code:MethodUnit") == 0) {

                nombreMetodo = emt.getAttribute("name").getValue();

                if (emt.getAttribute("export") != null)
                    exportValue = emt.getAttribute("export").getValue();

                atributos = new ArrayList<String>();

                List<Element> hijos = emt.getChildren();

                for (Element hijo : hijos) {

                    if (hijo.getAttribute("type", xmi) != null) {

                        if (hijo.getAttribute("type", xmi).getValue().compareTo("code:Signature") == 0) {

                            List<Element> parametros = hijo.getChildren();

                            for (Element parametro : parametros) {

                                if (parametro.getAttribute("kind") == null
                                        || parametro.getAttribute("kind").getValue().compareTo("return") != 0) {
                                    atributos.add(tipos.get(parametro.getAttribute("type").getValue()) + " "
                                            + parametro.getAttributeValue("name"));
                                } else {
                                    tipoRetorno = tipos.get(parametro.getAttribute("type").getValue());
                                }

                            }

                        }
                    } else if (hijo.getAttribute("snippet") != null) {

                        codigoFuente = hijo.getAttribute("snippet").getValue();

                    }

                }

                //System.out.println("MethodUnit!! " + emt.getName()+" "+emt.getAttribute("type",xmi)+" "+emt.getAttribute("name").getValue());
                //System.out.println(emt.getAttribute("name").getValue());

                if (tipoRetorno.compareTo("Void") == 0) {
                    tipoRetorno = "void";
                }

                contentInBytes = ("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " (")
                        .getBytes();

                fout.write(contentInBytes);
                fout.flush();
                int n = 0;
                for (String parametro : atributos) {

                    if (atributos.size() > 0 && n < atributos.size() - 1) {
                        contentInBytes = (" " + parametro + ",").getBytes();
                    } else {
                        contentInBytes = (" " + parametro).getBytes();
                    }

                    fout.write(contentInBytes);
                    fout.flush();
                    n++;
                }

                contentInBytes = (" ) {\n").getBytes();

                fout.write(contentInBytes);
                fout.flush();

                contentInBytes = ("\n/* \n " + codigoFuente + " \n */\n").getBytes();

                fout.write(contentInBytes);
                fout.flush();

                contentInBytes = ("\t}\n\n").getBytes();

                fout.write(contentInBytes);
                fout.flush();

                System.out.print("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " (");
                n = 0;
                for (String parametro : atributos) {
                    if (atributos.size() > 0 && n < atributos.size() - 1) {
                        System.out.print(" " + parametro + ", ");
                    } else {
                        System.out.print(" " + parametro);
                    }
                    n++;

                }
                System.out.println(" ) {");
                System.out.println("/* \n " + codigoFuente + " \n */");
                System.out.println("\t}\n");
            }

        }

        contentInBytes = ("}\n").getBytes();

        fout.write(contentInBytes);
        fout.flush();
        fout.close();

        XPathExpression<Attribute> xp = XPathFactory.instance().compile("//@*", Filters.attribute(xmi));
        for (Attribute a : xp.evaluate(doc)) {
            a.setName(a.getName().toLowerCase());
        }

        xpath = XPathFactory.instance().compile("//codeElement/@name='testvb.cls'", Filters.element());
        Element emt = xpath.evaluateFirst(doc);
        if (emt != null) {
            System.out.println("XPath has result: " + emt.getName());
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JDOMException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.medvision360.medrecord.basex.AbstractXmlConverter.java

License:Creative Commons License

protected <T> String xpath(org.jdom2.Document d, String p, Filter<T> filter) {
    XPathExpression<T> xpath = XPathFactory.instance().compile(p, filter);
    T value = xpath.evaluateFirst(d);
    if (value == null) {
        return null;
    }/*from  ww  w  . j a v  a2 s .c om*/
    if (value instanceof Element) {
        return ((Element) value).getValue();
    }
    if (value instanceof Attribute) {
        return ((Attribute) value).getValue();
    }
    return value.toString();
}