- Tested and documented EventConverter.

jjp32 [2002-09-02 04:58:41]
- Tested and documented EventConverter.
- Improved ConsoleOutput handling: specifically, EP now requires a toString method for any
implemented event format.  Implemented toString for existing event formats.  EPEvent
also now supports a "getInfo" call which supplies metadata as well.
Yuan, consider using these constructs to test your code in the EP framework...
Filename
ep/EPConfig.xml
ep/event/DOMEvent.java
ep/event/EPEvent.java
ep/event/FlatEvent.java
ep/event/HierarchicalEvent.java
ep/event/SienaEvent.java
ep/event/StringEvent.java
ep/output/ConsoleOutput.java
ep/transform/EventConverter.java
diff --git a/ep/EPConfig.xml b/ep/EPConfig.xml
index 865aa42..aad104b 100644
--- a/ep/EPConfig.xml
+++ b/ep/EPConfig.xml
@@ -20,6 +20,8 @@
     <Outputter Name="ConsoleOutput1" Type="psl.xues.ep.output.ConsoleOutput"></Outputter>
   </Outputters>
   <Transforms>
+    <Transform Name="EventConverter1"
+        Type="psl.xues.ep.transform.EventConverter" OutputFormat="SienaEvent"/>
   </Transforms>
   <Stores>
   </Stores>
@@ -44,6 +46,9 @@
       <Inputs>
 	<Input Name="ConsoleInput1"></Input>
       </Inputs>
+      <Transforms>
+        <!--<Transform Name="EventConverter1" />-->
+      </Transforms>
       <Outputs>
 	<Output Name="ConsoleOutput1"></Output>
       </Outputs>
diff --git a/ep/event/DOMEvent.java b/ep/event/DOMEvent.java
index 442161c..3ce11e9 100644
--- a/ep/event/DOMEvent.java
+++ b/ep/event/DOMEvent.java
@@ -109,21 +109,7 @@ public class DOMEvent extends EPEvent {
   public EPEvent convertEvent(String newFormat) {
     // TO STRING FORMAT ///////////////////////////////////////////////
     if(newFormat.equalsIgnoreCase("StringEvent")) {
-      // DOM output to String
-      StringWriter sw = new StringWriter();
-      // Use transformer
-      Transformer t = JAXPUtil.newTransformer();
-      // Set up the source and target
-      DOMSource source = new DOMSource(data);
-      StreamResult result = new StreamResult(sw);
-      try {
-        t.transform(source, result);
-      } catch(Exception e) {
-        debug.error("Could not convert event to String", e);
-        return null;
-      }
-      // Finally, construct the StringEvent and return it
-      return new StringEvent(getSource(), sw.toString());
+      return new StringEvent(getSource(), toString());
     }
     // TO OTHER FORMAT... /////////////////////////////////////////////

@@ -132,4 +118,26 @@ public class DOMEvent extends EPEvent {
     debug.warn("DOMEvent can't be converted to \"" + newFormat + "\"");
     return null;
   }
+
+  /**
+   * Return a String representation of this DOMEvent.
+   *
+   * @return A String representation (in our case, XML is outputted).
+   */
+  public String toString() {
+    // DOM output to String
+    StringWriter sw = new StringWriter();
+    // Use transformer
+    Transformer t = JAXPUtil.newTransformer();
+    // Set up the source and target
+    DOMSource source = new DOMSource(data);
+    StreamResult result = new StreamResult(sw);
+    try {
+      t.transform(source, result);
+    } catch(Exception e) {
+      debug.error("Could not convert event to String", e);
+      return null;
+    }
+    return sw.toString();
+  }
 }
\ No newline at end of file
diff --git a/ep/event/EPEvent.java b/ep/event/EPEvent.java
index 14c1bf2..2bfc00b 100644
--- a/ep/event/EPEvent.java
+++ b/ep/event/EPEvent.java
@@ -131,4 +131,22 @@ public abstract class EPEvent implements Comparable, Serializable {
     else if(this.timestamp == ((EPEvent)o).timestamp) return 0;
     else return 1; // Must be greater
   }
+
+  /**
+   * toString method -- all extenders must implement this.
+   *
+   * @return A string representation of this EPEvent
+   */
+  public abstract String toString();
+
+  /**
+   * Event information, returned in string format.  Utilizes the toString
+   * method implemented by children, but appends some metadata to the result.
+   *
+   * @return A string with all the information and data for this event.
+   */
+  public String getInfo() {
+    return getFormat() + "- source \"" + source +
+    "\", timestamp \"" + timestamp + "\", data {" + toString() + "}";
+  }
 }
\ No newline at end of file
diff --git a/ep/event/FlatEvent.java b/ep/event/FlatEvent.java
index 0b2b037..360d8c6 100644
--- a/ep/event/FlatEvent.java
+++ b/ep/event/FlatEvent.java
@@ -10,13 +10,14 @@ package psl.xues.ep.event;
  *
  * <!--
  * TODO:
- * - Implement this!
+ * - Finish abstract class implementation
+ * - Create necessary implementations of this abstract class
  * -->
  *
  * @author Janak J Parekh <janak@cs.columbia.edu>
  * @version $Revision$
  */
