Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

In this page you can find the example usage for org.dom4j Element 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.laudandjolynn.mytv.MyTvData.java

License:Apache License

public void writeData(String parent, String tag, String value) {
    logger.debug("write data to my tv data file: " + Constant.MY_TV_DATA_FILE_PATH);
    File file = new File(Constant.MY_TV_DATA_FILE_PATH);
    if (!file.exists()) {
        Document doc = DocumentHelper.createDocument();
        doc.addElement(Constant.APP_NAME);
        try {//  w  ww  .  ja  v a2 s.c  om
            FileUtils.writeWithNIO(doc.asXML().getBytes(), Constant.MY_TV_DATA_FILE_PATH);
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    }
    SAXReader reader = new SAXReader();
    try {
        Document xmlDoc = reader.read(file);
        Element parentElement = xmlDoc.getRootElement();
        if (parent != null) {
            List<?> nodes = xmlDoc.selectNodes("//" + parent);
            if (nodes != null && nodes.size() > 0) {
                parentElement = (Element) nodes.get(0);
            }
        }
        parentElement.addElement(tag).setText(value);
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(file));
            writer.write(xmlDoc);
            writer.close();
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    } catch (DocumentException e) {
        String msg = "can't parse xml file. -- " + Constant.MY_TV_DATA_FILE_PATH;
        throw new MyTvException(msg);
    }
}

From source file:com.liferay.alloy.tools.transformer.AlloyDocsTransformer.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("components");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));
        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element descriptionNode = componentNode.addElement("description");
        descriptionNode.addCDATA(component.getDescription());
        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");

            Element defaultValueNode = attributeNode.addElement("defaultValue");
            Element attributeDescriptionNode = attributeNode.addElement("description");
            Element javaScriptTypeNode = attributeNode.addElement("javaScriptType");
            Element nameNode = attributeNode.addElement("name");
            Element readOnlyNode = attributeNode.addElement("readOnly");

            defaultValueNode.setText(attribute.getDefaultValue());
            attributeDescriptionNode.addCDATA(_getAttributeDescription(attribute));
            javaScriptTypeNode.setText(attribute.getJavaScriptType());
            nameNode.setText(attribute.getName());
            readOnlyNode.setText(Boolean.toString(attribute.isReadOnly()));
        }//from www .j av a 2  s . co  m

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element elementDescriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            elementDescriptionNode.addCDATA(_getAttributeDescription(event));
            typeNode.setText(event.getType());
        }
    }

    try {
        File file = new File(_outputXML);

        file.getParentFile().mkdirs();

        FileOutputStream fos = new FileOutputStream(file);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _outputXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.alloy.tools.xmlbuilder.XMLBuilder.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("taglibs");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));

        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");
            Element nameNode = attributeNode.addElement("name");
            Element inputTypeNode = attributeNode.addElement("inputType");
            Element outputTypeNode = attributeNode.addElement("outputType");
            Element defaultValueNode = attributeNode.addElement("defaultValue");

            Element descriptionNode = attributeNode.addElement("description");

            nameNode.setText(attribute.getName());
            inputTypeNode.setText(attribute.getInputType());
            outputTypeNode.setText(attribute.getOutputType());
            defaultValueNode.setText(attribute.getDefaultValue());
            descriptionNode.addCDATA(_getAttributeDescription(attribute));
        }/*from  ww  w. j  a  va  2s  .  c  om*/

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element descriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            typeNode.setText(event.getInputType());
            descriptionNode.addCDATA(_getAttributeDescription(event));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(_componentXML);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _componentXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

public String updateTaskXml(long taskId, String transition, Map parameterMap)
        throws WorkflowComponentException {

    Map errors = updateTask(taskId, transition, parameterMap);

    Document doc = DocumentHelper.createDocument();

    Element root = doc.addElement("results");

    Iterator itr = errors.entrySet().iterator();

    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();

        String name = (String) entry.getKey();
        String code = (String) entry.getValue();

        Element el = root.addElement("error");

        DocUtil.add(el, "name", name);
        DocUtil.add(el, "code", code);
    }//w ww. j a v  a 2s  . co  m

    return doc.asXML();
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

