Example usage for org.dom4j QName QName

List of usage examples for org.dom4j QName QName

Introduction

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

Prototype

public QName(String name, Namespace namespace) 

Source Link

Usage

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

License:Open Source License

private void iterateTree(Element element, boolean lowercaseElements, boolean lowercaseAttributes,
        boolean lowercaseElementsAndAttributes) throws Exception {

    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node oldNode = element.node(i);
        if (oldNode instanceof Element) {

            XElement oldElement = (XElement) oldNode;

            if ((lowercaseAttributes) || (lowercaseElementsAndAttributes)) {

                oldElement.setAttributes(this.lowercaseAttributes(oldElement));

            }//from www.  j  a va  2 s  . c o m

            //Lowercase the element
            if (((lowercaseElements) || (lowercaseElementsAndAttributes)) && (oldElement.getName() != null)) {

                String name = oldElement.getName();
                name = lowercaseString(name);
                Namespace ns = oldElement.getNamespace();

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, lowercaseElements, lowercaseAttributes, lowercaseElementsAndAttributes);

        }
    }
}

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

License:Open Source License

private List lowercaseAttributes(XElement parent) throws Exception {

    int attributeCount = parent.attributeCount();
    List attributeList = parent.attributes();
    //create an array to hold all the attributes
    Attribute[] attArray = new Attribute[attributeCount];

    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        //add each attribute to the array
        attArray[cnt] = (Attribute) attributeList.get(cnt);
    }//from w w w  .  j a v a 2 s. c o  m
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        String name = attArray[cnt].getName();
        name = lowercaseString(name);
        String value = attArray[cnt].getValue();
        Namespace ns = attArray[cnt].getNamespace();

        attArray[cnt] = new XAttribute(new QName(name, ns), value);
    }

    //then remove all previous and add all the attributes back into the document
    List newAttributes = Arrays.asList(attArray);
    return (newAttributes);
}

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

License:Open Source License

private List lowercaseAttributes(XElement parent, Attribute att) throws Exception {

    int attributeCount = parent.attributeCount();
    List attributeList = parent.attributes();
    //create an array to hold all the attributes
    Attribute[] attArray = new Attribute[attributeCount];

    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        //add each attribute to the array
        attArray[cnt] = (Attribute) attributeList.get(cnt);
    }/*from w w w  . j  a v  a 2s .c  om*/
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        Attribute attOld = attArray[cnt];
        if (attOld == att) {
            String name = attArray[cnt].getName();
            name = lowercaseString(name);
            String value = attArray[cnt].getValue();
            Namespace ns = attArray[cnt].getNamespace();

            attArray[cnt] = new XAttribute(new QName(name, ns), value);
        }
    }

    //then remove all previous and add all the attributes back into the document
    List newAttributes = Arrays.asList(attArray);
    return (newAttributes);
}

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

License:Open Source License

private String renameByXPath(ExchangerDocument document, String xpathPredicate, String newName)
        throws Exception {

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

    try {/*from  w  w  w. ja va2  s .  co  m*/

        for (int cnt = 0; cnt < nodeList.size(); ++cnt) {
            Node node = (Node) nodeList.get(cnt);
            //              node.setName(newName);

            if (node instanceof Element) {

                XElement e = (XElement) node;

                e.setQName(new QName(newName, e.getNamespace()));
            } else if (node instanceof Attribute) {

                XAttribute newAtt = (XAttribute) node;
                /*int fcnt = findInList(newAtt,attributeList);
                if(fcnt > -1) {*/
                //if the attribute matches the one we are looking for
                //replace it with the new qname
                String name = newAtt.getName();
                String oldValue = newAtt.getValue();
                Namespace ns = newAtt.getNamespace();
                XElement parent = (XElement) node.getParent();
                parent.remove(newAtt);
                parent.add(new XAttribute(new QName(newName, ns), oldValue));

                //now remove it from the attributeList to speed up the next searches
                //attributeList.remove(fcnt);

                //}
                //attributeList.add((XAttribute) node);
            }

        }
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                "Tools Rename Node Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Cannot Rename Node", "Tools Rename Node Error");
        return (null);
    }
    document.update();

    return document.getText();
}

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

