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);
}
}