Example usage for org.dom4j DocumentHelper sort

List of usage examples for org.dom4j DocumentHelper sort

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper sort.

Prototype

public static void sort(List<Node> list, String expression, boolean distinct) 

Source Link

Document

sort sorts the given List of Nodes using an XPath expression as a java.util.Comparator and optionally removing duplicates.

Usage

From source file:com.cladonia.xngreditor.actions.ToolsSortNodeAction.java

License:Open Source License

private String sortByXPath(ExchangerDocument document, String xpathPredicate, String sortXPathPredicate,
        boolean isAscending) throws Exception {

    //List nodeList = document.getDocument().selectNodes(xpathPredicate);
    Vector nodeList = document.search(xpathPredicate);

    try {/* ww  w . ja va 2s  .c om*/
        Vector nodes = new Vector();
        if (nodeList.size() != 0) {
            this.originalCounter = 0;
            treeWalk(document, nodeList, nodes);

            //              get the sorted list
            DocumentHelper.sort(nodeList, sortXPathPredicate, false);
            //now update nodeinfo's with the sorted order

            for (int ocnt = 0; ocnt < nodeList.size(); ++ocnt) {

                //for each node in the sorted list,
                //find it in the nodeInfo list and set the sorted index
                Node node = (Node) nodeList.get(ocnt);

                for (int cnt = 0; cnt < nodes.size(); ++cnt) {
                    NodeInfo ni = (NodeInfo) nodes.get(cnt);
                    if (node == ni.getNode()) {
                        if (isAscending) {
                            ni.setSortedId(ocnt);
                        } else {
                            int id = (nodeList.size() - 1) - ocnt;
                            ni.setSortedId(id);
                        }
                        nodes.setElementAt(ni, cnt);
                    }
                }
            }
            // now need to remove all duplicates of this element from the
            // document

            //check for children of parents in the list
            if (checkForChildrenOfParents(nodes) == false) {
                removeOccurences(xpathPredicate, document);
                nodes = sortIndexes(nodes, isAscending);

                insertNodes(document, nodes, isAscending);
            } else {
                //error children and parents mixed
                MessageHandler.showError(parent,
                        "Cannot Sort: XPath: " + xpathPredicate + " refers to both children and parents",
                        "Tools Sort Node Error");
                return (null);
            }
        } else {
            MessageHandler.showError(parent,
                    "XPath: " + xpathPredicate + " and " + sortXPathPredicate + "\nCannot be resolved",
                    "Tools Sort Node Error");
            //e.printStackTrace();
            return (null);
        }
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                "Tools Sort Node Error");
        //e.printStackTrace();
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Cannot Sort Nodes", "Tools Sort Node Error");
        //e.printStackTrace();
        return (null);
    }
    document.update();
    return document.getText();
}

From source file:com.liferay.portal.xml.SAXReaderImpl.java

License:Open Source License

public void sort(List<Node> nodes, String xPathExpression, boolean distinct) {

    DocumentHelper.sort(toOldNodes(nodes), xPathExpression, distinct);
}