Example usage for org.dom4j DocumentFactory createElement

List of usage examples for org.dom4j DocumentFactory createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentFactory createElement.

Prototype

public Element createElement(String name) 

Source Link

Usage

From source file:nc.noumea.mairie.organigramme.services.impl.ExportGraphMLServiceImpl.java

License:Open Source License

private byte[] exportGraphML(EntiteDto entiteDto, Map<String, Boolean> mapIdLiOuvert) {

    DocumentFactory factory = DocumentFactory.getInstance();
    Element root = factory.createElement("graphml");
    Document document = factory.createDocument(root);
    document.setXMLEncoding("utf-8");

    initHeader(root);/* w w  w . j  a v a2 s  .  c  o m*/
    Element graph = initRoot(root);
    buildGraphMlTree(graph, entiteDto, mapIdLiOuvert);

    ByteArrayOutputStream os_writer = new ByteArrayOutputStream();
    try {
        BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(os_writer, "UTF-8"));
        document.write(wtr);
        wtr.flush();
        wtr.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return os_writer.toByteArray();
}

From source file:nl.tue.gale.ae.grapple.CourseListService.java

License:Open Source License

private Element createIMSLIP(Concept c) {
    DocumentFactory df = DocumentFactory.getInstance();
    Element sourcedid = df.createElement("sourcedid");
    Element result = df.createElement("activity");
    Element contentype = result.addElement("contentype");
    contentype.addElement("referential").add(sourcedid);
    sourcedid.addElement("source").addText(galeConfig.getRootGaleUrl() + c.getUriString());
    sourcedid.addElement("id").addText(c.getTitle());
    String camguid = c.getProperty("cam.model.guid");
    if (camguid != null && !"".equals(camguid)) {
        Element field = contentype.addElement("temporal").addElement("temporalfield");
        field.addElement("fieldlabel").addElement("typename").addElement("tyvalue").addText("camguid");
        field.addElement("fielddata").addText(camguid);
    }/*from w w  w  . j av a2 s  .  co m*/
    return result;
}

From source file:nl.tue.gale.ae.processor.view.AbstractView.java

License:Open Source License

protected Element createRowElement(Concept c, int width, boolean bold) {
    DocumentFactory df = DocumentFactory.getInstance();

    Element tr = GaleUtil.createHTMLElement("tr");
    Element td = GaleUtil.createHTMLElement("td").addAttribute("style", "padding-left: " + width + "px;");
    tr.add(td);/*  w w  w . ja v a2s .co  m*/
    if (bold) {
        Element b = GaleUtil.createHTMLElement("b");
        td.add(b);
        td = b;
    }
    Element a = df.createElement(df.createQName("a", "", "http://gale.tue.nl/adaptation"))
            .addAttribute("href", c.getUriString()).addText(c.getTitle());
    td.add(a);
    return tr;
}

From source file:nl.tue.gale.common.XPPEntityReader.java

License:Open Source License

protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException {
    DocumentFactory df = getDocumentFactory();
    Document document = df.createDocument();
    Element parent = null;//from  w  ww.  jav a  2  s.  c  om
    XmlPullParser pp = getXPPParser();
    pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    pp.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false);
    defineEntities(pp);

    while (true) {
        int type = pp.nextToken();

        switch (type) {
        case XmlPullParser.PROCESSING_INSTRUCTION: {
            String text = pp.getText();
            int loc = text.indexOf(" ");

            if (loc >= 0) {
                String target = text.substring(0, loc);
                String txt = text.substring(loc + 1);
                document.addProcessingInstruction(target, txt);
            } else {
                document.addProcessingInstruction(text, "");
            }

            break;
        }

        case XmlPullParser.COMMENT: {
            if (parent != null) {
                parent.addComment(pp.getText());
            } else {
                document.addComment(pp.getText());
            }

            break;
        }

        case XmlPullParser.CDSECT: {
            if (parent != null) {
                parent.addCDATA(pp.getText());
            } else {
                String msg = "Cannot have text content outside of the " + "root document";
                throw new DocumentException(msg);
            }

            break;
        }

        case XmlPullParser.ENTITY_REF:
            if (parent != null) {
                if (pp.getName().equals("gt")) {
                    parent.addText(">");
                } else if (pp.getName().equals("lt")) {
                    parent.addText("<");
                } else if (pp.getName().equals("amp")) {
                    parent.addText("&");
                } else if (pp.getName().equals("quot")) {
                    parent.addText("\"");
                } else
                    parent.addEntity(pp.getName(), "&" + pp.getName() + ";");
            }
            break;

        case XmlPullParser.END_DOCUMENT:
            return document;

        case XmlPullParser.START_TAG: {
            QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace())
                    : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace());
            Element newElement = df.createElement(qname);
            int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
            int nsEnd = pp.getNamespaceCount(pp.getDepth());

            for (int i = nsStart; i < nsEnd; i++) {
                if (pp.getNamespacePrefix(i) != null) {
                    newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i));
                }
            }

            for (int i = 0; i < pp.getAttributeCount(); i++) {
                QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i))
                        : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i),
                                pp.getAttributeNamespace(i));
                newElement.addAttribute(qa, pp.getAttributeValue(i));
            }

            if (parent != null) {
                parent.add(newElement);
            } else {
                document.add(newElement);
            }

            parent = newElement;

            break;
        }

        case XmlPullParser.END_TAG: {
            if (parent != null) {
                parent = parent.getParent();
            }

            break;
        }

        case XmlPullParser.TEXT: {
            String text = pp.getText();

            if (parent != null) {
                parent.addText(text);
            } else {
                String msg = "Cannot have text content outside of the " + "root document";
                throw new DocumentException(msg);
            }

            break;
        }

        default:
            break;
        }
    }
}

From source file:no.met.jtimeseries.service.ServiceDescriptionGenerator.java

License:Open Source License

/**
 * Create a description of the web service offered by a class in XML. The
 * documentation is created by looking at the web service annotations.
 *
 * @param c//from w w w.  ja va 2  s  .c  o  m
 *            The class to generate documentation for.
 * @return A XML document object.
 */
public static Document getXMLServiceDescription(Class<? extends Object> c) {

    List<Method> serviceMethods = getServiceMethods(c);

    DocumentFactory df = new DocumentFactory();
    Document xmlDoc = df.createDocument();

    Element rootElement = df.createElement("services");
    xmlDoc.add(rootElement);
    for (Method m : serviceMethods) {

        Element service = rootElement.addElement("service");
        MethodInfo mi = getMethodInfo(m);
        service.addAttribute("path", mi.path);

        if (mi.produces != null) {
            service.addAttribute("returmMimeType", StringUtils.join(mi.produces, ','));
        }

        if (mi.description != null) {
            service.addAttribute("description", mi.description);
        }

        List<ParameterInfo> params = getParameters(m);
        for (ParameterInfo pi : params) {
            Element param = service.addElement("parameter");
            param.addAttribute("name", pi.name);
            if (pi.defaultValue != null) {
                param.addAttribute("defaultValue", pi.defaultValue);
            }
        }
    }

    return xmlDoc;
}

From source file:org.danann.cernunnos.AbstractContainerTask.java

License:Apache License

private static List<Element> createSuppressEmptySubtasksWarningsList() {
    DocumentFactory fac = new DocumentFactory();
    List<Element> list = new LinkedList<Element>();
    list.add(fac.createElement("org.danann.cernunnos.NoOpTask"));
    return Collections.unmodifiableList(list);
}

From source file:org.danann.cernunnos.runtime.SerializeGrammarTask.java

License:Apache License

