Example usage for org.dom4j Node selectObject

List of usage examples for org.dom4j Node selectObject

Introduction

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

Prototype

Object selectObject(String xpathExpression);

Source Link

Document

selectObject evaluates an XPath expression and returns the result as an Object .

Usage

From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java

License:Open Source License

/**
 * ?XML Node?resource.// w w  w .  jav  a2 s .  com
 *
 * @param groupNode resource?XML Node
 * @throws ResourceBundleCreateException ?
 */
protected void initGroup(Node groupNode) throws ResourceBundleCreateException {
    String enumTypeName = (String) groupNode.selectObject(ResourceBundleConstant.XPATH_GROUP_ENUM);
    Class enumType = null;

    if (enumTypeName.length() > 0) {
        try {
            enumType = ContextClassLoader.loadClass(enumTypeName);
        } catch (ClassNotFoundException e) {
            throw new ResourceBundleCreateException(ResourceBundleConstant.RB_ENUM_CLASS_NOT_FOUND,
                    new Object[] { enumTypeName, ContextClassLoader.getClassLoader() }, e);
        }
    }

    for (Iterator i = groupNode.selectNodes(ResourceBundleConstant.XPATH_RESOURCES).iterator(); i.hasNext();) {
        Node resourceNode = (Node) i.next();

        initResource(resourceNode, enumType);
    }
}

From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java

License:Open Source License

/**
 * ?XML Node?resource.//ww w .jav a2  s .c  om
 *
 * @param resourceNode resource?XML Node
 * @param enumType     <code>Enum</code>
 * @throws ResourceBundleCreateException ?
 */
protected void initResource(Node resourceNode, Class enumType) throws ResourceBundleCreateException {
    String id = (String) resourceNode.selectObject(ResourceBundleConstant.XPATH_RESOURCE_ID);

    // enum, enumresource key.
    if (enumType != null) {
        Enum enumObj = Enum.getEnumByName(enumType, id);

        if (enumObj == null) {
            throw new ResourceBundleCreateException(ResourceBundleConstant.RB_ENUM_ID_NOT_FOUND,
                    new Object[] { id, enumType.getName() }, null);
        }

        id = enumObj.toString();
    }

    Object value = null;
    String type = resourceNode.getName();

    if (ResourceBundleConstant.RB_RESOURCE_TYPE_MESSAGE.equals(type)) {
        value = getMessageResource(id, resourceNode);
    } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_MAP.equals(type)) {
        value = getMapResource(id, resourceNode);
    } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_LIST.equals(type)) {
        value = getListResource(id, resourceNode);
    }

    if (values.containsKey(id)) {
        throw new ResourceBundleCreateException(ResourceBundleConstant.RB_DUPLICATED_RESOURCE_KEY,
                new Object[] { id }, null);
    }

    values.put(id, value);
}

From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java

License:Open Source License

/**
 * ?XML Nodemessage resource./*from w w  w  .  j a  va2 s .  c om*/
 *
 * @param id           resource ID
 * @param resourceNode resource?XML Node
 * @return resource
 * @throws ResourceBundleCreateException ?
 */
protected Object getMessageResource(String id, Node resourceNode) throws ResourceBundleCreateException {
    return resourceNode.selectObject(ResourceBundleConstant.XPATH_RESOURCE_MESSAGE_DATA);
}

From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java

License:Open Source License

/**
 * ?XML Nodemap resource.//from  w w w . j  a va 2s. co  m
 *
 * @param id           resource ID
 * @param resourceNode resource?XML Node
 * @return resource
 * @throws ResourceBundleCreateException ?
 */
protected Object getMapResource(String id, Node resourceNode) throws ResourceBundleCreateException {
    ListMap map = new ArrayHashMap();

    for (Iterator i = resourceNode.selectNodes(ResourceBundleConstant.XPATH_RESOURCES).iterator(); i
            .hasNext();) {
        Node mapItemNode = (Node) i.next();
        Object mapKey = mapItemNode.selectObject(ResourceBundleConstant.XPATH_RESOURCE_ID);

        if (map.containsKey(id)) {
            throw new ResourceBundleCreateException(ResourceBundleConstant.RB_DUPLICATED_MAP_RESOURCE_KEY,
                    new Object[] { mapKey, id }, null);
        }

        String mapItemType = mapItemNode.getName();
        Object value = null;

        if (ResourceBundleConstant.RB_RESOURCE_TYPE_MESSAGE.equals(mapItemType)) {
            value = getMessageResource(id, mapItemNode);
        } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_MAP.equals(mapItemType)) {
            value = getMapResource(id, mapItemNode);
        } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_LIST.equals(mapItemType)) {
            value = getListResource(id, mapItemNode);
        }

        map.put(mapKey, value);
    }

    return Collections.unmodifiableMap(map);
}

From source file:com.xebia.mojo.dashboard.util.DashboardUtil.java

License:Apache License

