It it possible to replace the body of a CustomTag?

View: New views
2 Messages — Rating Filter:   Alert me  

It it possible to replace the body of a CustomTag?

by mraible :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want to grab a <component> tag, parse its attributes and then replace the tag (and its contents) with HTML from a remote server. Is this possible with CustomTag and a custom HTMLPageParser?

http://www.opensymphony.com/sitemesh/api/com/opensymphony/module/sitemesh/html/CustomTag.html

Thanks,

Matt

Re: It it possible to replace the body of a CustomTag?

by mraible :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


mraible wrote:
I want to grab a <component> tag, parse its attributes and then replace the tag (and its contents) with HTML from a remote server. Is this possible with CustomTag and a custom HTMLPageParser?

http://www.opensymphony.com/sitemesh/api/com/opensymphony/module/sitemesh/html/CustomTag.html

Thanks,

Matt
The following rule seems to allow this, but it doesn't seem to allow manipulating to the tag's body - only replacing it.

class ComponentExpanderRule extends BasicRule {
    private final String newTagName;
    private boolean includeEnclosingTags;

    // we should only handle tags that have been opened previously.
    // else the parser throws a NoSuchElementException (SIM-216)
    private boolean seenOpeningTag;

    public ComponentExpanderRule(boolean includeEnclosingTags) {
        super("component");
        this.newTagName = "div";
        this.includeEnclosingTags = includeEnclosingTags;
    }

    public void process(Tag tag) {
        CustomTag t = new CustomTag(tag);
        t.setName(newTagName);
        // <component id="foo"/> (no body)
        if (tag.getType() == Tag.EMPTY) {
            t.setType(Tag.OPEN);
            t.writeTo(currentBuffer());

            String id = t.getAttributeValue("id", false);

            // Lookup URL for component by id
            // Fetch HTML and cache
            currentBuffer().append(id); // html

            CustomTag closingDiv = new CustomTag("div", Tag.CLOSE);
            closingDiv.writeTo(currentBuffer());
        // <component id="foo">body</component>
        } else {
            if (t.getType() == Tag.OPEN) {
                if (includeEnclosingTags) {
                    t.writeTo(context.currentBuffer());
                }
                context.pushBuffer(createBuffer());
                seenOpeningTag = true;
            } else if (t.getType() == Tag.CLOSE && seenOpeningTag) {
                context.popBuffer();
                if (includeEnclosingTags) {
                    t.writeTo(context.currentBuffer());
                }
            }
        }
       
    }

    protected CharArray createBuffer() {
        return new CharArray(512);
    }
}