Example usage for org.dom4j Branch content

List of usage examples for org.dom4j Branch content

Introduction

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

Prototype

List<Node> content();

Source Link

Document

Returns the content nodes of this branch as a backed List so that the content of this branch may be modified directly using the List interface.

Usage

From source file:com.itextpdf.rups.view.itext.treenodes.XdpTreeNode.java

License:Open Source License

/**
 * Constructs an XdpTreeNode/*from w ww .j a  v  a  2s . c om*/
 * @param node   the XML node
 */
@SuppressWarnings("unchecked")
public XdpTreeNode(Node node) {
    super(null, node);
    if (node instanceof Element) {
        Element element = (Element) node;
        addChildNodes(element.attributes());
    }
    if (node instanceof Branch) {
        Branch branch = (Branch) node;
        addChildNodes(branch.content());
    }
    if (node instanceof Attribute) {
        icon = IconFetcher.getIcon("attribute.png");
        return;
    }
    if (node instanceof Text) {
        icon = IconFetcher.getIcon("text.png");
        return;
    }
    if (node instanceof ProcessingInstruction) {
        icon = IconFetcher.getIcon("pi.png");
        return;
    }
    if (node instanceof Document) {
        icon = IconFetcher.getIcon("xfa.png");
        return;
    }
    icon = IconFetcher.getIcon("tag.png");
}

From source file:com.lowagie.rups.view.itext.treenodes.XdpTreeNode.java

License:Open Source License

/**
 * Constructs an XdpTreeNode//  ww  w.  java 2 s.  c o  m
 * @param node   the XML node
 */
public XdpTreeNode(Node node) {
    super(null, node);
    if (node instanceof Element) {
        Element element = (Element) node;
        addChildNodes(element.attributes());
    }
    if (node instanceof Branch) {
        Branch branch = (Branch) node;
        addChildNodes(branch.content());
    }
    if (node instanceof Attribute) {
        icon = IconFetcher.getIcon("attribute.png");
        return;
    }
    if (node instanceof Text) {
        icon = IconFetcher.getIcon("text.png");
        return;
    }
    if (node instanceof ProcessingInstruction) {
        icon = IconFetcher.getIcon("pi.png");
        return;
    }
    if (node instanceof Document) {
        icon = IconFetcher.getIcon("xfa.png");
        return;
    }
    icon = IconFetcher.getIcon("tag.png");
}

From source file:freemarker.ext.xml._Dom4jNavigator.java

License:Apache License

private void getDescendants(Branch node, List result) {
    List content = node.content();
    for (Iterator iter = content.iterator(); iter.hasNext();) {
        Node subnode = (Node) iter.next();
        if (subnode instanceof Element) {
            result.add(subnode);// www . java2s .c o m
            getDescendants(subnode, result);
        }
    }
}

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;
    int index;/*  w  ww  . j ava 2  s.  co  m*/
    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;
    int index;//  w w w  . ja va2s . c om
    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.hudsonci.xpath.impl.Rewriter.java

License:Open Source License

private boolean nodeContains(Node parent, Node node) {
    if (parent == node)
        return true;
    if (parent instanceof Branch) {
        Branch branch = (Branch) parent;
        List children = branch.content();
        for (int i = 0, n = children.size(); i < n; i++) {
            boolean contains = nodeContains((Node) children.get(i), node);
            if (contains)
                return true;
        }// ww w  .java 2  s.  c o m
    }
    return false;
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public static void surroundInsideBy(Branch branch, Branch wrapper)
        throws InvocationTargetException, IllegalAccessException {
    surroundBy(new ArrayList<Node>((List<Node>) branch.content()), wrapper);
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public static <T> Collection<T> selectTypedNodes(Class<T> type, Branch root, boolean preorder) {
    Set<T> nodes = new LinkedHashSet<T>();
    if (preorder && type.isInstance(root)) {
        nodes.add((T) root);/*from   w  w  w .  j a  v  a  2 s.  c o m*/
    }
    for (Node node : (List<Node>) root.content()) {
        if (node instanceof Branch) {
            nodes.addAll(selectTypedNodes(type, (Branch) node, preorder));
        } else if (type.isInstance(node)) {
            nodes.add((T) node);
        }
    }
    if (!preorder && type.isInstance(root)) {
        nodes.add((T) root);
    }
    return nodes;
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public static void mergeTexts(Branch root) {
    List<Node> nodes = (List<Node>) root.content();
    for (int i = nodes.size() - 2; i >= 0; i--) {
        Node node = nodes.get(i);
        Node next = nodes.get(i + 1);
        if (node instanceof Text && next instanceof Text) {
            node.setText(node.getText() + next.getText());
            next.detach();/*  ww  w  .j  av a  2s  . c o m*/
        }
    }
    for (Node node : (List<Node>) root.content()) {
        if (node instanceof Branch) {
            mergeTexts((Branch) node);
        }
    }
}

From source file:org.withinsea.izayoi.cortile.core.compile.dom.DOMCompiler.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
protected void compile(Node node) throws CortileException {

    CompileContext ctx = CompileContext.get();

    if (node instanceof Text) {

        Text text = (Text) node;
        for (TextGrammar tg : textGrammars) {
            if (isDetached(text))
                return;
            if (tg.acceptText(text)) {
                tg.processText(text);/*w w w . j  ava2s .  com*/
            }
        }

    } else if (node instanceof Comment) {

        Comment comment = (Comment) node;
        for (CommentGrammar cg : commentGrammars) {
            if (isDetached(comment))
                return;
            if (cg.acceptComment(comment)) {
                cg.processComment(comment);
            }
        }

    } else if (node instanceof Branch) {

        ctx.openScope();

        inScope: {
            if (node instanceof Element) {

                Element element = (Element) node;
                for (ElementGrammar eg : elementGrammars) {
                    if (isDetached(element))
                        break inScope;
                    if (eg.acceptElement(element)) {
                        eg.processElement(element);
                    }
                }

                for (Map.Entry<Integer, Map<String, List<AttrGrammar>>> entry : attrGrammars.entrySet()) {
                    Map<String, List<AttrGrammar>> ags = entry.getValue();
                    List<Attribute> attrs = new ArrayList<Attribute>((List<Attribute>) element.attributes());
                    for (Attribute attr : attrs) {
                        if (isDetached(element))
                            break inScope;
                        if (isDetached(attr))
                            continue;
                        Set<AttrGrammar> nsAgs = new LinkedHashSet<AttrGrammar>();
                        {
                            nsAgs.addAll(ags.get("*"));
                            nsAgs.addAll(ags.get(attr.getNamespacePrefix()));
                        }
                        for (AttrGrammar ag : nsAgs) {
                            if (isDetached(element))
                                break inScope;
                            if (isDetached(attr))
                                break;
                            if (ag.acceptAttr(attr)) {
                                ag.processAttr(attr);
                            }
                        }
                    }
                }
            }

            Branch branch = (Branch) node;
            List<Node> cnodes = new ArrayList<Node>((List<Node>) branch.content());
            for (Node cnode : cnodes) {
                if (isDetached(branch))
                    break inScope;
                if (isDetached(cnode))
                    continue;
                compile(cnode);
            }
        }

        ctx.closeScope();
    }
}