001: /*
002: * Created on 16.05.2005
003: *
004: */
005: package org.openwfe.gpe.xml;
006:
007: import java.lang.reflect.Field;
008: import java.lang.reflect.InvocationTargetException;
009: import java.lang.reflect.Method;
010:
011: import org.eclipse.jface.dialogs.MessageDialog;
012: import org.eclipse.ui.views.properties.IPropertyDescriptor;
013: import org.jdom.Attribute;
014: import org.jdom.Element;
015: import org.openwfe.gpe.model.AbstractFlow;
016: import org.openwfe.gpe.model.DefinedComposite;
017: import org.openwfe.gpe.model.WorkflowElement;
018:
019: /**
020: * @author Helena
021: *
022: * This class contains utility methods for creating method names
023: * from the attibutes of the model classes or the XMl elements
024: * Then the corresponding getter or setter methods are invoked
025: */
026:
027: public abstract class Utils {
028:
029: public static String setMethodName(Attribute attribute) {
030:
031: String methodName = "";
032: Attribute el = (Attribute) attribute;
033: String name = el.getName();
034: String[] tab = name.split("-");
035: if (tab.length != 0) {
036: String temp = tab[0].substring(0, 1).toUpperCase()
037: + tab[0].substring(1);
038: for (int j = 1; j < tab.length; j++) {
039: temp = temp + tab[j].substring(0, 1).toUpperCase()
040: + tab[j].substring(1);
041: ;
042:
043: }
044: name = temp;
045: }
046: methodName = name;
047: return methodName;
048: }
049:
050: public static String getFieldName(Attribute attribute,
051: Object instance) {
052: String fieldname = "";
053: if (instance instanceof DefinedComposite
054: || instance instanceof WorkflowElement) {
055: if (attribute.getName().equalsIgnoreCase("name")) {
056: fieldname = "privateName";
057: } else {
058: fieldname = Utils.setMethodName(attribute);
059: }
060: } else {
061: fieldname = Utils.setMethodName(attribute);
062: }
063: return fieldname;
064: }
065:
066: public static void setAttribute(Object o, String fieldname,
067: String value) {
068:
069: Class clazz = o.getClass();
070: Field field = null;
071: String methodName = "set"
072: + fieldname.substring(0, 1).toUpperCase()
073: + fieldname.substring(1);
074: Class partypes[] = new Class[1];
075: partypes[0] = String.class;
076: Method m = null;
077: try {
078: m = clazz.getMethod(methodName, partypes);
079: } catch (SecurityException e1) {
080: e1.printStackTrace();
081: } catch (NoSuchMethodException e1) {
082: Hashtable_Creation map = new Hashtable_Creation();
083: MessageDialog.openInformation(null, "Alert",
084: "The attribute " + fieldname
085: + " is undefined for element "
086: + map.findElement(o));
087: return;
088: //e1.printStackTrace();
089: }
090: Object arglist[] = new Object[1];
091: arglist[0] = new String(value);
092: try {
093: m.invoke(o, arglist);
094: } catch (IllegalArgumentException e2) {
095: e2.printStackTrace();
096: } catch (IllegalAccessException e2) {
097: e2.printStackTrace();
098: } catch (InvocationTargetException e2) {
099: e2.printStackTrace();
100: }
101: }
102:
103: public static void getAttributes(Object element, Element current) {
104: //Class clazz = element.getClass();
105: AbstractFlow flow = (AbstractFlow) element;
106: IPropertyDescriptor[] descriptors = flow
107: .getPropertyDescriptors();
108: if (descriptors.length != 0) {
109: for (int i = 0; i < descriptors.length; i++) {
110: String methodName = getMethodName(descriptors[i]);
111:
112: try {
113: Object temp = getAttribute(flow, methodName);
114: String attr = (String) temp;
115: if (!(attr.equalsIgnoreCase("") || (flow instanceof WorkflowElement && (descriptors[i]
116: .getDisplayName().equalsIgnoreCase(
117: WorkflowElement.DESCRIPTION) || descriptors[i]
118: .getDisplayName().equalsIgnoreCase(
119: WorkflowElement.LANGUAGE)))))
120: current.setAttribute(descriptors[i]
121: .getDisplayName(), (String) temp);
122: } catch (SecurityException e) {
123: e.printStackTrace();
124: } catch (NoSuchMethodException e) {
125: e.printStackTrace();
126: } catch (InvocationTargetException e) {
127: e.printStackTrace();
128: } catch (IllegalAccessException e) {
129: e.printStackTrace();
130: }
131: }
132: }
133: return;
134: }
135:
136: public static String getMethodName(IPropertyDescriptor attribute) {
137: String name = attribute.getId().toString();
138: String[] tab = name.split("-");
139: if (tab.length != 0) {
140: String temp = tab[0].substring(0, 1).toUpperCase()
141: + tab[0].substring(1);
142: for (int j = 1; j < tab.length; j++) {
143: temp = temp + tab[j].substring(0, 1).toUpperCase()
144: + tab[j].substring(1);
145: ;
146: }
147: name = temp;
148: }
149: String methodName = "get" + name;
150: return methodName;
151: }
152:
153: public static Object getAttribute(Object o, String methodName)
154: throws SecurityException, NoSuchMethodException,
155: InvocationTargetException, IllegalAccessException,
156: InvocationTargetException {
157: Class clazz = o.getClass();
158: Method getMethod = clazz.getMethod(methodName, new Class[] {});
159: return getMethod.invoke(o, new Object[] {});
160: }
161: }
|