private static Node serializeEntry(Entry e, DocumentFactory fac) {

    // Assertions...
    if (e == null) {
        String msg = "Argument 'e [Entry]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }/*  www. j a va2s  . co m*/
    if (fac == null) {
        String msg = "Argument 'fac' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    Element rslt = fac.createElement("entry");
    rslt.addAttribute("type", e.getType().name());

    // Name.
    Element name = fac.createElement("name");
    name.setText(e.getName());
    rslt.add(name);

    // Deprecation.
    if (e.isDeprecated()) {
        Element dep = fac.createElement("deprecation");
        dep.addAttribute("version", e.getDeprecation().getVersion());
        for (Element n : e.getDeprecation().getDescription()) {
            dep.add((Element) n.clone());
        }
        rslt.add(dep);
    }

    // Description.
    Element desc = e.getDescription() != null ? (Element) e.getDescription().clone()
            : fac.createElement("description");
    rslt.add(desc);

    // Formula.
    rslt.add(serializeFormula(e.getFormula(), e.getMappings(), fac));

    // Examples.
    for (Node x : e.getExamples()) {
        rslt.add((Node) x.clone());
    }

    return rslt;

}

From source file:org.danann.cernunnos.runtime.SerializeGrammarTask.java

License:Apache License

private static Node serializeFormula(Formula f, Map<Reagent, Object> mappings, DocumentFactory fac) {

    // Assertions...
    if (f == null) {
        String msg = "Argument 'f [Formula]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }/*from  w ww.j  a  v a 2 s .c o  m*/
    if (fac == null) {
        String msg = "Argument 'fac' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    Element rslt = fac.createElement("formula");

    // Impl.
    rslt.addAttribute("impl", f.getImplementationClass().getName());

    // Reagents.
    Element reagents = fac.createElement("reagents");
    for (Reagent r : f.getReagents()) {
        if (mappings.containsKey(r)) {
            // We don't list it, since there's a final value...
            continue;
        }
        Element reagent = fac.createElement("reagent");
        reagent.addAttribute("name", r.getName());
        reagent.addAttribute("xpath", r.getXpath().getText());
        reagent.addAttribute("reagent-type", r.getReagentType().name());
        reagent.addAttribute("expected-type", r.getExpectedType().getName());
        boolean req = !r.hasDefault();
        reagent.addAttribute("required", req ? "Yes" : "No");
        Element desc = fac.createElement("description");
        if (r.getDescription() != null) {
            desc.setText(r.getDescription());
        }
        reagent.add(desc);
        if (r.isDeprecated()) {
            Element dep = fac.createElement("deprecation");
            dep.addAttribute("version", r.getDeprecation().getVersion());
            for (Element e : r.getDeprecation().getDescription()) {
                dep.add((Element) e.clone());
            }
            reagent.add(dep);
        }
        reagents.add(reagent);
    }
    rslt.add(reagents);

    return rslt;

}

From source file:org.itracker.web.util.ImportExportUtilities.java

License:Open Source License

/**
 * Write the properties to simple XML tags
 * @param writer/*  ww w .j a v a 2 s.  c om*/
 * @param tags
 * @throws IOException
 */
private static void addPropertyTags(XMLWriter writer, Properties tags) throws IOException {
    DocumentFactory factory = getDocumentFactory();
    for (String tag : tags.stringPropertyNames()) {
        Element el = factory.createElement(tag);
        el.setText(tags.getProperty(tag));
        writer.write(el);
    }
}

From source file:org.itracker.web.util.ImportExportUtilities.java

License:Open Source License

/**
 * Write the properties to simple CDATA tags
 * @param writer/*from   w  w  w .  j  ava  2s.  com*/
 * @param tags
 * @throws IOException
 */
private static void addCdataPropertyTags(XMLWriter writer, Properties tags) throws IOException {
    DocumentFactory factory = getDocumentFactory();
    for (String tag : tags.stringPropertyNames()) {
        Element el = factory.createElement(tag);
        el.add(factory.createCDATA(tags.getProperty(tag)));
        writer.write(el);
    }
}