Example usage for org.dom4j Node ELEMENT_NODE

List of usage examples for org.dom4j Node ELEMENT_NODE

Introduction

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

Prototype

short ELEMENT_NODE

To view the source code for org.dom4j Node ELEMENT_NODE.

Click Source Link

Document

Matches Element nodes

Usage

From source file:io.mashin.oep.hpdl.XMLReadUtils.java

License:Open Source License

public static List<Node> nodesList(Element rootElement) {
    List<Node> list = new ArrayList<Node>();
    @SuppressWarnings("unchecked")
    Iterator<Node> iter = rootElement.nodeIterator();
    while (iter.hasNext()) {
        Node node = iter.next();/* w  ww .j a v  a2s .  c om*/
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getName() != null && !node.getName().isEmpty()) {
            switch (node.getName()) {
            case "start":
            case "end":
            case "decision":
            case "fork":
            case "join":
            case "kill":
            case "action":
                list.add(node);
            }
        }
    }
    return list;
}

From source file:net.dontdrinkandroot.lastfm.api.CheckImplementationStatus.java

License:Apache License

/**
 * Recursively sets the namespace of the List and all children if the current namespace is match
 *//*from  w  ww . j  ava  2 s. c o  m*/
public static void setNamespaces(final List<Node> l, final Namespace ns) {

    Node n = null;
    for (int i = 0; i < l.size(); i++) {
        n = l.get(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            ((Attribute) n).setNamespace(ns);
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            CheckImplementationStatus.setNamespaces((Element) n, ns);
        }
    }
}

From source file:net.unicon.warlock.fac.AbstractWarlockFactory.java

License:Open Source License

private static ILayoutElement parseLayoutElement(Node n, IWarlockFactory owner) throws WarlockException {

    // Assertions.
    if (n == null) {
        String msg = "Argument 'n [Node]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//from www  .  ja va2s .com
    if (owner == null) {
        String msg = "Argument 'owner' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    ILayoutElement rslt = null;

    // Choose which type to create.
    switch (n.getNodeType()) {

    case Node.TEXT_NODE:
        rslt = IgnoredLayoutText.parse(n);
        break;
    case Node.ELEMENT_NODE:
        Element e = (Element) n;
        String tagName = e.getName();
        if (tagName.equals("value-of")) {
            rslt = XpathValue.parse(e);
        } else if (tagName.equals("copy-of")) {
            rslt = CopyOf.parse(e);
        } else if (tagName.equals("call-template")) {
            rslt = TemplateCall.parse(e);
        } else {
            rslt = IgnoredLayoutElement.parse(e, owner);
        }
        break;
    default:
        rslt = EmptyLayoutElement.INSTANCE;
        break;
    }

    return rslt;

}

From source file:org.apache.archiva.xml.XMLReader.java

License:Apache License

/**
 * Remove namespaces from element recursively.
 *//*from   w  ww. ja v a2 s  . com*/
@SuppressWarnings("unchecked")
public void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));

    Node n;

    Iterator<Node> it = elem.elementIterator();
    while (it.hasNext()) {
        n = it.next();

        switch (n.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
            break;
        case Node.ELEMENT_NODE:
            removeNamespaces((Element) n);
            break;
        }
    }
}

From source file:org.apache.maven.archetype.common.DefaultPomManager.java

License:Apache License

