You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Adding a markup filter

In some very rare cases it might be necessary to add an additional markup filter to the default list. Lets say you want to log a warning or throw an exception in case of an empty src attribute like <img src="">. The Image component is not able to handle it because the wicket id is missing. A markup filter in contrast iterates over all xml tags and is able to detect the problem and to throw an exception.

Note: a markup filter is not able to detect an AttributeModifier changing the src attribute to an empty string.

Wicket 1.4:

 
public class MyApplication extends Application
{
    protected void init()
    {
...
        getMarkupSettings().setMarkupParserFactory(
                new MyMarkupParserFactory());
...
    }
}

public class MyMarkupParserFactory implements IMarkupParserFactory {

    @Override
    public MarkupParser newMarkupParser(MarkupResourceStream resource) {
        MarkupParser parser = new MarkupParser(new XmlPullParser(), resource);
        parser.appendMarkupFilter(new MyMarkupFilter());
        return parser;
    }
}

It's maybe a good idea to allow your MarkupParserFactory accept a list of filters which might get appended on creation of the parser in 'newMarkupParser'.

Wicket 1.2:

public class MyApplication extends Application
{
    protected void init()
    {
        IMarkupFilter filter = new HtmlProblemFinder(HtmlProblemFinder.ERR_THROW_EXCEPTION);
        getMarkupSettings().setMarkupParserFactory(new MarkupParserFactory(this, filter));
    }
}

Wicket 1.1

public class MyApplication extends Application
{
    public MarkupParser newMarkupParser()
    {
        final MarkupParser parser = new MarkupParser(container, 
            new XmlPullParser(settings.getDefaultMarkupEncoding())
            {
                public void initFilterChain()
                {
                    appendMarkupFilter(new HtmlProblemFinder(HtmlProblemFinder.ERR_THROW_EXCEPTION));
                }
            }
        );
        parser.configure(getSettings());
        return parser;
    }
}
  • No labels