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.ToolsChangeNSPrefixAction.java

License:Open Source License

/**
 * Renames a namespace at the element/*from   w  w  w . ja va 2 s . c  om*/
 * @param element the element at the namespace
 * @param allNamespaces the full vector of namespaces
 * @param newNamespaces the vector of new namespaces
 * @throws Exception
 */
private void renameNamespaces(XElement element, Vector allNamespaces, Vector newNamespaces) throws Exception {

    //need to handle if the root has a namespace associated

    try {

        Namespace used = element.getNamespace();

        int match = -1;

        if (used != null) {

            for (int cnt = 0; cnt < allNamespaces.size(); ++cnt) {
                if (used == (Namespace) allNamespaces.get(cnt)) {
                    match = cnt;
                }

            }
            if (match > -1) {

                //replace the elements prefix with the corresponding one in the
                //other vector
                Namespace tempNs = (Namespace) newNamespaces.get(match);
                QName qname = new QName(element.getName(), tempNs);
                element.setQName(qname);

            }
        }

        try {
            element.setAttributes(this.setPrefixOnAttributes(element, allNamespaces, newNamespaces));
        } catch (Exception e1) {
            MessageHandler.showError(parent, "Error reading the document",
                    "Tools Rename a Namespace Prefix Error");

        }

    } catch (NullPointerException e) {
        //no namespaces in the root            

    }

}

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

License:Open Source License

/**
 * //from w  ww .j  ava  2  s.c o  m
 * Renames a namespace at the root element
 * @param element the root element
 * @param allNamespaces the full vector of namespaces
 * @param newNamespaces the vector of new namespaces
 * @throws Exception
 */
private void renameNamespacesAtRoot(XElement element, Vector allNamespaces, Vector newNamespaces)
        throws Exception {

    //need to handle if the root has a namespace associated
    try {
        Namespace used = element.getNamespace();
        int match = -1;
        if (used != null) {
            for (int cnt = 0; cnt < allNamespaces.size(); ++cnt) {
                if (used == (Namespace) allNamespaces.get(cnt)) {
                    match = cnt;
                }
            }
            if (match > -1) {
                //replace the elements prefix with the corresponding one in the
                //other vector
                Namespace tempNs = (Namespace) newNamespaces.get(match);

                Namespace old = (Namespace) used.clone();

                QName qname = new QName(element.getName(), tempNs);
                element.setQName(qname);
                element.add(old);
            }
        }
        try {
            element.setAttributes(this.setPrefixOnAttributes(element, allNamespaces, newNamespaces));
        } catch (Exception e1) {
            MessageHandler.showError(parent, "Error reading the document",
                    "Tools Rename a Namespace Prefix Error");
        }
    } catch (NullPointerException e) {
        //no namespaces in the root            
        MessageHandler.showError(parent, "No namespace declared", "Tools Rename a Namespace Prefix Error");
    }
}

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

License:Open Source License

/**
 * Sets the prefixes on the attributes at a parent element
 * @param parent The parent element//from w ww . ja  v a  2 s. c o m
 * @param allNamespaces the full vector of namespaces
 * @param newNamespaces the vector of new namespaces
 * @return the new list of attributes for the element
 * @throws Exception
 */
