Example usage for org.dom4j.tree FlyweightProcessingInstruction FlyweightProcessingInstruction

List of usage examples for org.dom4j.tree FlyweightProcessingInstruction FlyweightProcessingInstruction

Introduction

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

Prototype

public FlyweightProcessingInstruction(String target, String text) 

Source Link

Document

This will create a new PI with the given target and values

Usage

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 ww .  j  ava  2s . 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.ToolsConvertNodeAction.java

License:Open Source License

/**
 * add one of various types of node to the xpath - selected nodes
 * //from   ww w .j  a  va  2s .c  o  m
 * @param document
 * @param xpathPredicate
 * @param nodeType
 * @return
 */
public String convertNode(ExchangerDocument document, String xpathPredicate, String nodeType) {

    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;
                    Element parentE = e.getParent();
                    if ((e.hasChildElements()) || (e.attributeCount() > 0)) {
                        if ((e.hasChildElements())) {
                            MessageHandler.showError(parent, "Cannont convert since node has child elements ",
                                    "Tools Convert Node Error");
                            return (null);
                        } else if (e.attributeCount() > 0) {
                            MessageHandler.showError("Cannont convert since node has attributes",
                                    "Tools Convert Node Error");
                            return (null);
                        }
                        cnt = nodeList.size();

                    } else {
                        if (nodeType.equalsIgnoreCase("Element Node")) {
                            MessageHandler.showError(parent, "Node is already an Element",
                                    "Tools Convert Node Error");
                            //end loop
                            cnt = nodeList.size();
                            return (null);
                        } else if (nodeType.equalsIgnoreCase("Attribute Node")) {
                            //check if it has child elements
                            QName qname = e.getQName();
                            //resolve the namespace string
                            parentE.add(new XAttribute(qname, e.getValue()));
                            parentE.remove(e);
                        } else if (nodeType.equalsIgnoreCase("Text Node")) {
                            FlyweightText newNode = new FlyweightText(e.getText());
                            parentE.add(newNode);
                            parentE.remove(e);
                        } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) {
                            FlyweightCDATA newNode = new FlyweightCDATA(e.getText());
                            parentE.add(newNode);
                            parentE.remove(e);
                        } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) {
                            FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(
                                    e.getText(), "");
                            parentE.add(newNode);
                            parentE.remove(e);
                        } else if (nodeType.equalsIgnoreCase("Comment Node")) {
                            FlyweightComment newNode = new FlyweightComment(e.getText());
                            parentE.add(newNode);
                            parentE.remove(e);
                        }
                    }
                } else if (node instanceof Attribute) {
                    XAttribute e = (XAttribute) node;
                    Element parentE = e.getParent();
                    if (nodeType.equalsIgnoreCase("Element Node")) {
                        QName qname = e.getQName();
                        //resolve the namespace string
                        XElement newE = new XElement(qname);
                        parentE.add(newE);
                        newE.setValue(e.getValue());
                        parentE.remove(e);
                    } else if (nodeType.equalsIgnoreCase("Attribute Node")) {
                        MessageHandler.showError(parent, "Node is already an Attribute",
                                "Tools Convert Node Error");
                        //end loop
                        cnt = nodeList.size();
                        return (null);
                    } else if (nodeType.equalsIgnoreCase("Text Node")) {
                        FlyweightText newNode = new FlyweightText(e.getText());
                        parentE.add(newNode);
                        parentE.remove(e);
                    } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) {
                        FlyweightCDATA newNode = new FlyweightCDATA(e.getText());
                        parentE.add(newNode);
                        parentE.remove(e);
                    } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) {
                        FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(e.getText(),
                                "");
                        parentE.add(newNode);
                        parentE.remove(e);
                    } else if (nodeType.equalsIgnoreCase("Comment Node")) {
                        FlyweightComment newNode = new FlyweightComment(e.getText());
                        parentE.add(newNode);
                        parentE.remove(e);
                    }
                } else {
                    //can only handle elements
                    MessageHandler
                            .showError(parent,
                                    "Can only Convert Nodes to elements\n+" + "XPath: " + xpathPredicate
                                            + "refers to a" + node.getNodeTypeName(),
                                    "Tools Convert Node Error");
                    //end for loop
                    cnt = nodeList.size();
                }
            }
        } catch (NullPointerException e) {
            MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                    "Tools Convert Node Error");
            return (null);
        } catch (Exception e) {
            MessageHandler.showError(parent, "Error Adding Node", "Tools Convert Node Error");
            return (null);
        }
        document.update();
    } else {
        MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate,
                "Tools Convert Node Error");
        return (null);
    }
    return (document.getText());
}