-public class FlatEvent extends EPEvent {
+public abstract class FlatEvent extends EPEvent {
   /**
    * Create an empty FlatEvent.
    *
diff --git a/ep/event/HierarchicalEvent.java b/ep/event/HierarchicalEvent.java
index 196f888..8cff17c 100644
--- a/ep/event/HierarchicalEvent.java
+++ b/ep/event/HierarchicalEvent.java
@@ -14,13 +14,14 @@ import org.w3c.dom.Node;
  *
  * <!--
  * TODO:
- * - Implement this!
+ * - Finish abstract class implementation
+ * - Create necessary implementations of this abstract class
  * -->
  *
  * @author Janak J Parekh <janak@cs.columbia.edu>
  * @revision $Revision$
  */
-public class HierarchicalEvent extends EPEvent {
+public abstract class HierarchicalEvent extends EPEvent {
   /** Format of this event */
   private final String format = "HierarchicalEvent";
   /** Associated data */
diff --git a/ep/event/SienaEvent.java b/ep/event/SienaEvent.java
index e53a2d9..d9501bd 100644
--- a/ep/event/SienaEvent.java
+++ b/ep/event/SienaEvent.java
@@ -89,8 +89,7 @@ public class SienaEvent extends EPEvent {
    * @return The string representation
    */
   public String toString() {
-    return "psl.xues.ep.event.SienaEvent - source \"" + source +
-    "\", timestamp \"" + timestamp + "\", notification " + n;
+    return n.toString();
   }

   /**
diff --git a/ep/event/StringEvent.java b/ep/event/StringEvent.java
index 11d5db1..1ba41db 100644
--- a/ep/event/StringEvent.java
+++ b/ep/event/StringEvent.java
@@ -98,5 +98,7 @@ public class StringEvent extends EPEvent {
     return null;
   }

-
+  public String toString() {
+    return data;
+  }
 }
\ No newline at end of file
diff --git a/ep/output/ConsoleOutput.java b/ep/output/ConsoleOutput.java
index f5170b6..2903616 100644
--- a/ep/output/ConsoleOutput.java
+++ b/ep/output/ConsoleOutput.java
@@ -19,12 +19,6 @@ 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 DOMEvent better :)
- * -->
- *
  * @author Janak J Parekh <janak@cs.columbia.edu>
  * @version $Revision$
  */
@@ -52,73 +46,12 @@ public class ConsoleOutput extends EPOutput {
    * @return Always true.
    */
   public boolean handleEvent(EPEvent epe) {
-    // Is it a DOM output?
-    if(epe instanceof DOMEvent) {
-      if(((DOMEvent)epe).getDOMRoot() != null) {
-        debug.info("Event (DOMEvent) received:");
-        // Now walk it
-        printDOMElement(((DOMEvent)epe).getDOMRoot(),0);
-      } else {
-        debug.info("It's an empty DOMEvent");
-      }
-    } else if(epe instanceof SienaEvent) {
-      debug.info("Event (Siena) received:");
-      printSienaEvent(((SienaEvent)epe).getSienaEvent());
-    } else { // Just try to print it out
-      debug.info("Event (" + epe.getFormat() + ") received");
-      debug.info("Event contents:\n---" + epe + "---");
-    }
+    debug.info(epe.getInfo());

     return true;  // Wasn't that hard?
   }

   /**
-   * Walk a DOM element.
-   */
-  private void printDOMElement(Element el, int level) {
-    // print myself
-    debug.info("" + level + "/Element/" + el.getNodeName());
-
-    // Get embedded attributes and print them out first
-    if(el.getAttributes() != null) {
-      NamedNodeMap attrs = el.getAttributes();
-      for(int i=0; i < attrs.getLength(); i++) {
-        if(attrs.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
-          Attr at = (Attr)attrs.item(i);
-          debug.info("" + level + "/Element/" + el.getNodeName() +
-          "/Attribute/" + at.getNodeName() + "/Value/" + at.getNodeValue());
-        } else { // Not an attribute, just print it out
-          debug.info("" + level + "/Element/" + el.getNodeName() +
-          "/Other/" + attrs.item(i));
-        }
-      }
-    }
-
-    // Now get the children and walk them
-    NodeList nl = el.getChildNodes();
-    for(int i=0; i < nl.getLength(); i++) {
-      if(nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
-        // Recurse
-        printDOMElement((Element)nl.item(i), level + 1);
-      } else { // Just print it out
-        debug.info("" + level + 1 + "/Other/" + nl.item(i));
-      }
-    }
-  }
-
-  /**
-   * Print Siena notification
-   */
-  private void printSienaEvent(Notification n) {
-    Iterator i = n.attributeNamesIterator();
-    while(i.hasNext()) {
-      String attrName = (String)i.next();
-      System.out.println("- " + attrName + " : " + n.getAttribute(attrName));
-    }
-  }
-
-
-  /**
    * Handle shutdown.  (Like there's a lot to do in ConsoleOutput's shutdown :))
    */
   public void shutdown() {
diff --git a/ep/transform/EventConverter.java b/ep/transform/EventConverter.java
index ae63ae0..fc25743 100644
--- a/ep/transform/EventConverter.java
+++ b/ep/transform/EventConverter.java
@@ -8,6 +8,17 @@ import psl.xues.ep.util.EPConst;
  * Transform that forces an event conversion.  By default, Event Packager does
  * lazy conversion; this forces it explicitly.
  * <p>
+ * Required attributes:<ol>
+ * <li><b>OutputFormat</b>: specify the output format to request event conversion to</li>
+ * </ol>
+ * <p>
+ * Output formats currently supported (you can use either designator):<ul>
+ * <li>DOMEvent (DOM_OBJECT)</li>
+ * <li>SienaEvent (SIENA_OBJECT)</li>
+ * <li>XMLEvent (XML_OBJECT)</li>
+ * <li>StringEvent (STRING_OBJECT)</li>
+ * </ul>
+ * <p>
  * Copyright (c) 2002: The Trustees of Columbia University in the
  * City of New York.  All Rights Reserved.
  *
@@ -78,6 +89,8 @@ public class EventConverter extends EPTransform implements EPConst {

   /**
    * Get the type.
+   *
+   * @return The type of EPTransform that this module is.
    */
   public String getType() {
     return "EventConverter";