Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:org.forzaframework.util.XmlUtils.java

License:Apache License

public static Document buildDocument(Document doc, List list, Integer size, String[] elements)
        throws Exception {
    Element root = doc.addElement("items");
    root.addAttribute("success", "true");
    doc.setRootElement(root);//  w  ww . j a  v a  2s  . c om

    for (Object bean : list) {
        Element item = root.addElement("item");
        buildElement(item, bean, elements);
    }
    root.addElement("totalCount").addText(String.valueOf(size));

    return doc;
}

From source file:org.forzaframework.util.XmlUtils.java

License:Apache License

public static Document buildDocument(Document doc, List list, Integer size, Map<String, String> elements,
        Map<String, String> emptyElement) throws Exception {
    Element root = doc.addElement("items");
    root.addAttribute("success", "true");
    doc.setRootElement(root);//from   www . j  a  va 2 s.  c  om

    if (emptyElement != null) {
        Element item = root.addElement("item");
        for (Map.Entry<String, String> e : emptyElement.entrySet()) {
            item.addElement(e.getKey()).addText(e.getValue());
        }
    }

    for (Object bean : list) {
        Element item = root.addElement("item");
        buildElement(item, bean, elements);
    }
    root.addElement("totalCount").addText(String.valueOf(size));

    return doc;
}

From source file:org.forzaframework.util.XmlUtils.java

License:Apache License

public static Document buildDocumentFromMap(List<Map> list) {
    Document doc = DocumentHelper.createDocument();
    doc.setXMLEncoding("ISO-8859-1");

    Element root = doc.addElement("items");
    root.addAttribute("success", "true");
    doc.setRootElement(root);//from   w ww. j  a v  a 2  s.c  om

    for (Map map : list) {
        Element el = DocumentHelper.createElement("item");
        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            if (entry.getKey().toString().equals("$type$"))
                continue;

            el.addElement(entry.getKey().toString())
                    .addText(entry.getValue() == null ? "" : entry.getValue().toString());
        }
        root.add(el);
    }

    return doc;
}

From source file:org.forzaframework.validation.Information.java

License:Apache License

public String toXml() {
    Document doc = DocumentHelper.createDocument();
    Element info = doc.addElement("information");
    info.addAttribute("success", this.getSuccess().toString());
    info.addAttribute("entityId", entityId);
    if (!this.simpleFields.isEmpty()) {
        for (Map.Entry<String, String> simpleField : simpleFields.entrySet()) {
            info.addAttribute(simpleField.getKey(), simpleField.getValue());
        }//from   ww  w . j av a2  s  . com
    }

    if (this.messages.size() > 0) {
        Element messages = info.addElement("messages");
        for (String message : this.messages) {
            messages.addElement("message").addCDATA(message);
        }
    }
    if (errors.size() > 0) {
        Element fieldEl = info.addElement("field");
        StringBuffer message = new StringBuffer();
        for (Error error : errors) {
            message.append(error.getMessage()).append("<br />");
        }
        fieldEl.addElement("msg").addCDATA(message.toString());
    }
    if (fields.size() > 0) {
        Element fieldsEl = info.addElement("fields");
        for (Field field : fields) {
            Element fieldEl = fieldsEl.addElement("field");
            fieldEl.addElement("id").addText(field.getId() == null ? "" : field.getId());
            fieldEl.addElement("code").addText(field.getCode() == null ? "" : field.getCode());
            StringBuffer message = new StringBuffer();
            for (Error error : field.getErrors()) {
                message.append(error.getMessage()).append("<br />");
            }
            fieldEl.addElement("msg").addCDATA(message.toString());
        }
    }
    return doc.asXML();
}

From source file:org.forzaframework.web.servlet.view.ListXmlView.java

License:Apache License

protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Document doc = DocumentHelper.createDocument();

    String query = request.getParameter("query");
    String id = request.getParameter("id");

    if (StringUtils.isNotBlank(id)) {
        SystemEntity entity = getSystemEntity(request, model);
        BaseEntity object = (BaseEntity) entityManager.get(entity.getType(), Long.valueOf(id));
        Element element = object.toXml();
        Element root = doc.addElement("response");
        root.addAttribute("success", "true");
        root.add(element);// w ww  .java 2s.c o  m
        doc.setRootElement(root);

    } else if (request.getParameter("id") != null) {
        XmlUtils.buildEmptyListDocument(doc);
    } else {
        List<? extends BaseEntity> list;
        if (request.getParameter("e").equals("entity") || request.getParameter("e").equals("externalSystem")) {
            if (request.getParameter("e").equals("entity")) {
                list = systemConfiguration.getSystemEntities();
            } else {
                list = systemConfiguration.getExternalSystems();
            }
        } else {
            SystemEntity entity = getSystemEntity(request, model);
            org.hibernate.Criteria crit = entityManager.getHibernateSession().createCriteria(entity.getType());
            if (entity.findAttribute("name") != null && query != null) {
                crit.add(org.hibernate.criterion.Restrictions.like("name", "%" + query + "%").ignoreCase());
            }
            if (StringUtils.isNotBlank(request.getParameter("orderBy"))) {
                crit.addOrder(Order.asc(request.getParameter("orderBy")));
            }
            list = crit.list();
        }
        if (StringUtils.isNotBlank(request.getParameter("sort"))) {
            Collections.sort(list,
                    new AlphanumBeanComparator(request.getParameter("sort"), request.getParameter("dir")));
        }
        List<? extends BaseEntity> objects = CollectionUtils.paginate(list, request.getParameterMap(), false);

        XmlUtils.buildDocument(doc, objects, list.size());
    }

    response.setContentType("text/xml");

    response.getWriter().write(doc.asXML());

}

From source file:org.geolatte.common.automapper.MappingsGenerator.java

License:Open Source License

private Element addRoot(Document doc) {
    Element root = doc.addElement("hibernate-mapping");
    root.addAttribute("package", databaseMapping.getPackageName());
    return root;// w w  w.  ja  va  2 s .  com
}

From source file:org.goodoldai.jeff.report.xml.XMLReportBuilder.java

License:Open Source License

/**
 * Creates a report based on the provided explanation and writes it to the
 * provided object that is type of org.dom4j.Document before it is written
 * in the file/*from   ww w. ja v  a2  s.  c  om*/
 * 
 * @param explanation
 *            the explanation that needs to be transformed into a report
 * @param stream
 *            output stream to which the report is to be written
 * 
 * @throws org.goodoldai.jeff.explanation.ExplanationException
 *             if any of the arguments are null
 */
@Override
public void buildReport(Explanation explanation, Object stream) {

    if (explanation == null) {
        throw new ExplanationException("The entered explanation must not be null");
    }

    if (stream == null) {
        throw new ExplanationException("The entered stream must not be null");
    }

    if (!(stream instanceof PrintWriter)) {
        throw new ExplanationException("The argument 'stream' must be the type of java.io.PrintWriter");
    }

    Document document = DocumentHelper.createDocument();
    document.addElement("explanation");

    insertHeader(explanation, document);

    ArrayList<ExplanationChunk> chunks = explanation.getChunks();

    for (int i = 0; i < chunks.size(); i++) {

        ExplanationChunk chunk = chunks.get(i);
        ReportChunkBuilder cbuilder = factory.getReportChunkBuilder(chunk);

        cbuilder.buildReportChunk(chunk, document, isInsertChunkHeaders());
    }

    XMLWriter writer = new XMLWriter(((PrintWriter) stream));
    try {
        writer.write(document);
        writer.close();
    } catch (IOException ex) {
        throw new ExplanationException(
                "The file could not be writen due to fallowing IO error: " + ex.getMessage());
    }
}

From source file:org.gradle.api.tasks.ide.eclipse.EclipseClasspath.java

License:Apache License

private Document createXmlDocument() {
    Document document = DocumentFactory.getInstance().createDocument();
    Element root = document.addElement(CLASSPATH);

    addSrcDirs(root);//  ww  w  . j av a 2s .  c o  m
    addTestSrcDirs(root);
    classpathEntry(root, CON, DEFAULT_JRE_CONTAINER);
    addDependsOnProjects(root);
    addLibs(root);

    return document;
}

From source file:org.gradle.api.tasks.ide.eclipse.EclipseProject.java

License:Apache License

private Document createXmlDocument() {
    Document document = DocumentFactory.getInstance().createDocument();
    Element root = document.addElement("projectDescription");
    root.addElement("name").setText(projectName);
    root.addElement("comment");
    root.addElement("projects");
    addBuildSpec(root);/*w  w  w  . j  a  v  a2s  .  c o  m*/
    addNatures(root);
    return document;
}

From source file:org.gradle.api.tasks.ide.eclipse.EclipseUtil.java

License:Apache License

static void addFacet(Document document, String facetType, Attribute... attributes) {
    Element root;/* w  w  w  . j  a  v a2  s  . c  om*/

    if (document.getRootElement() == null
            || (root = ((Element) document.selectSingleNode("//faceted-project"))) == null) {
        root = document.addElement("faceted-project");
    }

    Element facet = root.addElement(facetType);

    for (Attribute attribute : attributes) {
        facet.add(attribute);
    }
}