0001: package org.enhydra.jawe;
0002:
0003: import java.awt.Color;
0004: import java.awt.Dimension;
0005: import java.awt.GraphicsEnvironment;
0006: import java.awt.Point;
0007: import java.awt.Window;
0008: import java.io.BufferedReader;
0009: import java.io.BufferedWriter;
0010: import java.io.File;
0011: import java.io.FileInputStream;
0012: import java.io.FileOutputStream;
0013: import java.io.FileReader;
0014: import java.io.FileWriter;
0015: import java.io.FilenameFilter;
0016: import java.io.InputStream;
0017: import java.io.InputStreamReader;
0018: import java.io.Writer;
0019: import java.lang.reflect.Constructor;
0020: import java.lang.reflect.Field;
0021: import java.net.URL;
0022: import java.net.URLConnection;
0023: import java.net.URLDecoder;
0024: import java.nio.channels.FileChannel;
0025: import java.util.ArrayList;
0026: import java.util.Arrays;
0027: import java.util.Calendar;
0028: import java.util.Collections;
0029: import java.util.Enumeration;
0030: import java.util.GregorianCalendar;
0031: import java.util.HashMap;
0032: import java.util.HashSet;
0033: import java.util.Iterator;
0034: import java.util.List;
0035: import java.util.Map;
0036: import java.util.Properties;
0037: import java.util.Set;
0038: import java.util.StringTokenizer;
0039: import java.util.Vector;
0040: import java.util.jar.JarFile;
0041: import java.util.zip.ZipEntry;
0042:
0043: import javax.swing.ImageIcon;
0044:
0045: import org.enhydra.jawe.base.controller.JaWEController;
0046: import org.enhydra.jawe.base.xpdlvalidator.ValidationError;
0047: import org.enhydra.jawe.misc.PFLocale;
0048: import org.enhydra.shark.utilities.SequencedHashMap;
0049: import org.enhydra.shark.xpdl.XMLCollection;
0050: import org.enhydra.shark.xpdl.XMLComplexElement;
0051: import org.enhydra.shark.xpdl.XMLElement;
0052: import org.enhydra.shark.xpdl.XMLUtil;
0053: import org.enhydra.shark.xpdl.XMLValidationError;
0054: import org.enhydra.shark.xpdl.XPDLConstants;
0055: import org.enhydra.shark.xpdl.elements.Activity;
0056: import org.enhydra.shark.xpdl.elements.ActivitySet;
0057: import org.enhydra.shark.xpdl.elements.Application;
0058: import org.enhydra.shark.xpdl.elements.DataField;
0059: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
0060: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
0061: import org.enhydra.shark.xpdl.elements.FormalParameter;
0062: import org.enhydra.shark.xpdl.elements.Package;
0063: import org.enhydra.shark.xpdl.elements.Participant;
0064: import org.enhydra.shark.xpdl.elements.Transition;
0065: import org.enhydra.shark.xpdl.elements.TypeDeclaration;
0066: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
0067:
0068: /**
0069: * Various utilities.
0070: *
0071: * @author Sasa Bojanic
0072: */
0073: public class Utils {
0074:
0075: public static final String LANG_PROP_PREFIX = "JaWE";
0076:
0077: public static Properties getProperties(String path,
0078: Properties defaultProperties) throws Exception {
0079: Properties props = defaultProperties;
0080: if (path == null || path.equals(""))
0081: return props;
0082: File configFile = new File(path);
0083: if (configFile != null) {
0084: if (!configFile.isAbsolute()) {
0085: configFile = configFile.getAbsoluteFile();
0086: }
0087: if (!configFile.exists())
0088: configFile = null;
0089: }
0090: if (configFile == null) {
0091: path = System
0092: .getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME)
0093: + "/" + path;
0094: configFile = new File(path);
0095: if (configFile == null) {
0096: return props;
0097: }
0098: if (!configFile.isAbsolute()) {
0099: configFile = configFile.getAbsoluteFile();
0100: }
0101: }
0102: if (configFile.exists()) {
0103: FileInputStream fis = null;
0104: try {
0105: fis = new FileInputStream(configFile);
0106: props.load(fis);
0107: fis.close();
0108: } catch (Exception ex) {
0109: throw new Exception(
0110: "Something went wrong while reading configuration from file "
0111: + configFile + "!!!", ex);
0112: }
0113: } else {
0114: throw new Exception(
0115: "Component needs to be configured properly - configuration file "
0116: + configFile + " does not exist!!!");
0117: }
0118: return props;
0119: }
0120:
0121: public static boolean checkFileExistence(String path) {
0122: if (path == null || path.equals(""))
0123: return false;
0124: File configFile = new File(path);
0125: if (configFile != null) {
0126: if (!configFile.isAbsolute()) {
0127: configFile = configFile.getAbsoluteFile();
0128: }
0129: if (!configFile.exists())
0130: configFile = null;
0131: }
0132: if (configFile == null) {
0133: path = System
0134: .getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME)
0135: + "/" + path;
0136: configFile = new File(path);
0137: if (configFile == null) {
0138: return false;
0139: }
0140: if (!configFile.isAbsolute()) {
0141: configFile = configFile.getAbsoluteFile();
0142: }
0143: }
0144: if (!configFile.exists()) {
0145: return false;
0146: }
0147: return true;
0148: }
0149:
0150: public static boolean checkResourceExistence(String path,
0151: String name) {
0152: return Utils.class.getClassLoader().getResource(path + name) != null;
0153: }
0154:
0155: public static Map getProperties(Properties properties,
0156: String startsWith) {
0157: Map toRet = new SequencedHashMap();
0158: Iterator it = properties.entrySet().iterator();
0159: while (it.hasNext()) {
0160: Map.Entry me = (Map.Entry) it.next();
0161: if (((String) me.getKey()).startsWith(startsWith)) {
0162: toRet.put(me.getKey(), me.getValue());
0163: }
0164: }
0165: return toRet;
0166: }
0167:
0168: public static boolean copyPropertyFile(String path, String name,
0169: boolean overwrite) throws Exception {
0170: String confhome = System
0171: .getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME);
0172: if (confhome == null)
0173: return true;
0174: String filename = confhome + "/" + name;
0175: if (!overwrite) {
0176: boolean doesExist = Utils.checkFileExistence(filename);
0177:
0178: if (doesExist)
0179: return false;
0180: }
0181:
0182: System.out.println("Copying property file " + name + " to "
0183: + filename);
0184: InputStream is = null;
0185: URL u = Utils.class.getClassLoader().getResource(path + name);
0186:
0187: BufferedReader input = null;
0188: Writer output = null;
0189: try {
0190: URLConnection urlConnection = u.openConnection();
0191: is = urlConnection.getInputStream();
0192: input = new BufferedReader(new InputStreamReader(is));
0193: StringBuffer contents = new StringBuffer();
0194: String line = null; // not declared within while loop
0195: while ((line = input.readLine()) != null) {
0196: contents.append(line);
0197: contents.append(System.getProperty("line.separator"));
0198: }
0199:
0200: output = new BufferedWriter(new FileWriter(filename));
0201: output.write(contents.toString());
0202: } finally {
0203: if (input != null)
0204: input.close();
0205: if (output != null)
0206: output.close();
0207: }
0208: return true;
0209: // return false;
0210: }
0211:
0212: public static void manageProperties(Properties properties,
0213: String path, String name) throws Exception {
0214: try {
0215: // if (true) return;
0216: if (!Utils.copyPropertyFile(path, name, false)) {
0217: Properties external = new Properties();
0218: FileInputStream fis = null;
0219: try {
0220: fis = new FileInputStream(
0221: System
0222: .getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME)
0223: + "/" + name);
0224: external.load(fis);
0225: fis.close();
0226: Utils.adjustProperties(properties, external);
0227: } catch (Exception ex) {
0228: throw new Error(
0229: "Something went wrong while reading external component properties !!!",
0230: ex);
0231: }
0232: } else {
0233: URL u = JaWEManager.class.getClassLoader().getResource(
0234: path + name);
0235: if (u == null)
0236: return;
0237: URLConnection urlConnection = u.openConnection();
0238: InputStream is = urlConnection.getInputStream();
0239:
0240: properties.load(is);
0241: }
0242: } catch (Exception ex) {
0243: throw new Exception(
0244: "Something went wrong while reading component's properties !!!",
0245: ex);
0246: }
0247: }
0248:
0249: public static void adjustProperties(Properties original,
0250: Properties external) {
0251: Iterator it = external.entrySet().iterator();
0252: while (it.hasNext()) {
0253: Map.Entry me = (Map.Entry) it.next();
0254: String key = (String) me.getKey();
0255: String val = (String) me.getValue();
0256: original.setProperty(key, val);
0257: }
0258: }
0259:
0260: /**
0261: * Take the given string and chop it up into a series of strings on given boundries.
0262: * This is useful for trying to get an array of strings out of the resource file.
0263: */
0264: public static String[] tokenize(String input, String boundary) {
0265: if (input == null)
0266: input = "";
0267: Vector v = new Vector();
0268: StringTokenizer t = new StringTokenizer(input, boundary);
0269: String cmd[];
0270:
0271: while (t.hasMoreTokens())
0272: v.addElement(t.nextToken());
0273: cmd = new String[v.size()];
0274: for (int i = 0; i < cmd.length; i++)
0275: cmd[i] = (String) v.elementAt(i);
0276:
0277: return cmd;
0278: }
0279:
0280: /** Returns the class name without package. */
0281: public static String getUnqualifiedClassName(Class cls) {
0282: String name = cls.getName();
0283: int lastDot = name.lastIndexOf(".");
0284: if (lastDot >= 0) {
0285: name = name.substring(lastDot + 1, name.length());
0286: }
0287: return name;
0288: }
0289:
0290: /**
0291: * Returns the color parsed from the given string. The color can be given in three
0292: * different string form:
0293: * <ul>
0294: * <li> using prefix Color, dot and wanted color, e.g. <b>Color.red</b>
0295: * <li> using prefix SystemColor, dot and wanted color, e.g. <b>SystemColor.desktop</b>
0296: * <li> using RGB like string, e.g. <b>R=124,G=213,B=12</b>
0297: * </ul>
0298: *
0299: * @param col The string representation of wanted color.
0300: * @return The color based on given string or null if incorrect
0301: */
0302: public static Color getColor(String col) {
0303: Color c = null;
0304: int dotInd = col.indexOf(".");
0305: int r, g, b;
0306: if (col.indexOf("Color") != -1 && dotInd != -1) {
0307: try {
0308: ClassLoader cl = JaWEManager.class.getClassLoader();
0309: Class cls = cl.loadClass("java.awt."
0310: + col.substring(0, dotInd));
0311: Field f = cls.getField(col.substring(dotInd + 1));
0312: c = (Color) f.get(null);
0313: c = new Color(c.getRed(), c.getGreen(), c.getBlue());
0314: } catch (Exception ex) {
0315: }
0316: } else {
0317: try {
0318: int i1 = col.indexOf("R=");
0319: if (i1 == -1)
0320: i1 = col.indexOf("r=");
0321: int i1c = col.indexOf(",", i1 + 2);
0322: int i2 = col.indexOf("G=");
0323: if (i2 == -1)
0324: i2 = col.indexOf("g=");
0325: int i2c = col.indexOf(",", i2 + 2);
0326: int i3 = col.indexOf("B=");
0327: if (i3 == -1)
0328: i3 = col.indexOf("b=");
0329: if (i1 != -1 && i1c != -1 && i2 != -1 && i2c != -1
0330: && i3 != -1 && (i1c < i2) && (i2c < i3)) {
0331: r = Integer.valueOf(
0332: col.substring(i1 + 2, i1c).trim())
0333: .intValue();
0334: if (r < 0)
0335: r = 0;
0336: if (r > 255)
0337: r = 255;
0338: g = Integer.valueOf(
0339: col.substring(i2 + 2, i2c).trim())
0340: .intValue();
0341: if (g < 0)
0342: g = 0;
0343: if (g > 255)
0344: g = 255;
0345: b = Integer.valueOf(col.substring(i3 + 2).trim())
0346: .intValue();
0347: if (b < 0)
0348: b = 0;
0349: if (b > 255)
0350: b = 255;
0351: c = new Color(r, g, b);
0352: }
0353: } catch (Exception ex) {
0354: }
0355: }
0356: return c;
0357: }
0358:
0359: public static void flipCoordinates(Point p) {
0360: int x = p.x;
0361: p.x = p.y;
0362: p.y = x;
0363: }
0364:
0365: // ******************************** DEBUGGING STUFF ****************************
0366: /** Used for debug only */
0367: public static void printStrings(String[] s) {
0368: if (s != null) {
0369: for (int i = 0; i < s.length; i++) {
0370: System.out.println("String no " + i + " = " + s[i]);
0371: }
0372: } else {
0373: System.out.println("Passed string array is null !!!");
0374: }
0375: }
0376:
0377: // **************************** END OF DEBUGGING STUFF *************************
0378:
0379: public static void center(Window w, int minXDiffFromMax,
0380: int minYDiffFromMax) {
0381: w.pack();
0382:
0383: // make dialog smaller if needed, and center it
0384: // Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
0385: Dimension screenSize = GraphicsEnvironment
0386: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
0387: .getDefaultConfiguration().getBounds().getSize();
0388:
0389: Dimension windowSize = w.getPreferredSize();
0390: if (windowSize.width > screenSize.width - minXDiffFromMax) {
0391: windowSize.width = screenSize.width - minXDiffFromMax;
0392: }
0393: if (windowSize.height > screenSize.height - minYDiffFromMax) {
0394: windowSize.height = screenSize.height - minYDiffFromMax;
0395: }
0396: w.setSize(windowSize);
0397: w.setLocation(screenSize.width / 2 - (windowSize.width / 2),
0398: screenSize.height / 2 - (windowSize.height / 2));
0399: }
0400:
0401: public static java.util.List findPropertyFiles() {
0402: java.util.List pfs = new ArrayList();
0403:
0404: URL u = Utils.class.getClassLoader().getResource(
0405: "org/enhydra/jawe/language");
0406: File path = null;
0407: if (u != null) {
0408: path = new File(u.getPath());
0409: }
0410: // if folder exists and realy is a folder but not file
0411: if (path != null && path.exists() && path.isDirectory()) {
0412: // getting all .property files within folder
0413: pfs.addAll(Arrays.asList(path.list(new PFFilter())));
0414: } else {
0415: JarFile jfile = null;
0416: try {
0417: String jp = "org/enhydra/jawe/language/JaWE.properties";
0418: u = Utils.class.getClassLoader().getResource(jp);
0419: String jarPath = u.getPath().substring(0,
0420: u.getPath().length() - jp.length() - 2);
0421: jarPath = URLDecoder.decode(jarPath, "UTF-8");
0422: jarPath = jarPath.substring(5);
0423: jfile = new JarFile(jarPath, false);
0424: // get all entries
0425: Enumeration e = jfile.entries();
0426: // loop through entries and find appropriate ones
0427: while (e.hasMoreElements()) {
0428: try {
0429: ZipEntry entry = (ZipEntry) e.nextElement();
0430: String entryname = entry.getName();
0431: // entry must end with '.properties'
0432: if (entryname.indexOf(LANG_PROP_PREFIX) != -1
0433: && entryname.endsWith(".properties")) {
0434: pfs.add(entryname);
0435: }
0436: } catch (Exception ex) {
0437: }
0438: }
0439: } catch (Exception ex) {
0440: ex.printStackTrace();
0441: }
0442: }
0443:
0444: // if there are no property files, try to find the default ones distributed
0445: // with JaWE
0446: if (pfs.size() == 0) {
0447: u = Utils.class.getClassLoader().getResource(
0448: "org/enhydra/jawe/language/JaWE.properties");
0449: if (u != null) {
0450: pfs.add("JaWE.properties");
0451: }
0452: u = Utils.class.getClassLoader().getResource(
0453: "org/enhydra/jawe/language/JaWE_de.properties");
0454: if (u != null) {
0455: pfs.add("JaWE_de.properties");
0456: }
0457: u = Utils.class.getClassLoader().getResource(
0458: "org/enhydra/jawe/language/JaWE_fr.properties");
0459: if (u != null) {
0460: pfs.add("JaWE_fr.properties");
0461: }
0462: u = Utils.class.getClassLoader().getResource(
0463: "org/enhydra/jawe/language/JaWE_sh.properties");
0464: if (u != null) {
0465: pfs.add("JaWE_sh.properties");
0466: }
0467: u = Utils.class.getClassLoader().getResource(
0468: "org/enhydra/jawe/language/JaWE_es.properties");
0469: if (u != null) {
0470: pfs.add("JaWE_es.properties");
0471: }
0472: u = Utils.class.getClassLoader().getResource(
0473: "org/enhydra/jawe/language/JaWE_pt.properties");
0474: if (u != null) {
0475: pfs.add("JaWE_pt.properties");
0476: }
0477: }
0478: java.util.List pfLocales = new ArrayList();
0479: if (pfs.size() > 0) {
0480: // adding default locale (for default property file)
0481: pfLocales.add(new PFLocale());
0482: for (int i = 0; i < pfs.size(); i++) {
0483: String propFile = (String) pfs.get(i);
0484: int p1 = propFile.indexOf(LANG_PROP_PREFIX)
0485: + LANG_PROP_PREFIX.length();
0486: boolean isDefault = !propFile.substring(p1, p1 + 1)
0487: .equals("_");
0488: if (!isDefault) {
0489: PFLocale pfl = PFLocale.createPFLocale(propFile
0490: .substring(p1 + 1, propFile.length()
0491: - ".properties".length()));
0492: if (pfl != null) {
0493: pfLocales.add(pfl);
0494: }
0495: }
0496: }
0497: }
0498: return pfLocales;
0499: }
0500:
0501: public static void copyFile(String src, String dest) {
0502: try {
0503: FileChannel sourceChannel = new FileInputStream(src)
0504: .getChannel();
0505: FileChannel destinationChannel = new FileOutputStream(dest)
0506: .getChannel();
0507: // Copy source file to destination file
0508: destinationChannel.transferFrom(sourceChannel, 0,
0509: sourceChannel.size());
0510: sourceChannel.close();
0511: destinationChannel.close();
0512: } catch (Exception ex) {
0513: ex.printStackTrace();
0514: }
0515: }
0516:
0517: public static void showEAS(ExtendedAttributes el) {
0518: System.err.println("ExtendedAttributes, el = " + el
0519: + ", there are " + el.size() + " eas");
0520: Iterator it = el.toElements().iterator();
0521: while (it.hasNext()) {
0522: System.err.println();
0523: Utils.showEA((ExtendedAttribute) it.next());
0524: }
0525: }
0526:
0527: public static void showEA(ExtendedAttribute el) {
0528: System.err.println("ExtendedAttribute, el = " + el);
0529: System.err.println("ExtendedAttribute.name), el = "
0530: + el.get("Name"));
0531: System.err.println("ExtendedAttribute.value), el = "
0532: + el.get("Value"));
0533: System.err.println();
0534: }
0535:
0536: /** Gets the current date and time string in ISO-8601 format. */
0537: public static String getCurrentDateAndTime() {
0538: String dateSeparator = "-";
0539: String timeSeparator = ":";
0540: Calendar cal = new GregorianCalendar();
0541: String dateTime = "";
0542: dateTime = dateTime + String.valueOf(cal.get(Calendar.YEAR))
0543: + dateSeparator;
0544: int mnth = cal.get(Calendar.MONTH) + 1;
0545: if (mnth < 10) {
0546: dateTime = dateTime + "0";
0547: }
0548: dateTime = dateTime + String.valueOf(mnth) + dateSeparator;
0549: int dayOfMnth = cal.get(Calendar.DAY_OF_MONTH);
0550: if (dayOfMnth < 10) {
0551: dateTime = dateTime + "0";
0552: }
0553: dateTime = dateTime + String.valueOf(dayOfMnth) + " ";
0554: int hr = cal.get(Calendar.HOUR_OF_DAY);
0555: int ampm = cal.get(Calendar.AM_PM);
0556: if (ampm == Calendar.PM && hr < 12) {
0557: hr += 12;
0558: }
0559: if (hr < 10) {
0560: dateTime = dateTime + "0";
0561: }
0562: dateTime = dateTime + String.valueOf(hr) + timeSeparator;
0563: int min = cal.get(Calendar.MINUTE);
0564: if (min < 10) {
0565: dateTime = dateTime + "0";
0566: }
0567: dateTime = dateTime + String.valueOf(min) + timeSeparator;
0568: int sec = cal.get(Calendar.SECOND);
0569: if (sec < 10) {
0570: dateTime = dateTime + "0";
0571: }
0572: dateTime = dateTime + String.valueOf(sec);
0573:
0574: return dateTime;
0575: }
0576:
0577: public static String getActivityStringType(int activityType) {
0578: String retVal = "";
0579:
0580: switch (activityType) {
0581: case XPDLConstants.ACTIVITY_TYPE_NO:
0582: retVal = JaWEConstants.ACTIVITY_TYPE_NO;
0583: break;
0584: case XPDLConstants.ACTIVITY_TYPE_TOOL:
0585: retVal = JaWEConstants.ACTIVITY_TYPE_TOOL;
0586: break;
0587: case XPDLConstants.ACTIVITY_TYPE_BLOCK:
0588: retVal = JaWEConstants.ACTIVITY_TYPE_BLOCK;
0589: break;
0590: case XPDLConstants.ACTIVITY_TYPE_ROUTE:
0591: retVal = JaWEConstants.ACTIVITY_TYPE_ROUTE;
0592: break;
0593: case XPDLConstants.ACTIVITY_TYPE_SUBFLOW:
0594: retVal = JaWEConstants.ACTIVITY_TYPE_SUBFLOW;
0595: break;
0596: }
0597:
0598: return retVal;
0599: }
0600:
0601: public static List sortValidationErrorList(List verrs) {
0602: List sorted = new ArrayList();
0603:
0604: Map epves = JaWEManager.getInstance().getXPDLValidator()
0605: .getExtPkgValidationErrors();
0606: Iterator it = epves.values().iterator();
0607: while (it.hasNext()) {
0608: List l = (List) it.next();
0609: verrs.removeAll(l);
0610: }
0611: sorted.addAll(Utils.sortValidationErrorList(verrs, JaWEManager
0612: .getInstance().getJaWEController().getMainPackage()));
0613: it = epves.entrySet().iterator();
0614: while (it.hasNext()) {
0615: Map.Entry me = (Map.Entry) it.next();
0616: Package p = (Package) me.getKey();
0617: List l = (List) me.getValue();
0618: sorted.addAll(Utils.sortValidationErrorList(l, p));
0619: }
0620:
0621: return sorted;
0622:
0623: }
0624:
0625: public static List sortValidationErrorList(List verrs, Package p) {
0626: List sorted = new ArrayList();
0627:
0628: sorted.addAll(findErrorList(verrs, Package.class,
0629: XMLValidationError.SUB_TYPE_SCHEMA));
0630:
0631: sorted.addAll(findErrorList(verrs, p,
0632: XMLValidationError.SUB_TYPE_LOGIC));
0633: sorted.addAll(findErrorListParent(verrs, p));
0634: sorted.addAll(findErrorListParent(verrs, p
0635: .getTypeDeclarations()));
0636: sorted.addAll(findErrorListParent(verrs, p.getApplications()));
0637: sorted.addAll(findErrorListParent(verrs, p.getParticipants()));
0638: sorted.addAll(findErrorListParent(verrs, p.getDataFields()));
0639: Iterator it = p.getApplications().toElements().iterator();
0640: while (it.hasNext()) {
0641: Application app = (Application) it.next();
0642: sorted.addAll(findErrorListParent(verrs, app
0643: .getApplicationTypes().getFormalParameters()));
0644: }
0645:
0646: it = p.getWorkflowProcesses().toElements().iterator();
0647: while (it.hasNext()) {
0648: WorkflowProcess wp = (WorkflowProcess) it.next();
0649: sorted.addAll(findErrorList(verrs, wp));
0650: }
0651: it = p.getWorkflowProcesses().toElements().iterator();
0652: while (it.hasNext()) {
0653: WorkflowProcess wp = (WorkflowProcess) it.next();
0654: Iterator it2 = wp.getActivitySets().toElements().iterator();
0655: while (it2.hasNext()) {
0656: ActivitySet as = (ActivitySet) it2.next();
0657: sorted.addAll(findErrorList(verrs, as));
0658: }
0659: }
0660: it = p.getWorkflowProcesses().toElements().iterator();
0661: while (it.hasNext()) {
0662: WorkflowProcess wp = (WorkflowProcess) it.next();
0663: sorted.addAll(findErrorListParent(verrs, wp
0664: .getApplications()));
0665: sorted.addAll(findErrorListParent(verrs, wp
0666: .getParticipants()));
0667: sorted
0668: .addAll(findErrorListParent(verrs, wp
0669: .getDataFields()));
0670: sorted.addAll(findErrorListParent(verrs, wp
0671: .getFormalParameters()));
0672: Iterator it2 = wp.getApplications().toElements().iterator();
0673: while (it2.hasNext()) {
0674: Application app = (Application) it2.next();
0675: sorted.addAll(findErrorListParent(verrs, app
0676: .getApplicationTypes().getFormalParameters()));
0677: }
0678: it2 = wp.getActivitySets().toElements().iterator();
0679: while (it2.hasNext()) {
0680: ActivitySet as = (ActivitySet) it2.next();
0681: Iterator it3 = as.getActivities().toElements()
0682: .iterator();
0683: while (it3.hasNext()) {
0684: Activity act = (Activity) it3.next();
0685: sorted.addAll(findErrorList(verrs, act));
0686: }
0687: it3 = as.getTransitions().toElements().iterator();
0688: while (it3.hasNext()) {
0689: Transition tra = (Transition) it3.next();
0690: sorted.addAll(findErrorList(verrs, tra));
0691: }
0692: }
0693: it2 = wp.getActivities().toElements().iterator();
0694: while (it2.hasNext()) {
0695: Activity act = (Activity) it2.next();
0696: sorted.addAll(findErrorList(verrs, act));
0697: }
0698: it2 = wp.getTransitions().toElements().iterator();
0699: while (it2.hasNext()) {
0700: Transition tra = (Transition) it2.next();
0701: sorted.addAll(findErrorList(verrs, tra));
0702: }
0703: }
0704: verrs.removeAll(sorted);
0705: sorted.addAll(verrs);
0706:
0707: return sorted;
0708: }
0709:
0710: public static List findErrorList(List allErrs, Object obj,
0711: String validationType) {
0712: List toRet = new ArrayList();
0713:
0714: for (int i = 0; i < allErrs.size(); i++) {
0715: ValidationError verr = (ValidationError) allErrs.get(i);
0716: if (verr.getSubType().equals(validationType)
0717: && verr.getElement() == obj) {
0718: toRet.add(verr);
0719: }
0720: }
0721: return toRet;
0722: }
0723:
0724: public static List findErrorList(List allErrs, Object obj) {
0725: List toRet = new ArrayList();
0726:
0727: for (int i = 0; i < allErrs.size(); i++) {
0728: ValidationError verr = (ValidationError) allErrs.get(i);
0729: if (verr.getElement() == obj) {
0730: toRet.add(verr);
0731: }
0732: }
0733: return toRet;
0734: }
0735:
0736: public static List findErrorList(List allErrs, Class objClass) {
0737: List toRet = new ArrayList();
0738:
0739: for (int i = 0; i < allErrs.size(); i++) {
0740: ValidationError verr = (ValidationError) allErrs.get(i);
0741: if (verr.getElement().getClass() == objClass) {
0742: toRet.add(verr);
0743: }
0744: }
0745: return toRet;
0746: }
0747:
0748: public static List findErrorList(List allErrs, Class objClass,
0749: String validationType) {
0750: List toRet = new ArrayList();
0751:
0752: for (int i = 0; i < allErrs.size(); i++) {
0753: ValidationError verr = (ValidationError) allErrs.get(i);
0754: if (verr.getSubType().equals(validationType)
0755: && verr.getElement().getClass() == objClass) {
0756: toRet.add(verr);
0757: }
0758: }
0759: return toRet;
0760: }
0761:
0762: public static List findErrorListParent(List allErrs,
0763: Object parentObj) {
0764: List toRet = new ArrayList();
0765:
0766: for (int i = 0; i < allErrs.size(); i++) {
0767: ValidationError verr = (ValidationError) allErrs.get(i);
0768: if (verr.getElement().getParent() == parentObj) {
0769: toRet.add(verr);
0770: }
0771: }
0772: return toRet;
0773: }
0774:
0775: public static int countErrors(List verrs) {
0776: int errs = 0;
0777: for (int i = 0; i < verrs.size(); i++) {
0778: ValidationError verr = (ValidationError) verrs.get(i);
0779: if (verr.getType().equalsIgnoreCase(
0780: XMLValidationError.TYPE_ERROR)) {
0781: errs++;
0782: }
0783: }
0784: return errs;
0785: }
0786:
0787: /**
0788: * Java doesn't support direct opening of arbitrary documents, but this hack should do
0789: * it. For Windows executing "start", and for KDE "kfmclient exec" will open document
0790: * with associated application. Associations are of course system dependant, and we
0791: * cannot do anything about them.
0792: */
0793: public static boolean showExternalDocument(String document) {
0794: if (!(new File(document).canRead())) {
0795: return false;
0796: }
0797: String startCommand = System.getProperty("path.to.start");
0798: if (null != startCommand) {
0799: if (!new File(startCommand).canRead()) {
0800: return false;
0801: }
0802: if (System.getProperty("path.separator").equals(";")) {
0803: document = "\"" + document + "\"";
0804: }
0805: } else {
0806: if (System.getProperty("path.separator").equals(":")) {
0807: startCommand = "kfmclient exec";
0808: } else {
0809: startCommand = "cmd /c start";
0810: document = "\"" + document + "\"" + " \"" + document
0811: + "\"";
0812: }
0813: }
0814: try {
0815: Runtime.getRuntime().exec(startCommand + " " + document);
0816: } catch (Throwable t) {
0817: t.printStackTrace();
0818: return false;
0819: }
0820: return true;
0821: }
0822:
0823: public static XMLElement getLocation(XMLElement el) {
0824: XMLElement location = XMLUtil.getActivity(el);
0825: if (location == null) {
0826: location = XMLUtil.getTransition(el);
0827: }
0828: if (location == null) {
0829: location = XMLUtil.getTransition(el);
0830: }
0831: if (location == null) {
0832: location = XMLUtil.getParentElement(TypeDeclaration.class,
0833: el);
0834: }
0835: if (location == null) {
0836: location = XMLUtil.getParentElement(FormalParameter.class,
0837: el);
0838: }
0839: if (location == null) {
0840: location = XMLUtil.getParentElement(Application.class, el);
0841: }
0842: if (location == null) {
0843: location = XMLUtil.getParentElement(Participant.class, el);
0844: }
0845: if (location == null) {
0846: location = XMLUtil.getParentElement(DataField.class, el);
0847: }
0848: ActivitySet as = XMLUtil.getActivitySet(el);
0849: if (location == null) {
0850: location = as;
0851: }
0852: WorkflowProcess wp = XMLUtil.getWorkflowProcess(el);
0853: if (location == null) {
0854: location = wp;
0855: }
0856: Package pkg = XMLUtil.getPackage(el);
0857: if (location == null) {
0858: location = pkg;
0859: }
0860: return location;
0861: }
0862:
0863: public static String getLocString(XMLElement location, XMLElement el) {
0864: ActivitySet as = XMLUtil.getActivitySet(el);
0865: WorkflowProcess wp = XMLUtil.getWorkflowProcess(el);
0866: Package pkg = XMLUtil.getPackage(el);
0867:
0868: String loc = ResourceManager
0869: .getLanguageDependentString("PackageKey")
0870: + " '" + pkg.getId() + "'";
0871: if (wp != null) {
0872: loc += ", "
0873: + ResourceManager
0874: .getLanguageDependentString("WorkflowProcessKey")
0875: + " '" + wp.getId() + "'";
0876: }
0877: if (as != null) {
0878: loc += ", "
0879: + ResourceManager
0880: .getLanguageDependentString("ActivitySetKey")
0881: + " '" + as.getId() + "'";
0882: }
0883: if (location != as && location != wp && location != pkg) {
0884: loc += ", "
0885: + ResourceManager
0886: .getLanguageDependentString(location
0887: .toName()
0888: + "Key")
0889: + " '"
0890: + ((XMLComplexElement) location).get("Id")
0891: .toValue() + "'";
0892: }
0893: if (el != location && el != as && el != wp && el != pkg) {
0894: XMLElement parent = el.getParent();
0895: if (parent != location
0896: && !(parent instanceof XMLCollection)) {
0897: loc += ", "
0898: + ResourceManager
0899: .getLanguageDependentString(parent
0900: .toName()
0901: + "Key");
0902: }
0903: loc += " -> "
0904: + ResourceManager.getLanguageDependentString(el
0905: .toName()
0906: + "Key");
0907: }
0908:
0909: return loc;
0910: }
0911:
0912: public static List makeSearchResultList(List results) {
0913: List srl = new ArrayList();
0914:
0915: for (int i = 0; i < results.size(); i++) {
0916: srl.add(new SearchResult((XMLElement) (results.get(i))));
0917: }
0918:
0919: return srl;
0920: }
0921:
0922: public static Map loadActions(Properties properties,
0923: JaWEComponent comp) {
0924: return loadActions(properties, comp, new HashMap());
0925: }
0926:
0927: public static Map loadActions(Properties properties,
0928: JaWEComponent comp, Map defaultActions) {
0929: Map componentAction = new HashMap();
0930:
0931: // ******** actions
0932: Set actions = new HashSet();
0933: List actionsN = ResourceManager.getResourceStrings(properties,
0934: "Action.Name.", false);
0935: List actionsC = ResourceManager.getResourceStrings(properties,
0936: "Action.Class.", false);
0937: List actionsI = ResourceManager.getResourceStrings(properties,
0938: "Action.Image.", false);
0939: actions.addAll(actionsN);
0940: actions.addAll(actionsC);
0941: actions.addAll(actionsI);
0942: for (Iterator it = actions.iterator(); it.hasNext();) {
0943: String actionName = (String) it.next();
0944:
0945: try {
0946: JaWEAction ja;
0947: if (defaultActions.containsKey(actionName))
0948: ja = (JaWEAction) defaultActions.get(actionName);
0949: else
0950: ja = new JaWEAction();
0951:
0952: try {
0953: // Action Base
0954: String className = ResourceManager
0955: .getResourceString(properties,
0956: "Action.Class." + actionName);
0957: Constructor c = Class
0958: .forName(className)
0959: .getConstructor(
0960: new Class[] { JaWEComponent.class });
0961: ActionBase action = (ActionBase) c
0962: .newInstance(new Object[] { comp });
0963: ja.setAction(action);
0964: } catch (Exception e) {
0965: }
0966:
0967: try {
0968: // Icon
0969: URL url = ResourceManager.getResource(properties,
0970: "Action.Image." + actionName);
0971: ImageIcon icon = null;
0972: if (url != null)
0973: icon = new ImageIcon(url);
0974:
0975: ja.setIcon(icon);
0976: } catch (Exception e) {
0977: }
0978:
0979: try {
0980: // Language dependent name
0981: String langDepName = ResourceManager
0982: .getResourceString(properties,
0983: "Action.Name." + actionName);
0984: ja.setLangDepName(langDepName);
0985: } catch (Exception e) {
0986: }
0987:
0988: if (ja.getAction() != null) {
0989: componentAction.put(actionName, ja);
0990: JaWEManager.getInstance().getLoggingManager()
0991: .info(
0992: "Created " + comp.getName()
0993: + " action for class "
0994: + actionName);
0995: } else {
0996: JaWEManager.getInstance().getLoggingManager().info(
0997: "Missing action for " + actionName);
0998: }
0999: } catch (Throwable thr) {
1000: JaWEManager.getInstance().getLoggingManager().error(
1001: "Can't create " + comp.getName()
1002: + " action for class " + actionName);
1003: }
1004: }
1005:
1006: return componentAction;
1007: }
1008:
1009: public static Map loadAllMenusAndToolbars(Properties properties) {
1010: Map mst = new HashMap();
1011:
1012: mst = loadSubMenus(properties);
1013: mst.putAll(loadPopups(properties));
1014: mst.putAll(loadToolbars(properties));
1015:
1016: return mst;
1017: }
1018:
1019: public static Map loadSubMenus(Properties properties) {
1020: Map componentSettings = new HashMap();
1021:
1022: List subMenus = ResourceManager.getResourceStrings(properties,
1023: "SubMenu.Name.", false);
1024: for (int i = 0; i < subMenus.size(); i++) {
1025: String aorder = ResourceManager.getResourceString(
1026: properties, "SubMenu.ActionOrder."
1027: + subMenus.get(i));
1028: String langName = ResourceManager.getResourceString(
1029: properties, "SubMenu.Name." + subMenus.get(i));
1030: componentSettings.put(subMenus.get(i) + "Menu", aorder);
1031: componentSettings.put(subMenus.get(i) + "LangName",
1032: langName);
1033: }
1034:
1035: return componentSettings;
1036: }
1037:
1038: public static Map loadToolbars(Properties properties) {
1039: Map componentSettings = new HashMap();
1040:
1041: List toolbars = ResourceManager.getResourceStrings(properties,
1042: "Toolbar.ActionOrder.", false);
1043: for (int i = 0; i < toolbars.size(); i++) {
1044: String aorder = ResourceManager.getResourceString(
1045: properties, "Toolbar.ActionOrder."
1046: + toolbars.get(i));
1047: componentSettings.put(toolbars.get(i) + "Toolbar", aorder);
1048: }
1049:
1050: return componentSettings;
1051: }
1052:
1053: public static Map loadPopups(Properties properties) {
1054: Map componentSettings = new HashMap();
1055: List popupMenus = ResourceManager.getResourceStrings(
1056: properties, "PopupMenu.ActionOrder.", false);
1057: for (int i = 0; i < popupMenus.size(); i++) {
1058: String aorder = ResourceManager.getResourceString(
1059: properties, "PopupMenu.ActionOrder."
1060: + popupMenus.get(i));
1061: componentSettings.put(popupMenus.get(i) + "Menu", aorder);
1062: }
1063: return componentSettings;
1064: }
1065:
1066: protected static SequencedHashMap actImgResources = null;
1067:
1068: protected static List actImgResourceNames = null;
1069:
1070: public static List getActivityIconNamesList() {
1071: if (actImgResources == null) {
1072: getActivityIconsMap();
1073: }
1074: return new ArrayList(actImgResourceNames);
1075: }
1076:
1077: public static SequencedHashMap getOriginalActivityIconsMap() {
1078: if (actImgResources == null) {
1079: getActivityIconsMap();
1080: }
1081: return actImgResources;
1082: }
1083:
1084: public static SequencedHashMap getActivityIconsMap() {
1085: if (actImgResources == null) {
1086: Map m = new HashMap();
1087: String classPath = System.getProperty("java.class.path");
1088: String[] cps = Utils
1089: .tokenize(classPath, File.pathSeparator);
1090: String jed = System.getProperty("java.ext.dirs");
1091: String[] jeds = Utils.tokenize(jed, File.pathSeparator);
1092: for (int i = 0; i < cps.length; i++) {
1093: String cp = cps[i];
1094: if (cp.indexOf("activityicons") >= 0) {
1095: m.putAll(Utils.getResourcesForPath(cp,
1096: JaWEConstants.JAWE_ACTIVITY_ICONS));
1097: }
1098: }
1099: for (int i = 0; i < jeds.length; i++) {
1100: String jd = jeds[i];
1101: File f = new File(jd);
1102: if (f.exists() && f.isDirectory()) {
1103: File[] files = f.listFiles();
1104: if (files != null) {
1105: for (int j = 0; j < files.length; j++) {
1106: if (files[j].isFile()
1107: && files[j].getName().indexOf(
1108: "activityicons") >= 0) {
1109: m
1110: .putAll(Utils
1111: .getResourcesForPath(
1112: files[j]
1113: .getAbsolutePath(),
1114: JaWEConstants.JAWE_ACTIVITY_ICONS));
1115: }
1116: }
1117: }
1118: }
1119: }
1120: actImgResources = new SequencedHashMap();
1121: actImgResourceNames = new ArrayList(m.keySet());
1122: Collections.sort(actImgResourceNames);
1123: for (int i = 0; i < actImgResourceNames.size(); i++) {
1124: Object key = actImgResourceNames.get(i);
1125: actImgResources.put(key, m.get(key));
1126: }
1127: }
1128: return new SequencedHashMap(actImgResources);
1129: }
1130:
1131: /**
1132: * Returns a Map of images from located on given path.
1133: */
1134: public static Map getResourcesForPath(String classPath,
1135: String prefix) {
1136: Map resources = new HashMap();
1137: // case of resources contained in a jar file.
1138: if (classPath.endsWith(".jar")) {
1139: JarFile jfile = null;
1140: try {
1141: jfile = new JarFile(classPath, false);
1142: } catch (Throwable ex) {
1143: ex.printStackTrace();
1144: return resources;
1145: }
1146: // get all entries
1147: Enumeration e = jfile.entries();
1148: // loop through entries and find appropriate ones
1149: while (e.hasMoreElements()) {
1150: try {
1151: ZipEntry entry = (ZipEntry) e.nextElement();
1152: String entryname = entry.getName();
1153: String res = entryname;
1154: // removes starting '/'
1155: if (res.startsWith("/")) {
1156: res = res.substring(1);
1157: }
1158: // entry must start with prefix
1159: if (entryname.startsWith(prefix)) {
1160: // checking if resource is image
1161: ImageIcon ii = null;
1162: try {
1163: ii = new ImageIcon(Utils.class
1164: .getClassLoader().getResource(
1165: entryname));
1166: } catch (Exception ex) {
1167: }
1168:
1169: if (ii != null) {
1170: // removes the prefix
1171: res = res.substring(prefix.length());
1172: if (res.startsWith("/")) {
1173: res = res.substring(1);
1174: }
1175: if (res.length() > 0) {
1176: resources.put(res, ii);
1177: }
1178: }
1179: }
1180: } catch (Throwable thr) {
1181: }
1182: }
1183: } else { // if it is not .jar file, but directory
1184: // getting the folder
1185: File startingFolder = new File(classPath + "/" + prefix);
1186: // if folder exists and realy is a folder but not file
1187: if (startingFolder.exists() && startingFolder.isDirectory()) {
1188: File[] children = startingFolder.listFiles();
1189: for (int i = 0; i < children.length; i++) {
1190: if (children[i].isFile()) {
1191: String fileName = children[i].getName();
1192: // checking if resource is image
1193: ImageIcon ii = null;
1194: try {
1195: ii = new ImageIcon(Utils.class
1196: .getClassLoader().getResource(
1197: prefix + "/" + fileName));
1198: } catch (Exception ex) {
1199: }
1200: if (ii != null) {
1201: resources.put(fileName, ii);
1202: }
1203: }
1204: }
1205:
1206: }
1207: }
1208: return resources;
1209: }
1210:
1211: public static boolean reconfigure(String newConfig) {
1212: JaWEController jc = JaWEManager.getInstance()
1213: .getJaWEController();
1214:
1215: String fn = JaWEConstants.JAWE_CONF_HOME + "/defaultconfig";
1216: File file = new File(fn);
1217:
1218: if (!file.exists()) {
1219: return false;
1220: }
1221:
1222: File newFile = new File(JaWEConstants.JAWE_CONF_HOME + "/temp");
1223:
1224: WaitScreen ws = new WaitScreen(null);
1225: try {
1226: newFile.createNewFile();
1227:
1228: BufferedReader reader = new BufferedReader(new FileReader(
1229: file));
1230: BufferedWriter writer = new BufferedWriter(new FileWriter(
1231: newFile));
1232:
1233: String line = null;
1234: boolean done = false;
1235: while ((line = reader.readLine()) != null) {
1236: if (!done
1237: && (line
1238: .startsWith("#"
1239: + JaWEConstants.JAWE_CURRENT_CONFIG_HOME) || line
1240: .startsWith(JaWEConstants.JAWE_CURRENT_CONFIG_HOME))) {
1241: line = JaWEConstants.JAWE_CURRENT_CONFIG_HOME
1242: + " = " + newConfig;
1243:
1244: done = true;
1245: }
1246:
1247: writer.write(line + "\n");
1248: }
1249:
1250: reader.close();
1251: writer.close();
1252:
1253: file.delete();
1254: newFile.renameTo(file);
1255:
1256: System.setProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME,
1257: JaWEConstants.JAWE_CONF_HOME + "/" + newConfig);
1258:
1259: String filename = jc.getPackageFilename(jc
1260: .getMainPackageId());
1261: jc.tryToClosePackage(jc.getMainPackageId(), true);
1262:
1263: ws.show(null, "", jc.getSettings()
1264: .getLanguageDependentString("ReconfiguringKey"));
1265:
1266: JaWEManager.getInstance().restart(filename);
1267: ws.setVisible(false);
1268: return true;
1269: } catch (Throwable e) {
1270: e.printStackTrace();
1271: return false;
1272: } finally {
1273: ws.setVisible(false);
1274: }
1275: }
1276:
1277: }
1278:
1279: class PFFilter implements FilenameFilter {
1280: String pfStr = ".properties";
1281:
1282: public boolean accept(File dir, String name) {
1283: String f = new File(name).getName();
1284: int fi = f.indexOf(Utils.LANG_PROP_PREFIX);
1285: int li = f.lastIndexOf(Utils.LANG_PROP_PREFIX);
1286: return (fi != -1 && fi == 0 && fi == li && f.endsWith(pfStr));
1287: }
1288: }
|