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:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

protected Document _process(Entity entity, View view) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element rootEl = document.addElement("instances");
    encodeEntity(entity, new HashSet<Entity>(), view, rootEl);
    return document;
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

protected Document _process(List<Entity> entities, MetaClass metaClass, View view) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element rootEl = document.addElement("instances");
    for (Entity entity : entities) {
        encodeEntity(entity, new HashSet<Entity>(), view, rootEl);
    }/*from  w  w w  .  j  av  a 2 s  .  c om*/
    return document;
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
public String process(Set<Entity> entities) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element rootEl = document.addElement("instances");
    for (Entity entity : entities) {
        encodeEntity(entity, new HashSet<Entity>(), null, rootEl);
    }/*w  w w .j a  v a  2 s  .com*/
    return documentToString(document);
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
@Nonnull// w w w  .  j  a va 2s.c  o  m
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element resultEl = document.addElement("result");
    if (result instanceof Entity) {
        Entity entity = (Entity) result;
        Document convertedEntity = _process(entity, null);
        resultEl.add(convertedEntity.getRootElement());
    } else if (result instanceof Collection) {
        if (!checkCollectionItemTypes((Collection) result, Entity.class))
            throw new IllegalArgumentException(
                    "Items that are not instances of Entity class found in service method result");
        ArrayList list = new ArrayList((Collection) result);
        MetaClass metaClass = null;
        if (!list.isEmpty())
            metaClass = ((Entity) list.get(0)).getMetaClass();

        Document processed = _process(list, metaClass, null);
        resultEl.add(processed.getRootElement());
    } else {
        if (result != null && resultType != Void.TYPE) {
            Datatype datatype = getDatatype(resultType);
            resultEl.setText(datatype != null ? datatype.format(result) : result.toString());
        } else {
            encodeNull(resultEl);
        }
    }
    return documentToString(document);
}

From source file:com.haulmont.cuba.web.gui.components.presentations.PresentationEditor.java

License:Apache License

protected void commit() {
    Presentations presentations = component.getPresentations();

    Document doc = DocumentHelper.createDocument();
    doc.setRootElement(doc.addElement("presentation"));

    component.saveSettings(doc.getRootElement());

    String xml = Dom4j.writeDocument(doc, false);
    presentation.setXml(xml);//from w  ww.  ja  va 2  s .c  o m

    presentation.setName(nameField.getValue());
    presentation.setAutoSave(autoSaveField.getValue());
    presentation.setDefault(defaultField.getValue());

    User user = sessionSource.getUserSession().getCurrentOrSubstitutedUser();

    boolean userOnly = !allowGlobalPresentations || !BooleanUtils.isTrue(globalField.getValue());
    presentation.setUser(userOnly ? user : null);

    if (log.isTraceEnabled()) {
        log.trace(String.format("XML: %s", Dom4j.writeDocument(doc, true)));
    }

    if (isNew) {
        presentations.add(presentation);
    } else {
        presentations.modify(presentation);
    }
    presentations.commit();

    addCloseListener(e -> {
        if (isNew) {
            component.applyPresentation(presentation.getId());
        }
    });
}

From source file:com.haulmont.yarg.structure.xml.impl.DefaultXmlWriter.java

License:Apache License

@Override
public String buildXml(Report report) {
    try {/*from www.  j  ava  2s.  co m*/
        Document document = DocumentFactory.getInstance().createDocument();
        Element root = document.addElement("report");

        root.addAttribute("name", report.getName());
        writeTemplates(report, root);
        writeInputParameters(report, root);
        writeValueFormats(report, root);
        writeRootBand(report, root);

        StringWriter stringWriter = new StringWriter();
        new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()).write(document);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new ReportingException(e);
    }
}

From source file:com.hazzard.gui.PiNet.java

public void sendCommand(String command, String data) throws Exception {
    Document document;
    document = DocumentHelper.createDocument();
    Element root = document.addElement("commands");
    Element commandElem = root.addElement("command");
    commandElem.setText(command);/*from  w  ww . jav a2s .  c  o m*/

    commandElem.addAttribute("data", data);

    final Writer writer = new StringWriter();
    new XMLWriter(writer).write(root);
    String text = writer.toString().trim();

    DatagramPacket pack = new DatagramPacket(text.getBytes(), text.length(), InetAddress.getByName(group),
            port);
    sock_send.send(pack);
}

From source file:com.heren.turtle.entry.util.LoadUtils.java

License:Open Source License

public static String resultMessage(String receiveMessage, String resultMessageCode, String resultMessage) {
    switch (ModeUtils.whichMode(receiveMessage)) {
    case ModeUtils.TYPE_JSON:
        Map<String, Object> message = new HashMap<>();
        Map<String, Object> headers = new HashMap<>();
        Map<String, Object> payload = new HashMap<>();
        Map<String, Object> errorInfo = new HashMap<>();
        errorInfo.put(GlobalConstant.ERROR_CODE, resultMessageCode);
        errorInfo.put(GlobalConstant.ERROR_MESSAGE, resultMessage);
        message.put(GlobalConstant.HEAD, headers);
        message.put(GlobalConstant.PAYLOAD, payload);
        message.put(GlobalConstant.ERROR_INFO, errorInfo);
        String response = JSON.toJSONString(message);
        receiveMessage = response;//  www. java2s.  c  o m
        break;
    case ModeUtils.TYPE_XML:
        Document document = DocumentHelper.createDocument();
        Element messageEl = document.addElement("Message");
        Element headersEl = messageEl.addElement(GlobalConstant.HEAD);
        Element payloadEl = messageEl.addElement(GlobalConstant.PAYLOAD);
        Element errorInfoEl = messageEl.addElement(GlobalConstant.ERROR_INFO);
        Element errorCodeEl = errorInfoEl.addElement(GlobalConstant.ERROR_CODE);
        errorCodeEl.setText(resultMessageCode);
        Element errorMessageEl = errorInfoEl.addElement(GlobalConstant.ERROR_MESSAGE);
        errorMessageEl.setText(resultMessage);
        receiveMessage = document.asXML();
        break;
    case ModeUtils.TYPE_UN_DEFINITION:
        break;
    default:
        break;
    }
    return receiveMessage;
}

From source file:com.heren.turtle.gear.meta.AbstractService.java

License:Open Source License

protected String createXml(List<Map<String, Object>> paramsList, String resultCode, String errorMsg,
        String itemName) {//from   w w  w .  j  av  a  2s .c o m
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element response = document.addElement("response");
        for (Map<String, Object> map : paramsList) {
            Element itemEle = response.addElement(itemName);
            for (String key : map.keySet()) {
                Element element = itemEle.addElement(key);
                element.setText(String.valueOf(map.get(key)));
            }
        }
        Element resultCodeEle = response.addElement("resultCode");
        resultCodeEle.setText(resultCode);
        Element errorMsgEle = response.addElement("errorMsg");
        errorMsgEle.setText(errorMsg);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") : null;
}

From source file:com.heren.turtle.gear.meta.AbstractService.java

License:Open Source License

protected String createExamXml(List<Map<String, Object>> paramsList, String resultCode, String errorMsg,
        String itemName) {//from w  w w  .  ja  v  a2 s. co m
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element response = document.addElement("response");
        for (Map<String, Object> map : paramsList) {
            Element itemEle = response.addElement(itemName);
            for (String key : map.keySet()) {
                if (key.equals("1")) {
                    Element elecList = itemEle.addElement("elecList");
                    Element elecItem = elecList.addElement("elecItem");
                    Element element = elecItem.addElement(key);
                    element.setText(String.valueOf(map.get(key)));
                } else {
                    Element ucgList = itemEle.addElement("ucgList");
                    Element ucgItem = ucgList.addElement("ucgItem");
                    Element element = ucgItem.addElement(key);
                    element.setText(String.valueOf(map.get(key)));
                }
            }
        }
        Element resultCodeEle = response.addElement("resultCode");
        resultCodeEle.setText(resultCode);
        Element errorMsgEle = response.addElement("errorMsg");
        errorMsgEle.setText(errorMsg);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML() : null;
}