License:Open Source License

/**
 * sets the value of the xpath - selected nodes
 * //from  w  w  w  . j  av a  2  s.com
 * @param document the exchanger document
 * @param xpathPredicate - the xpath predicate to resolve
 * @param value - the new value for the nodes
 * @return the documents text
 */
private String setNodeValue(ExchangerDocument document, String xpathPredicate, String value) throws Exception {

    /*XPath xpathSelector = DocumentHelper.createXPath(xpathPredicate);
     List nodeList = xpathSelector.selectNodes(document.getDocument());*/
    Vector nodeList = document.search(xpathPredicate);
    Vector attributeList = new Vector();

    String warning = "";
    if (nodeList.size() > 0) {
        try {

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

                Node node = (Node) nodeList.get(cnt);

                if (node instanceof Element) {

                    XElement e = (XElement) node;
                    e.setValue(value);
                } else if (node instanceof Attribute) {

                    XAttribute newAtt = (XAttribute) node;

                    //if the attribute matches the one we are looking for
                    //replace it with the new qname
                    String name = newAtt.getName();
                    String oldValue = newAtt.getValue();
                    Namespace ns = newAtt.getNamespace();
                    XElement parent = (XElement) node.getParent();
                    parent.remove(newAtt);
                    parent.add(new XAttribute(new QName(name, ns), value));

                }

            }
            //now change all the attributes
            changeAttribute(document, attributeList, value);

        } catch (NullPointerException e) {
            MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                    "Tools Set Node Value Error");
            return (null);
        } catch (Exception e) {
            MessageHandler.showError(parent, "Error setting node value", "Tools Set Node Value Error");
            return (null);
        }

        document.update();
    } else {
        MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate,
                "Tools Set Node Value Error");
        return (null);
    }

    return (document.getText());
}

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

License:Open Source License

/**
 * Walk through the document tree and change the attributes if they are the selected ones
 * //www . jav  a 2  s. c o m
 * @param document the exchanger document
 * @param attributeList the list of attributes that match the xpath result
 * @param value the new value for the nodes
 * @throws Exception
 */
private void treeWalk(Element element, List attributeList, String value) throws Exception {

    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            //if it's an element
            XElement e = (XElement) node;
            if (e.attributeCount() > 0) {
                //and it has attributes

                List attList = e.attributes();

                for (int cnt = 0; cnt < attList.size(); ++cnt) {
                    //for each attribute
                    XAttribute newAtt = (XAttribute) attList.get(cnt);
                    int fcnt = findInList(newAtt, attributeList);
                    if (fcnt > -1) {
                        //if the attribute matches the one we are looking for
                        //replace it with the new qname
                        String name = newAtt.getName();
                        String oldValue = newAtt.getValue();
                        Namespace ns = newAtt.getNamespace();

                        attList.set(cnt, new XAttribute(new QName(name, ns), value));

                        //now remove it from the attributeList to speed up the next searches
                        attributeList.remove(fcnt);

                    }
                }
            }
            treeWalk((Element) node, attributeList, value);
        }
    }

}

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

License:Open Source License

/**
 * Convert the complete document to uppercase with the various flags
 * @param document the document to be converted
 * @param uppercaseElements uppercase elements flag
 * @param uppercaseAttributes uppercase attributes flag
 * @param uppercaseElementsAndAttributes uppercase elements and attributes flag
 * @return the converted document as a string
 */// www .  j a v a 2s  .  c o m
private String uppercase(ExchangerDocument document, boolean uppercaseElements, boolean uppercaseAttributes,
        boolean uppercaseElementsAndAttributes) {

    try {
        XElement root = document.getRoot();

        if ((uppercaseAttributes) || (uppercaseElementsAndAttributes)) {

            root.setAttributes(this.uppercaseAttributes(root));

        }
        //Uppercase the element
        if (((uppercaseElements) || (uppercaseElementsAndAttributes)) && (root.getName() != null)) {

            String name = root.getName();
            name = uppercaseString(name);
            Namespace ns = root.getNamespace();

            root.setQName(new QName(name, ns));
        }

        //then Uppercase its children      
        iterateTree(root, uppercaseElements, uppercaseAttributes, uppercaseElementsAndAttributes);

        document.update();
    } catch (NullPointerException e) {
        MessageHandler.showError(parent,
                "Error - Cannot convert to uppercase,\nElements or Attributes not found",
                "Tools Uppercase Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot convert to uppercase", "Tools Uppercase Error");
        return (null);
    }
    return document.getText();
}

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

