Author: koen.aers(a)jboss.com
Date: 2008-10-15 13:43:41 -0400 (Wed, 15 Oct 2008)
New Revision: 10862
Added:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlSerializer.java
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlEditor.java
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Process.java
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Transition.java
Log:
basic serialization
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlEditor.java
===================================================================
---
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlEditor.java 2008-10-15
16:23:38 UTC (rev 10861)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlEditor.java 2008-10-15
17:43:41 UTC (rev 10862)
@@ -3,17 +3,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.List;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.palette.PaletteRoot;
import org.jboss.tools.flow.common.editor.GenericModelEditor;
import org.jboss.tools.flow.common.editpart.DefaultEditPartFactory;
-import org.jboss.tools.flow.common.model.Element;
import org.jboss.tools.flow.common.registry.ElementRegistry;
-import org.jboss.tools.flow.common.wrapper.ConnectionWrapper;
-import org.jboss.tools.flow.common.wrapper.ContainerWrapper;
-import org.jboss.tools.flow.common.wrapper.NodeWrapper;
import org.jboss.tools.flow.common.wrapper.Wrapper;
public class JpdlEditor extends GenericModelEditor {
@@ -30,33 +25,10 @@
return
ElementRegistry.createWrapper("org.jboss.tools.flow.jpdl4.process");
}
- private void write(Wrapper wrapper, OutputStream os) {
- Object object = wrapper.getElement();
- if (object instanceof Element) {
- Element element = (Element)object;
- String xml = (String)element.getMetaData("xml");
- System.out.println(xml);
- }
- if (wrapper instanceof ContainerWrapper) {
- ContainerWrapper containerWrapper = (ContainerWrapper)wrapper;
- List<NodeWrapper> children = containerWrapper.getElements();
- for (NodeWrapper nodeWrapper : children) {
- write(nodeWrapper, os);
- }
- }
- if (wrapper instanceof NodeWrapper) {
- NodeWrapper nodeWrapper = (NodeWrapper)wrapper;
- List<ConnectionWrapper> children = nodeWrapper.getOutgoingConnections();
- for (ConnectionWrapper connectionWrapper : children) {
- write(connectionWrapper, os);
- }
- }
- }
-
protected void writeModel(OutputStream os) throws IOException {
Object object = getModel();
if (object instanceof Wrapper) {
- write((Wrapper)object, os);
+ JpdlSerializer.serialize((Wrapper)object, os);
}
}
Added:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlSerializer.java
===================================================================
---
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlSerializer.java
(rev 0)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlSerializer.java 2008-10-15
17:43:41 UTC (rev 10862)
@@ -0,0 +1,149 @@
+package org.jboss.tools.flow.jpdl4.editor;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.List;
+
+import org.jboss.tools.flow.common.model.Element;
+import org.jboss.tools.flow.common.wrapper.ConnectionWrapper;
+import org.jboss.tools.flow.common.wrapper.ContainerWrapper;
+import org.jboss.tools.flow.common.wrapper.NodeWrapper;
+import org.jboss.tools.flow.common.wrapper.Wrapper;
+import org.jboss.tools.flow.jpdl4.model.EndState;
+import org.jboss.tools.flow.jpdl4.model.Process;
+import org.jboss.tools.flow.jpdl4.model.StartState;
+import org.jboss.tools.flow.jpdl4.model.State;
+import org.jboss.tools.flow.jpdl4.model.SuperState;
+import org.jboss.tools.flow.jpdl4.model.Transition;
+
+public class JpdlSerializer {
+
+ public static void serialize(Wrapper wrapper, OutputStream os) throws IOException {
+ StringBuffer buffer = new StringBuffer();
+ appendToBuffer(buffer, wrapper, 0);
+ Writer writer = new OutputStreamWriter(os);
+ writer.write(buffer.toString());
+ System.out.println(buffer.toString());
+ }
+
+ private static void appendToBuffer(StringBuffer buffer, Wrapper wrapper, int level)
{
+ Object object = wrapper.getElement();
+ if (!(object instanceof Element)) return;
+ appendOpening(buffer, wrapper, level);
+ appendBody(buffer, wrapper, level);
+ appendClosing(buffer, wrapper, level);
+ }
+
+ private static void appendOpening(StringBuffer buffer, Wrapper wrapper, int level) {
+ Element element = (Element)wrapper.getElement();
+ buffer.append("\n");
+ appendPadding(buffer, level);
+ if (element instanceof Transition) {
+ buffer.append("<transition");
+ Transition transition = (Transition)element;
+ if (transition.getTo() != null) {
+ String value = transition.getTo().getName();
+ value = value == null ? "" : value;
+ buffer.append("to=\"" + value + "\"");
+ }
+ buffer.append(">");
+ } else if (element instanceof EndState) {
+ buffer.append("<end-state");
+ EndState endState = (EndState)element;
+ if (endState.getName() != null) {
+ buffer.append(" ");
+ String value = endState.getName();
+ buffer.append("name=\"" + value + "\"");
+ }
+ buffer.append(">");
+ } else if (element instanceof StartState) {
+ buffer.append("<start-state");
+ StartState startState = (StartState)element;
+ if (startState.getName() != null) {
+ buffer.append(" ");
+ String value = startState.getName();
+ buffer.append("name=\"" + value + "\"");
+ }
+ buffer.append(">");
+ } else if (element instanceof SuperState) {
+ buffer.append("<super-state");
+ SuperState superState = (SuperState)element;
+ if (superState.getName() != null) {
+ buffer.append(" ");
+ String value = superState.getName();
+ buffer.append("name=\"" + value + "\"");
+ }
+ buffer.append(">");
+ } else if (element instanceof State) {
+ buffer.append("<state");
+ State state = (State)element;
+ if (state.getName() != null) {
+ buffer.append(" ");
+ String value = state.getName();
+ buffer.append("name=\"" + value + "\"");
+ }
+ buffer.append(">");
+ } else if (element instanceof Process) {
+ buffer.append("<process");
+ Process process = (Process)element;
+ if (process.getInitial() != null) {
+ buffer.append(" ");
+ String value = process.getInitial().getName();
+ value = value == null ? "" : value;
+ buffer.append("initial=\"" + value + "\"");
+ }
+ if (process.getName() != null) {
+ buffer.append(" ");
+ String value = process.getName();
+ buffer.append("name=\"" + value + "\"");
+ }
+ buffer.append(">");
+ }
+
+ }
+
+ private static void appendPadding(StringBuffer buffer, int level) {
+ for (int i = 0; i < level; i++) {
+ buffer.append(" ");
+ }
+ }
+
+ private static void appendClosing(StringBuffer buffer, Wrapper wrapper, int level) {
+ Element element = (Element)wrapper.getElement();
+ buffer.append("\n");
+ appendPadding(buffer, level);
+ if (element instanceof Transition) {
+ buffer.append("</transition>");
+ } else if (element instanceof EndState) {
+ buffer.append("</end-state>");
+ } else if (element instanceof StartState) {
+ buffer.append("</start-state>");
+ } else if (element instanceof SuperState) {
+ buffer.append("</super-state>");
+ } else if (element instanceof State) {
+ buffer.append("</state>");
+ } else if (element instanceof Process) {
+ buffer.append("</process>");
+ }
+ }
+
+ private static void appendBody(StringBuffer buffer, Wrapper wrapper, int level) {
+ if (wrapper instanceof ContainerWrapper) {
+ ContainerWrapper containerWrapper = (ContainerWrapper)wrapper;
+ List<NodeWrapper> children = containerWrapper.getElements();
+ for (NodeWrapper nodeWrapper : children) {
+ appendToBuffer(buffer, nodeWrapper, ++level);
+ }
+ }
+ if (wrapper instanceof NodeWrapper) {
+ NodeWrapper nodeWrapper = (NodeWrapper)wrapper;
+ List<ConnectionWrapper> children = nodeWrapper.getOutgoingConnections();
+ for (ConnectionWrapper connectionWrapper : children) {
+ appendToBuffer(buffer, connectionWrapper, ++level);
+ }
+ }
+ }
+
+}
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Process.java
===================================================================
---
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Process.java 2008-10-15
16:23:38 UTC (rev 10861)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Process.java 2008-10-15
17:43:41 UTC (rev 10862)
@@ -7,6 +7,8 @@
public class Process extends DefaultFlow {
+ private Node initial = null;
+
public Process() {
super();
setMetaData("xml", "process-definition");
@@ -21,5 +23,13 @@
}
return null;
}
+
+ public Node getInitial() {
+ return initial;
+ }
+
+ public void setInitial(Node node) {
+ initial = node;
+ }
}
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Transition.java
===================================================================
---
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Transition.java 2008-10-15
16:23:38 UTC (rev 10861)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/model/Transition.java 2008-10-15
17:43:41 UTC (rev 10862)
@@ -5,13 +5,28 @@
public class Transition extends DefaultConnection {
+ private static final String SCHEMA =
+ "<element name='transition'>" +
+ " <complexType>" +
+ " <attribute name='to' type='string'/>" +
+ " </complexType>" +
+ "</element>";
+
public Transition() {
this(null, null);
}
public Transition(Node from, Node to) {
super(from, to);
- setMetaData("xml", "transition");
+ setMetaData("schema", SCHEMA);
}
+ String getToAsString() {
+ if (getTo() == null) {
+ return null;
+ } else {
+ return getTo().getName();
+ }
+ }
+
}