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: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 w w . j a  v a2 s.co 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.cuba.web.gui.components.WebAbstractTable.java

License:Apache License

@Override
public void applySettings(Element element) {
    if (!isSettingsEnabled()) {
        return;//from   w w w .j  a va  2s .c o  m
    }

    if (defaultSettings == null) {
        // save default view before apply custom
        defaultSettings = DocumentHelper.createDocument();
        defaultSettings.setRootElement(defaultSettings.addElement("presentation"));

        saveSettings(defaultSettings.getRootElement());
    }

    String textSelection = element.attributeValue("textSelection");
    if (StringUtils.isNotEmpty(textSelection)) {
        component.setTextSelectionEnabled(Boolean.parseBoolean(textSelection));

        if (component.getPresentations() != null) {
            component.getPresentations().updateTextSelection();
        }
    }

    final Element columnsElem = element.element("columns");
    if (columnsElem != null) {
        boolean refreshWasEnabled = component.disableContentBufferRefreshing();

        Collection<String> modelIds = new LinkedList<>();
        for (Object column : component.getVisibleColumns()) {
            modelIds.add(String.valueOf(column));
        }

        Collection<String> loadedIds = new LinkedList<>();
        for (Element colElem : Dom4j.elements(columnsElem, "columns")) {
            loadedIds.add(colElem.attributeValue("id"));
        }

        Configuration configuration = AppBeans.get(Configuration.NAME);
        ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);

        if (clientConfig.getLoadObsoleteSettingsForTable()
                || CollectionUtils.isEqualCollection(modelIds, loadedIds)) {
            applyColumnSettings(element);
        }

        component.enableContentBufferRefreshing(refreshWasEnabled);
    }
}

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

public void sendCommand(String command, String data) throws Exception {
    Document document;/*from  w w  w  . ja  va2  s . c  om*/
    document = DocumentHelper.createDocument();
    Element root = document.addElement("commands");
    Element commandElem = root.addElement("command");
    commandElem.setText(command);

    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;//from   w w  w  . j  a  va 2 s .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 .jav  a2s. 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) {/*www .  ja v  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()) {
                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;
}

From source file:com.heren.turtle.server.service.Summoner.java

License:Open Source License

/**
 * create list xml//w  ww  .j ava  2s  .c  o  m
 *
 * @param params
 * @param listElement
 * @return
 */
public String listCreateXml(List<Map<String, Object>> params, String listElement) {
    Document document = null;
    try {
        if (params != null && params.size() > 0) {
            document = DocumentHelper.createDocument();
            Element root = document.addElement("payload");
            Element response = root.addElement("response");
            for (Map<String, Object> param : params) {
                Element dept = response.addElement(listElement);
                param.keySet().stream().forEach(key -> {
                    Element element = dept.addElement(key);
                    element.setText(String.valueOf(param.get(key)));
                });
            }
            Element userId = response.addElement("user_id");
            userId.setText("0001");
        } else {
            document = DocumentHelper.createDocument();
            Element root = document.addElement("payload");
            Element response = root.addElement("response");
            Element userId = response.addElement("user_id");
            userId.setText("0001");
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML() : null;
}

From source file:com.heren.turtle.server.service.Summoner.java

License:Open Source License

/**
 * create normal xml//  w  w  w .java  2s  . c o  m
 *
 * @param params
 * @return
 */
public String createXml(Map<String, Object> params) {
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        params.keySet().stream().forEach(key -> {
            Element element = response.addElement(key);
            element.setText(String.valueOf(params.get(key)));
        });
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML() : null;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back failure information//w ww.  ja va  2 s .  c om
 *
 * @param message
 * @return
 */
public static String errorMessage(String message) {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    resultText.setText(message == null ? "" : message);
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back correct information/*  w  ww  .  j av  a 2s . com*/
 *
 * @param flag 0 no data 1 some exception
 * @return
 */
public static String correctMessage(int flag) {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    switch (flag) {
    case 0:
        resultText.setText("no data found!");
        break;
    case -1:
        resultText.setText("success!");
        break;
    default:
        break;
    }
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}