License:Open Source License

/**
 * Convert the document to uppercase with the various flags and an xpath filter
 * @param document the document to be converted
 * @param xpath the xpath filter to be applied
 * @param uppercaseElements uppercase elements flag
 * @param uppercaseAttributes uppercase attributes flag
 * @param uppercaseElementsAndAttributes uppercase elements and attributes flag
 * @param traverseChildren process sub elements flag
 * @return the converted document as a string
 *//*from w ww  .  ja v a  2s. co  m*/
private String uppercase(ExchangerDocument document, String xpath, boolean uppercaseElements,
        boolean uppercaseAttributes, boolean uppercaseElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {
        //XElement[] root = document.getElements(xpath);
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Could Not Resolve XPath:\n" + xpath, "Tools Uppercase");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //uppercase the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((uppercaseAttributes) || (uppercaseElementsAndAttributes)) {
                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.uppercaseAttributes(root));
                    }
                }
                if ((uppercaseElements) || (uppercaseElementsAndAttributes)) {
                    //uppercase the element
                    if (root.getName() != null) {

                        String name = root.getName();
                        name = uppercaseString(name);
                        Namespace ns = root.getNamespace();

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then uppercase its children           
                    iterateTree(root, uppercaseElements, uppercaseAttributes, uppercaseElementsAndAttributes);
                }
            } else if (((uppercaseAttributes) || (uppercaseElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(uppercaseAttributes((XElement) (node.getParent()), att));

            }
        }
        document.update();
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "Error - Cannot Uppercase,\nElements or Attributes not found",
                "Tools Uppercase Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot convert to uppercase", "Tools Uppercase Error");
        return (null);
    }
    return document.getText();
}

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

License:Open Source License

/**
 * Iterate through the tree and uppercase each node depending on the flags
 * @param element the element node/*from w ww  . java 2  s . c o m*/
 * @param uppercaseElements uppercase elements flag
 * @param uppercaseAttributes uppercase attributes flag
 * @param uppercaseElementsAndAttributes uppercase elements and attributes flag
 * @throws Exception
 */
private void iterateTree(Element element, boolean uppercaseElements, boolean uppercaseAttributes,
        boolean uppercaseElementsAndAttributes) throws Exception {

    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node oldNode = element.node(i);
        if (oldNode instanceof Element) {

            XElement oldElement = (XElement) oldNode;

            if ((uppercaseAttributes) || (uppercaseElementsAndAttributes)) {

                oldElement.setAttributes(this.uppercaseAttributes(oldElement));

            }

            //Uppercase the element
            if (((uppercaseElements) || (uppercaseElementsAndAttributes)) && (oldElement.getName() != null)) {

                String name = oldElement.getName();
                name = uppercaseString(name);
                Namespace ns = oldElement.getNamespace();

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, uppercaseElements, uppercaseAttributes, uppercaseElementsAndAttributes);

        }
    }
}

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

License:Open Source License

/**
 * Uppercase all attributes of a parent element
 * @param parent The parent element//from  ww w  .j ava 2 s.  co m
 * @return the new list of attributes
 * @throws Exception
 */
private List uppercaseAttributes(XElement parent) throws Exception {

    int attributeCount = parent.attributeCount();
    List attributeList = parent.attributes();
    //create an array to hold all the attributes
    Attribute[] attArray = new Attribute[attributeCount];

    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        //add each attribute to the array
        attArray[cnt] = (Attribute) attributeList.get(cnt);
    }
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        String name = attArray[cnt].getName();
        name = uppercaseString(name);
        String value = attArray[cnt].getValue();
        Namespace ns = attArray[cnt].getNamespace();

        attArray[cnt] = new XAttribute(new QName(name, ns), value);
    }

    //then remove all previous and add all the attributes back into the document
    List newAttributes = Arrays.asList(attArray);
    return (newAttributes);
}