private List setPrefixOnAttributes(XElement parent, Vector allNamespaces, Vector newNamespaces)
        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 prefix = attArray[cnt].getNamespacePrefix();
        Namespace ns = attArray[cnt].getNamespace();

        int match = -1;

        //if(!prefix.equals("")) {
        if (!ns.equals(null)) {
            //see if this prefix matches anyone in the vector
            //match = searchForPrefix(prefix, allNamespaces);
            for (int icnt = 0; icnt < allNamespaces.size(); ++icnt) {
                if (ns == (Namespace) allNamespaces.get(icnt)) {
                    match = icnt;
                }

            }
            if (match > -1) {

                //replace the elements prefix with the corresponding one in the
                //other vector
                Namespace tempNs = (Namespace) newNamespaces.get(match);

                //get any declared namespaces as well

                //prefix = tempNs.getPrefix();
                String name = attArray[cnt].getName();
                String value = attArray[cnt].getValue();

                attArray[cnt] = new XAttribute(new QName(name, tempNs), 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.ToolsDeCapitalizeAction.java

License:Open Source License

/**
 * Decapitalize a document based on the various flags
 * @param document the document to be decapitalized
 * @param deCapitalizeElements flag as to whether to decapitalize elements
 * @param deCapitalizeAttributes flag as to whether to decapitalize attributes
 * @param deCapitalizeElementsAndAttributes flag as to whether to decapitalize elements and attributes
 * @return the decapitalized document as a string
 *//*w ww.  ja  v  a 2s  . c o m*/
private String deCapitalize(ExchangerDocument document, boolean deCapitalizeElements,
        boolean deCapitalizeAttributes, boolean deCapitalizeElementsAndAttributes) {

    try {
        XElement root = document.getRoot();

        if ((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes)) {

            root.setAttributes(this.deCapitalizeAttributes(root));

        }
        //DeCapitalize the element
        if (((deCapitalizeElements) || (deCapitalizeElementsAndAttributes)) && (root.getName() != null)) {

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

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

        //then DeCapitalize its children      
        iterateTree(root, deCapitalizeElements, deCapitalizeAttributes, deCapitalizeElementsAndAttributes);

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

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

License:Open Source License

/**
 * /* www .  ja v  a2s  .  co m*/
 * Decapitalize a document based on the various flags
 * @param document the document to be decapitalized
 * @param xpath the xpath filter
 * @param deCapitalizeElements flag as to whether to decapitalize elements
 * @param deCapitalizeAttributes flag as to whether to decapitalize attributes
 * @param deCapitalizeElementsAndAttributes flag as to whether to decapitalize elements and attributes
 * @param traverseChildren process sub elements flag
 * @return the decapitalized document as a string
 */
private String deCapitalize(ExchangerDocument document, String xpath, boolean deCapitalizeElements,
        boolean deCapitalizeAttributes, boolean deCapitalizeElementsAndAttributes, 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 DeCapitalize Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //DeCapitalize the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes)) {
                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.deCapitalizeAttributes(root));
                    }
                }
                if ((deCapitalizeElements) || (deCapitalizeElementsAndAttributes)) {
                    //DeCapitalize the element
                    if (root.getName() != null) {

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

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then DeCapitalize its children           
                    iterateTree(root, deCapitalizeElements, deCapitalizeAttributes,
                            deCapitalizeElementsAndAttributes);
                }
            } else if (((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(deCapitalizeAttributes((XElement) (node.getParent()), att));

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

    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot DeCapitalize Document", "Tools DeCapitalize Error");
        return (null);
    }
    return document.getText();
}

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

License:Open Source License

/**
 * Iterate through the tree to decapitalize elements and attributes based on the flags
 * @param element the element the method is working on
  * @param deCapitalizeElements flag as to whether to decapitalize elements
 * @param deCapitalizeAttributes flag as to whether to decapitalize attributes
 * @param deCapitalizeElementsAndAttributes flag as to whether to decapitalize elements and attributes
 * @throws Exception/*  w  w w  . j  a v a 2s  . c  om*/
 */
private void iterateTree(Element element, boolean deCapitalizeElements, boolean deCapitalizeAttributes,
        boolean deCapitalizeElementsAndAttributes) 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 ((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes)) {

                oldElement.setAttributes(this.deCapitalizeAttributes(oldElement));

            }

            //DeCapitalize the element
            if (((deCapitalizeElements) || (deCapitalizeElementsAndAttributes))
                    && (oldElement.getName() != null)) {

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

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

            }
            iterateTree(oldElement, deCapitalizeElements, deCapitalizeAttributes,
                    deCapitalizeElementsAndAttributes);

        }
    }
}

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

License:Open Source License

/**
 * decapitalize an elements attributes/*from  w  ww.  j  a va 2  s.  c o m*/
 * @param parent the element containing the attributes
 * @return the new list of attributes for that element
 * @throws Exception
 */
private List deCapitalizeAttributes(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 = deCapitalizeString(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.ToolsDeCapitalizeAction.java

License:Open Source License

/**
 * /*  www . j ava  2  s .co m*/
 * decapitalize an elements attribute which matches the passed in attribute
 * @param parent the element containing the attributes
 * @param att the attribute to match
 * @return the new list of attributes for that element
 * @throws Exception
 */
private List deCapitalizeAttributes(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);
    }
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        Attribute attOld = attArray[cnt];
        if (attOld == att) {
            String name = attArray[cnt].getName();
            name = deCapitalizeString(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 String lowercase(ExchangerDocument document, boolean lowercaseElements, boolean lowercaseAttributes,
        boolean lowercaseElementsAndAttributes) {

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

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

            root.setAttributes(this.lowercaseAttributes(root));

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

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

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

        //then Lowercase its children      
        iterateTree(root, lowercaseElements, lowercaseAttributes, lowercaseElementsAndAttributes);

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

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

License:Open Source License

private String lowercase(ExchangerDocument document, String xpath, boolean lowercaseElements,
        boolean lowercaseAttributes, boolean lowercaseElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {//from  w  ww.j  a v a2  s  .  c  om
        //XElement[] root = document.getElements(xpath);
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Could Not Resolve XPath:\n" + xpath, "Tools Lowercase Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //lowercase the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((lowercaseAttributes) || (lowercaseElementsAndAttributes)) {
                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.lowercaseAttributes(root));
                    }
                }
                if ((lowercaseElements) || (lowercaseElementsAndAttributes)) {
                    //lowercase the element
                    if (root.getName() != null) {

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

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then lowercase its children           
                    iterateTree(root, lowercaseElements, lowercaseAttributes, lowercaseElementsAndAttributes);
                }
            } else if (((lowercaseAttributes) || (lowercaseElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(lowercaseAttributes((XElement) (node.getParent()), att));

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