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.xml.XElement.java

License:Open Source License

/**
 * Constructs a default element with an initial type.
 *
 * @param name the unmutable name./*from w  w w  . j  a  v a 2 s.co  m*/
 */
public XElement(String name, String namespace, String prefix) {
    this(new QName(name, Namespace.get(prefix, namespace)), true);
}

From source file:com.cladonia.xml.XElement.java

License:Open Source License

/**
 * Constructs a default element with an initial type.
 *
 * @param name the unmutable name.//ww w.  j  a  v  a  2s. c  o m
 * @param name the unmutable namespace.
 * @param parsed the element has been parsed from text.
 */
public XElement(String name, String namespace, boolean parsed) {
    this(new QName(name, Namespace.get(namespace)), parsed);
}

From source file:com.cladonia.xml.XElement.java

License:Open Source License

/**
 * Constructs a default element with an initial type.
 *
 * @param name the unmutable name.// w ww. j  a v  a  2  s .c  o m
 * @param name the unmutable namespace.
 * @param parsed the element has been parsed from text.
 */
public XElement(String name, String namespace, String prefix, boolean parsed) {
    this(new QName(name, Namespace.get(prefix, namespace)), parsed);
}

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

License:Open Source License

/**
 * add one of various types of node to the xpath - selected nodes
 * //from w  w  w . ja v  a2 s . c  o  m
 * @param document
 * @param xpathPredicate
 * @param nodeType
 * @param namespace
 * @param name
 * @param value
 * @return
 */
public String addNode(ExchangerDocument document, String xpathPredicate, String nodeType, String namespace,
        String prefix, String name, String value) throws Exception {

    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;

                    if (nodeType.equalsIgnoreCase("Element Node")) {

                        QName qname = null;
                        //resolve the namespace string

                        if (!namespace.equalsIgnoreCase("none")) {
                            Namespace newNs;
                            try {
                                newNs = new Namespace(prefix, namespace);
                                qname = new QName(name, newNs);
                            } catch (StringIndexOutOfBoundsException e1) {
                                //cannot parse string
                                MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace,
                                        "Tools Add Node");
                                qname = new QName(name);
                            }

                        } else {
                            qname = new QName(name);
                        }
                        XElement newNode = new XElement(qname);
                        e.add(newNode);
                        newNode.setValue(value);

                    } else if (nodeType.equalsIgnoreCase("Attribute Node")) {

                        QName qname = null;
                        //resolve the namespace string

                        if (!namespace.equalsIgnoreCase("none")) {
                            Namespace newNs;
                            try {
                                newNs = new Namespace(namespace.substring(0, namespace.indexOf(":")),
                                        namespace.substring(namespace.indexOf(":") + 1, namespace.length()));
                                qname = new QName(name, newNs);
                            } catch (StringIndexOutOfBoundsException e1) {
                                //cannot parse string
                                MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace,
                                        "Tools Add Node");
                                qname = new QName(name);
                            }

                        } else {
                            qname = new QName(name);
                        }
                        XAttribute newNode = new XAttribute(qname, value);

                        e.add(newNode);

                    } else if (nodeType.equalsIgnoreCase("Text Node")) {
                        FlyweightText newNode = new FlyweightText(value);

                        e.add(newNode);

                    } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) {
                        FlyweightCDATA newNode = new FlyweightCDATA(value);

                        e.add(newNode);

                    } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) {
                        FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name,
                                value);
                        e.add(newNode);
                    } else if (nodeType.equalsIgnoreCase("Comment Node")) {
                        FlyweightComment newNode = new FlyweightComment(value);
                        e.add(newNode);

                    }

                } else if (node instanceof Document) {
                    XDocument d = (XDocument) node;

                    if (nodeType.equalsIgnoreCase("Processing Instruction Node")) {
                        FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name,
                                value);
                        d.add(newNode);
                    } else if (nodeType.equalsIgnoreCase("Comment Node")) {
                        FlyweightComment newNode = new FlyweightComment(value);
                        d.add(newNode);

                    } else {
                        //cant handle any others
                        //                          can only handle elements
                        MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: "
                                + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node");
                        //end for loop
                        cnt = nodeList.size();
                        return (null);
                    }

                }

                else {
                    //can only handle elements
                    MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: "
                            + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node");
                    //end for loop
                    cnt = nodeList.size();
                    return (null);
                }
            }

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

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

    return (document.getText());
}

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

