Espinosa
Espinosa

Reputation: 2613

Using vanilla W3C Document and Apache Batik SVG rasterizer

As SVG is a regular XML file and ImageTranscoder.transcode() API accepts org.w3c.dom.Document, respective TranscoderInput constructor accepts org.w3c.dom.Document; one would expect that loading and parsing file with a Java stock XML parser would work:

TranscoderInput input = new TranscoderInput(loadSvgDocument(new FileInputStream(svgFile)));
BufferedImageTranscoder t = new BufferedImageTranscoder();
t.transcode(input, null);

Where loadSvgDocument() method is defined as:

Document loadSvgDocument(String svgFileName, InputStream is) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // using stock Java 8 XML parser
    Document document;
    try {
        DocumentBuilder db = dbf.newDocumentBuilder(); 
        document = db.parse(is);
    } catch (...) {...}
    return document;
}

It does not work. I am getting strange casting exceptions.

Exception in thread "main" java.lang.ClassCastException: org.apache.batik.dom.GenericElement cannot be cast to org.w3c.dom.svg.SVGSVGElement
at org.apache.batik.anim.dom.SVGOMDocument.getRootElement(SVGOMDocument.java:235)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:193)
at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:92)
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)

Note: class BufferedImageTranscoder is my class, created as per Batik blueprints, extending ImageTranscoder which in turn extends SVGAbstractTranscoder mentioned in the stack trace above.

Unfortunately I cannot use Batik own parser, SAXSVGDocumentFactory:

String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
svgDocument = (SVGDocument) f.createDocument(..);

I am trying to render Papirus SVG icons but they all have <svg ... version="1"> and SAXSVGDocumentFactory does not like that and fails on the createDocument(..) with Unsupport SVG version '1'. They probably meant unsupported.

Exception in thread "main" java.lang.RuntimeException: Unsupport SVG version '1'
at org.apache.batik.anim.dom.SAXSVGDocumentFactory.getDOMImplementation(SAXSVGDocumentFactory.java:327)
at org.apache.batik.dom.util.SAXDocumentFactory.startElement(SAXDocumentFactory.java:640)
. . .
at org.apache.batik.anim.dom.SAXSVGDocumentFactory.createDocument(SAXSVGDocumentFactory.java:225)

Changing version="1" to version="1.0" in the file itself fixes the problem and the icon is rendered nicely for me. But there are hundreds (thousands) of icons and fixing them all is tedious and I would effectively create a port of their project. This is not a way forward for me. Much easier is to make the fix in run time, using DOM API:

Element svgDocumentNode = svgDocument.getDocumentElement();
String svgVersion = svgDocumentNode.getAttribute("version");
if (svgVersion.equals("1")) {
    svgDocumentNode.setAttribute("version", "1.0");
}

But that can be done only with stock Java XML parser, Batik XML parser blows too early, before this code can be reached, before Document is generated. But when I use stock XML parser, make the version fix, then Batik Transcoder (rasterizer) does not like it. So I hit a wall here.

Is there a convertor from a stock XML parser produced org.w3c.dom.Document and Batik compatible org.w3c.dom.svg.SVGDocument?

Upvotes: 2

Views: 1751

Answers (1)

Espinosa
Espinosa

Reputation: 2613

OK, I found a solution bypassing the problem. Luckily class SAXSVGDocumentFactory can be easily subclasses and critical method getDOMImplementation() overriden.

protected Document loadSvgDocument(InputStream is) {
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory f = new LenientSaxSvgDocumentFactory(parser);
    SVGDocument svgDocument;
    try {
        svgDocument = (SVGDocument) f.createDocument("aaa", is);
    } catch (...) {
        ...
    }
    return svgDocument;
}

static class LenientSaxSvgDocumentFactory extends SAXSVGDocumentFactory {
    public LenientSaxSvgDocumentFactory(String parser) {
        super(parser);
    }

    @Override
    public DOMImplementation getDOMImplementation(String ver) {
        // code is mostly rip-off from original Apache Batik 1.9 code
        // only the condition was extended to accept also "1" string
        if (ver == null || ver.length() == 0
                || ver.equals("1.0") || ver.equals("1.1") || ver.equals("1")) {
            return SVGDOMImplementation.getDOMImplementation();
        } else if (ver.equals("1.2")) {
            return SVG12DOMImplementation.getDOMImplementation();
        }
        throw new RuntimeException("Unsupported SVG version '" + ver + "'");
    }
}

This time I got lucky, the main question remains however: is there a convertor from a stock XML parser produced org.w3c.dom.Document and Batik compatible org.w3c.dom.svg.SVGDocument?

Upvotes: 3

Related Questions