tags:

views:

104

answers:

1

I have an XSLT Transformer in Java (actually its Apache FOP rendering to PDF) where I have already set a custom URIResolver (for 'servlet-context:' URIs).

Now I need to use another URIResolver in addition (a CatalogResolver for caching DTDs).

Do I need to write my own URIResolver now that calls either of the two or is there a better way to do this?

A: 

A URIResolver is supposed to return null when it cannot resolve the reference. This allows the underlying resolver to try.

If you design your resolver so that it takes as a parameter another URIResolver on construction, then you can use resolver chaining.

TransformerFactory tf;
tf.setURIResolver(new SomeOtherResolver());
tf.setURIResolver(new MyResolver(tf.getURIResolver());

public class MyResolver implements URIResolver {
    public MyResolver(URIResolver nextInLine) {
        this.nextInLine = nextInLine;
    }
    private URIResolver nextInLine;
    public Source resolve(String href, String base) throws TransformerException {
        ...try to solve and return Source object...
        return nextInLine; // instead of returning null on failure, return nextInLine
    }
}

An alternative would be to write a resolver which allowed you to set a chain of resolvers as a list, and would keep calling them until a non-null answer was returned, and then return that.

lavinio