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.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forWrInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/w:r");

    for (Node t : ts) {
        Element wr = t.getParent();

        if (wr == null)
            continue;

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;/*from   www .ja  v  a 2  s  .  c o m*/
        List<Element> wrs = new ArrayList<Element>();

        for (Object el : els) {
            if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                } else if ("r".equals(elm.getName())) {
                    sb.append(elm.getStringValue());
                    wrs.add(elm);
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (Element w : wrs) {
            wr.remove(w);
        }
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forTextInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/text()");

    for (Node t : ts) {
        if (t.getText().matches("[\n\r]*")) {
            continue;
        }/*w  w  w  . ja  v a 2s  .  co  m*/

        Element wr = t.getParent();

        if (wr == null) {
            continue;
        }

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<DefaultText> texts = new ArrayList<DefaultText>();

        for (Object el : els) {
            if (el instanceof DefaultText) {
                DefaultText text = (DefaultText) el;
                texts.add(text);
                sb.append(text.getStringValue());
            } else if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (DefaultText text : texts) {
            wr.remove(text);
        }
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forTextInWp(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:p/text()");

    for (Node t : ts) {
        String c = t.getText();//from   w ww .j a v  a  2 s. c om
        if (c.matches("[\n\r]*")) {
            continue;
        }

        Element wp = t.getParent();
        Element wr = DocumentHelper.createElement("w:r");
        wp.content().add(wp.indexOf(t), wr);
        Element wt = wr.addElement("w:t");
        wt.setText(t.getText());
        wp.remove(t);
    }
}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Creates the Decision nodes in the template xml.
 * /* w w w.  ja  va  2s  .c  o  m*/
 * @param p_root
 *            - the root element of the xml document of the workflow
 *            template.
 * @param p_wfTask
 *            - the current workflow task.
 * @throws Exception
 */
private void createDecisionNodes(Element p_root, WorkflowTask p_wfTask) throws Exception {
    Element decision = p_root.addElement(WorkflowConstants.DECISION).addAttribute(WorkflowConstants.ATTR_NAME,
            p_wfTask.getNodeName());
    // add handler element to decision node
    Element handler = decision.addElement(WorkflowConstants.HANDLER).addAttribute(WorkflowConstants.ATTR_CLASS,
            WorkflowDecision.class.getName());
    // point field
    handler.addElement(WorkflowConstants.FIELD_POINT).addText(String.valueOf(p_wfTask.getPosition().getX())
            + WorkflowConstants.POINT_SEPARATOR + String.valueOf(p_wfTask.getPosition().getY()));
    // sequence field
    handler.addElement(WorkflowConstants.FIELD_SEQUENCE).addText(String.valueOf(p_wfTask.getSequence()));
    // workflow-condition-spec field
    WorkflowConditionSpec wfConditionSpec = p_wfTask.getConditionSpec();
    if (wfConditionSpec != null) {
        Element wcs = handler.addElement(WorkflowConstants.FIELD_WORKFLOW_CONDITION_SPEC);
        // condition-attribute field
        String attributeName = wfConditionSpec.getConditionAttribute() == null ? ""
                : wfConditionSpec.getConditionAttribute();
        wcs.addElement(WorkflowConstants.FIELD_CONDITION_ATTRIBUTE).addText(attributeName);
        // workflow-branch-spec field
        List workflowBranchSpecs = wfConditionSpec.getBranchSpecs();
        if (workflowBranchSpecs != null && !workflowBranchSpecs.isEmpty()) {
            for (int t = 0; t < workflowBranchSpecs.size(); t++) {
                WorkflowBranchSpec workflowBranchSpec = (WorkflowBranchSpec) workflowBranchSpecs.get(t);
                // arrow-label field
                Element wbs = wcs.addElement(
                        WorkflowConstants.FIELD_WORKFLOW_BRANCH_SPEC + WorkflowConstants.NAME_SEPARATOR + t);
                wbs.addElement(WorkflowConstants.FIELD_ARROW_LABEL).addText(workflowBranchSpec.getArrowLabel());
                // comparison-operator field
                wbs.addElement(WorkflowConstants.FIELD_COMPARISON_OPERATOR)
                        .addText(String.valueOf(workflowBranchSpec.getComparisonOperator()));
                // structual-state field
                wbs.addElement(WorkflowConstants.FIELD_STRUCTUAL_STATE)
                        .addText(String.valueOf(workflowBranchSpec.getStructuralState()));
                // is-default field
                wbs.addElement(WorkflowConstants.FIELD_IS_DEFAULT)
                        .addText(String.valueOf(workflowBranchSpec.isDefault()));
                // branch-value field
                String branchValue = workflowBranchSpec.getValue() == null ? "" : workflowBranchSpec.getValue();
                wbs.addElement(WorkflowConstants.FIELD_BRANCH_VALUE).addText(branchValue);
            }
        }
    }

    Vector outgoingArrows = p_wfTask.getOutgoingArrows();
    for (int j = 0; j < outgoingArrows.size(); j++) {
        WorkflowArrow outgoingArrow = (WorkflowArrow) outgoingArrows.get(j);
        if (outgoingArrow.getStructuralState() != WorkflowConstants.REMOVED) {
            // transition filed
            decision.addElement(WorkflowConstants.TRANSITION)
                    .addAttribute(WorkflowConstants.ATTR_NAME, outgoingArrow.getName())
                    .addAttribute(WorkflowConstants.ATTR_TO, outgoingArrow.getTargetNode().getNodeName());
        }
    }
    // add controller element to task node
    Element controller = decision.addElement(WorkflowConstants.CONTROLLER);
    controller.addElement(WorkflowConstants.VARIABLE)
            .addAttribute(WorkflowConstants.ATTR_NAME, WorkflowConstants.VARIABLE_GOTO)
            .addAttribute(WorkflowConstants.ATTR_ACCESS, WorkflowConstants.VARIABLE_ACCESS_RW);
}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Creates the activity nodes in the template xml.
 * /*from   w w  w  .  j a va2 s .c o m*/
 * @param p_root
 *            - the root element of the xml document of the workflow
 *            template.
 * @param p_wfTask
 *            - the current workflow task.
 * @param p_workflowOwners
 *            - the owner(s) of the workflow instances.
 * @throws Exception
 */
private void createActivityNodes(Element p_root, WorkflowTask p_wfTask, WorkflowOwners p_workflowOwners)
        throws Exception {
    Element taskNode = p_root.addElement(WorkflowConstants.TASK_NODE).addAttribute(WorkflowConstants.ATTR_NAME,
            p_wfTask.getNodeName());
    // add task element to task-node node
    Element task = taskNode.addElement(WorkflowConstants.TASK).addAttribute(WorkflowConstants.ATTR_NAME,
            WorkflowJbpmUtil.generateTaskName(p_wfTask.getActivityName()));
    // add assignment element to task node
    Element assignment = task.addElement(WorkflowConstants.ASSIGNMENT)
            .addAttribute(WorkflowConstants.ATTR_CLASS, WorkflowAssignment.class.getName());
    // workflow PM field
    String pm = p_workflowOwners.getProjectManagerId() == null ? "" : p_workflowOwners.getProjectManagerId();
    assignment.addElement(WorkflowConstants.FIELD_PM).addText(pm);
    // workflow manager field
    String[] managers = p_workflowOwners.getWorkflowManagerIds();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < managers.length; i++) {
        if (i != 0) {
            sb.append(",");
        }
        sb.append(managers[i]);
    }
    assignment.addElement(WorkflowConstants.FIELD_MANAGER).addText(sb.toString());
    // activity field
    assignment.addElement(WorkflowConstants.FIELD_ACTIVITY).addText(p_wfTask.getActivityName());
    // report_upload_check field
    assignment.addElement(WorkflowConstants.FIELD_REPORT_UPLOAD_CHECK)
            .addText(String.valueOf(p_wfTask.getReportUploadCheck()));
    // roles field
    assignment.addElement(WorkflowConstants.FIELD_ROLES).addText(p_wfTask.getRolesAsString());
    // accepted-time field
    assignment.addElement(WorkflowConstants.FIELD_ACCEPTED_TIME)
            .addText(String.valueOf(p_wfTask.getAcceptTime()));
    // completed-time field
    assignment.addElement(WorkflowConstants.FIELD_COMPLETED_TIME)
            .addText(String.valueOf(p_wfTask.getCompletedTime()));

    if (p_wfTask.getOverdueToPM() == -1) {
        assignment.addElement(WorkflowConstants.FIELD_OVERDUE_PM_TIME).addText("0");
    } else {
        assignment.addElement(WorkflowConstants.FIELD_OVERDUE_PM_TIME)
                .addText(String.valueOf(p_wfTask.getOverdueToPM()));
    }

    if (p_wfTask.getOverdueToUser() == -1) {
        assignment.addElement(WorkflowConstants.FIELD_OVERDUE_USER_TIME).addText("0");
    } else {
        assignment.addElement(WorkflowConstants.FIELD_OVERDUE_USER_TIME)
                .addText(String.valueOf(p_wfTask.getOverdueToUser()));
    }

    // role-type field
    assignment.addElement(WorkflowConstants.FIELD_ROLE_TYPE).addText(String.valueOf(p_wfTask.getRoleType()));
    // sequence field
    assignment.addElement(WorkflowConstants.FIELD_SEQUENCE).addText(String.valueOf(p_wfTask.getSequence()));
    // structual-state field
    assignment.addElement(WorkflowConstants.FIELD_STRUCTUAL_STATE)
            .addText(String.valueOf(p_wfTask.getStructuralState()));
    // rate-selection-criteria field
    if (WorkflowAdapterHelper.isCostingEnabled()) {
        assignment.addElement(WorkflowConstants.FIELD_RATE_SELECTION_CRITERIA)
                .addText(String.valueOf(p_wfTask.getRateSelectionCriteria()));
        // expense-rate-id field
        assignment.addElement(WorkflowConstants.FIELD_EXPENSE_RATE_ID)
                .addText(String.valueOf(p_wfTask.getExpenseRateId()));
        // revenue-rate-id field
        if (WorkflowAdapterHelper.isRevenueEnabled()) {
            assignment.addElement(WorkflowConstants.FIELD_REVENUE_RATE_ID)
                    .addText(String.valueOf(p_wfTask.getRevenueRateId()));
        }
    }
    // role-name field
    String roleName = p_wfTask.getDisplayRoleName() == null ? "" : p_wfTask.getDisplayRoleName();
    assignment.addElement(WorkflowConstants.FIELD_ROLE_NAME).addText(UserUtil.getUserIdsByNames(roleName));
    // role-id field
    String roleId = "0";
    assignment.addElement(WorkflowConstants.FIELD_ROLE_ID).addText(roleId);
    // action-type field
    String actionType = p_wfTask.getActionType() == null ? "" : p_wfTask.getActionType();
    assignment.addElement(WorkflowConstants.FIELD_ACTION_TYPE).addText(actionType);
    // role-preference field
    String rolePreference = p_wfTask.getRolePreference() == null ? "" : p_wfTask.getRolePreference();
    assignment.addElement(WorkflowConstants.FIELD_ROLE_PREFERENCE).addText(rolePreference);
    // workflow-data-item field
    WorkflowDataItem[] dataItems = p_wfTask.getDataItemRefs();
    if (dataItems != null && dataItems.length > 0) {
        for (int t = 0; t < dataItems.length; t++) {
            WorkflowDataItem di = dataItems[t];
            String name = di.getName() == null ? "" : di.getName();
            Element dataItem = assignment.addElement(
                    WorkflowConstants.FIELD_WORKFLOW_DATA_ITEM + WorkflowConstants.NAME_SEPARATOR + t);
            dataItem.addElement(WorkflowConstants.FIELD_WORKFLOW_DATA_ITEM_NAME).addText(name);
            String type = di.getType() == null ? "" : di.getType();
            dataItem.addElement(WorkflowConstants.FIELD_WORKFLOW_DATA_ITEM_TYPE).addText(type);
            String value = di.getValue() == null ? "" : di.getValue();
            dataItem.addElement(WorkflowConstants.FIELD_WORKFLOW_DATA_ITEM_VALUE).addText(value);
        }
    }

    // point field
    assignment.addElement(WorkflowConstants.FIELD_POINT).addText(String.valueOf(p_wfTask.getPosition().getX())
            + WorkflowConstants.POINT_SEPARATOR + String.valueOf(p_wfTask.getPosition().getY()));

    Vector outgoingArrows = p_wfTask.getOutgoingArrows();
    for (int j = 0; j < outgoingArrows.size(); j++) {
        WorkflowArrow outgoingArrow = (WorkflowArrow) outgoingArrows.get(j);
        if (outgoingArrow.getStructuralState() != WorkflowConstants.REMOVED) {
            // transition filed
            taskNode.addElement(WorkflowConstants.TRANSITION)
                    .addAttribute(WorkflowConstants.ATTR_NAME, outgoingArrow.getName())
                    .addAttribute(WorkflowConstants.ATTR_TO, outgoingArrow.getTargetNode().getNodeName());
        }
    }
    // add controller element to task node
    Element controller = task.addElement(WorkflowConstants.CONTROLLER);
    controller.addElement(WorkflowConstants.VARIABLE)
            .addAttribute(WorkflowConstants.ATTR_NAME, WorkflowConstants.VARIABLE_IS_REJECTED)
            .addAttribute(WorkflowConstants.ATTR_ACCESS, WorkflowConstants.VARIABLE_ACCESS_RW);
    controller.addElement(WorkflowConstants.VARIABLE)
            .addAttribute(WorkflowConstants.ATTR_NAME, WorkflowConstants.VARIABLE_USER_ID)
            .addAttribute(WorkflowConstants.ATTR_ACCESS, WorkflowConstants.VARIABLE_ACCESS_RW);
    controller.addElement(WorkflowConstants.VARIABLE)
            .addAttribute(WorkflowConstants.ATTR_NAME, WorkflowConstants.VARIABLE_GOTO)
            .addAttribute(WorkflowConstants.ATTR_ACCESS, WorkflowConstants.VARIABLE_ACCESS_RW);
}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Creates the end state node in the template xml.
 * //from   ww  w.  java  2s .c om
 * @param p_root
 *            - the root element of the xml document of the workflow
 *            template.
 * @param p_wfTask
 *            - the current workflow task.
 * @param p_workflowTemplate
 *            - the workflow template.
 * @param p_workflowOwners
 *            - the owner(s) of the workflow instances.
 * @param p_max
 *            the max node id
 */
private void createEndState(Element p_root, WorkflowTask p_wfTask, WorkflowTemplate p_workflowTemplate,
        WorkflowOwners p_workflowOwners, int p_max) {
    Element endState = p_root.addElement(WorkflowConstants.END_STATE).addAttribute(WorkflowConstants.ATTR_NAME,
            p_wfTask.getActivityName());
    // add action element to end-state node
    Element action = endState.addElement(WorkflowConstants.ACTION).addAttribute(WorkflowConstants.ATTR_CLASS,
            WorkflowAction.class.getName());
    // start-state field
    Element startState = action.addElement(WorkflowConstants.FIELD_START_STATE);
    setPropertiesForStartState(startState, p_workflowTemplate, p_workflowOwners);
    // point field
    action.addElement(WorkflowConstants.FIELD_POINT).addText(String.valueOf(p_wfTask.getPosition().getX())
            + WorkflowConstants.POINT_SEPARATOR + String.valueOf(p_wfTask.getPosition().getY()));
    // sequence field
    action.addElement(WorkflowConstants.FIELD_SEQUENCE).addText(String.valueOf(p_wfTask.getSequence()));
    // structual-state field
    action.addElement(WorkflowConstants.FIELD_STRUCTUAL_STATE)
            .addText(String.valueOf(p_wfTask.getStructuralState()));
    // max-node-id field
    action.addElement(WorkflowConstants.FIELD_MAX_NODE_ID).addText(String.valueOf(p_max));

}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Sets the properties for start state in the end state node.
 * /*from   w  ww. j a  va2 s . c om*/
 * @param p_startState
 *            - the start state node in the action element in the end state
 *            node.
 * @param p_workflowTemplate
 *            - the workflow template.
 * @param p_worklfowOwners
 *            - The owner(s) of the workflow instances.
 */
private void setPropertiesForStartState(Element p_startState, WorkflowTemplate p_workflowTemplate,
        WorkflowOwners p_workflowOwners) {
    // workflow description field
    p_startState.addElement(WorkflowConstants.FIELD_DESCRIPTION).addText(p_workflowTemplate.getDescription());
    // workflow PM field
    String pm = p_workflowOwners.getProjectManagerId() == null ? "" : p_workflowOwners.getProjectManagerId();
    p_startState.addElement(WorkflowConstants.FIELD_PM).addText(pm);
    // workflow manager field
    String[] managers = p_workflowOwners.getWorkflowManagerIds();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < managers.length; i++) {
        if (i != 0) {
            sb.append(",");
        }
        sb.append(managers[i]);
    }
    p_startState.addElement(WorkflowConstants.FIELD_MANAGER).addText(sb.toString());
    // point field
    p_startState.addElement(WorkflowConstants.FIELD_POINT)
            .addText(String.valueOf(m_startTask.getPosition().getX()) + WorkflowConstants.POINT_SEPARATOR
                    + String.valueOf(m_startTask.getPosition().getY()));
    // sequence field
    p_startState.addElement(WorkflowConstants.FIELD_SEQUENCE)
            .addText(String.valueOf(m_startTask.getSequence()));
    // structual-state field
    p_startState.addElement(WorkflowConstants.FIELD_STRUCTUAL_STATE)
            .addText(String.valueOf(m_startTask.getStructuralState()));

}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Creates the start state node in the template xml.
 * //from  w w  w. ja  va2s. c  o m
 * @param p_root
 *            - the root element of the xml document of the workflow
 *            template.
 * @param p_wfTask
 *            - the current workflow task.
 */
private void createStartState(Element p_root, WorkflowTask p_wfTask) {
    Element startState = p_root.addElement(WorkflowConstants.START_STATE)
            .addAttribute(WorkflowConstants.ATTR_NAME, WorkflowConstants.START_NODE);

    Vector outgoingArrows = p_wfTask.getOutgoingArrows();
    for (int i = 0; i < outgoingArrows.size(); i++) {
        WorkflowArrow outgoingArrow = (WorkflowArrow) outgoingArrows.get(i);
        if (outgoingArrow.getStructuralState() != WorkflowConstants.REMOVED) {
            // transition field
            startState.addElement(WorkflowConstants.TRANSITION)
                    .addAttribute(WorkflowConstants.ATTR_NAME, outgoingArrow.getName())
                    .addAttribute(WorkflowConstants.ATTR_TO, outgoingArrow.getTargetNode().getNodeName());
        }
    }
}

From source file:com.globalsight.smartbox.bussiness.process.Usecase02PreProcess.java

License:Apache License

private void writeRow(Element w, String[] values) throws XMLStreamException {
    /*//from w  w  w  . ja v a  2  s  .  co  m
     * Current colum definitions SID, Source Locale Name, Source Locale
     * Code, (Unknown), Translation Source(eg, Machine Translation), Target
     * Locale, Creation date, (Source Segment), (Source Segment), (Source
     * Segment), (Source Segment)
     */
    sourceLocale = values[2];
    Element row = w.addElement("row");
    Element sid = row.addElement("sid");
    sid.setText(values[0]);

    Element sourceLocaleName = row.addElement("sourceLocaleName");
    sourceLocaleName.setText(values[1]);
    Element sourceLocaleCode = row.addElement("sourceLocaleCode");
    sourceLocaleCode.setText(values[2]);
    Element unknown = row.addElement("unknown");
    unknown.setText(values[3]);
    Element translationSource = row.addElement("translationSource");
    translationSource.setText(values[4]);
    Element targetLocale = row.addElement("targetLocale");
    targetLocale.setText(values[5]);
    Element creationDate = row.addElement("creationDate");
    creationDate.setText(values[6]);
    Element segment1 = row.addElement("segment");
    segment1.setText(values[7]);
    Element segment2 = row.addElement("segment");
    segment2.setText(values[8]);
    Element segment3 = row.addElement("segment");
    segment3.setText(values[9]);
    Element segment4 = row.addElement("segment");
    segment4.setText(values[10]);
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * Sets the entry's concept ID./*  ww  w .  java 2  s  . c  om*/
 */
static public void setConceptId(Entry p_entry, long p_cid) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();
    Element concept = root.element("concept");

    if (concept == null) {
        // create new <concept> element and add to tree
        root.addElement("concept").addText(String.valueOf(p_cid));
    } else {
        concept.setText(String.valueOf(p_cid));
    }

    // let entry know its dom is dirty
    p_entry.setDom(dom);
}