public void addModule(File pom, String artifactId)
        throws IOException, XmlPullParserException, DocumentException, InvalidPackaging {
    boolean found = false;

    StringWriter writer = new StringWriter();
    Reader fileReader = null;/* ww  w .ja  v a2  s. c om*/

    try {
        fileReader = ReaderFactory.newXmlReader(pom);

        SAXReader reader = new SAXReader();
        Document document = reader.read(fileReader);
        Element project = document.getRootElement();

        String packaging = null;
        Element packagingElement = project.element("packaging");
        if (packagingElement != null) {
            packaging = packagingElement.getStringValue();
        }
        if (!"pom".equals(packaging)) {
            throw new InvalidPackaging(
                    "Unable to add module to the current project as it is not of packaging type 'pom'");
        }

        Element modules = project.element("modules");
        if (modules == null) {
            modules = project.addText("  ").addElement("modules");
            modules.setText("\n  ");
            project.addText("\n");
        }
        // TODO: change to while loop
        for (@SuppressWarnings("unchecked")
        Iterator<Element> i = modules.elementIterator("module"); i.hasNext() && !found;) {
            Element module = i.next();
            if (module.getText().equals(artifactId)) {
                found = true;
            }
        }
        if (!found) {
            Node lastTextNode = null;
            for (@SuppressWarnings("unchecked")
            Iterator<Node> i = modules.nodeIterator(); i.hasNext();) {
                Node node = i.next();
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    lastTextNode = null;
                } else if (node.getNodeType() == Node.TEXT_NODE) {
                    lastTextNode = node;
                }
            }

            if (lastTextNode != null) {
                modules.remove(lastTextNode);
            }

            modules.addText("\n    ");
            modules.addElement("module").setText(artifactId);
            modules.addText("\n  ");

            XMLWriter xmlWriter = new XMLWriter(writer);
            xmlWriter.write(document);

            FileUtils.fileWrite(pom.getAbsolutePath(), writer.toString());
        } // end if
    } finally {
        IOUtil.close(fileReader);
    }
}

From source file:org.apache.maven.archetype.old.DefaultOldArchetype.java

License:Apache License

static boolean addModuleToParentPom(String artifactId, Reader fileReader, Writer fileWriter)
        throws DocumentException, IOException, ArchetypeTemplateProcessingException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(fileReader);
    Element project = document.getRootElement();

    String packaging = null;/*w ww  . j a  v a2s. com*/
    Element packagingElement = project.element("packaging");
    if (packagingElement != null) {
        packaging = packagingElement.getStringValue();
    }
    if (!"pom".equals(packaging)) {
        throw new ArchetypeTemplateProcessingException(
                "Unable to add module to the current project as it is not of packaging type 'pom'");
    }

    Element modules = project.element("modules");
    if (modules == null) {
        modules = project.addText("  ").addElement("modules");
        modules.setText("\n  ");
        project.addText("\n");
    }
    boolean found = false;
    for (Iterator<?> i = modules.elementIterator("module"); i.hasNext() && !found;) {
        Element module = (Element) i.next();
        if (module.getText().equals(artifactId)) {
            found = true;
        }
    }
    if (!found) {
        Node lastTextNode = null;
        for (Iterator<?> i = modules.nodeIterator(); i.hasNext();) {
            Node node = (Node) i.next();
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                lastTextNode = null;
            } else if (node.getNodeType() == Node.TEXT_NODE) {
                lastTextNode = node;
            }
        }

        if (lastTextNode != null) {
            modules.remove(lastTextNode);
        }

        modules.addText("\n    ");
        modules.addElement("module").setText(artifactId);
        modules.addText("\n  ");

        XMLWriter writer = new XMLWriter(fileWriter);
        writer.write(document);
    }
    return !found;
}

From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java

License:Apache License

/**
 * Locates and returns ther {@link Element} specified by a passed in XPATH
 * expression in a target {@link Document}.
 * //from  w ww  .  ja v a2s. c  om
 * @param doc target {@link Document} instance where the element is to be
 *            located.
 * @param xpathToNode XPATH expression to locate the element.
 * @return {@link Element} instance that matches the XPATH expression. An
 *         {@link AssertionFailedError} is thrown if no matching Element was
 *         found.
 */
public static Element findElement(Document doc, String xpathToNode) {
    if (StringUtils.isEmpty(xpathToNode)) {
        throw new AssertionFailedError("Unable to assert an attribute using an empty xpath.");
    }

    if (doc == null) {
        throw new AssertionFailedError("Unable to assert an attribute using a null document.");
    }

    XPath xpath = doc.createXPath(xpathToNode);

    Node node = xpath.selectSingleNode(doc);

    if (node == null) {
        throw new AssertionFailedError("Expected Node at '" + xpathToNode + "', but was not found.");
    }

    if (node.getNodeType() != Node.ELEMENT_NODE) {
        throw new AssertionFailedError("Node at '" + xpathToNode + "' is not an xml element.");
    }

    return (Element) node;
}

From source file:org.danann.cernunnos.xml.AppendNodeTask.java

License:Apache License

