cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Timothy Larson <timlarsonw...@yahoo.com>
Subject Re: EffectTransformer/WoodyTemplateTransformer
Date Wed, 15 Oct 2003 03:35:24 GMT
--- Javier del Gesu <delgesu-cocoon@thinknola.com> wrote:
> Futher consideration (these are examples, not actual code):

>         } else if (localName.equals("command")) {
>             
>             // Create an anonymous inner class to intercept a value.
> 
>             ContentFilter anonymous = new AbstractContentFilter () {
>                 public void characters (FilterContext filterContext,
>                     SAXCharacterEvent event) throws SAXException {
>                     ExampleFilter.this.command =
>                         event.getCharactersAsString();
>                 }
>             }
>             filterContext.startContentFilter(anonymous);

This use of an anonymous class is almost the same as with the Effect
transformer.

>         } else if (localName.equals("item")) {
> 
>             // Use the same filter for a certian set of element
>             // names, preserves state.
> 
>             // Is this what you mean by continuations?
> 
>             filterContext.startContentFilter(collectionFilter);
> 
>         }

This is similar to the following code using the Effect transformer:

private class SomeClass extends Handler {
    private String name = null;
    public Handler process() throws SAXException {
        switch(event) {
        case EVENT_START_ELEMENT:
            if (name == null) {
                // This is the first time this handler was called.
                name = new String(loc);
            } else {
                // We are processing a child element this time.
            }
            return this;
        case EVENT_ELEMENT: // If child element...
            if ("item".equals(loc))
                // ...then process with this same object, preserving state.
                // Immediately after "return this", this same object will be called
                // again for this same element, but with event == EVENT_START_ELEMENT.
                return this;
        case EVENT_END_ELEMENT:
            // Do finishing work
            return this;
        default:
            return this;
        }
    }
}

By a continuation I mean something a bit different.  I can preserve both the processing
object's state, as in the example above, and also the position in the processing method,
like saving an instruction pointer.  When the handling method does a "return" it first
remembers where it left off, so the next time it is called it automatically picks
in the middle of the method right after the return statement that was last executed.
This allow for some easy sophisticated processing like the following:

private class SomeClass extends Handler {
    private int step = 0;
    private String name = null;
    public Handler process() throws SAXException {
        switch(step) {
        case 1:
            // This is the first time this handler was called.
            name = new String(loc);
            out.copy(); // Copy start element to output.
            step++; return this; // The next call to this method for any event...
        case 2: // ...will automatically pick up right here at step 2.
            if (event == EVENT_ELEMENT) { // Handle first child element.
                // The "step++;" will cause the the next call to this method
                // to automatically pick up at step 3 below.
                step++; return new Handler {
                    // Put code for anonymous handler class here.
                }
            }
            out.copy(); // If not a child element, then just copy to output.
            // No "step++;" is present here, so this step will be repeated
            // until a child element is finally processed.
            return this;
        case 3: // Step 3.
            if (event == EVENT_ELEMENT) { // Handle second child element differently.
                step++; return new Handler {
                    // Put different code for anonymous handler class here.
                }
            } else if (event == EVENT_CHARACTERS) {
                // Do special processing based on what the text is.
            }
            throw new SAXException("Oh, no. Unexpected event!");
        default: // Handle remaining events.
            switch(event) {
            case EVENT_END_ELEMENT:
                out.copy();
                return this;
            default:
                out.copy(); // Copy the rest of the input straight to the output.
                return this;
            }
        }
    }
}

> Did I cover all the bases?

Does this answer your questions?

--Tim Larson


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

Mime
View raw message