package com.vci.client.workflow.editor; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import com.mxgraph.model.mxCell; import com.mxgraph.model.mxICell; import com.mxgraph.view.mxGraph; import com.vci.client.LogonApplication; import com.vci.client.ui.locale.LocaleDisplay; import com.vci.client.workflow.editor.ui.FlowEditor; import com.vci.client.workflow.editor.ui.IProcessProperty; import com.vci.corba.common.VCIError; public class EditorCreateXmlToJbpm { private FlowEditor editor = null; BasicGraphEditor designer = null; private mxGraph graph = null; private String sXml = ""; Document document = null; private Object[] ocjectCells = null; private String[] elements = { FlowConstants.XMLSTART, FlowConstants.XMLEND, FlowConstants.XMLTASK, FlowConstants.TRANSITION, FlowConstants.XMLDECISION, FlowConstants.XMLFORK, FlowConstants.XMLJOIN, FlowConstants.DECISION, FlowConstants.MAIL,FlowConstants.SUBPROCESS }; public EditorCreateXmlToJbpm(BasicGraphEditor designer) { super(); this.graph = designer.getGraphComponent().getGraph(); this.editor = designer.getEditor(); this.designer = designer; //createXml(); } private void createXml() { try { // dom4j创建xml Object parent = graph.getDefaultParent(); this.ocjectCells = graph.getChildCells(parent); getXmlNodeFromFlowNode(ocjectCells); } catch (Exception e) { e.printStackTrace(); } return; } private void getXmlNodeFromFlowNode(Object[] ocjectCells) throws IOException, VCIError { // 实例化XMLDOC document = DocumentHelper.createDocument(); // 创建根节点 Element root = document.addElement(FlowConstants.BODYSNAME, FlowConstants.XMLNS); // 获取配置信息 getFlowInfo(root); // 创建流程节点 for (Object objectCell : ocjectCells) { if (objectCell instanceof mxCell) { mxCell cell = (mxCell) objectCell; if (cell.getValue() instanceof IProcessProperty && !cell.isEdge()) { IProcessProperty object = (IProcessProperty) cell .getValue(); String nodeType = object.getNodeType(); if (checkArray(elements, nodeType)) { // 创建流程节点 Element flowType = root.addElement(nodeType); object.createAttribute(flowType); // 节点坐标 getCoordinate(cell, flowType); // 创建连接线 getTransition(cell, flowType); } } } } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, format); writer.write(document); writer.flush(); this.sXml = out.toString(); } public Document getDocument() { return document; } /** * 获取流程配置信息 * * @return * @throws VCIError */ private void getFlowInfo(Element owner) throws VCIError { mxCell cell = (mxCell) graph.getModel().getRoot(); IProcessProperty flowInfo = (IProcessProperty) cell.getValue(); flowInfo.createAttribute(owner); } /** * 获取链接线DOM4J * * @param cell * @throws VCIError */ private void getTransition(mxCell sourceCell, Element owner) throws VCIError { int edgeCount = sourceCell.getEdgeCount(); for (int i = 0; i < edgeCount; i++) { mxCell edge = (mxCell) sourceCell.getEdgeAt(i); if (edge.getSource() == sourceCell) { String edgeValue = ""; if(edge.getValue() !=null ){ edgeValue = edge.getValue().toString(); } mxICell target = edge.getTarget(); String target2 = target.getValue().toString(); if (owner.getQName().getName().equals("decision")) { if (edgeValue.trim().length() == 0) { edgeValue = target2; } } Element elementLine = owner .addElement(FlowConstants.TRANSITION); if (edgeValue.trim().length() > 0) { Attribute attributeKey = DocumentHelper.createAttribute( elementLine, FlowConstants.XMLNAME, edgeValue.toString()); elementLine.add(attributeKey); } if (edge.getValue() instanceof IProcessProperty) { IProcessProperty object = (IProcessProperty) edge .getValue(); object.createAttribute(elementLine); } // PropertyInfo[] transmTrainsitionInfo_start = TransitionPanel.transmTrainsitionInfo_start; // if (transmTrainsitionInfo_start != null // && transmTrainsitionInfo_start.length > 0) { // createTransitionEventXml(elementLine, // transmTrainsitionInfo_start, // FlowConstants.TRANSITION_EVENT); // // TransitionPanel.transmTrainsitionInfo_start = new PropertyInfo[] {}; // } String targetName = target.getValue().toString(); Attribute toAttribute = DocumentHelper.createAttribute( elementLine, FlowConstants.XMLTARGET, targetName); elementLine.add(toAttribute); } } } /** * 获取坐标DOM4J * * @return */ private void getCoordinate(mxCell cell, Element owner) { String sCoordinate = ""; // x sCoordinate = sCoordinate + (int) cell.getGeometry().getX() + ","; // y sCoordinate = sCoordinate + (int) cell.getGeometry().getY() + ","; // w sCoordinate = sCoordinate + (int) cell.getGeometry().getWidth() + ","; // h sCoordinate = sCoordinate + (int) cell.getGeometry().getHeight(); Attribute flowAttribute = DocumentHelper.createAttribute(owner, FlowConstants.XMLCOORDINATEKEY, sCoordinate); owner.add(flowAttribute); } /** * 校验数组是否存在 */ private boolean checkArray(String[] arrString, String sElement) { for (int i = 0; i < arrString.length; i++) { if (arrString[i].equalsIgnoreCase(sElement)) { return true; } } return false; } public FlowEditor getEditor() { return editor; } /** * 得到XML * * @return */ public String getsXml() { createXml(); return sXml; } private String getI18nForEvent(String key) { return LocaleDisplay.getI18nString(key, "RMIPWorkflow", LogonApplication.frame.getLocale()); } }