License:Open Source License

/**
 * Walk through the document tree and change the attributes if they are the selected ones
 * //from www.j ava2s . co m
 * @param document the exchanger document
 * @param attributeList the list of attributes that match the xpath result
 * @param newNs the new namespace for the nodes
 * @throws Exception
 */
public void treeWalk(Element element, List attributeList, Namespace newNs) 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 value = newAtt.getValue();
                        Namespace ns = newNs;

                        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, newNs);
        }
    }

}

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

License:Open Source License

private String capitalize(ExchangerDocument document, boolean capitalizeElements, boolean capitalizeAttributes,
        boolean capitalizeElementsAndAttributes) {

    try {/*www  .ja  v a  2  s  . c  o m*/
        XElement root = document.getRoot();

        if (((capitalizeAttributes) || (capitalizeElementsAndAttributes)) && (root.attributeCount() > 0)) {
            root.setAttributes(this.capitalizeAttributes(root));

        }
        //capitalize the element
        if (((capitalizeElements) || (capitalizeElementsAndAttributes)) && (root.getName() != null)) {

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

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

        //then capitalize its children      
        iterateTree(root, capitalizeElements, capitalizeAttributes, capitalizeElementsAndAttributes);

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

    return document.getText();
}

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

License:Open Source License

private String capitalize(ExchangerDocument document, String xpath, boolean capitalizeElements,
        boolean capitalizeAttributes, boolean capitalizeElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {/*from w  w w.j  a v  a 2  s.c  om*/
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Error - Cannot Resolve XPath", "Tools Capitalize Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //Capitalize the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((capitalizeAttributes) || (capitalizeElementsAndAttributes)) {

                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.capitalizeAttributes(root));
                    }
                }
                //capitalize the element
                if ((capitalizeElements) || (capitalizeElementsAndAttributes)) {
                    if (root.getName() != null) {

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

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then capitalize its children           
                    iterateTree(root, capitalizeElements, capitalizeAttributes,
                            capitalizeElementsAndAttributes);
                }
            }

            else if (((capitalizeAttributes) || (capitalizeElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(capitalizeAttributes((XElement) (node.getParent()), att));

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

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

License:Open Source License

private void iterateTree(Element element, boolean capitalizeElements, boolean capitalizeAttributes,
        boolean capitalizeElementsAndAttributes) 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 (((capitalizeAttributes) || (capitalizeElementsAndAttributes))
                    && (oldElement.attributeCount() > 0)) {
                oldElement.setAttributes(this.capitalizeAttributes(oldElement));

            }/*from   w  w  w .  j  a v a 2  s.c  o m*/

            //capitalize the element
            if (((capitalizeElements) || (capitalizeElementsAndAttributes)) && (oldElement.getName() != null)) {

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

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

            }
            iterateTree(oldElement, capitalizeElements, capitalizeAttributes, capitalizeElementsAndAttributes);

        }
    }
}

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

License:Open Source License

private List capitalizeAttributes(XElement root) throws Exception {

    int attributeCount = root.attributeCount();
    List attributeList = root.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  ww w. j  a v  a2  s.  c om
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        String name = attArray[cnt].getName();
        name = capitalizeString(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.ToolsCapitalizeAction.java

License:Open Source License

private List capitalizeAttributes(XElement root, Attribute att) throws Exception {

    int attributeCount = root.attributeCount();
    List attributeList = root.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);
    }/*www  .  j a  va 2 s  .c  o m*/
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        Attribute attOld = attArray[cnt];
        if (attOld == att) {
            String name = attArray[cnt].getName();
            name = capitalizeString(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);

}