001: /*
002: * Created on 24.04.2005
003: *
004: */
005: package org.openwfe.gpe.xml;
006:
007: import java.io.FileWriter;
008: import java.lang.reflect.InvocationTargetException;
009:
010: import org.jdom.Document;
011: import org.jdom.Element;
012: import org.jdom.output.Format;
013: import org.jdom.output.XMLOutputter;
014:
015: import org.openwfe.gpe.model.AbstractFlow;
016: import org.openwfe.gpe.model.WorkflowElement;
017: import org.openwfe.gpe.ui.*;
018:
019: import org.eclipse.gef.editparts.AbstractEditPart;
020:
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.jface.viewers.*;
023:
024: import org.eclipse.swt.widgets.Tree;
025: import org.eclipse.swt.widgets.TreeItem;
026: import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
027: import org.eclipse.ui.views.properties.IPropertyDescriptor;
028:
029: /**
030: * @author Helena
031: *
032: * This class traverses the GEF tree and creates the corresponding XML elements
033: * The xml document is then exported
034: */
035: public class XMLCreation extends ContentOutlinePage {
036:
037: Element el = new Element("noElement");
038: public Document myDocument = null;
039:
040: public void getTree() throws SecurityException,
041: NoSuchMethodException, InvocationTargetException,
042: IllegalAccessException {
043:
044: TreeViewer treeViewer = DiagramOutlinePage.treeViewer;
045: treeViewer.expandAll();
046: Tree tree = treeViewer.getTree();
047: tree.getTopItem();
048: TreeItem[] root = tree.getItems();
049: //create root element
050: if (root.length == 0) {
051: MessageDialog.openInformation(null, "Alert",
052: "The workflow does not have any elements");
053: return;
054: } else {
055: el = getPart(root[0]);
056: myDocument = new Document(el);
057: Object item = root[0].getData();
058:
059: Object item2 = ((AbstractEditPart) item).getModel();
060: IPropertyDescriptor[] descriptors = ((AbstractFlow) item2)
061: .getPropertyDescriptors();
062:
063: if (descriptors.length != 0) {
064: for (int i = 0; i < descriptors.length; i++) {
065: if (item2 instanceof WorkflowElement) {
066: String language = "";
067: if (WorkflowElement.DESCRIPTION == descriptors[i]
068: .getId().toString()) {
069: for (int j = 0; j < descriptors.length; j++) {
070: if (WorkflowElement.LANGUAGE == descriptors[j]
071: .getId().toString()) {
072: String methodName = Utils
073: .getMethodName(descriptors[j]);
074: Object temp = null;
075: temp = Utils.getAttribute(item2,
076: methodName);
077: language = (String) temp;
078: }
079: }
080: String methodName = Utils
081: .getMethodName(descriptors[i]);
082: Object temp = null;
083: temp = Utils
084: .getAttribute(item2, methodName);
085: String attr = (String) temp;
086: Element description = new Element(
087: "description");
088: description.setAttribute(
089: WorkflowElement.LANGUAGE, language);
090: description.addContent(attr);
091: el.addContent(description);
092: }
093: }
094: }
095: }
096:
097: //get children of root element
098: TreeItem[] rootchildren = root[0].getItems();
099: if (rootchildren.length != 0) {
100: for (int i = 0; i < rootchildren.length; i++) {
101: Element currentElement = visitor(rootchildren[i],
102: 0, getPart(root[0]));
103: el.addContent(currentElement);
104: }
105: }
106:
107: }
108: }
109:
110: protected Element visitor(TreeItem treeItem, int level,
111: Element parent) {
112: //this is for pretty print
113: for (int i = 0; i < level; i++)
114: System.out.print("\t");
115: System.out.print("<" + treeItem.getText());
116:
117: Element element = getPart(treeItem);
118:
119: TreeItem[] children = treeItem.getItems();
120:
121: if (children.length != 0) {
122: if (level != 0)
123: parent.addContent(element);
124: System.out.println(">");
125: for (int i = 0; i < children.length; i++) {
126: visitor(children[i], level + 1, element);
127: }
128: for (int i = 0; i < level; i++)
129: System.out.print("\t");
130: System.out.println("</" + treeItem.getText() + ">");
131:
132: } else {
133: if (level != 0)
134: parent.addContent(element);
135: System.out.println(" />");
136:
137: }
138: return element;
139: }
140:
141: protected Element getPart(TreeItem treeItem) {
142:
143: Element part = new Element("noPartYet");
144:
145: Object item = treeItem.getData();
146:
147: Object item2 = ((AbstractEditPart) item).getModel();
148:
149: Hashtable_Creation titi = new Hashtable_Creation();
150: String toto = titi.findElement(item2);
151: part = new Element(toto);
152: Utils.getAttributes(item2, part);
153:
154: return part;
155: }
156:
157: public void makeElements(String docpath) {
158: try {
159: getTree();
160: } catch (SecurityException e1) {
161: // TODO Auto-generated catch block
162: e1.printStackTrace();
163: } catch (NoSuchMethodException e1) {
164: // TODO Auto-generated catch block
165: e1.printStackTrace();
166: } catch (InvocationTargetException e1) {
167: // TODO Auto-generated catch block
168: e1.printStackTrace();
169: } catch (IllegalAccessException e1) {
170: // TODO Auto-generated catch block
171: e1.printStackTrace();
172: }
173: try {
174: if (myDocument != null) {
175: XMLOutputter outputter = new XMLOutputter(Format
176: .getPrettyFormat());
177: outputter.output(myDocument, System.out);
178: FileWriter writer = new FileWriter(docpath);
179: outputter.output(myDocument, writer);
180: writer.close();
181: }
182: } catch (java.io.IOException e) {
183: e.printStackTrace();
184: }
185: }
186:
187: }
|