Author: koen.aers(a)jboss.com
Date: 2009-01-14 22:28:24 -0500 (Wed, 14 Jan 2009)
New Revision: 13055
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlDeserializer.java
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/JpdlSerializer.java
Log:
accommodate deserializer to store whitespace and other nodes so that they can be preserved
while saving modifications
Modified:
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlDeserializer.java
===================================================================
---
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlDeserializer.java 2009-01-15
02:35:55 UTC (rev 13054)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlDeserializer.java 2009-01-15
03:28:24 UTC (rev 13055)
@@ -23,21 +23,197 @@
public class JpdlDeserializer {
+ interface AttributeHandler {
+ void processAttributes(Wrapper wrapper, Element element);
+ }
+
+ interface ChildNodeHandler {
+ Wrapper processChildNode(Wrapper parent, Node node);
+ }
+
+ interface PostProcessor {
+ void postProcess(Wrapper wrapper);
+ }
+
+ class ProcessAttributeHandler implements AttributeHandler {
+ public void processAttributes(Wrapper wrapper, Element element) {
+ if (!(wrapper instanceof FlowWrapper)) return;
+ FlowWrapper flowWrapper = (FlowWrapper)wrapper;
+ ((Flow)flowWrapper.getElement()).setName(element.getAttribute("name"));
+ }
+ }
+
+ class NodeAttributeHandler implements AttributeHandler {
+ public void processAttributes(Wrapper wrapper, Element element) {
+ if (!(wrapper instanceof NodeWrapper)) return;
+ NodeWrapper nodeWrapper = (NodeWrapper)wrapper;
+ addGraphics(nodeWrapper, element);
+ nodeWrapper.setName(element.getAttribute("name"));
+ }
+ }
+
+ class ConnectionAttributeHandler implements AttributeHandler {
+ public void processAttributes(Wrapper wrapper, Element element) {
+ if (!(wrapper instanceof ConnectionWrapper)) return;
+ ConnectionWrapper connectionWrapper = (ConnectionWrapper)wrapper;
+ addGraphics(connectionWrapper, element);
+ connectionWrapper.getElement().setMetaData("to",
element.getAttribute("to"));
+ }
+ }
+
+ class ProcessChildNodeHandler implements ChildNodeHandler {
+ public Wrapper processChildNode(Wrapper parent, Node node) {
+ if (!(parent instanceof FlowWrapper)) return null;
+ FlowWrapper flowWrapper = (FlowWrapper)parent;
+ if (node instanceof Element) {
+ Wrapper childWrapper = createWrapper((Element)node);
+ if (childWrapper != null && childWrapper instanceof NodeWrapper) {
+ flowWrapper.addElement((NodeWrapper)childWrapper);
+ }
+ }
+ return null;
+ }
+ }
+
+ class NodeChildNodeHandler implements ChildNodeHandler {
+ @SuppressWarnings("unchecked")
+ public Wrapper processChildNode(Wrapper parent, Node node) {
+ if (!(parent instanceof NodeWrapper)) return null;
+ NodeWrapper nodeWrapper = (NodeWrapper)parent;
+ ArrayList<ConnectionWrapper> flows =
(ArrayList<ConnectionWrapper>)nodeWrapper.getElement().getMetaData("flows");
+ if (flows == null) {
+ flows = new ArrayList<ConnectionWrapper>();
+ nodeWrapper.getElement().setMetaData("flows", flows);
+ }
+ if (node instanceof Element) {
+ Wrapper childWrapper = createWrapper((Element)node);
+ if (childWrapper != null && childWrapper instanceof ConnectionWrapper) {
+ flows.add((ConnectionWrapper)childWrapper);
+ }
+ }
+ return null;
+ }
+ }
+
+ class ProcessPostProcessor implements PostProcessor {
+ @SuppressWarnings("unchecked")
+ public void postProcess(Wrapper wrapper) {
+ if (!(wrapper instanceof FlowWrapper)) return;
+ FlowWrapper flowWrapper = (FlowWrapper)wrapper;
+ for (NodeWrapper source : flowWrapper.getElements()) {
+ ArrayList<ConnectionWrapper> flows =
(ArrayList<ConnectionWrapper>)source.getElement().getMetaData("flows");
+ if (flows == null) continue;
+ for (ConnectionWrapper connectionWrapper : flows) {
+ String to = (String)connectionWrapper.getElement().getMetaData("to");
+ if (to == null) {
+ Logger.logInfo("Ignoring sequenceflow without target");
+ continue;
+ }
+ NodeWrapper target = getNamedNode(to, flowWrapper);
+ if (target == null) {
+ Logger.logInfo("Ignoring unknown target " + to + " while resolving
sequenceflow target.");
+ continue;
+ }
+ connectionWrapper.connect(source, target);
+ }
+ }
+ }
+ }
+
private static DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
- public static Wrapper deserialize(InputStream is) {
+ public Wrapper deserialize(InputStream is) {
Wrapper result = null;
try {
Document document = documentBuilderFactory.newDocumentBuilder().parse(is);
- Element element = document.getDocumentElement();
- result = createFlowWrapper(element);
+ result = createWrapper(document.getDocumentElement());
+// Element element = document.getDocumentElement();
+// result = createFlowWrapper(element);
} catch (Exception e) {
Logger.logError("An error occurred while creating the diagram", e);
}
return result;
}
- private static Wrapper createFlowWrapper(Element element) {
+ private Wrapper createWrapper(Element element) {
+ String elementId = getElementId(element.getNodeName());
+ if (elementId == null) return null;
+ Wrapper result = ElementRegistry.createWrapper(elementId);
+ if (result == null) return null;
+ AttributeHandler attributeHandler = getAttributeHandler(result);
+ if (attributeHandler != null) {
+ attributeHandler.processAttributes(result, element);
+ }
+ ChildNodeHandler childNodeHandler = getChildNodeHandler(result);
+ if (childNodeHandler != null) {
+ NodeList nodeList = element.getChildNodes();
+ ArrayList<Node> unknownNodeList = new ArrayList<Node>();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Wrapper childWrapper = childNodeHandler.processChildNode(result,
nodeList.item(i));
+ if (childWrapper != null) {
+ childWrapper.getElement().setMetaData("leadingNodes", unknownNodeList);
+ unknownNodeList = new ArrayList<Node>();
+ } else {
+ unknownNodeList.add(nodeList.item(i));
+ }
+ }
+ result.getElement().setMetaData("trailingNodes", unknownNodeList);
+ }
+ PostProcessor postProcessor = getPostProcessor(result);
+ if (postProcessor != null) {
+ postProcessor.postProcess(result);
+ }
+ return result;
+ }
+
+ private PostProcessor getPostProcessor(Wrapper wrapper) {
+ if (wrapper instanceof FlowWrapper) {
+ return new ProcessPostProcessor();
+ }
+ return null;
+ }
+
+ private AttributeHandler getAttributeHandler(Wrapper wrapper) {
+ if (wrapper instanceof FlowWrapper) {
+ return new ProcessAttributeHandler();
+ } else if (wrapper instanceof NodeWrapper) {
+ return new NodeAttributeHandler();
+ } else if (wrapper instanceof ConnectionWrapper) {
+ return new ConnectionAttributeHandler();
+ }
+ return null;
+ }
+
+ private ChildNodeHandler getChildNodeHandler(Wrapper wrapper) {
+ if (wrapper instanceof FlowWrapper) {
+ return new ProcessChildNodeHandler();
+ } else if (wrapper instanceof NodeWrapper) {
+ return new NodeChildNodeHandler();
+ }
+ return null;
+ }
+
+ private String getElementId(String nodeName) {
+ if ("process".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.process";
+ else if ("start".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.startEvent";
+ else if ("end".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.terminateEndEvent";
+ else if ("end-error".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.errorEndEvent";
+ else if ("end-cancel".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.cancelEndEvent";
+ else if ("state".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.waitTask";
+ else if ("hql".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.hqlTask";
+ else if ("sql".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.sqlTask";
+ else if ("java".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.javaTask";
+ else if ("script".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.scriptTask";
+ else if ("esb".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.serviceTask";
+ else if ("task".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.humanTask";
+ else if ("exclusive".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.exclusiveGateway";
+ else if ("join".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.parallelJoinGateway";
+ else if ("fork".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.parallelForkGateway";
+ else if ("flow".equals(nodeName)) return
"org.jboss.tools.flow.jpdl4.sequenceFlow";
+ else return null;
+ }
+
+ private Wrapper createFlowWrapper(Element element) {
FlowWrapper result = null;
if ("process".equals(element.getNodeName())) {
result = createProcessWrapper(element);
@@ -45,7 +221,7 @@
return result;
}
- private static FlowWrapper createProcessWrapper(Element element) {
+ private FlowWrapper createProcessWrapper(Element element) {
FlowWrapper result =
(FlowWrapper)ElementRegistry.createWrapper("org.jboss.tools.flow.jpdl4.process");
if (result != null) {
result.getElement().setMetaData("flows", new
ArrayList<ConnectionWrapper>());
@@ -58,14 +234,14 @@
}
@SuppressWarnings("unchecked")
- private static void resolveSequenceFlowTargets(FlowWrapper flowWrapper) {
+ private void resolveSequenceFlowTargets(FlowWrapper flowWrapper) {
ArrayList<ConnectionWrapper> flows =
(ArrayList<ConnectionWrapper>)flowWrapper.getElement().getMetaData("flows");
for (ConnectionWrapper flow : flows) {
resolveSequenceFlowTarget(flow);
}
}
- private static void resolveSequenceFlowTarget(ConnectionWrapper connectionWrapper) {
+ private void resolveSequenceFlowTarget(ConnectionWrapper connectionWrapper) {
String to = (String)connectionWrapper.getElement().getMetaData("to");
if (to == null) {
Logger.logInfo("Ignoring sequenceflow without target");
@@ -87,7 +263,7 @@
}
}
- private static NodeWrapper getNamedNode(String name, FlowWrapper flowWrapper) {
+ private NodeWrapper getNamedNode(String name, FlowWrapper flowWrapper) {
if (name == null) return null;
for (NodeWrapper nodeWrapper : flowWrapper.getElements()) {
if (name.equals(nodeWrapper.getName())) return nodeWrapper;
@@ -95,7 +271,7 @@
return null;
}
- private static void addName(Wrapper wrapper, Element element) {
+ private void addName(Wrapper wrapper, Element element) {
String name = element.getAttribute("name");
if (name == null) return;
if (wrapper instanceof FlowWrapper) {
@@ -105,7 +281,7 @@
}
}
- private static void addNodes(FlowWrapper wrapper, Element element) {
+ private void addNodes(FlowWrapper wrapper, Element element) {
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node child = nodeList.item(i);
@@ -149,7 +325,7 @@
}
@SuppressWarnings("unchecked")
- private static void addSequenceFlow(NodeWrapper wrapper, Element element) {
+ private void addSequenceFlow(NodeWrapper wrapper, Element element) {
NodeList nodeList = element.getElementsByTagName("flow");
for (int i = 0; i < nodeList.getLength(); i++) {
Node child = nodeList.item(i);
@@ -169,7 +345,7 @@
}
- private static ConnectionWrapper createConnectionWrapper(Element element, String
elementType) {
+ private ConnectionWrapper createConnectionWrapper(Element element, String elementType)
{
ConnectionWrapper result =
(ConnectionWrapper)ElementRegistry.createWrapper(elementType);
if (result != null) {
addName(result, element);
@@ -178,7 +354,7 @@
return result;
}
- private static void addGraphics(ConnectionWrapper wrapper, Element element) {
+ private void addGraphics(ConnectionWrapper wrapper, Element element) {
String graphics = element.getAttribute("g");
if (graphics != null) {
StringTokenizer bendpoints = new StringTokenizer(graphics, ";");
@@ -199,7 +375,7 @@
}
- private static void addGraphics(NodeWrapper wrapper, Element element) {
+ private void addGraphics(NodeWrapper wrapper, Element element) {
String graphics = element.getAttribute("g");
Rectangle constraint = new Rectangle(0, 0, 80, 40);
if (graphics != null) {
@@ -221,7 +397,7 @@
wrapper.setConstraint(constraint);
}
- private static int convertStringToInt(String str) {
+ private int convertStringToInt(String str) {
int result = 0;
try {
result = new Integer(str).intValue();
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 2009-01-15
02:35:55 UTC (rev 13054)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlEditor.java 2009-01-15
03:28:24 UTC (rev 13055)
@@ -30,7 +30,7 @@
protected void writeModel(OutputStream os) throws IOException {
Object object = getModel();
if (object instanceof Wrapper) {
- JpdlSerializer.serialize((Wrapper)object, os);
+ new JpdlSerializer().serialize((Wrapper)object, os);
}
}
@@ -41,7 +41,7 @@
} catch (IOException e) {
// ignored
}
- setModel(empty ? createModel() : JpdlDeserializer.deserialize(is));
+ setModel(empty ? createModel() : new JpdlDeserializer().deserialize(is));
}
}
Modified:
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 2009-01-15
02:35:55 UTC (rev 13054)
+++
trunk/jbpm/plugins/org.jboss.tools.flow.jpdl4/src/org/jboss/tools/flow/jpdl4/editor/JpdlSerializer.java 2009-01-15
03:28:24 UTC (rev 13055)
@@ -33,16 +33,15 @@
public class JpdlSerializer {
- public static void serialize(Wrapper wrapper, OutputStream os) throws IOException {
+ public 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());
writer.close();
-// System.out.println(buffer.toString());
}
- private static void appendToBuffer(StringBuffer buffer, Wrapper wrapper, int level)
{
+ private void appendToBuffer(StringBuffer buffer, Wrapper wrapper, int level) {
Object object = wrapper.getElement();
if (!(object instanceof Element)) return;
appendOpening(buffer, wrapper, level);
@@ -57,7 +56,7 @@
}
}
- private static void appendOpening(StringBuffer buffer, Wrapper wrapper, int level) {
+ private void appendOpening(StringBuffer buffer, Wrapper wrapper, int level) {
Element element = (Element)wrapper.getElement();
if (element instanceof SequenceFlow) {
SequenceFlow transition = (SequenceFlow)element;
@@ -255,17 +254,17 @@
}
- private static boolean isEmpty(String str) {
+ private boolean isEmpty(String str) {
return str == null || "".equals(str);
}
- private static void appendPadding(StringBuffer buffer, int level) {
+ private 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) {
+ private void appendClosing(StringBuffer buffer, Wrapper wrapper, int level) {
Element element = (Element)wrapper.getElement();
if (element instanceof SequenceFlow) {
buffer.append("\n");
@@ -338,7 +337,7 @@
}
}
- private static void appendBody(StringBuffer buffer, Wrapper wrapper, int level) {
+ private void appendBody(StringBuffer buffer, Wrapper wrapper, int level) {
if (wrapper instanceof ContainerWrapper) {
ContainerWrapper containerWrapper = (ContainerWrapper)wrapper;
List<NodeWrapper> children = containerWrapper.getElements();
@@ -355,7 +354,7 @@
}
}
- private static void appendNodeGraphics(StringBuffer buffer, NodeWrapper wrapper) {
+ private void appendNodeGraphics(StringBuffer buffer, NodeWrapper wrapper) {
Rectangle constraint = wrapper.getConstraint();
buffer.append(" g=\"");
buffer.append(constraint.x);
@@ -368,7 +367,7 @@
buffer.append("\"");
}
- private static void appendConnectionGraphics(StringBuffer buffer, ConnectionWrapper
wrapper) {
+ private void appendConnectionGraphics(StringBuffer buffer, ConnectionWrapper wrapper)
{
List<Point> bendPoints = wrapper.getBendpoints();
if (bendPoints.size() == 0) return;
buffer.append(" g=\"");