001: /*
002: * Created on 24.04.2005
003: *
004: */
005: package org.openwfe.gpe.xml;
006:
007: import java.io.File;
008: import java.io.IOException;
009: import java.util.List;
010:
011: import org.eclipse.jface.dialogs.MessageDialog;
012: import org.jdom.Attribute;
013: import org.jdom.Document;
014: import org.jdom.Element;
015: import org.jdom.JDOMException;
016: import org.jdom.input.SAXBuilder;
017: import org.openwfe.gpe.model.ActivityDiagram;
018: import org.openwfe.gpe.model.CompositeOrOneChild;
019: import org.openwfe.gpe.model.FlowElement;
020: import org.openwfe.gpe.model.WorkflowElement;
021:
022: /**
023: * @author Helena
024: *
025: * this class creates graphical elements base on a given xml document
026: */
027: public class XMLImport {
028:
029: Hashtable_Import map = new Hashtable_Import();
030:
031: public ActivityDiagram createDocument(String docpath) {
032: ActivityDiagram diagram = new ActivityDiagram();
033: File file = new File(docpath);
034: SAXBuilder sax = new SAXBuilder();
035: Element el = new Element("nothing");
036: WorkflowElement when = new WorkflowElement();
037:
038: try {
039: Document doc = sax.build(file);
040: Element root = doc.getRootElement();
041: List child = root.getChildren();
042: List attributes = root.getAttributes();
043: if (attributes.size() != 0) {
044: for (int i = 0; i < attributes.size(); i++) {
045: Attribute attribute = (Attribute) attributes.get(i);
046: String fieldname = Utils.getFieldName(attribute,
047: when);
048: Utils.setAttribute(when, fieldname, attribute
049: .getValue());
050: }
051: }
052: for (int i = 0; i < child.size(); i++) {
053: el = (Element) child.get(i);
054: // special case for description as the <description> element becomes an attribute
055: if (el.getName().equalsIgnoreCase("description")) {
056: String fieldname = Utils.getFieldName(el
057: .getAttribute(WorkflowElement.LANGUAGE),
058: when);
059: Utils.setAttribute(when, fieldname, el
060: .getAttribute(WorkflowElement.LANGUAGE)
061: .getValue());
062: Attribute description = new Attribute(
063: "description", el.getValue());
064: String fieldname2 = Utils.getFieldName(description,
065: when);
066: Utils.setAttribute(when, fieldname2, description
067: .getValue());
068:
069: } else {
070: FlowElement element = visitor(el, 0, when);
071: when.addChild(element);
072: }
073:
074: }
075: diagram.addChild(when);
076: } catch (JDOMException e) {
077: MessageDialog.openInformation(null, "Alert",
078: "The XML Document could not be read\n"
079: + "Error message: " + e.getMessage());
080: //e.printStackTrace();
081: } catch (IOException e) {
082: e.printStackTrace();
083: }
084: return diagram;
085: }
086:
087: protected FlowElement visitor(Element treeItem, int level,
088: CompositeOrOneChild parent) {
089:
090: FlowElement tree = new FlowElement();
091:
092: Object toto = map.findElement(treeItem.getName());
093: List attributes = treeItem.getAttributes();
094: if (toto != null) {
095: Class clazz = null;
096: try {
097: clazz = Class.forName(toto.toString().substring(6));
098: } catch (ClassNotFoundException e) {
099: e.printStackTrace();
100: }
101: Object instance = null;
102: try {
103: instance = clazz.newInstance();
104: if (attributes.size() != 0) {
105:
106: for (int i = 0; i < attributes.size(); i++) {
107: Attribute attribute = (Attribute) attributes
108: .get(i);
109: String fieldname = Utils.getFieldName(
110: attribute, instance);
111: Utils.setAttribute(instance, fieldname,
112: attribute.getValue());
113: }
114: }
115: tree = (FlowElement) instance;
116: } catch (InstantiationException e1) {
117: e1.printStackTrace();
118: } catch (IllegalAccessException e1) {
119: e1.printStackTrace();
120: }
121:
122: List children = treeItem.getChildren();
123: if (children.size() != 0) {
124: if (level != 0)
125: parent.addChild(tree);
126: for (int i = 0; i < children.size(); i++) {
127: visitor((Element) children.get(i), level + 1,
128: (CompositeOrOneChild) tree);
129: }
130: } else {
131: if (level != 0)
132: parent.addChild(tree);
133: }
134: }
135: return tree;
136: }
137: }
|