Example usage for org.dom4j.xpath DefaultXPath DefaultXPath

List of usage examples for org.dom4j.xpath DefaultXPath DefaultXPath

Introduction

In this page you can find the example usage for org.dom4j.xpath DefaultXPath DefaultXPath.

Prototype

public DefaultXPath(String text) throws InvalidXPathException 

Source Link

Document

Construct an XPath

Usage

From source file:br.gov.jfrj.siga.wf.tag.ProcessImageTag.java

License:Open Source License

/**
 * Extrai os limites da caixa para que seja desenhada a marcao  sua volta.
 * @param root/*ww  w.  j a v  a2 s  .c om*/
 * @return
 */
private int[] extractBoxConstraint(Element root) {
    int[] result = new int[4];
    String nodeName = currentToken.getNode().getName();
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
    result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
    result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
    result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
    return result;
}

From source file:br.gov.jfrj.siga.wf.tag.ProcessImageTag.java

License:Open Source License

/**
 * Extrai os limites da caixa para que seja desenhada a marcao  sua volta.
 * @param root//w  w w .  j  av a 2 s  .co  m
 * @param token
 * @return
 */
private int[] extractBoxConstraint(Element root, Token token) {

    int[] result = new int[4];
    String nodeName = "";
    try {
        nodeName = new String(token.getNode().getName().getBytes("ISO-8859-1"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
    result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
    result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
    result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
    return result;
}

From source file:com.collabnet.ccf.core.ga.GenericArtifactHelper.java

License:Open Source License

/**
 * Extracts the value of the supplied attribute
 * //from w ww  .j a va 2  s.c  o  m
 * @param element
 *            element with attribute in question
 * @param attributeName
 *            name of the attribute in question
 * @return value of the attribute in question
 * @throws GenericArtifactParsingException
 *             exception s thrown is attribute is missing
 */
private static String getAttributeValue(Element element, String attributeName)
        throws GenericArtifactParsingException {
    // TODO Cash constructed XPath objects?
    // XPath xpath = new DefaultXPath("@" + CCF_NAMESPACE_PREFIX + ":" +
    // attributeName);
    XPath xpath = new DefaultXPath("@" + attributeName);
    xpath.setNamespaceURIs(ccfNamespaceMap);
    Node attributeNode = xpath.selectSingleNode(element);
    if (attributeNode == null)
        throw new GenericArtifactParsingException(
                "Missing attribute: " + attributeName + " in element " + element.getName());
    else
        return attributeNode.getText();
}

From source file:com.collabnet.ccf.core.ga.GenericArtifactHelper.java

License:Open Source License

/**
 * Extracts the value of the supplied attribute without throwing an
 * exception if missing/*from ww w  . j  a v  a  2  s  . co m*/
 * 
 * @param element
 *            element with attribute in question
 * @param attributeName
 *            name of the attribute in question
 * @return value of the attribute in question, null if attribute is missing
 * 
 */
private static String getAttributeValueWithoutException(Element element, String attributeName) {
    // TODO Cash constructed XPath objects?
    // XPath xpath = new DefaultXPath("@" + CCF_NAMESPACE_PREFIX + ":" +
    // attributeName);
    XPath xpath = new DefaultXPath("@" + attributeName);
    xpath.setNamespaceURIs(ccfNamespaceMap);
    Node attributeNode = xpath.selectSingleNode(element);
    if (attributeNode == null)
        return null;
    else
        return attributeNode.getText();
}

From source file:com.collabnet.ccf.core.utils.XPathUtils.java

License:Open Source License

/**
 * Extracts the value of the supplied attribute
 * //from  w ww  .  j  a  va  2 s.c o  m
 * @param element
 *            element with attribute in question
 * @param attributeName
 *            name of the attribute in question
 * @param failIfNotFound
 *            determines if exception should be thrown if attribute is not
 *            found
 * @return value of the attribute in question, null if not found and
 *         failIfNotFound is set to false
 * @throws GenericArtifactParsingException
 *             exception thrown is attribute is missing and failIfNotFound
 *             is set to true
 */
public static String getAttributeValue(Element element, String attributeName, boolean failIfNotFound)
        throws GenericArtifactParsingException {
    XPath xpath = new DefaultXPath("@" + attributeName);
    xpath.setNamespaceURIs(ccfNamespaceMap);
    Node attributeNode = xpath.selectSingleNode(element);
    if (attributeNode == null) {
        if (failIfNotFound) {
            throw new GenericArtifactParsingException(
                    "Missing attribute: " + attributeName + " in element " + element.getName());
        } else {
            return null;
        }
    } else {
        return attributeNode.getText();
    }
}

From source file:com.devoteam.srit.xmlloader.genscript.Param.java

License:Open Source License

public String applySubstitution(String text, Msg msg) throws Exception {

    String msgAvecParametres = "";

    if (getRegle() != null) {
        String[] regexTab = getRegle().split("#");

        // Si la regle est sous forme d'expression rgulire
        if (regexTab[0].toUpperCase().contains("REGEX")) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                    "Replace parameter " + getRegle() + " => " + name);
            String[] regexRule = Arrays.copyOfRange(regexTab, 1, regexTab.length);
            msgAvecParametres = regexRemplace(regexRule, 0, text);
        }/*  w ww  .  j  a  va2  s .co  m*/
        // Si la regle est sous forme de xpath
        else if (regexTab[0].toUpperCase().contains("XPATH")) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                    "Replace parameter " + getRegle() + " => " + name);
            // Rcupration des paramtres
            String xpathRule = regexTab[1];
            String attribut = regexTab[2];

            attribut = attribut.replace("@", "");

            // Cration de l'arbre DOM correspondant au message
            SAXReader reader = new SAXReader();
            try {
                Document document = reader.read(new ByteArrayInputStream(text.getBytes("UTF-8")));
                // Cration de l'objet XPATH ) selectionner 
                XPath xpath = new DefaultXPath(xpathRule);

                // Rcupration des noeuds correspondants
                List<Node> nodes = xpath.selectNodes(document.getRootElement(), xpath);

                // Pour chaque noeuds  modifier
                Element aRemplacer = null;
                for (Node n : nodes) {
                    setRemplacedValue(n.getText());
                    if (n instanceof Element) {
                        aRemplacer = (Element) n;
                    } else {
                        aRemplacer = n.getParent();
                    }
                    String newValue = getName();
                    String oldValue = aRemplacer.attribute(attribut).getValue();
                    // On regarde si on est dans le cas de paramtres mixtes
                    if (regexTab.length > 3) {
                        if (regexTab[3].equals("REGEX")) {
                            setRemplacedValue(null);
                            String[] regexRule = Arrays.copyOfRange(regexTab, 4, regexTab.length);
                            newValue = regexRemplace(regexRule, 0, oldValue);
                        }
                    }
                    aRemplacer.setAttributeValue(attribut, newValue);
                }

                // Convertion en chane de caractre de l'arbre DOM du message
                msgAvecParametres = document.getRootElement().asXML();
            } catch (Exception e) {

            }
        }
        // si la rgle est sous forme de pathkey
        else if (regexTab[0].toUpperCase().contains("PATHKEY")) {

            String valeurARemplacer = null;
            // On rcupre la valeur  remplacer
            String pathKeyWord = regexTab[1];
            if (msg != null) {
                Parameter valeurParamARemplacer = msg.getParameter(pathKeyWord);
                if (valeurParamARemplacer.length() > 0) {
                    valeurARemplacer = valeurParamARemplacer.get(0).toString();
                }

                // On remplace dans tout le message par le parametre
                if (valeurARemplacer != null) {
                    msgAvecParametres = text.replace(valeurARemplacer, getName());
                    if (!msgAvecParametres.equals(text)) {
                        GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                                "Replace parameter " + valeurARemplacer + " => " + name);
                        setRemplacedValue(valeurARemplacer);
                    }
                }
            }
        }
    }
    // Si le message n'a pas subit de modification, on retourne null
    if (!isUsed()) {
        return null;
    }

    // Sinon on retourne le message modifi avec les paramtres
    return msgAvecParametres;
}

