开发者

Serialize Eclipse Graphical Models (e.g. BPMN Diagram) to XML

I'm working on a project where I have to build a graphical process modeler for a proprietary BPM (business process management) system made in java. I'm mentioning that the software is proprietary because it does not follow ANY international standards like BPEL, BPMN, XPDL, and this is a vital information for any answers I may get.

My intended approach is to use the eclipse GMF to create a standalone RCP application, similar to Bonita Studio, where business analysts will graphically model the business processes and deploy the process model into the web application.

This application has an internal wizard-like process modeler, very rudimentary. But this modeler has a functionality to import and export models in a custom, well defined XML format, which is also proprietary and has no international standards.

What I need is to persist the graphical model created with the eclipse gmf rcp application I'm developing into this custom XML format. This way I can simply invoque the import function in the web application and the process will be deployed. I also need to be able to do the reverse: open a custom process in this XML format into my eclipse RCP and show it in a graphical manner.

So, what I need is:

I need to save 开发者_如何学Pythona graphical diagram in a custom XML format and open a custom XML file in this format and show it as graphical diagrams

Thank you very much for any help!


GMF is using EMF models to store its diagrams. As EMF Models are already able to map to XML, a XSL stylesheet transformation might be enough in most cases to map your model to your custom XML format and vice versa.


Daniel, I had this requirement for exporting diagram as XML and vice versa. I'll give you the procedure that I have followed below.

GUI to XML : - Use your model file in the below code to return the root element as java object.

Note : - Assuming GMF editor generates 2 files, one for model and one for the diagram, you have to choose the model file as input to the below code.

    File model_file = new File(Path_to_your_model_file);
    ResourceSet rs = new ResourceSetImpl();
    URI fileUri = URI.createFileURI(model_file.getAbsolutePath());
    Resource res = rs.getResource(fileUri, true);
    Model model = res.getContents().get(0);

In the above line Model represent your root object of your ecore model. Now as you got the root object you can use it to get the all the values, references, etc

Using these you can manually write a java code to write a XML file.

XML to GUI : - This is pretty straight forward too

Create a new object for the root element for your model by using the below code.

ModelImpl model = (ModelImpl)ModelFactoryImpl.eINSTANCE.createModel(); // Replace Model with your model element name

You have to first parse the XML file using JAVA and read all the values step by step and assing the attributes to the model object step by step (for example : model.setName(doc.getDocumentElement().getAttribute("name")); and ChildObjImpl childObject = (ChildObj)ModelFactoryImpl.eINSTANCE.createChildObj();)

You can create all the different required objects that are supported by your code and then add them to the parent object (for example : model.getChildObject().add(childObject)); )

Once you have finished adding all the info from XML into these objects, you can use the root model object to create the model file as shown below

    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi",new XMIResourceFactoryImpl());
    Resource resource = resourceSet.createResource(URI.createFileURI(path_where_u_want_to_save_the_model_with_filename));
    resource.getContents().add(dd);
    resource.save(null);

Above code will generate the model fine, you can right click that file and generate the diagram file.

I don't know if there is a better way to do this, but this one worked for me well. Try it and let me know if you need further help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