/**
 * Execute an XPath function (like count) on a {@link Document}, and return a String containing the result of the
 * function. If the result was a {@link Double}, it returns the integer value.
 *
 * @param  document The Dom4J {@link Document} to parse using the XPath function.
 * @param  xpath    The XPath function to set loose on the document.
 *
 * @return A String containing the result of the function.
 *///from www.  j a v a2  s. co  m
public static String executeXpathFunctionOnDocument(Node document, String xpath) {
    String value = null;
    Object o = document.selectObject(xpath);

    if (o instanceof Double) {
        value = String.valueOf(((Double) o).intValue());
    } else {
        value = o.toString();
    }

    return value;
}

From source file:org.craftercms.core.util.XmlUtils.java

License:Open Source License

/**
 * Executes an XPath query to retrieve an object. Normally, if the XPath result doesn't have a result, an
 * empty collection is returned. This object checks that case and returns null accordingly.
 *///from w w  w  .j  ava2 s. co m
public static Object selectObject(Node node, String xpathQuery) {
    Object result = node.selectObject(xpathQuery);
    if (result != null && result instanceof Collection && ((Collection) result).isEmpty()) {
        return null;
    } else {
        return result;
    }
}

From source file:org.onecmdb.core.utils.transform.xml.XPathInstanceSelector.java

License:Open Source License

public List<IInstance> getInstances(DataSet ds) throws IOException {
    if (ds.getDataSource() instanceof XMLDataSource) {
        List<IInstance> rows = new ArrayList<IInstance>();
        for (Node node : ((XMLDataSource) ds.getDataSource()).getNodes()) {
            List<Node> selectedNodes = node.selectNodes(getXpath());
            for (Node selectedNode : selectedNodes) {

                XMLRow row = new XMLRow(ds, selectedNode);
                row.setAutoCreate(isAutoCreate());
                String temp = null;
                if (templatePath != null) {
                    Node n = row.getNode();

                    Object sNode = n.selectObject(templatePath);

                    if (sNode instanceof Node) {
                        temp = ((Node) sNode).getText();
                    } else if (sNode instanceof String) {
                        temp = (String) sNode;
                    }/*from w w  w  .  ja va  2  s .c o  m*/
                }
                if (temp == null) {
                    temp = getTemplate();
                }
                row.setTemplate(temp);

                rows.add(row);
            }
        }
        return (rows);
    }
    throw new IllegalArgumentException("XPath selector must operate on an XML data source");
}

From source file:org.quickbundle.mda.gc.ConfigTableDialog.java

License:Open Source License

protected void buttonPressed(int buttonId) {
    if (buttonId == IDialogConstants.OK_ID) {
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@tableNameDisplay"))
                .setValue(tableNameDisplay.getText());
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@tableFilterKeyword"))
                .setValue(tableFilterKeyword.getText());
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@tableDirName"))
                .setValue(tableDirName.getText());
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@tablePk"))
                .setValue(tablePk.getText());
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@statisticColumn"))
                .setValue(statisticColumn.getText());
        ((Attribute) gcRule.getTableDoc(currentTable).selectObject("/meta/tables/table[1]/@keyColumn"))
                .setValue(keyColumn.getText());
        {// ww w  .j a v a 2 s .  c o  m
            //?
            String customBundleCode = "";
            for (Button b : lBCustomBundle) {
                if (b.getSelection()) {
                    if (customBundleCode.length() > 0) {
                        customBundleCode += ",";
                    }
                    customBundleCode += b.getData().toString();
                }
            }
            ((Attribute) gcRule.getTableDoc(currentTable)
                    .selectObject("/meta/tables/table[1]/@customBundleCode")).setValue(customBundleCode);
        }

        for (Iterator itMColumn = mColumn.keySet().iterator(); itMColumn.hasNext();) {
            Object[] columnInfo = (Object[]) mColumn.get(itMColumn.next());
            String columnName = String.valueOf(columnInfo[0]);
            Button isBuild = (Button) columnInfo[1];
            Text columnNameDisplay = (Text) columnInfo[3];
            Combo humanDisplayType = (Combo) columnInfo[5];
            Text humanDisplayTypeKeyword = (Text) columnInfo[6];
            Button isBuild_list = (Button) columnInfo[7];
            Node node = (Node) gcRule.getTableDoc(currentTable)
                    .selectObject("/meta/tables/table[1]/column[@columnName='" + columnName + "']");
            ((Attribute) node.selectObject("@columnNameDisplay")).setValue(columnNameDisplay.getText());
            ((Attribute) node.selectObject("@isBuild")).setValue(isBuild.getSelection() ? "true" : "false");
            ((Attribute) node.selectObject("@humanDisplayType")).setValue(humanDisplayType.getText());
            ((Attribute) node.selectObject("@humanDisplayTypeKeyword"))
                    .setValue(humanDisplayTypeKeyword.getText());
            ((Attribute) node.selectObject("@isBuild_list"))
                    .setValue(isBuild_list.getSelection() ? "true" : "false");
        }
        if (!validateAll()) {
            return;
        }
    }
    super.buttonPressed(buttonId);
}