protected void createElement(ProcessDefinition definition, Element root) {
    Element el = root.addElement("definition");

    DocUtil.add(el, "definitionId", definition.getId());
    DocUtil.add(el, "name", definition.getName());
    DocUtil.add(el, "version", definition.getVersion());
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

protected void createElement(ProcessInstance instance, Element root, boolean includeToken)
        throws WorkflowComponentException {

    Element el = root.addElement("instance");

    DocUtil.add(el, "instanceId", instance.getId());
    DocUtil.add(el, "startDate", formatDateTime(instance.getStart()));
    DocUtil.add(el, "endDate", formatDateTime(instance.getEnd()));

    if (instance.hasEnded()) {
        DocUtil.add(el, "ended", "true");
    } else {//from w  ww .j  a  v a  2s  .c om
        DocUtil.add(el, "ended", "false");
    }

    createElement(instance.getProcessDefinition(), el);

    if (includeToken) {
        createElement(instance.getRootToken(), el, true);
    }
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

protected void createElement(TaskInstance task, Element root) throws WorkflowComponentException {

    Element el = root.addElement("task");

    DocUtil.add(el, "taskId", task.getId());
    DocUtil.add(el, "name", task.getName());
    DocUtil.add(el, "assignedUserId", task.getActorId());
    DocUtil.add(el, "createDate", formatDateTime(task.getCreate()));
    DocUtil.add(el, "startDate", formatDateTime(task.getStart()));
    DocUtil.add(el, "endDate", formatDateTime(task.getEnd()));

    createElement(task.getToken().getProcessInstance(), el, false);
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

protected void createElement(TaskFormElement taskFormElement, Element root) {

    Element el = root.addElement("taskFormElement");

    DocUtil.add(el, "type", taskFormElement.getType());
    DocUtil.add(el, "displayName", taskFormElement.getDisplayName());
    DocUtil.add(el, "variableName", taskFormElement.getVariableName());
    DocUtil.add(el, "value", taskFormElement.getValue());
    DocUtil.add(el, "readable", taskFormElement.isReadable());
    DocUtil.add(el, "writable", taskFormElement.isWritable());
    DocUtil.add(el, "required", taskFormElement.isRequired());

    List values = taskFormElement.getValueList();

    Element valuesEl = el.addElement("values");

    for (int i = 0; i < values.size(); i++) {
        String value = (String) values.get(i);

        DocUtil.add(valuesEl, "value", value);
    }//from ww  w  . ja  v  a2  s  .c  o m
}

From source file:com.liferay.jbpm.WorkflowComponentImpl.java

License:Open Source License

protected void createElement(Token token, Element root, boolean checkChildren)
        throws WorkflowComponentException {

    Element tokenEl = root.addElement("token");

    DocUtil.add(tokenEl, "tokenId", token.getId());
    DocUtil.add(tokenEl, "name", token.getNode().getName());

    if (token.getNode().toString().startsWith("Join")) {
        DocUtil.add(tokenEl, "type", "join");
    } else {// w  ww.ja  va2s .  c  o m
        DocUtil.add(tokenEl, "type", "default");
    }

    List tasks = getCurrentTasks(token.getProcessInstance().getId(), token.getId());

    if (tasks == null) {
        Element task = tokenEl.addElement("task");

        task.addElement("taskId").addText("null");
    } else {
        for (int i = 0; i < tasks.size(); i++) {
            TaskInstance task = (TaskInstance) tasks.get(i);

            createElement(task, tokenEl);
        }
    }

    if (checkChildren) {
        Map activeChildren = getActiveChildren(token.getProcessInstance().getId());

        if (hasActiveChildren(activeChildren)) {
            Iterator itr = activeChildren.values().iterator();

            while (itr.hasNext()) {
                Token child = (Token) itr.next();

                createElement(child, tokenEl, false);
            }
        }
    }
}

From source file:com.liferay.portal.tools.service.builder.ServiceBuilder.java

License:Open Source License

public static String getContent(String fileName) throws Exception {
    Document document = _getContentDocument(fileName);

    Element rootElement = document.getRootElement();

    Element authorElement = null;
    Element namespaceElement = null;
    Map<String, Element> entityElements = new TreeMap<>();
    Map<String, Element> exceptionElements = new TreeMap<>();

    List<Element> elements = rootElement.elements();

    for (Element element : elements) {
        String elementName = element.getName();

        if (elementName.equals("author")) {
            element.detach();/*from  w ww .ja  va  2  s.  com*/

            if (authorElement != null) {
                throw new IllegalArgumentException("There can only be one author element");
            }

            authorElement = element;
        } else if (elementName.equals("namespace")) {
            element.detach();

            if (namespaceElement != null) {
                throw new IllegalArgumentException("There can only be one namespace element");
            }

            namespaceElement = element;
        } else if (elementName.equals("entity")) {
            element.detach();

            String name = element.attributeValue("name");

            entityElements.put(StringUtil.toLowerCase(name), element);
        } else if (elementName.equals("exceptions")) {
            element.detach();

            List<Element> exceptionElementsList = element.elements("exception");

            for (Element exceptionElement : exceptionElementsList) {
                exceptionElement.detach();

                exceptionElements.put(exceptionElement.getText(), exceptionElement);
            }
        }
    }

    if (authorElement != null) {
        rootElement.add(authorElement);
    }

    if (namespaceElement == null) {
        throw new IllegalArgumentException("The namespace element is required");
    } else {
        rootElement.add(namespaceElement);
    }

    _addElements(rootElement, entityElements);

    if (!exceptionElements.isEmpty()) {
        Element exceptionsElement = rootElement.addElement("exceptions");

        _addElements(exceptionsElement, exceptionElements);
    }

    return document.asXML();
}