> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
>
>
> Well, I am going to do the 1.0.1-rc1 release this morning (ran out of
> gas last night...).  I am hoping for some more feedback from Jose.  If
> there is no serious opposition, we then tag the source tree and start
> arguing about chaining, I guess.
>
> I will fix and put a jar in whiteboard so you have a working
> jar to work
> with.
>

Well finally I have a chance to get back to this. I had to build one of
those barebone systems for a friend, first time for me. So it took quite a
few mistakes until I got a working system. I am such a software...

Anyway, here are some comments:

I like very much the ;concept of an EventCartridge, it gives a unified API
for once. Actually I would like it to be more than just a container for
handlers. I would it to be the one doing the work. That is move all the
listener processing code to the EventCartridge.

The advantage of doing this is that it allows providing diferent
implementations, subclasses of EventCartridge that can be taylored to the
particular applicartion, in particular in things related to chaining. I will
try to attach some code of what I mean, but in principle what I am proposing
is to define EventCartridge as follows:

public class EventCartridge
  implements NullReferenceEventHandler,
        NullSetEventHandler, ReferenceInsertionEventHandler {

        public boolean addEventHandler(EventHandler ev){...}

        /* Just for completion */
        public boolean removeEventHandler(EventHandler ev) {...}

        public final boolean attachToContext(Context context) {...}

        /* the methods of the handlers */
}

So, in the ASTNodes code the only thing we do is, for example:

   if (ec != null) localNullString = ec.nullReferenceRender(nullString);

similar on the other calls. All the combining is delegated to inside the
EventCartridge which can do whatever it needs to do. We can provide several
implementation with different performance characteristics, if we want. Or
just one that does everything for everybody. It becomes an orthogonal issue.

Now going to the chaining issue, if we follow the idea above, we can have a
quick and simple EventCartridge that only stores one reference on each kind.
For people that do not require more than that. And we can provide an
EventCartridgeChainer close to what Geir has defined.

The attached patch just modifies the files on gier's whiteboard. I have not
compile them. But they should give you an idea of what I have in mind.

Jose Alberto
? jose_patch.txt
Index: ASTReference.java
===================================================================
RCS file: /home/cvspublic/jakarta-velocity/whiteboard/geir/listener/ASTReference.java,v
retrieving revision 1.2
diff -u -r1.2 ASTReference.java
--- ASTReference.java   2001/04/09 01:03:26     1.2
+++ ASTReference.java   2001/04/11 00:23:28
@@ -238,12 +238,7 @@
             
             if (ec != null)
             {
-                NullReferenceEventHandler[] rieh = ec.getNullReferenceHandlerArray();
-                
-                for(int i = 0; i < rieh.length; i++)
-                {
-                    localNullstring = rieh[i].nullReferenceRender( localNullstring );
-                }
+              localNullstring = ec.nullReferenceRender(nullString);
             }
            
             /* 
@@ -262,12 +257,7 @@
             
             if (ec != null)
             {
-                ReferenceInsertionEventHandler[] rieh = 
ec.getReferenceInsertionHandlerArray();
-             
-                for(int i = 0; i < rieh.length; i++)
-                {
-                    val =  rieh[i].referenceInsert( nullString, val );
-                }
+                val = ec.referenceInsert(nullString, value);
             }
             
             String s = NodeUtils.specialText(getFirstToken()) + prefix + 
val.toString();
Index: ASTSetDirective.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/whiteboard/geir/listener/ASTSetDirective.java,v
retrieving revision 1.2
diff -u -r1.2 ASTSetDirective.java
--- ASTSetDirective.java        2001/04/09 01:03:27     1.2
+++ ASTSetDirective.java        2001/04/11 00:23:29
@@ -148,15 +148,7 @@
 
                 if (ec != null)
                 {
-                    NullSetEventHandler[] nseh = ec.getNullSetEventHandlerArray();
-                    
-                    for(int i = 0; i < nseh.length; i++)
-                    {
-                        if( ! nseh[i].nullSetLogMessage( left.literal() ))
-                        {
-                            doit = false;
-                        }
-                    }
+                    doit = ec.nullSetLogMessage(left.literal());
                 }
 
                 if (doit)
Index: EventCartridge.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/whiteboard/geir/listener/EventCartridge.java,v
retrieving revision 1.2
diff -u -r1.2 EventCartridge.java
--- EventCartridge.java 2001/04/09 01:03:27     1.2
+++ EventCartridge.java 2001/04/11 00:23:29
@@ -66,7 +66,98 @@
  * @version $Id: EventCartridge.java,v 1.2 2001/04/09 01:03:27 geirm Exp $
  */
 public class EventCartridge
