Example usage for org.dom4j Node createXPath

List of usage examples for org.dom4j Node createXPath

Introduction

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

Prototype

XPath createXPath(String xpathExpression) throws InvalidXPathException;

Source Link

Document

createXPath creates an XPath object for the given xpathExpression.

Usage

From source file:org.infoglue.cms.util.dom.DOMBuilder.java

License:Open Source License

/**
 * A helper method to get nodes that has a namespace.
 *///from  www  . j  av a2  s  . c o  m

public Node selectSingleNode(Node contextNode, String xpathExpression, String namespaceName,
        String namespaceValue) {
    Map namespaces = new HashMap();
    namespaces.put(namespaceName, namespaceValue);
    org.dom4j.XPath xpath = contextNode.createXPath(xpathExpression);
    xpath.setNamespaceURIs(namespaces);
    return xpath.selectSingleNode(contextNode);
}

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

License:Apache License

private Object[] processPutRow(Node node) throws KettleException {
    // Create new row...
    Object[] outputRowData = buildEmptyRow();

    // Create new row or clone
    if (meta.isInFields()) {
        System.arraycopy(data.readrow, 0, outputRowData, 0, data.nrReadRow);
    }/*from   www .  jav  a 2s  .c  o  m*/
    try {
        data.nodenr++;

        // Read fields...
        for (int i = 0; i < data.nrInputFields; i++) {
            // Get field
            GetXMLDataField xmlDataField = meta.getInputFields()[i];
            // Get the Path to look for
            String XPathValue = xmlDataField.getResolvedXPath();

            if (meta.isuseToken()) {
                // See if user use Token inside path field
                // The syntax is : @_Fieldname-
                // PDI will search for Fieldname value and replace it
                // Fieldname must be defined before the current node
                XPathValue = substituteToken(XPathValue, outputRowData);
                if (isDetailed()) {
                    logDetailed(XPathValue);
                }
            }

            // Get node value
            String nodevalue;

            // Handle namespaces
            if (meta.isNamespaceAware()) {
                XPath xpathField = node.createXPath(addNSPrefix(XPathValue, data.PathValue));
                xpathField.setNamespaceURIs(data.NAMESPACE);
                if (xmlDataField.getResultType() == GetXMLDataField.RESULT_TYPE_VALUE_OF) {
                    nodevalue = xpathField.valueOf(node);
                } else {
                    // nodevalue=xpathField.selectSingleNode(node).asXML();
                    Node n = xpathField.selectSingleNode(node);
                    if (n != null) {
                        nodevalue = n.asXML();
                    } else {
                        nodevalue = "";
                    }
                }
            } else {
                if (xmlDataField.getResultType() == GetXMLDataField.RESULT_TYPE_VALUE_OF) {
                    nodevalue = node.valueOf(XPathValue);
                } else {
                    // nodevalue=node.selectSingleNode(XPathValue).asXML();
                    Node n = node.selectSingleNode(XPathValue);
                    if (n != null) {
                        nodevalue = n.asXML();
                    } else {
                        nodevalue = "";
                    }
                }
            }

            // Do trimming
            switch (xmlDataField.getTrimType()) {
            case GetXMLDataField.TYPE_TRIM_LEFT:
                nodevalue = Const.ltrim(nodevalue);
                break;
            case GetXMLDataField.TYPE_TRIM_RIGHT:
                nodevalue = Const.rtrim(nodevalue);
                break;
            case GetXMLDataField.TYPE_TRIM_BOTH:
                nodevalue = Const.trim(nodevalue);
                break;
            default:
                break;
            }

            // Do conversions
            //
            ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta(data.totalpreviousfields + i);
            ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta(data.totalpreviousfields + i);
            outputRowData[data.totalpreviousfields + i] = targetValueMeta.convertData(sourceValueMeta,
                    nodevalue);

            // Do we need to repeat this field if it is null?
            if (meta.getInputFields()[i].isRepeated()) {
                if (data.previousRow != null && Utils.isEmpty(nodevalue)) {
                    outputRowData[data.totalpreviousfields + i] = data.previousRow[data.totalpreviousfields
                            + i];
                }
            }
        } // End of loop over fields...

        int rowIndex = data.totalpreviousfields + data.nrInputFields;

        // See if we need to add the filename to the row...
        if (meta.includeFilename() && !Utils.isEmpty(meta.getFilenameField())) {
            outputRowData[rowIndex++] = data.filename;
        }
        // See if we need to add the row number to the row...
        if (meta.includeRowNumber() && !Utils.isEmpty(meta.getRowNumberField())) {
            outputRowData[rowIndex++] = data.rownr;
        }
        // Possibly add short filename...
        if (meta.getShortFileNameField() != null && meta.getShortFileNameField().length() > 0) {
            outputRowData[rowIndex++] = data.shortFilename;
        }
        // Add Extension
        if (meta.getExtensionField() != null && meta.getExtensionField().length() > 0) {
            outputRowData[rowIndex++] = data.extension;
        }
        // add path
        if (meta.getPathField() != null && meta.getPathField().length() > 0) {
            outputRowData[rowIndex++] = data.path;
        }
        // Add Size
        if (meta.getSizeField() != null && meta.getSizeField().length() > 0) {
            outputRowData[rowIndex++] = data.size;
        }
        // add Hidden
        if (meta.isHiddenField() != null && meta.isHiddenField().length() > 0) {
            outputRowData[rowIndex++] = Boolean.valueOf(data.path);
        }
        // Add modification date
        if (meta.getLastModificationDateField() != null && meta.getLastModificationDateField().length() > 0) {
            outputRowData[rowIndex++] = data.lastModificationDateTime;
        }
        // Add Uri
        if (meta.getUriField() != null && meta.getUriField().length() > 0) {
            outputRowData[rowIndex++] = data.uriName;
        }
        // Add RootUri
        if (meta.getRootUriField() != null && meta.getRootUriField().length() > 0) {
            outputRowData[rowIndex] = data.rootUriName;
        }

        RowMetaInterface irow = getInputRowMeta();

        if (irow == null) {
            data.previousRow = outputRowData;
        } else {
            // clone to previously allocated array to make sure next step doesn't
            // change it in between...
            System.arraycopy(outputRowData, 0, this.prevRow, 0, outputRowData.length);
            // Pick up everything else that needs a real deep clone
            data.previousRow = irow.cloneRow(outputRowData, this.prevRow);
        }
    } catch (Exception e) {
        if (getStepMeta().isDoingErrorHandling()) {
            // Simply add this row to the error row
            putError(data.outputRowMeta, outputRowData, 1, e.toString(), null, "GetXMLData001");
            data.errorInRowButContinue = true;
            return null;
        } else {
            logError(e.toString());
            throw new KettleException(e.toString());
        }
    }
    return outputRowData;
}