@SuppressWarnings("unchecked")
public void perform(TaskRequest req, TaskResponse res) {

    // Figure out where to put the content...
    Branch p = null;//w w  w  . j av a 2  s  .  c om
    int index;
    if (sibling != null) {
        Node sib = (Node) sibling.evaluate(req, res);
        p = sib.getParent();
        index = p.indexOf(sib) + 1;
    } else {
        // Work from the PARENT...
        p = (Branch) parent.evaluate(req, res);
        index = p.content().size();
    }

    // Figure out what content to add...
    List list = null;
    if (content != null && content.size() > 0) {
        list = content;
    } else {
        list = new LinkedList();
        list.add(node.evaluate(req, res));
    }

    // Evaluate phrases & add...
    for (Object o : list) {

        Node n = (Node) ((Node) o).clone();
        NodeProcessor.evaluatePhrases(n, grammar, req, res);

        // If the parent is an element, check if we should
        // carry the parent namespace over to the child...
        if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE
                && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) {
            // We know the parent is an Element w/ a namespace,
            // is the child (also) an Element w/ none?
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) {
                // Yes -- we need to port the namespace forward...
                Namespace nsp = ((Element) p).getNamespace();
                if (log.isTraceEnabled()) {
                    StringBuffer msg = new StringBuffer();
                    msg.append("Adding the following namespace to <").append(n.getName()).append(">:  ")
                            .append(nsp);
                    log.trace(msg.toString());
                }
                NodeProcessor.applyNamespace(nsp, (Element) n);
            }
        }

        // Although they *are* nodes, attributes are not technically
        // content, and therefore they must have special treatment...
        if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) {
            // Add attributes by calling addAttribute on the Element contract...
            ((Element) p).add((Attribute) n);
        } else {
            // Add everything else as 'content'...
            p.content().add(index++, n);
        }

    }

}

From source file:org.danann.cernunnos.xml.PrependNodeTask.java

License:Apache License

@SuppressWarnings("unchecked")
public void perform(TaskRequest req, TaskResponse res) {

    // Figure out where to put the content...
    Branch p = null;//from  w  w w .  j  a  v a2 s  .  c om
    int index;
    if (sibling != null) {
        Node sib = (Node) sibling.evaluate(req, res);
        p = sib.getParent();
        index = p.indexOf(sib);
    } else {
        // Work from the PARENT...
        p = (Branch) parent.evaluate(req, res);
        index = 0;
    }

    // Figure out what content to add...
    List list = null;
    if (content != null && content.size() > 0) {
        list = content;
    } else {
        list = new LinkedList();
        list.add(node.evaluate(req, res));
    }

    // Evaluate phrases & add...
    for (Object o : list) {

        Node n = (Node) ((Node) o).clone();
        NodeProcessor.evaluatePhrases(n, grammar, req, res);

        // If the parent is an element, check if we should
        // carry the parent namespace over to the child...
        if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE
                && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) {
            // We know the parent is an Element w/ a namespace,
            // is the child (also) an Element w/ none?
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) {
                // Yes -- we need to port the namespace forward...
                Namespace nsp = ((Element) p).getNamespace();
                if (log.isTraceEnabled()) {
                    StringBuffer msg = new StringBuffer();
                    msg.append("Adding the following namespace to <").append(n.getName()).append(">:  ")
                            .append(nsp);
                    log.trace(msg.toString());
                }
                NodeProcessor.applyNamespace(nsp, (Element) n);
            }
        }

        // Although they *are* nodes, attributes are not technically
        // content, and therefore they must have special treatment...
        if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) {
            // Add attributes by calling addAttribute on the Element contract...
            ((Element) p).add((Attribute) n);
        } else {
            // Add everything else as 'content'...
            p.content().add(index++, n);
        }

    }

}

From source file:org.dommons.dom.dom4j.XDom4jNode.java

License:Open Source License

/**
 * ??/*from  w  ww  .  ja v a2s.  c om*/
 * @param parent 
 * @param name ???
 * @return 
 */
private static Element element(Branch parent, String name) {
    for (Iterator<Node> it = parent.nodeIterator(); it.hasNext();) {
        Node node = it.next();
        if (node.getNodeType() == Node.ELEMENT_NODE && name.equals(node.getName()))
            return (Element) node;
    }
    return null;
}