Custom Tag Support - shannah/cn1-xmlview GitHub Wiki

Notice that this component comes default with support for only a minimal set of XML tags. This is intentional, to keep it light-weight. Adding support for additional tags is quite simple though. All you need to do is implement the ViewFactory interface, and then register your view factory with the XMLView before loading the page.

Example: Adding Basic Support for <button> Tag

Suppose you wanted to add support for a "button" in your XML pages. You might implement your view factory as follows:

public class ButtonView implements XMLView.ViewFactory {
    @Override
    public Component createView(Element el, XMLView context) {
        String text = getText(el);
        Button btn = new Button(text);
        String uiid = el.getAttribute("uiid");
        if (uiid != null) {
            button.setUIID(uiid);
        }

        return btn;
    }
    private void getText(Element root, StringBuilder sb) {
        if (root.isTextElement()) {
            sb.append(root.getText());
        } else {
            Iterator<Element> it = root.iterator();
            while (it.hasNext()) {
                getText(it.next(), sb);
            }
        }
    }

    private String getText(Element root) {
        StringBuilder sb = new StringBuilder();
        getText(root, sb);
        return sb.toString();
    }

}

We would register this factory with the XMLView as follows

myXmlView.registerViewFactory("button", new ButtonView());

Now, whenever the view encouters a <button> tag, it will render it as a button.

Note
This example skips the step of what to do with action events. I’ll leave that as an exercise for the reader.

Learn More

Check out the source for the existing tags and their view factories to get other ideas on how to implement your own tag support.

⚠️ **GitHub.com Fallback** ⚠️