Example usage for org.dom4j Node getPath

List of usage examples for org.dom4j Node getPath

Introduction

In this page you can find the example usage for org.dom4j Node getPath.

Prototype

String getPath();

Source Link

Document

Returns the XPath expression which will return a node set containing the given node such as /a/b/@c.

Usage

From source file:org.pentaho.di.ui.trans.steps.getxmldata.GetXMLDataDialog.java

License:Open Source License

@SuppressWarnings("unchecked")
private void setNodeField(Node node, String realXPathCleaned) {
    Element e = (Element) node;
    // get all attributes
    List<Attribute> lista = e.attributes();
    for (int i = 0; i < lista.size(); i++) {
        setAttributeField(lista.get(i), realXPathCleaned);
    }//from  ww w  .j  av a2 s.c  om

    // Get Node Name
    String nodename = node.getName();
    String nodenametxt = cleanString(node.getPath(), realXPathCleaned);

    if (!Const.isEmpty(nodenametxt) && !list.contains(nodenametxt)) {
        TableItem item = new TableItem(wFields.table, SWT.NONE);
        item.setText(1, nodename);
        item.setText(2, nodenametxt);
        item.setText(3, GetXMLDataField.ElementTypeDesc[0]);

        // Get Node value
        String valueNode = node.getText();

        // Try to get the Type
        if (IsDate(valueNode)) {
            item.setText(4, "Date");
            item.setText(5, "yyyy/MM/dd");
        } else if (IsInteger(valueNode))
            item.setText(4, "Integer");
        else if (IsNumber(valueNode))
            item.setText(4, "Number");
        else
            item.setText(4, "String");

        list.add(nodenametxt);

    } // end if
}

From source file:org.pentaho.di.ui.trans.steps.getxmldata.LoopNodesImportProgressDialog.java

License:Apache License

@SuppressWarnings("unchecked")
private String[] doScan(IProgressMonitor monitor) throws Exception {
    monitor.beginTask(// w  w w  .  j  ava  2  s.  c om
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ScanningFile", filename),
            1);

    SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
    monitor.worked(1);
    if (monitor.isCanceled()) {
        return null;
    }
    // Validate XML against specified schema?
    if (meta.isValidating()) {
        reader.setValidation(true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
    } else {
        // Ignore DTD
        reader.setEntityResolver(new IgnoreDTDEntityResolver());
    }
    monitor.worked(1);
    monitor.beginTask(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingDocument"), 1);
    if (monitor.isCanceled()) {
        return null;
    }
    InputStream is = null;
    try {
        Document document = null;
        if (!Utils.isEmpty(filename)) {
            is = KettleVFS.getInputStream(filename);
            document = reader.read(is, encoding);
        } else {
            if (!Utils.isEmpty(xml)) {
                document = reader.read(new StringReader(xml));
            } else {
                document = reader.read(new URL(url));
            }
        }
        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.DocumentOpened"), 1);
        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingNode"), 1);

        if (monitor.isCanceled()) {
            return null;
        }
        List<Node> nodes = document.selectNodes(document.getRootElement().getName());
        monitor.worked(1);
        monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes"));

        if (monitor.isCanceled()) {
            return null;
        }
        for (Node node : nodes) {
            if (monitor.isCanceled()) {
                return null;
            }
            if (!listpath.contains(node.getPath())) {
                nr++;
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr)));
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode", node.getPath()));
                listpath.add(node.getPath());
                addLoopXPath(node, monitor);
            }
        }
        monitor.worked(1);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) { /* Ignore */
        }
    }
    String[] list_xpath = listpath.toArray(new String[listpath.size()]);

    monitor.setTaskName(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.NodesReturned"));

    monitor.done();

    return list_xpath;

}

From source file:org.pentaho.di.ui.trans.steps.getxmldata.LoopNodesImportProgressDialog.java

License:Apache License

private void addLoopXPath(Node node, IProgressMonitor monitor) {
    Element ce = (Element) node;
    monitor.worked(1);//from   w w  w.ja  v a  2 s.  co  m
    // List child
    for (int j = 0; j < ce.nodeCount(); j++) {
        if (monitor.isCanceled()) {
            return;
        }
        Node cnode = ce.node(j);

        if (!Utils.isEmpty(cnode.getName())) {
            Element cce = (Element) cnode;
            if (!listpath.contains(cnode.getPath())) {
                nr++;
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr)));
                monitor.subTask(BaseMessages.getString(PKG,
                        "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode", cnode.getPath()));
                listpath.add(cnode.getPath());
            }
            // let's get child nodes
            if (cce.nodeCount() > 1) {
                addLoopXPath(cnode, monitor);
            }
        }
    }
}

From source file:org.pentaho.di.ui.trans.steps.getxmldata.XMLInputFieldsImportProgressDialog.java

License:Apache License

@SuppressWarnings("unchecked")
private RowMetaAndData[] doScan(IProgressMonitor monitor) throws Exception {
    monitor.beginTask(/*from   w  ww.java  2  s .c om*/
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ScanningFile", filename),
            1);

    SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
    monitor.worked(1);
    if (monitor.isCanceled()) {
        return null;
    }
    // Validate XML against specified schema?
    if (meta.isValidating()) {
        reader.setValidation(true);
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
    } else {
        // Ignore DTD
        reader.setEntityResolver(new IgnoreDTDEntityResolver());
    }
    monitor.worked(1);
    monitor.beginTask(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingDocument"), 1);
    if (monitor.isCanceled()) {
        return null;
    }
    InputStream is = null;
    try {

        Document document = null;
        if (!Utils.isEmpty(filename)) {
            is = KettleVFS.getInputStream(filename);
            document = reader.read(is, encoding);
        } else {
            if (!Utils.isEmpty(xml)) {
                document = reader.read(new StringReader(xml));
            } else {
                document = reader.read(new URL(url));
            }
        }

        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.DocumentOpened"), 1);
        monitor.worked(1);
        monitor.beginTask(
                BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingNode"), 1);

        if (monitor.isCanceled()) {
            return null;
        }
        List<Node> nodes = document.selectNodes(this.loopXPath);
        monitor.worked(1);
        monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes"));

        if (monitor.isCanceled()) {
            return null;
        }
        for (Node node : nodes) {
            if (monitor.isCanceled()) {
                return null;
            }

            nr++;
            monitor.subTask(BaseMessages.getString(PKG,
                    "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr)));
            monitor.subTask(BaseMessages.getString(PKG,
                    "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", node.getPath()));
            setNodeField(node, monitor);
            childNode(node, monitor);

        }
        monitor.worked(1);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) { /* Ignore */
        }
    }

    RowMetaAndData[] listFields = fieldsList.toArray(new RowMetaAndData[fieldsList.size()]);

    monitor.setTaskName(
            BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.NodesReturned"));

    monitor.done();

    return listFields;

}

From source file:org.pentaho.di.ui.trans.steps.getxmldata.XMLInputFieldsImportProgressDialog.java

License:Apache License

@SuppressWarnings("unchecked")
private void setNodeField(Node node, IProgressMonitor monitor) {
    Element e = (Element) node;
    // get all attributes
    List<Attribute> lista = e.attributes();
    for (int i = 0; i < lista.size(); i++) {
        setAttributeField(lista.get(i), monitor);
    }//from w  ww. j  a  va  2s.co m

    // Get Node Name
    String nodename = node.getName();
    String nodenametxt = cleanString(node.getPath());

    if (!Utils.isEmpty(nodenametxt) && !list.contains(nodenametxt)) {
        nr++;
        monitor.subTask(BaseMessages.getString(PKG,
                "GetXMLDataXMLInputFieldsImportProgressDialog.Task.FetchFields", String.valueOf(nr)));
        monitor.subTask(BaseMessages.getString(PKG,
                "GetXMLDataXMLInputFieldsImportProgressDialog.Task.AddingField", nodename));

        RowMetaAndData row = new RowMetaAndData();
        row.addValue(VALUE_NAME, Value.VALUE_TYPE_STRING, nodename);
        row.addValue(VALUE_PATH, Value.VALUE_TYPE_STRING, nodenametxt);
        row.addValue(VALUE_ELEMENT, Value.VALUE_TYPE_STRING, GetXMLDataField.ElementTypeDesc[0]);
        row.addValue(VALUE_RESULT, Value.VALUE_TYPE_STRING, GetXMLDataField.ResultTypeDesc[0]);

        // Get Node value
        String valueNode = node.getText();

        // Try to get the Type

        if (IsDate(valueNode)) {
            row.addValue(VALUE_TYPE, Value.VALUE_TYPE_STRING, "Date");
            row.addValue(VALUE_FORMAT, Value.VALUE_TYPE_STRING, "yyyy/MM/dd");
        } else if (IsInteger(valueNode)) {
            row.addValue(VALUE_TYPE, Value.VALUE_TYPE_STRING, "Integer");
            row.addValue(VALUE_FORMAT, Value.VALUE_TYPE_STRING, null);
        } else if (IsNumber(valueNode)) {
            row.addValue(VALUE_TYPE, Value.VALUE_TYPE_STRING, "Number");
            row.addValue(VALUE_FORMAT, Value.VALUE_TYPE_STRING, null);
        } else {
            row.addValue(VALUE_TYPE, Value.VALUE_TYPE_STRING, "String");
            row.addValue(VALUE_FORMAT, Value.VALUE_TYPE_STRING, null);
        }
        fieldsList.add(row);
        list.add(nodenametxt);

    } // end if
}

From source file:org.pentaho.platform.engine.services.ActionSequenceJCRHelper.java

License:Open Source License

public void localizeDoc(final Node document, final RepositoryFile file) {
    String fileName = file.getName();
    int dotIndex = fileName.indexOf('.');
    String baseName = fileName.substring(0, dotIndex);
    // TODO read in nodes from the locale file and use them to override the
    // ones in the main document
    try {/*from   w w  w.ja  v  a 2s. co  m*/
        List nodes = document.selectNodes("descendant::*"); //$NON-NLS-1$
        Iterator nodeIterator = nodes.iterator();
        while (nodeIterator.hasNext()) {
            Node node = (Node) nodeIterator.next();
            String name = node.getText();
            if (name.startsWith("%") && !node.getPath().endsWith("/text()")) { //$NON-NLS-1$ //$NON-NLS-2$
                try {
                    String localeText = getLocaleString(name, baseName, file, true);
                    if (localeText != null) {
                        node.setText(localeText);
                    }
                } catch (Exception e) {
                    logger.warn(Messages.getInstance().getString(
                            "ActionSequenceJCRHelper.WARN_MISSING_RESOURCE_PROPERTY", name.substring(1), //$NON-NLS-1$
                            baseName, getLocale().toString()));
                }
            }
        }
    } catch (Exception e) {
        logger.error(Messages.getInstance().getErrorString(
                "ActionSequenceJCRHelper.ERROR_0007_COULD_NOT_READ_PROPERTIES", file.getPath()), e); //$NON-NLS-1$
    }
}

From source file:org.pentaho.platform.repository.solution.SolutionRepositoryBase.java

License:Open Source License

public void localizeDoc(final Node document, final ISolutionFile file) {
    String fileName = file.getFileName();
    int dotIndex = fileName.indexOf('.');
    String baseName = fileName.substring(0, dotIndex);
    // TODO read in nodes from the locale file and use them to override the
    // ones in the main document
    try {/*from www  .ja  v a 2s  .  c  o m*/
        List nodes = document.selectNodes("descendant::*"); //$NON-NLS-1$
        Iterator nodeIterator = nodes.iterator();
        while (nodeIterator.hasNext()) {
            Node node = (Node) nodeIterator.next();
            String name = node.getText();
            if (name.startsWith("%") && !node.getPath().endsWith("/text()")) { //$NON-NLS-1$ //$NON-NLS-2$
                try {
                    String localeText = getLocaleString(name, baseName, file, true);
                    if (localeText != null) {
                        node.setText(localeText);
                    }
                } catch (Exception e) {
                    warn(Messages.getString("SolutionRepository.WARN_MISSING_RESOURCE_PROPERTY", //$NON-NLS-1$
                            name.substring(1), baseName, getLocale().toString()));
                }
            }
        }
    } catch (Exception e) {
        error(Messages.getErrorString("SolutionRepository.ERROR_0007_COULD_NOT_READ_PROPERTIES", //$NON-NLS-1$
                file.getFullPath()), e);
    }
}

From source file:org.talend.mdm.webapp.browserecords.server.util.DownloadUtil.java

License:Open Source License

public static String getJoinFieldValue(Document doc, String xPath, int count) {
    if (doc.getRootElement().elements().size() > count) {
        Node node = (Node) doc.getRootElement().elements().get(count);
        String joinConcept = xPath.substring(0, xPath.indexOf("/")); //$NON-NLS-1$
        String joinFieldXpath = xPath.replaceFirst(joinConcept + "/", "/result/"); //$NON-NLS-1$//$NON-NLS-2$
        if (node.getPath().equalsIgnoreCase(joinFieldXpath)) {
            return node.getText();
        }/*from  ww w.  j  a v a  2s  .  co m*/
    }
    return ""; //$NON-NLS-1$
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleStyler.java

License:Open Source License

/**
 *  Sets style for the Element. Uses XPath expressions ("pattern" style
 *  property) and XML Node corresponding to the Element.
 *//*from  w w  w.  j  a  va 2s  . c om*/
public void applyStyle(FBSimpleElement element) {
    Node node = element.getNode();
    for (String pattern : patternList) {
        if (node.matches(pattern)) {
            element.setAttributeSet(patternToStyle.get(pattern));
            if (log.isDebugEnabled()) {
                log.debug(node.getPath() + " <- " + patternToStyle.get(pattern).getName());
            }
            break;
        }
    }
}