Example usage for org.dom4j Element content

List of usage examples for org.dom4j Element content

Introduction

In this page you can find the example usage for org.dom4j Element 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:nl.tue.gale.ae.processor.xmlmodule.ExampleModule.java

License:Open Source License

@Override
public Element traverse(Element element, Resource resource) throws ProcessorException {
    Element pre = createHTMLElement("pre");
    @SuppressWarnings("unchecked")
    List<Node> content = (List<Node>) element.content();
    content = ImmutableList.copyOf(content);
    for (Node node : content) {
        node.detach();/*from w w  w. j  a  va2 s .co m*/
        pre.add(node);
    }
    replaceNode(element, pre);
    return pre;
}

From source file:nl.tue.gale.ae.processor.xmlmodule.ForModule.java

License:Open Source License

@SuppressWarnings("unchecked")
public Element traverse(Element element, Resource resource) throws ProcessorException {
    try {/* w w  w.j  a v a  2s. c o  m*/
        GaleContext gale = GaleContext.of(resource);
        String expr = element.attributeValue("expr");
        String var = element.attributeValue("var");
        List<Object> alist = new LinkedList<Object>();
        Object list = gale.eval(expr);
        if (list.getClass().isArray())
            for (Object o : (Object[]) list)
                alist.add(o);
        else
            for (Object o : (Iterable<Object>) list)
                alist.add(o);
        if (alist.size() == 0) {
            element.getParent().remove(element);
            return null;
        }

        // optionally sort the array
        String sort = element.attributeValue("sort");
        if (sort != null && !"".equals(sort)) {
            final boolean ascending = ("false".equals(element.attributeValue("ascending")) ? false : true);
            List<Object[]> slist = new ArrayList<Object[]>();
            for (Object o : alist) {
                Object result = null;
                if (o instanceof Concept) {
                    CacheSession<EntityValue> session = gale.openUmSession();
                    session.setBaseUri(((Concept) o).getUri());
                    result = gale.cm().evaluate(gale.cr(), sort,
                            Argument.of("gale", "nl.tue.gale.ae.GaleContext", gale, "session",
                                    "nl.tue.gale.common.cache.CacheSession", session, "value",
                                    "nl.tue.gale.dm.data.Concept", o));
                } else {
                    CacheSession<EntityValue> session = gale.openUmSession();
                    result = gale.cm().evaluate(gale.cr(), sort,
                            Argument.of("gale", "nl.tue.gale.ae.GaleContext", gale, "session",
                                    "nl.tue.gale.common.cache.CacheSession", session, "value",
                                    o.getClass().getName(), o));
                }
                slist.add(new Object[] { result, o });
            }
            Collections.sort(slist, new Comparator<Object[]>() {
                public int compare(Object[] arg0, Object[] arg1) {
                    Object o1 = arg0[0];
                    Object o2 = arg1[0];
                    if (!ascending) {
                        Object temp = o1;
                        o1 = o2;
                        o2 = temp;
                    }
                    if (o1 == null)
                        return (o2 == null ? 0 : -1);
                    if (o2 == null)
                        return 1;
                    if (o1 instanceof Number)
                        return (new Double(((Number) o1).doubleValue()))
                                .compareTo(new Double(((Number) o2).doubleValue()));
                    return o1.toString().compareTo(o2.toString());
                }
            });
            alist.clear();
            for (Object[] o : slist)
                alist.add(o[1]);
        }

        // process for-loop
        String guid = GaleUtil.newGUID();
        resource.put(guid, alist.toArray());

        Element parent = element.getParent();
        int index = parent.indexOf(element);
        Pattern p = Pattern.compile("\\Q%" + var + "\\E\\W");
        for (int i = 0; i < alist.size(); i++) {
            Element clone = element.createCopy();
            Object object = ((Object[]) resource.get(guid))[i];
            String type = object.getClass().getName();
            if (type.indexOf("_$$_") >= 0)
                type = type.substring(0, type.indexOf("_$$_"));
            if (object instanceof Concept) {
                replace(clone, p, ((Concept) object).getUri().toString());
            } else if (object instanceof String) {
                replace(clone, p, (String) object);
            } else if (object instanceof Number || object instanceof Boolean) {
                replace(clone, p, object.toString());
            } else {
                replace(clone, p,
                        "((" + type + ")((Object[])gale.getResource().get(\"" + guid + "\"))[" + i + "])");
            }
            processor.traverseChildren(clone, resource);
            for (Node n : (List<Node>) clone.content()) {
                parent.content().add(index, n);
                index++;
            }
        }
        element.detach();
    } catch (Exception e) {
        e.printStackTrace();
        return (Element) GaleUtil.replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]"));
    }
    return null;
}