From source file:com.eurelis.tools.xml.transformation.Main.java

License:Open Source License

/**
 * @param args//from   w  w  w.  j av a 2s.c  om
 */
public static void main(String[] args) {

    LocalDocumentFeeder ldf = new LocalDocumentFeeder();
    ldf.addLocalDocumentSource(
            new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xml/old/articles/batteries-li-ion.xml"),
            false);

    XMLTransformationBuilder xtb = XMLTransformationBuilder.newInstance();

    //xtb.addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/article.xsd")));
    /*
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/opencms-xmlcontent.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/advanced-link-list.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/intro-media.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/paragraph.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/video.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/paragraph-column.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/paragraph-column-element.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/paragraph-column-elementtype.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/quote.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/nested/image-hd-link.xsd")))
       .addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/new/generic-nested/advanced-link.xsd")));
    */

    //xtb.addValidationSource(new StreamSource(new File("/Users/eurelis/Downloads/eurelis-xmltransormation/xsd/test.xsd")));

    UnitaryTransformationBuilder utb = xtb.newUnitaryTransformationBuilder();
    utb.setSource(new DefaultXPath(
            "/Articles/Article/Paragraph/Column/Element/ElementType/AdvancedLinksList/Link/Resource/Resource"));
    utb.setXPathDestination(
            "/Articles/Article/Paragraph/Column/Element/ElementType/AdvancedLinksList/Link/Resource");

    UnitaryTransformationBuilder utb2 = xtb.newUnitaryTransformationBuilder();
    utb2.setSource(new DefaultXPath(
            "/Articles/Article/Paragraph/Column/Element/ElementType/AdvancedLinksList/Link/Resource/Pointer"));
    utb2.setXPathDestination(
            "/Articles/Article/Paragraph/Column/Element/ElementType/AdvancedLinksList/Link/Resource");

    UnitaryTransformationBuilder utb3 = xtb.newUnitaryTransformationBuilder();
    utb3.setSource(new DefaultXPath("/Articles/Article/Headline/@name"));
    utb3.setXPathDestination("/Articles/Article/Headline/@nom");

    UnitaryTransformationBuilder utb4 = xtb.newUnitaryTransformationBuilder();
    utb4.setSource(new DefaultXPath(
            "/Articles/Article[@language='en']/Paragraph/Column/Element/ElementType/Quote/Label"));
    utb4.setSXPathDestination(
            "/Articles/Article[@language='en']/Paragraph/Column/Element/ElementType/Quote2/Label[@language='en' and @test='true']/victoire");

    UnitaryTransformationBuilder utb5 = xtb.newUnitaryTransformationBuilder();
    utb5.setSource(
            new DefaultXPath("/Articles/Article[@language='fr']/Paragraph/Column/Element/ElementType/Quote"));
    utb5.setXPathDestination("/Articles/Article[@language='fr']/Paragraph/Column/Element/ElementType");
    TemplateTransformationBuilder ttb = utb5.getTemplateTransformationBuilder();
    try {
        ttb.setTemplate("<MyQuote><MyLabel>$label</MyLabel><Author>$name $function</Author></MyQuote>");
        ttb.addParameter("label", new DefaultXPath("Label/text()"));
        ttb.addParameter("name", new DefaultXPath("Name/text()"));
        ttb.addParameter("function", new DefaultXPath("Function/text()"));
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    XMLTransformation xmlTrans = xtb.build();

    BatchProcessor batchProcessor = new BatchProcessor(ldf, xmlTrans);
    List<Journal> journalList = batchProcessor.run(false);

    for (Journal journal : journalList) {
        System.out.println("----");

        for (JournalEntry je : journal.getEntries(EntryKind.WARNING)) {
            System.out.println(je.description());

        }

    }

}

From source file:com.glaf.jbpm.tag.JbpmProcessImageTag.java

License:Apache License

private int[] extractBoxConstraint(Element root) {
    int[] result = new int[4];
    String nodeName = currentToken.getNode().getName();
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    if (node != null) {
        result[0] = Integer.parseInt(node.attribute("x").getValue());
        result[1] = Integer.parseInt(node.attribute("y").getValue());
        result[2] = Integer.parseInt(node.attribute("width").getValue());
        result[3] = Integer.parseInt(node.attribute("height").getValue());
    }//from w  w w.j ava  2  s.  com
    return result;
}

From source file:com.glaf.jbpm.tag.JbpmProcessImageTag.java

License:Apache License

private int[] extractBoxConstraint(Element root, Token token) {
    int[] result = new int[4];
    String nodeName = token.getNode().getName();
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    if (node != null) {
        result[0] = Integer.parseInt(node.attribute("x").getValue());
        result[1] = Integer.parseInt(node.attribute("y").getValue());
        result[2] = Integer.parseInt(node.attribute("width").getValue());
        result[3] = Integer.parseInt(node.attribute("height").getValue());
    }/* ww w  .jav a2 s.  co  m*/
    return result;
}

From source file:com.surenpi.autotest.suite.parser.XmlSuiteParser.java

License:Apache License

/**
 * ??//from  ww  w.  j a  v  a 2  s  . com
 * @param suiteInputStream ??
 * @return 
 * @throws DocumentException
 */
public Suite parse(InputStream suiteInputStream) throws DocumentException {
    SAXReader reader = new SAXReader();
    reader.setEncoding("utf-8");

    Document document = reader.read(suiteInputStream);

    simpleNamespaceContext.addNamespace("ns", NS_URI);

    XPath xpath = new DefaultXPath("/ns:suite");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element suiteEle = (Element) xpath.selectSingleNode(document);
    if (suiteEle == null) {
        suiteEle = document.getRootElement();
        //         throw new RuntimeException("Can not found suite config.");
    }

    Suite suite = new Suite();
    String xmlConfPath = suiteEle.attributeValue("pageConfig");
    String pagePackage = suiteEle.attributeValue("pagePackage", "");
    String rows = suiteEle.attributeValue("rows", "1");
    String lackLines = suiteEle.attributeValue("lackLines", "nearby");
    String errorLines = suiteEle.attributeValue("errorLines", "stop");
    String afterSleep = suiteEle.attributeValue("afterSleep", "0");

    suite.setXmlConfPath(xmlConfPath);
    suite.setPagePackage(pagePackage);
    suite.setRows(rows);
    suite.setLackLines(lackLines);
    suite.setErrorLines(errorLines);
    suite.setAfterSleep(Long.parseLong(afterSleep));

    pagesParse(document, suite);

    return suite;
}