Author: Grid.Qian
Date: 2008-08-15 05:33:33 -0400 (Fri, 15 Aug 2008)
New Revision: 9739
Added:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/DomXmlWriter.java
Removed:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/JpdlXmlWriter.java
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/action/B2JAction.java
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2J.properties
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2JMessages.java
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/BPMN2JPDL.java
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/TranslateHelper.java
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/util/B2JUtil.java
Log:
use dom to translate bpmn file
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/action/B2JAction.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/action/B2JAction.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/action/B2JAction.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,12 +1,21 @@
package org.jboss.tools.b2j.action;
+import java.io.InputStreamReader;
+
+import org.dom4j.Document;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
+import org.jboss.tools.b2j.messages.B2JMessages;
import org.jboss.tools.b2j.translate.BPMN2JPDL;
+import org.jboss.tools.b2j.util.B2JUtil;
+import org.xml.sax.InputSource;
public class B2JAction implements IObjectActionDelegate{
@@ -18,9 +27,23 @@
}
public void run(IAction arg0) {
- BPMN2JPDL translator = new BPMN2JPDL();
- translator.setBpmnFile(bpmnFile);
+ Document bpmnDocument = null;
+ try {
+ bpmnDocument = B2JUtil.parse(new InputSource(new
InputStreamReader(bpmnFile.getContents())));
+ } catch (CoreException e) {
+ System.out.println(B2JMessages.Translate_Error_BpmnFile_CanNotRead + e.getMessage());
+ } catch (Exception e1) {
+ System.out.println(B2JMessages.Translate_Error_BpmnFile_CanNotParse +
e1.getMessage());
+ }
+ BPMN2JPDL translator = new
BPMN2JPDL(bpmnFile.getName(),bpmnFile.getParent().getLocation().toOSString(),bpmnDocument);
translator.translateToFiles();
+
+ try {
+ ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE,
null);
+ } catch (CoreException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
}
public void selectionChanged(IAction arg0, ISelection selection) {
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2J.properties
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2J.properties 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2J.properties 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,4 +1,4 @@
-Loop_Decision=Loop Decition
+Loop_Decision=Loop_Decition
Loop_Decision_Description=this decision is for mocking bpmn loop activity
To=to
Bpmn_Suffix=bpmn
@@ -17,5 +17,6 @@
Translate_Error_JpdlFile_CanNotGenerate=Couldn't write process definition xml:
Translate_Error_JpdlFile_CanNotWrite=Couldn't write process definition to a jpdl
file:
Translate_Error_BpmnFile_CanNotRead=Couldn't read bpmn file:
+Translate_Error_BpmnFile_CanNotParse=Couldn't parse bpmn file to a DOM document:
Translate_Warning_Bpmn_Element_Name= The bpmn element's name is null or same to
another element's name:
Translate_Warning_Bpmn_Element_Type=The type of this bpmn element is not translated to
corresponding jpdl element:
\ No newline at end of file
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2JMessages.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2JMessages.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/messages/B2JMessages.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -29,6 +29,7 @@
public static String Translate_Error_JpdlFile_CanNotWrite;
public static String Translate_Error_JpdlFile_CanNotGenerate;
public static String Translate_Error_BpmnFile_CanNotRead;
+ public static String Translate_Error_BpmnFile_CanNotParse;
public static String Translate_Warning_Bpmn_Element_Name;
public static String Translate_Warning_Bpmn_Element_Type;
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/BPMN2JPDL.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/BPMN2JPDL.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/BPMN2JPDL.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,29 +1,31 @@
package org.jboss.tools.b2j.translate;
import java.io.IOException;
-import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.stp.bpmn.*;
+import org.dom4j.Document;
+import org.dom4j.Element;
import org.jboss.tools.b2j.B2J;
import org.jboss.tools.b2j.messages.B2JMessages;
-import org.jbpm.graph.def.*;
-import org.jbpm.graph.node.*;
+import org.jboss.tools.b2j.util.B2JUtil;
public class BPMN2JPDL {
- IFile bpmnFile;
- IFile[] jpdlFiles;
- List<ProcessDefinition> processDefs = new LinkedList<ProcessDefinition>();
- Map<String, String> map = new HashMap<String, String>();
+ String bpmnFileName;
+ String bpmnFilePath;
+ Document bpmnDocument;
+ List<String> poolIDList;
+ List<Document> processDefs = new LinkedList<Document>();
+ Map<String, Element> map = new HashMap<String, Element>();
+ // the warning messages when translate
List<String> warnings = new ArrayList<String>();
+
+ // the error messages when translate
List<String> errors = new ArrayList<String>();
public List<String> getErrors() {
@@ -42,40 +44,38 @@
this.warnings = warnings;
}
- public IFile[] getJpdlFiles() {
- return jpdlFiles;
+ public BPMN2JPDL() {
}
- public IFile getBpmnFile() {
- return bpmnFile;
+ public BPMN2JPDL(String bpmnFileName, String bpmnFilePath,
+ Document bpmnDocument) {
+ this(bpmnFileName, bpmnFilePath, null, bpmnDocument);
}
- public void setBpmnFile(IFile bpmnFile) {
- this.bpmnFile = bpmnFile;
+ public BPMN2JPDL(String bpmnFileName, String bpmnFilePath,
+ List<String> poolIDList, Document bpmnDocument) {
+ this.bpmnFileName = bpmnFileName;
+ this.bpmnFilePath = bpmnFilePath;
+ this.poolIDList = poolIDList;
+ this.bpmnDocument = bpmnDocument;
}
- public BPMN2JPDL() {
- }
-
- public BPMN2JPDL(IFile bpmnFile) {
- this.bpmnFile = bpmnFile;
- }
-
/*
* Translate a bpmn diagram to string[]. Every string is jpdl process
* definition
*/
public String[] translateToStrings() {
- this.translateDiagram(this.bpmnFile);
+ this.translateDiagram(this.bpmnDocument);
String[] strForProcessDefs = new String[processDefs.size()];
-
- JpdlXmlWriter writer = new JpdlXmlWriter();
int i = 0;
- for (ProcessDefinition def : processDefs) {
- writer.setWriter(new StringWriter());
- writer.setUseNamespace(true);
- strForProcessDefs[i] = translatetoString(writer, def);
- this.errors.addAll(writer.getProblems());
+ for (Document def : processDefs) {
+ try {
+ strForProcessDefs[i] = DomXmlWriter.toString(def);
+ } catch (IOException e) {
+ this.errors
+ .add(B2JMessages.Translate_Error_JpdlFile_CanNotGenerate
+ + e.getMessage());
+ }
i++;
}
@@ -83,13 +83,6 @@
}
/*
- * Tranlate a jpdl process definition to a string
- */
- private String translatetoString(JpdlXmlWriter writer, ProcessDefinition def) {
- return writer.toString(def);
- }
-
- /*
* Translate a bpmn diagram to file[]. Every file is a jpdl file
*/
public void translateToFiles() {
@@ -97,15 +90,15 @@
String[] jpdlFileNames = new String[processDefs.size()];
int i = 0;
- for (ProcessDefinition def : processDefs) {
+ for (Document def : processDefs) {
jpdlFileNames[i] = def.getName();
i++;
}
try {
- jpdlFiles = TranslateHelper.createJpdlFiles(this.getBpmnFile(),
+ B2JUtil.createJpdlFiles(bpmnFilePath, bpmnFileName,
strForProcessDefs, jpdlFileNames);
- } catch (CoreException e) {
+ } catch (Exception e) {
errors.add(B2JMessages.Translate_Error_JpdlFile_CanNotWrite
+ e.getMessage());
}
@@ -124,127 +117,148 @@
}
/*
- * Translate a bpmn diagram to jpdl process definitions
+ * Translate a bpmn diagram Domument tree to some jpdl process Dom trees
*/
- public void translateDiagram(IFile bpmnFile) {
- BpmnDiagram bpmnDiagram = null;
- try {
- bpmnDiagram = TranslateHelper.getBpmnDiagram(bpmnFile);
- } catch (IOException e) {
- errors.add(B2JMessages.Translate_Error_BpmnFile_CanNotRead
- + e.getMessage());
+ public void translateDiagram(Document bpmnDocument) {
+
+ // set the namemap = null
+ TranslateHelper.setNameMap(new HashMap<String, Integer>());
+
+ Element diagram = bpmnDocument.getRootElement();
+ if (this.poolIDList == null || this.poolIDList.size() == 0) {
+ for (Object pool : diagram.elements("pools")) {
+ translateGraph((Element) pool);
+ }
+ } else {
+ for (Object pool : diagram.elements("pools")) {
+ if (this.poolIDList.contains(((Element) pool)
+ .attributeValue("iD"))) {
+ translateGraph((Element) pool);
+ }
+ }
}
- for (Pool pool : bpmnDiagram.getPools()) {
- ProcessDefinition processDef = new ProcessDefinition();
- translateGraph(pool, processDef);
- }
-
}
/*
- * Translate a bpmn graph(pool or subprocess) to a jpdl process difinition
+ * Translate a bpmn pool or subprocess to a jpdl process Dom tree
*/
- private void translateGraph(Graph graph, ProcessDefinition processDef) {
+ private Element translateGraph(Element graph) {
+ Document processDef = DomXmlWriter.createDomTree(true);
+ Element processRoot = processDef.getRootElement();
- // if graph is subprocess type, we will check the name when look it as activity not as
sub process
- if (!(graph instanceof SubProcess) &&
!TranslateHelper.check_mapElementName((NamedBpmnObject) graph,processDef)) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Name
- + graph.getID());
- }
- processDef.setName(TranslateHelper
- .generateProcessName((NamedBpmnObject) graph));
- processDef.setDescription(graph.getDocumentation());
-
- if (graph instanceof Pool && ((Pool) graph).getLanes().size() != 0) {
- for (Lane lane : ((Pool) graph).getLanes()) {
- for (Activity activity : lane.getActivities()) {
- translateActivity(activity, processDef);
- }
+ DomXmlWriter.addAttribute(processRoot, B2JMessages.Jpdl_Element_Name,
+ TranslateHelper.generateProcessName(graph));
+ processDef.setName(processRoot
+ .attributeValue(B2JMessages.Jpdl_Element_Name));
+
+ for (Object activity : graph.elements()) {
+ if ("vertices".equals(((Element) activity).getName())) {
+ translateActivity(((Element) activity), processRoot);
}
- } else {
- for (Vertex activity : graph.getVertices()) {
- if (activity instanceof Activity) {
- translateActivity((Activity) activity, processDef);
- }
- }
}
- translateSequenceFlows(graph, processDef);
+ translateSequenceFlows(graph, processRoot);
processDefs.add(processDef);
+ return processRoot;
}
/*
* Translate a bpmn activity to a jpdl node according to activity type
*/
- private void translateActivity(Activity activity,
- ProcessDefinition processDef) {
- ActivityType type = activity.getActivityType();
- Node element = null;
+ private void translateActivity(Element activity, Element processRoot) {
+ String type = activity.attributeValue("type");
+ Element element = null;
// According to bpmn activity type, map to different jpdl node
// Some type can not be supported by this translation, we give
// a warining message for it.
- if (ActivityType.TASK == type.getValue()) {
- element = new Node();
- } else if (ActivityType.SUB_PROCESS == type.getValue()) {
- element = new ProcessState();
- translateSubprocess((Graph)activity,
- (ProcessState) element);
- } else if (ActivityType.EVENT_START_EMPTY <= type.getValue()
- && type.getValue() <= ActivityType.EVENT_START_TIMER
- || ActivityType.EVENT_START_SIGNAL == type.getValue()) {
- element = new StartState();
- if (type.getValue() != ActivityType.EVENT_START_EMPTY) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Type
- + type.getName());
+ if ("bpmn:Activity".equals(type)) {
+ String activityType = activity.attributeValue("activityType");
+ if (activityType == null) {
+ element = DomXmlWriter.addElement(processRoot, "node");
+ } else if ("EventStartEmpty".equals(activityType)
+ || "EventStartMessage".equals(activityType)
+ || "EventStartRule".equals(activityType)
+ || "EventStartTimer".equals(activityType)
+ || "EventStartLink".equals(activityType)
+ || "EventStartMultiple".equals(activityType)
+ || "EventStartSignal".equals(activityType)) {
+ element = DomXmlWriter.addElement(processRoot, "start-state");
+ if (!"EventStartEmpty".equals(activityType)) {
+ warnings
+ .add(B2JMessages.Translate_Warning_Bpmn_Element_Type
+ + activityType);
+ }
+ } else if ("EventIntermediateEmpty".equals(activityType)
+ || "EventIntermediateMessage".equals(activityType)
+ || "EventIntermediateTimer".equals(activityType)
+ || "EventIntermediateError".equals(activityType)
+ || "EventIntermediateCompensation".equals(activityType)
+ || "EventIntermediateRule".equals(activityType)
+ || "EventIntermediateMultiple".equals(activityType)
+ || "EventIntermediateCancel".equals(activityType)
+ || "EventIntermediateLink".equals(activityType)
+ || "EventIntermediateSignal".equals(activityType)) {
+
+ element = DomXmlWriter.addElement(processRoot, "state");
+ if (!"EventIntermediateEmpty".equals(activityType)) {
+ warnings
+ .add(B2JMessages.Translate_Warning_Bpmn_Element_Type
+ + activityType);
+ }
+ } else if ("EventEndEmpty".equals(activityType)
+ || "EventEndMessage".equals(activityType)
+ || "EventEndError".equals(activityType)
+ || "EventEndCompensation".equals(activityType)
+ || "EventEndTerminate".equals(activityType)
+ || "EventEndLink".equals(activityType)
+ || "EventEndMultiple".equals(activityType)
+ || "EventEndCancel".equals(activityType)
+ || "EventEndSignal".equals(activityType)) {
+
+ element = DomXmlWriter.addElement(processRoot, "end-state");
+ if (!"EventEndEmpty".equals(activityType)) {
+ warnings
+ .add(B2JMessages.Translate_Warning_Bpmn_Element_Type
+ + activityType);
+ }
+ } else if ("GatewayDataBasedExclusive".equals(activityType)
+ || "GatewayEventBasedExclusive".equals(activityType)
+ || "GatewayComplex".equals(activityType)) {
+ element = DomXmlWriter.addElement(processRoot, "decision");
+ if (!"GatewayDataBasedExclusive".equals(activityType)) {
+ warnings
+ .add(B2JMessages.Translate_Warning_Bpmn_Element_Type
+ + activityType);
+ }
+ } else if ("GatewayParallel".equals(activityType)
+ || "GatewayDataBasedInclusive".equals(activityType)) {
+ if (activity.attributeValue("incomingEdges") == null
+ || activity.attributeValue("incomingEdges").split(" ").length
== 1) {
+
+ element = DomXmlWriter.addElement(processRoot, "fork");
+ } else {
+ element = DomXmlWriter.addElement(processRoot, "join");
+ }
+ if (!"GatewayDataBasedInclusive".equals(activityType)) {
+ warnings
+ .add(B2JMessages.Translate_Warning_Bpmn_Element_Type
+ + activityType);
+ }
}
- } else if (ActivityType.EVENT_INTERMEDIATE_EMPTY <= type.getValue()
- && type.getValue() <= ActivityType.EVENT_INTERMEDIATE_CANCEL
- || ActivityType.EVENT_INTERMEDIATE_SIGNAL == type.getValue()) {
- element = new State();
- if (type.getValue() != ActivityType.EVENT_INTERMEDIATE_EMPTY) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Type
- + type.getName());
- }
- } else if (ActivityType.EVENT_END_EMPTY <= type.getValue()
- && type.getValue() <= ActivityType.EVENT_END_CANCEL
- || ActivityType.EVENT_END_SIGNAL == type.getValue()) {
- element = new EndState();
- if (type.getValue() != ActivityType.EVENT_END_EMPTY) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Type
- + type.getName());
- }
- } else if (ActivityType.GATEWAY_DATA_BASED_EXCLUSIVE <= type.getValue()
- && type.getValue() <= ActivityType.GATEWAY_EVENT_BASED_EXCLUSIVE
- || ActivityType.GATEWAY_COMPLEX == type.getValue()) {
- element = new Decision();
- if (type.getValue() != ActivityType.GATEWAY_DATA_BASED_EXCLUSIVE) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Type
- + type.getName());
- }
- } else if (ActivityType.GATEWAY_PARALLEL == type.getValue()
- || ActivityType.GATEWAY_DATA_BASED_INCLUSIVE == type.getValue()) {
- if (activity.getIncomingEdges().size() <= 1) {
- element = new Fork();
- } else {
- element = new Join();
- }
- if (type.getValue() != ActivityType.GATEWAY_PARALLEL) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Type
- + type.getName());
- }
+ } else if ("bpmn:SubProcess".equals(type)) {
+ element = DomXmlWriter.addElement(processRoot, "process-state");
+ translateSubprocess(activity, element);
}
- element.setProcessDefinition(processDef);
- processDef.addNode(element);
- if (!TranslateHelper.check_mapElementName(activity,element)) {
+ if (!TranslateHelper.check_mapElementName(activity, element)) {
warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Name
- + activity.getID());
+ + activity.attributeValue("iD"));
}
- map.put(activity.getID(), element.getName());
+ map.put(activity.attributeValue("id"), element);
// If bpmn activity is loop type, then create a structure to mock it.
- if (activity.isLooping()) {
+ if ("true".equals(activity.attributeValue("looping"))) {
createMockLoop(activity, element);
}
}
@@ -253,79 +267,75 @@
* Translate a bpmn subprocess to a jpdl processstate and a new jpdl process
* difinition
*/
- private void translateSubprocess(Graph subProcess,
- ProcessState element) {
- ProcessDefinition processDef = new ProcessDefinition();
- translateGraph(subProcess, processDef);
- element.setSubProcessDefinition(processDef);
+ private void translateSubprocess(Element subProcess, Element element) {
+ Element processRoot = translateGraph(subProcess);
+ Element ele = DomXmlWriter.addElement(element,
+ B2JMessages.Jpdl_SubProcess_Element_Name);
+ DomXmlWriter.addAttribute(ele, B2JMessages.Jpdl_Element_Name,
+ processRoot.attributeValue(B2JMessages.Jpdl_Element_Name));
}
/*
* Translate bpmn sequenceflows to jpdl transitions
*/
- private void translateSequenceFlows(Graph graph,
- ProcessDefinition processDef) {
- for (SequenceEdge edge : graph.getSequenceEdges()) {
- translateSequenceFlow(edge, processDef);
+ private void translateSequenceFlows(Element graph, Element processRoot) {
+ for (Object edge : graph.elements("sequenceEdges")) {
+ translateSequenceFlow((Element) edge, processRoot);
}
}
/*
* Translate a bpmn sequenceflow to a jpdl transition
*/
- private void translateSequenceFlow(SequenceEdge edge,
- ProcessDefinition processDef) {
- Transition transition = new Transition();
- Node element = processDef.getNode(map.get(edge.getSource().getID()));
- if (!TranslateHelper.check_mapElementName(edge,transition)) {
- warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Name
- + edge.getID());
- }
- transition.setProcessDefinition(processDef);
+ private void translateSequenceFlow(Element edge, Element processRoot) {
- transition.setFrom(element);
- transition.setTo(processDef.getNode(map.get(edge.getTarget().getID())));
- element.addLeavingTransition(transition);
- if (SequenceFlowConditionType.DEFAULT == edge.getConditionType()
- .getValue()) {
+ Element source = map.get(edge.attributeValue("source"));
+
+ Element transition = null;
+ if ("true".equals(edge.attributeValue("isDefault"))
+ && source.element("transition") != null) {
// move default transition to the first of transition list
- element.reorderLeavingTransition(element.getLeavingTransitions()
- .size() - 1, 0);
- } else if (SequenceFlowConditionType.EXPRESSION == edge
- .getConditionType().getValue()) {
- transition.setCondition("");
+ transition = DomXmlWriter.addElement(source, "transition", 0);
+ } else {
+ transition = DomXmlWriter.addElement(source, "transition");
}
+
+ if (!TranslateHelper.check_mapElementName(edge, transition)) {
+ warnings.add(B2JMessages.Translate_Warning_Bpmn_Element_Name
+ + edge.attributeValue("iD"));
+ }
+ transition.addAttribute(B2JMessages.To, map.get(
+ edge.attributeValue("target")).attributeValue(
+ B2JMessages.Jpdl_Element_Name));
}
/*
* create a jpdl decision structure to map bpmn loop activity
*/
- private void createMockLoop(Activity activity, Node element) {
+ private void createMockLoop(Element activity, Element element) {
// create a decision
- Decision decision = new Decision();
- String name = activity.getName() + B2JMessages.Loop_Decision;
- decision.setName(name);
- decision.setDescription(B2JMessages.Loop_Decision_Description);
- decision.setProcessDefinition(element.getProcessDefinition());
- element.getProcessDefinition().addNode(decision);
- map.put(activity.getID(), name);
+ Element decision = DomXmlWriter.addElement(element.getParent(),
+ "decision");
+ String name = activity.attributeValue(B2JMessages.Jpdl_Element_Name)
+ + "_" + B2JMessages.Loop_Decision;
+ DomXmlWriter
+ .addAttribute(decision, B2JMessages.Jpdl_Element_Name, name);
+ map.put(activity.attributeValue("id"), decision);
// create a transition from element to decision
- Transition first = new Transition();
- first.setName(B2JMessages.To + name);
- first.setFrom(element);
- first.setTo(decision);
- first.setProcessDefinition(element.getProcessDefinition());
- element.addLeavingTransition(first);
+ Element first = DomXmlWriter.addElement(element, "transition");
+ first.addAttribute(B2JMessages.Jpdl_Element_Name, B2JMessages.To + " "
+ + name);
+ first.addAttribute(B2JMessages.To, decision
+ .attributeValue(B2JMessages.Jpdl_Element_Name));
// create a transition from decision to element
- Transition second = new Transition();
- second.setName(B2JMessages.To + element.getName());
- second.setFrom(decision);
- second.setTo(element);
- second.setProcessDefinition(element.getProcessDefinition());
- decision.addLeavingTransition(second);
+ Element second = DomXmlWriter.addElement(decision, "transition");
+ second.addAttribute(B2JMessages.Jpdl_Element_Name, B2JMessages.To + " "
+ + element.attributeValue(B2JMessages.Jpdl_Element_Name));
+ second.addAttribute(B2JMessages.To, element
+ .attributeValue(B2JMessages.Jpdl_Element_Name));
}
Added:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/DomXmlWriter.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/DomXmlWriter.java
(rev 0)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/DomXmlWriter.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -0,0 +1,162 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.tools.b2j.translate;
+
+import java.io.*;
+import java.util.*;
+
+import org.dom4j.*;
+import org.dom4j.io.*;
+import org.jboss.tools.b2j.messages.B2JMessages;
+import org.jbpm.graph.def.*;
+import org.jbpm.graph.node.*;
+
+public class DomXmlWriter {
+
+ public static String toString(Document document) throws IOException {
+ OutputFormat outputFormat = new OutputFormat(" ", true);
+ Writer writer = new StringWriter();
+ XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
+ xmlWriter.write(document);
+ xmlWriter.flush();
+ writer.flush();
+ return writer.toString();
+ }
+
+
+ public static Document createDomTree(boolean useNamespace) {
+ Document document = DocumentHelper.createDocument();
+ Element root = null;
+
+ if (useNamespace) {
+ Namespace jbpmNamespace = new Namespace(null,
+ B2JMessages.Jpdl_32_Namespace_Url);
+ root = document.addElement(
+ B2JMessages.Jpdl_Process_Definition_Element, jbpmNamespace
+ .getURI());
+ } else {
+ root = document
+ .addElement(B2JMessages.Jpdl_Process_Definition_Element);
+ }
+ // addAttribute(root, B2JMessages.Jpdl_Element_Name, processDefinition
+ // .getName());
+ //
+ // // write the start-state
+ // if (processDefinition.getStartState() != null) {
+ // writeStartNode(root, (StartState) processDefinition.getStartState());
+ // }
+ //
+ // // write the nodeMap
+ // if ((processDefinition.getNodes() != null)
+ // && (processDefinition.getNodes().size() > 0)) {
+ // writeNodes(root, processDefinition.getNodes());
+ // }
+
+ root.addText(System.getProperty("line.separator"));
+
+ return document;
+ }
+
+ private void writeStartNode(Element element, StartState startState) {
+ if (startState != null) {
+ // writeNode(addElement(element, getTypeName(startState)),
+ // startState);
+ }
+ }
+
+ private void writeNodes(Element parentElement, Collection<?> nodes) {
+ Iterator<?> iter = nodes.iterator();
+ while (iter.hasNext()) {
+ org.jbpm.graph.def.Node node = (org.jbpm.graph.def.Node) iter
+ .next();
+ if (!(node instanceof StartState)) {
+ Element nodeElement = addElement(parentElement, ProcessFactory
+ .getTypeName(node));
+ node.write(nodeElement);
+ // writeNode(nodeElement, node);
+ }
+ }
+ }
+
+ public static void writeNode(Element activity, Element node) {
+ addAttribute(node, B2JMessages.Jpdl_Element_Name, activity.getName());
+
+ // if
+ // (B2JMessages.Jpdl_ProcessState_Element_Name.equals(getTypeName(node
+ // ))) {
+ // Element ele =
+ // addElement(node,B2JMessages.Jpdl_SubProcess_Element_Name);
+ // addAttribute(ele, B2JMessages.Jpdl_Element_Name,((ProcessState)
+ // node).getSubProcessDefinition().getName());
+ // }
+ }
+
+ private void writeTransitions(Element element, org.jbpm.graph.def.Node node) {
+ if (node.getLeavingTransitionsMap() != null) {
+ Iterator<?> iter = node.getLeavingTransitionsList().iterator();
+ while (iter.hasNext()) {
+ Transition transition = (Transition) iter.next();
+ writeTransition(element
+ .addElement(B2JMessages.Jpdl_Transition_Element),
+ transition);
+ }
+ }
+ }
+
+ private void writeTransition(Element transitionElement,
+ Transition transition) {
+ if (transition.getTo() != null) {
+ transitionElement.addAttribute(B2JMessages.To, transition.getTo()
+ .getName());
+ }
+ if (transition.getName() != null) {
+ transitionElement.addAttribute(B2JMessages.Jpdl_Element_Name,
+ transition.getName());
+ }
+ }
+
+ public static Element addElement(Element element, String elementName) {
+ Element newElement = element.addElement(elementName);
+ return newElement;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Element addElement(Element element, String elementName,
+ int location) {
+ Element newElement = null;
+ if (element.elements("transtion") != null
+ && element.elements("transtion").size() > location
+ && location >= 0) {
+ element = DocumentHelper.createElement(elementName);
+ element.elements("transtion").add(location, newElement);
+ }
+ return newElement;
+ }
+
+ public static void addAttribute(Element e, String attributeName,
+ String value) {
+ if (value != null) {
+ e.addAttribute(attributeName, value);
+ }
+ }
+
+}
Deleted:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/JpdlXmlWriter.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/JpdlXmlWriter.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/JpdlXmlWriter.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,211 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.tools.b2j.translate;
-
-import java.io.*;
-import java.util.*;
-
-import org.dom4j.*;
-import org.dom4j.io.*;
-import org.jboss.tools.b2j.messages.B2JMessages;
-import org.jbpm.graph.def.*;
-import org.jbpm.graph.node.*;
-
-public class JpdlXmlWriter {
-
- Namespace jbpmNamespace = new Namespace(null,
- B2JMessages.Jpdl_32_Namespace_Url);
- Writer writer;
- boolean useNamespace = false;
-
- List<String> errors;
-
- public List<String> getProblems() {
- return errors;
- }
-
- public void setProblems(List<String> errors) {
- this.errors = errors;
- }
-
- public Namespace getJbpmNamespace() {
- return jbpmNamespace;
- }
-
- public void setJbpmNamespace(Namespace jbpmNamespace) {
- this.jbpmNamespace = jbpmNamespace;
- }
-
- public JpdlXmlWriter() {
- }
-
- public void addError(String msg) {
- errors.add(msg);
- }
-
- public String toString(ProcessDefinition processDefinition) {
- write(processDefinition);
- return writer.toString();
- }
-
- public void setUseNamespace(boolean useNamespace) {
- this.useNamespace = useNamespace;
- }
-
- public void write(ProcessDefinition processDefinition) {
- errors = new ArrayList<String>();
- if (writer == null) {
- addError(B2JMessages.Translate_Error_JpdlWriter_Null);
- }
- if (processDefinition == null) {
- addError(B2JMessages.Translate_Error_JpdlProcess_Definition_Null);
- }
- try {
- // collect the actions of the process definition
- // we will remove each named event action and the remaining ones
- // will be written
- // on the process definition.
- // create a dom4j dom-tree for the process definition
- Document document = createDomTree(processDefinition);
-
- // write the dom-tree to the given writer
- OutputFormat outputFormat = new OutputFormat(" ", true);
- // OutputFormat outputFormat = OutputFormat.createPrettyPrint();
- XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
- xmlWriter.write(document);
- xmlWriter.flush();
- writer.flush();
- } catch (IOException e) {
- addError(B2JMessages.Translate_Error_JpdlFile_CanNotGenerate
- + e.getMessage());
- }
- }
-
- private Document createDomTree(ProcessDefinition processDefinition) {
- Document document = DocumentHelper.createDocument();
- Element root = null;
-
- if (useNamespace) {
- root = document.addElement(
- B2JMessages.Jpdl_Process_Definition_Element, jbpmNamespace
- .getURI());
- } else {
- root = document
- .addElement(B2JMessages.Jpdl_Process_Definition_Element);
- }
- addAttribute(root, B2JMessages.Jpdl_Element_Name, processDefinition
- .getName());
-
- // write the start-state
- if (processDefinition.getStartState() != null) {
- writeStartNode(root, (StartState) processDefinition.getStartState());
- }
-
- // write the nodeMap
- if ((processDefinition.getNodes() != null)
- && (processDefinition.getNodes().size() > 0)) {
- writeNodes(root, processDefinition.getNodes());
- }
-
- root.addText(System.getProperty("line.separator"));
-
- return document;
- }
-
- private void writeStartNode(Element element, StartState startState) {
- if (startState != null) {
- writeNode(addElement(element, getTypeName(startState)), startState);
- }
- }
-
- private void writeNodes(Element parentElement, Collection<?> nodes) {
- Iterator<?> iter = nodes.iterator();
- while (iter.hasNext()) {
- org.jbpm.graph.def.Node node = (org.jbpm.graph.def.Node) iter
- .next();
- if (!(node instanceof StartState)) {
- Element nodeElement = addElement(parentElement, ProcessFactory
- .getTypeName(node));
- node.write(nodeElement);
- writeNode(nodeElement, node);
- }
- }
- }
-
- private void writeNode(Element element, org.jbpm.graph.def.Node node) {
- addAttribute(element, B2JMessages.Jpdl_Element_Name, node.getName());
-
- if (B2JMessages.Jpdl_ProcessState_Element_Name
- .equals(getTypeName(node))) {
- Element ele = addElement(element,
- B2JMessages.Jpdl_SubProcess_Element_Name);
- addAttribute(ele, B2JMessages.Jpdl_Element_Name,
- ((ProcessState) node).getSubProcessDefinition().getName());
- }
-
- writeTransitions(element, node);
- }
-
- private void writeTransitions(Element element, org.jbpm.graph.def.Node node) {
- if (node.getLeavingTransitionsMap() != null) {
- Iterator<?> iter = node.getLeavingTransitionsList().iterator();
- while (iter.hasNext()) {
- Transition transition = (Transition) iter.next();
- writeTransition(element
- .addElement(B2JMessages.Jpdl_Transition_Element),
- transition);
- }
- }
- }
-
- private void writeTransition(Element transitionElement,
- Transition transition) {
- if (transition.getTo() != null) {
- transitionElement.addAttribute(B2JMessages.To, transition.getTo()
- .getName());
- }
- if (transition.getName() != null) {
- transitionElement.addAttribute(B2JMessages.Jpdl_Element_Name,
- transition.getName());
- }
- }
-
- private Element addElement(Element element, String elementName) {
- Element newElement = element.addElement(elementName);
- return newElement;
- }
-
- private void addAttribute(Element e, String attributeName, String value) {
- if (value != null) {
- e.addAttribute(attributeName, value);
- }
- }
-
- private String getTypeName(Object o) {
- return ProcessFactory.getTypeName((org.jbpm.graph.def.Node) o);
- }
-
- public void setWriter(Writer writer) {
- this.writer = writer;
- }
-
-}
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/TranslateHelper.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/TranslateHelper.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/translate/TranslateHelper.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,82 +1,35 @@
package org.jboss.tools.b2j.translate;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
-import org.eclipse.gmf.runtime.emf.core.resources.GMFResourceFactory;
-import org.eclipse.stp.bpmn.BpmnDiagram;
-import org.eclipse.stp.bpmn.NamedBpmnObject;
+import org.dom4j.Element;
import org.jboss.tools.b2j.messages.B2JMessages;
-import org.jboss.tools.b2j.util.B2JUtil;
-import org.jbpm.graph.def.GraphElement;
public class TranslateHelper {
static Map<String, Integer> nameMap = new HashMap<String, Integer>();
+
+
- /*
- * get a bpmn diagram from a bpmn file
- */
- public static BpmnDiagram getBpmnDiagram(IFile bpmnFile) throws IOException {
- nameMap.clear();
- GMFResourceFactory RESOURCE_FACTORY = new GMFResourceFactory();
- ResourceSet resourceSet = new ResourceSetImpl();
- resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
- .put(B2JMessages.Bpmn_Suffix, RESOURCE_FACTORY);
- URI uriBpmn = URI.createURI(B2JMessages.Workspace_Prefix
- + bpmnFile.getFullPath());
- Resource resBpmn = resourceSet.createResource(uriBpmn);
- resBpmn.load(GMFResourceFactory.getDefaultLoadOptions());
- return (BpmnDiagram) resBpmn.getContents().get(0);
+ public static void setNameMap(Map<String, Integer> nameMap) {
+ TranslateHelper.nameMap = nameMap;
}
- public static IFile[] createJpdlFiles(IFile bpmnFile,
- String[] strForProcessDefs, String[] jpdlFileNames)
- throws CoreException {
- IFolder jpdlFolder = B2JUtil.createFolder(bpmnFile.getParent(),
- B2JMessages.Jpdl_Suffix);
- IFolder diagramFolder = B2JUtil.createFolder(jpdlFolder, bpmnFile
- .getName());
-
- IFile[] jpdlFiles = new IFile[strForProcessDefs.length];
- IFolder processFolder = null;
- int i = 0;
- ByteArrayInputStream bytes = null;
- for (String str : strForProcessDefs) {
- bytes = new ByteArrayInputStream(str.getBytes());
- processFolder = B2JUtil.createFolder(diagramFolder,
- jpdlFileNames[i]);
- jpdlFiles[i] = B2JUtil.createFile(processFolder,
- B2JMessages.Jpdl_Process_Definition_Name, bytes);
- i++;
- }
- return jpdlFiles;
- }
-
/*
* generate a process definition name. the name is composed of the names
* from bpmn diagram to the element
*/
- public static String generateProcessName(NamedBpmnObject graph) {
- if (graph instanceof BpmnDiagram) {
- return graph.getName();
+ public static String generateProcessName(Element graph) {
+ if ("BpmnDiagram".equals(graph.getName())) {
+ return graph.attributeValue("name");
} else {
- String str = generateProcessName((NamedBpmnObject) graph
- .eContainer());
+ String str = generateProcessName(graph.getParent());
if (str == null) {
- return graph.getName();
+ return graph.attributeValue("name");
} else {
return str + B2JMessages.Folder_Name_Separator
- + graph.getName();
+ + graph.attributeValue("name");
}
}
@@ -84,24 +37,25 @@
/*
* check the bpmn element name is null or "" or same to another element name
- * and generate jpdl name
+ * and generate a different name
*/
- public static boolean check_mapElementName(NamedBpmnObject graph,
- GraphElement graphEle) {
+ public static boolean check_mapElementName(Element graph, Element graphEle) {
- boolean isOk = false;
- String name = graph.getName();
- if (graph.getName() == null || "".equals(graph.getName())) {
- name = "graph";
+ boolean isOk = true;
+ String name = graph.attributeValue(B2JMessages.Jpdl_Element_Name);
+
+ if (name == null || "".equals(name)) {
+ name = "jboss_autogen";
+ isOk = false;
}
Integer i = nameMap.get(name);
if (i == null) {
- graphEle.setName(name);
+ DomXmlWriter.addAttribute(graphEle,B2JMessages.Jpdl_Element_Name, name);
nameMap.put(name, new Integer("1"));
- isOk = true;
} else {
- graphEle.setName(name + "_" + i);
+ DomXmlWriter.addAttribute(graphEle,B2JMessages.Jpdl_Element_Name, name + "_"
+ i);
nameMap.put(name, ++i);
+ isOk = false;
}
return isOk;
Modified:
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/util/B2JUtil.java
===================================================================
---
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/util/B2JUtil.java 2008-08-15
09:10:55 UTC (rev 9738)
+++
workspace/grid/org.jboss.tools.bpmn2jpdl/src/org/jboss/tools/b2j/util/B2JUtil.java 2008-08-15
09:33:33 UTC (rev 9739)
@@ -1,61 +1,117 @@
package org.jboss.tools.b2j.util;
-import java.io.InputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.Path;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.dom4j.Document;
+import org.dom4j.io.SAXReader;
+import org.jboss.tools.b2j.messages.B2JMessages;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
public class B2JUtil {
- public static IFolder createFolder(IContainer parent, String folderName)
- throws CoreException {
- IResource child = parent.findMember(folderName);
- if (child == null) {
- IFolder folder = parent.getFolder(new Path(folderName));
- folder.create(true, true, null);
- return folder;
+ public static File createFile(String parentFolder, String fileName,
+ String inputStr) throws IOException {
+ File child = new File(parentFolder, fileName);
+ if (inputStr == null) {
+ if (!child.exists()) {
+ child.mkdir();
+ }
} else {
- if (child.getType() == IResource.FOLDER) {
- return (IFolder) child;
- } else {
- return null;
+ if (!child.exists()) {
+ child.createNewFile();
}
+ FileWriter childWriter = new FileWriter(child);
+ PrintWriter printFile = new PrintWriter(childWriter);
+ printFile.println(inputStr);
+ printFile.close();
+ childWriter.close();
+
}
+ return child;
}
- public static IFile createFile(IContainer parent, String fileName,
- InputStream inputStream) throws CoreException {
- IResource child = parent.findMember(fileName);
+ public static File[] createJpdlFiles(String parentFolder,
+ String bpmnFileName, String[] strForProcessDefs,
+ String[] jpdlFileNames) throws IOException {
+ File jpdlFolder = B2JUtil.createFile(parentFolder,
+ B2JMessages.Jpdl_Suffix, null);
+ File diagramFolder = B2JUtil.createFile(jpdlFolder.getAbsolutePath(),
+ bpmnFileName, null);
- if (child != null) {
- if (child.getType() == IResource.FILE) {
+ File[] jpdlFiles = new File[strForProcessDefs.length];
+ File processFolder = null;
+ int i = 0;
+ for (String str : strForProcessDefs) {
+ processFolder = B2JUtil.createFile(diagramFolder.getAbsolutePath(),
+ jpdlFileNames[i], null);
+ jpdlFiles[i] = B2JUtil.createFile(processFolder.getAbsolutePath(),
+ B2JMessages.Jpdl_Process_Definition_Name, str);
+ i++;
+ }
+ return jpdlFiles;
+ }
- // We have permission to overwrite so check if file is read-only
- if (child.getResourceAttributes() != null
- && child.getResourceAttributes().isReadOnly()) {
- IFile[] files = new IFile[1];
- files[0] = (IFile) child;
- }
+ public static InputSource getInpuSource(String path, String name)
+ throws FileNotFoundException {
+ File file = new File(path, name);
+ return new InputSource(new FileReader(file));
- // Change the contents of the existing file.
- IFile file = parent.getFile(new Path(fileName));
- file.setContents(inputStream, true, true, null);
+ }
- return file;
+ public static Document parse(InputSource inputSource) throws Exception {
+ Document document = null;
+ SAXReader saxReader = createSaxReader();
+ document = saxReader.read(inputSource);
+ return document;
+ }
- } else {
- return null;
- }
- } else {
- // Create a new file.
- IFile file = parent.getFile(new Path(fileName));
- file.create(inputStream, true, null);
- return file;
+ public static SAXReader createSaxReader() throws Exception {
+ XMLReader xmlReader = createXmlReader();
+ SAXReader saxReader = new SAXReader(xmlReader);
+ return saxReader;
+ }
+
+ public static XMLReader createXmlReader() throws Exception {
+
+ SAXParser saxParser = createSaxParserFactory().newSAXParser();
+ XMLReader xmlReader = saxParser.getXMLReader();
+
+ try {
+ saxParser.setProperty(
+ "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
+ "http://www.w3.org/2001/XMLSchema");
+ } catch (SAXException e) {
+ // log.warn(
+ // "couldn't set xml parser property
'http://java.sun.com/xml/jaxp/properties/schemaLanguage' to
'http://www.w3.org/2001/XMLSchema'"
+ // , e);
}
+
+ try {
+ xmlReader.setFeature(
+ "http://apache.org/xml/features/validation/dynamic", true);
+ } catch (SAXException e) {
+ // log.warn(
+ // "couldn't set xml parser feature
'http://apache.org/xml/features/validation/dynamic'"
+ // , e);
+ }
+ return xmlReader;
}
+ private static SAXParserFactory createSaxParserFactory() {
+ SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
+ saxParserFactory.setValidating(true);
+ saxParserFactory.setNamespaceAware(true);
+ return saxParserFactory;
+ }
+
}