From source file:nl.tue.gale.ae.processor.xmlmodule.HTMLModule.java

License:Open Source License

@SuppressWarnings("unchecked")
public Element traverse(Element element, Resource resource) throws ProcessorException {
    Element head = element.element("head");
    if (head == null) {
        head = element.addElement("head");
        head.detach();//from  w w w. j  ava  2  s.  c o  m
        element.content().add(0, head);
    }
    traverse_head(head, resource);
    return element;
}

From source file:nl.tue.gale.ae.processor.xmlmodule.HTMLModule.java

License:Open Source License

@SuppressWarnings("unchecked")
private void traverse_head(Element head, Resource resource) {
    GaleContext gale = GaleContext.of(resource);
    String css = (String) gale.cfgm().getObject("gale://gale.tue.nl/config/presentation#css", resource);
    Element base = head.element("base");
    if (base == null)
        base = head.addElement("base").addAttribute("href", getBaseLocation(resource));
    base.detach();//from   ww  w . j  av a  2s . com
    head.content().add(0, base);
    for (String cssPart : css.split(";")) {
        Element ecss = head.addElement("link").addAttribute("rel", "stylesheet")
                .addAttribute("type", "text/css").addAttribute("href", cssPart);
        ecss.detach();
        head.content().add((cssPart.contains("${home}") ? 0 : 1), ecss);
    }
    String titleValue = (String) gale.cfgm().getObject("gale://gale.tue.nl/config/presentation#title",
            resource);
    Element title = head.addElement("title").addText(titleValue);
    title.detach();
    head.content().add(0, title);
}

From source file:nl.tue.gale.ae.processor.xmlmodule.IfModule.java

License:Open Source License

@SuppressWarnings("unchecked")
public Element traverse(Element element, Resource resource) throws ProcessorException {
    try {//from w w  w  .  j a  v  a 2 s  .  c  o m
        GaleContext gale = GaleContext.of(resource);
        String expr = element.attributeValue("expr");
        Element block = null;
        List<Element> blocks = new LinkedList<Element>();
        blocks.addAll(element.elements("block"));
        if (element.element("then") != null)
            blocks.add(element.element("then"));
        if (element.element("else") != null)
            blocks.add(element.element("else"));
        if (blocks.size() == 0)
            blocks.add(element);
        if (((Boolean) gale.eval(expr)).booleanValue())
            block = (Element) blocks.get(0);
        else if (blocks.size() > 1)
            block = (Element) blocks.get(1);
        if (block == null) {
            element.detach();
            return null;
        }
        processor.traverseChildren(block, resource);
        List<Node> content = (List<Node>) element.getParent().content();
        int index = content.indexOf(element);
        for (Node node : (List<Node>) block.content()) {
            content.add(index, node);
            index++;
        }
        content.remove(element);
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return (Element) GaleUtil.replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]"));
    }
}

From source file:nl.tue.gale.ae.processor.xmlmodule.MCModule.java

License:Open Source License

@SuppressWarnings("unchecked")
private Question readQuestion(Element element) {
    Element q = DocumentFactory.getInstance().createElement("span", xhtmlns);
    for (Node n : (List<Node>) element.content()) {
        boolean b = true;
        if (n instanceof Element)
            if (((Element) n).getName().equals("answer") || ((Element) n).getName().equals("concept_relation"))
                b = false;/* w  ww. ja  va 2 s .c  o m*/
        if (b)
            q.add((Node) n.clone());
    }
    Question result = (new Question()).setQuestion(q).setCount(new Integer(element.attributeValue("answers")))
            .setRight(new Integer(element.attributeValue("right")));
    //Check if the question has an ID.
    if (element.attributeValue("id") != null && !"".equals(element.attributeValue("id")))
        result.setId(new Integer(element.attributeValue("id")));
    if ("checkbox".equals(element.attributeValue("type")))
        result.setType(1);
    for (Element a : (List<Element>) element.elements("answer"))
        result.getAnswers().add(readAnswer(a));

    /*
     * Created by: Vinicius Ramos
     * DATE: Sept 13th 2012
     * 
     * READ the concepts in XHTML
     */
    int v = 0;
    //Read in xml file all the concepts related to the question
    for (Element c : (List<Element>) element.elements("concept_relation")) {
        result.getConcepts().add(c.getText());
    }

    return result;
}