From source file:org.soyatec.windowsazure.internal.util.xml.XPathQueryHelper.java

License:Apache License

/**
 * Select the service xml//from w  w  w .  j  av a2  s  .c o m
 * 
 * @param doc
 * @param path
 * @return a list
 */
@SuppressWarnings("unchecked")
static List selectNodes(final Node doc, String path) {
    Map xmlMap = new HashMap();
    xmlMap.put(XMLNS, ServiceManagementConstants.ServiceManagementNS);
    XPath x = doc.createXPath(path);
    x.setNamespaceURIs(xmlMap);
    return x.selectNodes(doc);
}

From source file:org.soyatec.windowsazure.internal.util.xml.XPathQueryHelper.java

License:Apache License

/**
 * Select single node//  w w w  .ja  v a  2 s . c o  m
 * 
 * @param doc
 * @param path
 * @return a node object
 */
@SuppressWarnings("unchecked")
static Node selectSingleNode(final Node doc, String path) {
    Map xmlMap = new HashMap();
    xmlMap.put(XMLNS, ServiceManagementConstants.ServiceManagementNS);
    XPath x = doc.createXPath(path);
    x.setNamespaceURIs(xmlMap);
    Node selectSingleNode = x.selectSingleNode(doc);
    return selectSingleNode;
}