+  implements ReferenceInsertionEventHandler,
+             NullSetEventHandler,
+             NullReferenceEventHandler
 {
+  private ReferenceInsertionEventHandler rieh = null;
+  private NullSetEventHandler nseh = null;
+  private NullReferenceEventHandler nreh = null;
+
+  public boolean attachToContext( Context context )
+  {
+    
+    if (  context instanceof InternalHousekeepingContext )
+    {         
+      InternalHousekeepingContext icb = (InternalHousekeepingContext) context;
+      
+      icb.attachEventCartridge( this );
+      
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+  public boolean addEventHandler(EventHandler ev)
+    throws Exception
+  {
+    if (ev == null) return false;
+
+    boolean added = false;
+
+    if (ev instanceof ReferenceInsertionEventHandler) {
+      rieh = (ReferenceInsertionEventHandler)ev;
+      added = true;
+    }
+    if (ev instanceof NullSetEventHandler) {
+      nseh = (NullSetEventHandler)ev;
+      added = true;
+    }
+    if (ev instanceof NullReferenceEventHandler) {
+      nreh = (NullReferenceEventHandler)ev;
+      added = true;
+    }
+    
+    // Not throwing exception, not clear what would be the value
+    // of returning boolean, if we throw an exception.
+    return added;
+  }
+
+  public boolean removeEventHandler(EventHandler ev)
+    throws Exception
+  {
+    boolean removed = false;
+
+    if (ev == rieh) {
+      rieh = null;
+      removed = true;
+    }
+    if (ev == nseh) {
+      nseh = null;
+      removed = true;
+    }
+    if (ev == nreh) {
+      nreh = null;
+      removed = true;
+    }
+    return removed;
+  }
+
+  public Object referenceInsert(String reference, Object value)
+  {
+    if (rieh == null) return value;
+    return rieh.referenceInsert(reference, value);
+  }
+
+  public boolean nullSetLogMessage(String reference)
+  {
+    if (nseh == null) return true;
+    return nseh.nullSetLogMessage(reference);
+  }
+
+  public String nullReferenceRender(String reference)
+  {
+    if (nreh == null) return reference;
+    return nreh.nullReferenceRender(reference);
+  }
+
+  // Am too lazy to create a new file, 
+  // just using an inner class for the chaining version
+  public static class Chainer extends EventCartridge 
+  {
     private ArrayList riehList = null;
     private ReferenceInsertionEventHandler[] riehArr = null;
 
@@ -188,36 +279,44 @@
         return true;
     }
 
-    public ReferenceInsertionEventHandler[] getReferenceInsertionHandlerArray()
+    public Object referenceInsert(String reference, Object value)
     {
-        return riehArr;
-    }
+      if (riehArr == null) return value;
+      String val = value;
 
-    public NullReferenceEventHandler[] getNullReferenceHandlerArray()
-    {
-        return nrehArr;
+      for(int i = 0; i < riehArr.length; i++)
+      {
+        val =  riehArr[i].referenceInsert( reference, val );
+      }
+      return val;
     }
-
-
-    public NullSetEventHandler[] getNullSetEventHandlerArray()
-    {
-        return nsehArr;
-    }
-
-    public boolean attachToContext( Context context )
+    
+    public boolean nullSetLogMessage(String reference)
     {
-        
-        if (  context instanceof InternalHousekeepingContext )
-        {         
-            InternalHousekeepingContext icb = (InternalHousekeepingContext) context;
-
-            icb.attachEventCartridge( this );
-
-            return true;
-        }
-        else
+      if (nsehArr == null) return true;
+      boolean doit = true;
+                    
+      for(int i = 0; i < nsehArr.length; i++)
+      {
+        if( ! nsehArr[i].nullSetLogMessage( reference ))
         {
-            return false;
+          doit = false;
+          break;
         }
+      }
+      return doit;
+    }
+    
+    public String nullReferenceRender(String reference)
+    {
+      if (nrehArr == null) return reference;
+      String localNullstring = reference;
+      
+      for(int i = 0; i < riehArr.length; i++)
+      {
+        localNullstring = riehArr[i].nullReferenceRender( localNullstring );
+      }
+      return localNullstring;
     }
+  } /* Chainer */
 }

Reply via email to