From source file:nl.tue.gale.ae.processor.xmlmodule.MCModule.java

License:Open Source License

@SuppressWarnings("unchecked")
private Answer readAnswer(Element element) {
    Element a = DocumentFactory.getInstance().createElement("span", xhtmlns);
    for (Node n : (List<Node>) element.content()) {
        boolean b = true;
        if (n instanceof Element)
            if (((Element) n).getName().equals("explain"))
                b = false;/*from   w  ww  .ja v a  2s  .  co m*/
        if (b)
            a.add((Node) n.clone());
    }
    Answer result = (new Answer()).setAnswer(a).setExplain(element.element("explain"))
            .setCorrect(new Boolean(element.attributeValue("correct")));
    //Check if the answer has an ID.
    if (element.attributeValue("id") != null && !"".equals(element.attributeValue("id")))
        result.setId(new Integer(element.attributeValue("id")));

    return result;
}

From source file:nl.tue.gale.ae.processor.xmlmodule.RepositoryModule.java

License:Open Source License

private void moveContent(Element source, Element target) {
    @SuppressWarnings("unchecked")
    List<Node> content = (List<Node>) source.content();
    for (Node node : ImmutableList.copyOf(content)) {
        node.detach();//from  ww  w  .j av a2  s  .c om
        target.add(node);
    }
}

From source file:nl.tue.gale.ae.processor.xmlmodule.RepositoryModule.java

License:Open Source License

private void removeTextNodes(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> content = (List<Node>) element.content();
    for (Node node : ImmutableList.copyOf(content))
        if (node.getNodeType() == Node.TEXT_NODE)
            content.remove(node);/*from w ww. j a  v  a 2s .  c o  m*/
}

From source file:nl.tue.gale.ae.processor.xmlmodule.TextModule.java

License:Open Source License

@Override
public Element traverse(Element element, Resource resource) throws ProcessorException {
    GaleContext gale = GaleContext.of(resource);

    if (!includeBlock(element, resource))
        return (Element) replaceNode(element, null);

    // find possible class
    String classexpr = null;/*from   ww w  .  j ava2  s  .  c  o  m*/
    EntityValue ev = gale.um()
            .get(URIs.builder().uri(gale.conceptUri()).userInfo(gale.userId()).fragment("tags.class").build());
    if (ev != null)
        classexpr = ev.getValueString();
    if (classexpr != null && !"".equals(classexpr)) {
        CacheSession<EntityValue> session = gale.um().openSession();
        try {
            String cl = (String) gale.cm().evaluate(gale.cr(), classexpr,
                    Argument.of("gale", "nl.tue.gale.ae.GaleContext", gale, "session",
                            "nl.tue.gale.common.cache.CacheSession", session, "element", "org.dom4j.Element",
                            element));
            if (cl != null)
                element.addAttribute("class", cl);
        } catch (Exception e) {
            e.printStackTrace();
            return (Element) replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]"));
        }
    }

    // create result element
    Element result;
    if (element.attributeValue("class") != null)
        result = createHTMLElement("div").addAttribute("class", element.attributeValue("class"));
    else if (element.attributeValue("id") != null)
        result = createHTMLElement("div").addAttribute("id", element.attributeValue("id"));
    else
        result = createHTMLElement("span");

    // handle textual content
    TextHandler handler = handlerMap.get(element.attributeValue("type"));
    if (handler != null) {
        processor.traverseChildren(element, resource);
        handler.handleTextElement(element);
    }

    // copy the content to the new node
    @SuppressWarnings("unchecked")
    List<Node> content = ImmutableList.copyOf((List<Node>) element.content());
    for (Node node : content) {
        node.detach();
        result.add(node);
    }
    replaceNode(element, result);
    return result;
}