Example usage for org.dom4j DocumentHelper createDocument

List of usage examples for org.dom4j DocumentHelper createDocument

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createDocument.

Prototype

public static Document createDocument() 

Source Link

Usage

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 a2 s. c o m

    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  w w  w.  java2s  . c o  m*/
    }

    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  w  w .  j a  va  2  s. c om
        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 Document initializeDocument() {
    Document doc = DocumentHelper.createDocument();
    doc.addDocType("hibernate-mapping", "-//Hibernate/Hibernate Mapping DTD 3.0//EN",
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd");
    return doc;//from   w  w w  .  java 2 s. c  o  m
}