EventConverter written. Next up, test it. Also consider standardizing

jjp32 [2002-08-29 22:14:10]
EventConverter written.  Next up, test it.  Also consider standardizing
event designators
Filename
ep/output/ConsoleOutput.java
ep/transform/EventConverter.java
ep/util/EPConst.java
diff --git a/ep/output/ConsoleOutput.java b/ep/output/ConsoleOutput.java
index 29acf30..f5170b6 100644
--- a/ep/output/ConsoleOutput.java
+++ b/ep/output/ConsoleOutput.java
@@ -9,6 +9,7 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;

 import psl.xues.ep.event.*;
+import psl.xues.ep.util.EPConst;

 import siena.Notification;

@@ -18,9 +19,10 @@ import siena.Notification;
  * Copyright (c) 2002: The Trustees of Columbia University in the
  * City of New York.  All Rights Reserved.
  *
- * <!--
+ * <!--
  * TODO:
- * - Handle every other kind of event other than DOMEvent better:)
+ * - Handle every other kind of event other than DOMEvent better
+ * - Handle DOMEvent better :)
  * -->
  *
  * @author Janak J Parekh <janak@cs.columbia.edu>
@@ -30,7 +32,7 @@ public class ConsoleOutput extends EPOutput {
   /**
    * CTOR.
    */
-  public ConsoleOutput(EPOutputInterface ep, Element el)
+  public ConsoleOutput(EPOutputInterface ep, Element el)
   throws InstantiationException {
     super(ep,el);
   }
@@ -65,8 +67,8 @@ public class ConsoleOutput extends EPOutput {
     } else { // Just try to print it out
       debug.info("Event (" + epe.getFormat() + ") received");
       debug.info("Event contents:\n---" + epe + "---");
-    }
-
+    }
+
     return true;  // Wasn't that hard?
   }

@@ -103,7 +105,7 @@ public class ConsoleOutput extends EPOutput {
       }
     }
   }
-
+
   /**
    * Print Siena notification
    */
diff --git a/ep/transform/EventConverter.java b/ep/transform/EventConverter.java
new file mode 100644
index 0000000..ae63ae0
--- /dev/null
+++ b/ep/transform/EventConverter.java
@@ -0,0 +1,85 @@
+package psl.xues.ep.transform;
+
+import org.w3c.dom.Element;
+import psl.xues.ep.event.EPEvent;
+import psl.xues.ep.util.EPConst;
+
+/**
+ * Transform that forces an event conversion.  By default, Event Packager does
+ * lazy conversion; this forces it explicitly.
+ * <p>
+ * Copyright (c) 2002: The Trustees of Columbia University in the
+ * City of New York.  All Rights Reserved.
+ *
+ * @author Janak J Parekh <janak@cs.columbia.edu>
+ * @version $Revision$
+ */
+public class EventConverter extends EPTransform implements EPConst {
+  private short outputFormat = -1;  // -1 construes whatever comes our way
+
+  /**
+   * CTOR.
+   *
+   * @param el The element with initialization info for this transform.
+   */
+  public EventConverter(EPTransformInterface ep, Element el)
+  throws InstantiationException {
+    super(ep,el);
+
+
+    // Do we force output format?
+    String outputFormat = el.getAttribute("OutputFormat");
+    if(outputFormat != null && outputFormat.length() > 0) {
+      if(outputFormat.equalsIgnoreCase("DOMEvent") ||
+      outputFormat.equalsIgnoreCase("DOM_OBJECT")) {
+        this.outputFormat = DOM_OBJECT;
+      } else if(outputFormat.equalsIgnoreCase("SienaEvent") ||
+      outputFormat.equalsIgnoreCase("SIENA_OBJECT")) {
+        this.outputFormat = SIENA_OBJECT;
+      } else if(outputFormat.equalsIgnoreCase("XMLEvent") ||
+      outputFormat.equalsIgnoreCase("XML_OBJECT")) {
+        this.outputFormat = XML_OBJECT;
+      } else if(outputFormat.equalsIgnoreCase("StringEvent") ||
+      outputFormat.equalsIgnoreCase("STRING_OBJECT")) {
+        this.outputFormat = STRING_OBJECT;
+      } else {
+        debug.error("Invalid OutputFormat specified, ignoring");
+      }
+    }
+  }
+
+  /**
+   * Handle a transform request.
+   *
+   * @param original The EPEvent that needs transformation.
+   * @return The transformed EPEvent, or null if you can't handle the
+   * transform.
+   */
+  public EPEvent transform(EPEvent original) {
+    EPEvent newepe = null;
+    if(outputFormat != -1) {
+      if(outputFormat == STRING_OBJECT) {
+        newepe = original.convertEvent("StringEvent");
+      } else if(outputFormat == XML_OBJECT || outputFormat == DOM_OBJECT) {
+        newepe = original.convertEvent("DOMEvent");
+      } else if(outputFormat == SIENA_OBJECT) {
+        newepe = original.convertEvent("SienaEvent");
+      } else {
+        debug.error("Unhandled OutputFormat");
+      }
+    }
+    if(newepe == null) {
+      debug.warn("Couldn't convert event, returning original event");
+      return original;
+    }
+
+    return newepe;
+  }
+
+  /**
+   * Get the type.
+   */
+  public String getType() {
+    return "EventConverter";
+  }
+}
\ No newline at end of file
diff --git a/ep/util/EPConst.java b/ep/util/EPConst.java
index 1c551a2..06ba996 100644
--- a/ep/util/EPConst.java
+++ b/ep/util/EPConst.java
@@ -26,4 +26,8 @@ public interface EPConst {
   public static final short STRING_OBJECT = 2;
   /** Specifies a XML object-based data type */
   public static final short XML_OBJECT = 3;
+  /** For console output only (currently) */
+  public static final short SIENA_OBJECT = 4;
+  /** For console output only (currently) */
+  public static final short DOM_OBJECT = 5;
 }