org.drools.xml.XmlWorkflowProcessDumper Maven / Gradle / Ivy
package org.drools.xml;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.drools.definition.process.Connection;
import org.drools.definition.process.Node;
import org.drools.definition.process.WorkflowProcess;
import org.drools.process.core.context.exception.ActionExceptionHandler;
import org.drools.process.core.context.exception.ExceptionHandler;
import org.drools.process.core.context.exception.ExceptionScope;
import org.drools.process.core.context.swimlane.Swimlane;
import org.drools.process.core.context.swimlane.SwimlaneContext;
import org.drools.process.core.context.variable.Variable;
import org.drools.process.core.context.variable.VariableScope;
import org.drools.process.core.datatype.DataType;
import org.drools.process.core.datatype.impl.type.ObjectDataType;
import org.drools.workflow.core.DroolsAction;
import org.drools.workflow.core.impl.NodeImpl;
import org.drools.xml.processes.AbstractNodeHandler;
public class XmlWorkflowProcessDumper {
private final static String EOL = System.getProperty( "line.separator" );
private String type;
private String namespace;
private String schemaLocation;
private SemanticModule semanticModule;
public XmlWorkflowProcessDumper(String type, String namespace, String schemaLocation, SemanticModule semanticModule) {
this.type = type;
this.namespace = namespace;
this.schemaLocation = schemaLocation;
this.semanticModule = semanticModule;
}
public String dump(WorkflowProcess process) {
return dump(process, true);
}
public String dump(WorkflowProcess process, boolean includeMeta) {
StringBuffer xmlDump = new StringBuffer();
visitProcess(process, xmlDump, includeMeta);
return xmlDump.toString();
}
protected void visitProcess(WorkflowProcess process, StringBuffer xmlDump, boolean includeMeta) {
xmlDump.append(" " + EOL
+ "" + EOL + EOL);
visitHeader(process, xmlDump, includeMeta);
visitNodes(process, xmlDump, includeMeta);
visitConnections(process.getNodes(), xmlDump, includeMeta);
xmlDump.append(" ");
}
private void visitHeader(WorkflowProcess process, StringBuffer xmlDump, boolean includeMeta) {
xmlDump.append(" " + EOL);
visitImports(((org.drools.process.core.Process) process).getImports(), xmlDump);
visitGlobals(((org.drools.process.core.Process) process).getGlobals(), xmlDump);
visitFunctionImports(((org.drools.process.core.Process) process).getFunctionImports(), xmlDump);
VariableScope variableScope = (VariableScope)
((org.drools.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
if (variableScope != null) {
visitVariables(variableScope.getVariables(), xmlDump);
}
SwimlaneContext swimlaneContext = (SwimlaneContext)
((org.drools.process.core.Process) process).getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
if (swimlaneContext != null) {
visitSwimlanes(swimlaneContext.getSwimlanes(), xmlDump);
}
ExceptionScope exceptionScope = (ExceptionScope)
((org.drools.process.core.Process) process).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope != null) {
visitExceptionHandlers(exceptionScope.getExceptionHandlers(), xmlDump);
}
xmlDump.append(" " + EOL + EOL);
}
private void visitImports(List imports, StringBuffer xmlDump) {
if (imports != null && imports.size() > 0) {
xmlDump.append(" " + EOL);
for (String importString: imports) {
xmlDump.append(" " + EOL);
}
xmlDump.append(" " + EOL);
}
}
private void visitFunctionImports(List imports, StringBuffer xmlDump) {
if (imports != null && imports.size() > 0) {
xmlDump.append(" " + EOL);
for (String importString: imports) {
xmlDump.append(" " + EOL);
}
xmlDump.append(" " + EOL);
}
}
private void visitGlobals(Map globals, StringBuffer xmlDump) {
if (globals != null && globals.size() > 0) {
xmlDump.append(" " + EOL);
for (Map.Entry global: globals.entrySet()) {
xmlDump.append(" " + EOL);
}
xmlDump.append(" " + EOL);
}
}
public static void visitVariables(List variables, StringBuffer xmlDump) {
if (variables != null && variables.size() > 0) {
xmlDump.append(" " + EOL);
for (Variable variable: variables) {
xmlDump.append(" " + EOL);
visitDataType(variable.getType(), xmlDump);
Object value = variable.getValue();
if (value != null) {
visitValue(variable.getValue(), variable.getType(), xmlDump);
}
xmlDump.append(" " + EOL);
}
xmlDump.append(" " + EOL);
}
}
private void visitSwimlanes(Collection swimlanes, StringBuffer xmlDump) {
if (swimlanes != null && swimlanes.size() > 0) {
xmlDump.append(" " + EOL);
for (Swimlane swimlane: swimlanes) {
xmlDump.append(" " + EOL);
}
xmlDump.append(" " + EOL);
}
}
public static void visitExceptionHandlers(Map exceptionHandlers, StringBuffer xmlDump) {
if (exceptionHandlers != null && exceptionHandlers.size() > 0) {
xmlDump.append(" " + EOL);
for (Map.Entry entry: exceptionHandlers.entrySet()) {
ExceptionHandler exceptionHandler = entry.getValue();
if (exceptionHandler instanceof ActionExceptionHandler) {
ActionExceptionHandler actionExceptionHandler = (ActionExceptionHandler) exceptionHandler;
xmlDump.append(" 0) {
xmlDump.append("faultVariable=\"" + faultVariable + "\" ");
}
xmlDump.append(">" + EOL);
DroolsAction action = actionExceptionHandler.getAction();
if (action != null) {
AbstractNodeHandler.writeAction(action, xmlDump);
}
xmlDump.append(" " + EOL);
} else {
throw new IllegalArgumentException(
"Unknown exception handler type: " + exceptionHandler);
}
}
xmlDump.append(" " + EOL);
}
}
public static void visitDataType(DataType dataType, StringBuffer xmlDump) {
xmlDump.append(" " + EOL);
}
public static void visitValue(Object value, DataType dataType, StringBuffer xmlDump) {
xmlDump.append(" " + XmlDumper.replaceIllegalChars(dataType.writeValue(value)) + " " + EOL);
}
private void visitNodes(WorkflowProcess process, StringBuffer xmlDump, boolean includeMeta) {
xmlDump.append(" " + EOL);
for (Node node: process.getNodes()) {
visitNode(node, xmlDump, includeMeta);
}
xmlDump.append(" " + EOL + EOL);
}
public void visitNode(Node node, StringBuffer xmlDump, boolean includeMeta) {
Handler handler = semanticModule.getHandlerByClass(node.getClass());
if (handler != null) {
((AbstractNodeHandler) handler).writeNode((org.drools.workflow.core.Node) node, xmlDump, includeMeta);
} else {
throw new IllegalArgumentException(
"Unknown node type: " + node);
}
}
private void visitConnections(Node[] nodes, StringBuffer xmlDump, boolean includeMeta) {
List connections = new ArrayList();
for (Node node: nodes) {
for (List connectionList: node.getIncomingConnections().values()) {
connections.addAll(connectionList);
}
}
xmlDump.append(" " + EOL);
for (Connection connection: connections) {
visitConnection(connection, xmlDump, includeMeta);
}
xmlDump.append(" " + EOL + EOL);
}
public void visitConnection(Connection connection, StringBuffer xmlDump, boolean includeMeta) {
xmlDump.append(" " + EOL);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy