apache > xml > graphics
Apache Batik
 

SVG 1.2 support

This page details information atbout Batik’s implementation of features from SVG 1.2, Tiny and Full. Note that the information on this page is based on the code in the Subversion repository trunk.

DOM Level 3

DOM Level 3 Core, Events and XPath are all supported. These features are available regardless of whether the document has version="1.1" or version="1.2" set on the root svg element.

There are a few issues with the DOM Level 3 Core implementation:

  1. The Document.compareDocumentPosition method does not give the correct result when used on DTD notation or entity nodes in the document.
  2. Node.renameNode always creates a new node and replaces the old node with it. This is technically allowed, but sub-optimal.
  3. XML Schema information is never used. Batik does not implement XML Schema, so any methods or attributes that would utilise or expose schema information (such as the schemaTypeInfo attribute on the Attr and Element interfaces) do not do so.
  4. Document.normalizeDocument ignores the "entities" parameter in the document’s DOMConfiguration.

Using DOM Level 3 functionality from Java

JREs before 1.5 include the DOM Level 2 interfaces and this can cause problems when trying to use the DOM Level 3 versions of these same interface files (org.w3c.dom.*). Though the concrete Batik DOM classes implement the DOM Level 3 functionality, you won’t be able to access those methods through the org.w3c.dom interfaces on these earlier JREs.

There are two ways to overcome this problem. The first is to install the DOM Level 3 interfaces using the Endorsed Standards Override Mechanism. Copy the file lib/xml-apis-ext.jar into the endorsed standards override directory and the DOM Level 3 interfaces will be visible. You can then write code against them (for example, call Document.renameNode directly). However, this will mean that other people cannot run or compile your code unless they have JRE 1.5 or later, or they have also installed the xml-apis-ext.jar in the same way.

The second method, which requires less messing about with the JRE, is to cast your DOM objects to the concrete Batik DOM objects and call your DOM Level 3 methods directly on them. The Batik DOM classes are in the org.apache.batik.dom package. The classes named Abstract* implement the DOM interfaces, and also contain the DOM Level 3 methods. The advantage of this method is that for your code to compile and run in others’ environments, they need not install any jars with the endorsed standards overrides.

Here is an example of using the second method to get access to DOM Level 3 specific methods:

import org.apache.batik.dom.AbstractDocument;
import org.apache.batik.dom.svg.SVGDOMImplementation;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class C {
    public void f() {
        // Create a new SVG document
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", null);

        // Create a 'g' element and append it to the root 'svg' element
        Element e = doc.createElementNS("http://www.w3.org/2000/svg", "g");
        doc.getDocumentElement().appendChild(e);

        // Cast the document object to org.apache.batik.dom.AbstractDocument,
        // so that DOM 3 methods will be guaranteed to be visible
        AbstractDocument document = (AbstractDocument) doc;

        // Now a DOM 3 method can be used
        document.renameNode(e, "http://www.w3.org/2000/svg", "text");
    }
}

For cases where the DOM Level 3 versions of these interfaces contain constants that you wish to use, the constants have been copied into the Batik DOM classes. For example:

import org.apache.batik.dom.AbstractNode;
import org.apache.batik.dom.svg.SVGDOMImplementation;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class C {
    public void f() {
        // Create a new SVG document
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", null);

        // Create a 'g' element and append it to the root 'svg' element
        Element svg = doc.getDocumentElement();
        Element e = doc.createElementNS("http://www.w3.org/2000/svg", "g");
        svg.appendChild(e);

        AbstractNode n1 = (AbstractNode) svg;
        AbstractNode n2 = (AbstractNode) e;
        int position = n1.compareDocumentPosition(n2);
        if (position == AbstractNode.DOCUMENT_POSITION_PRECEDING
                      | AbstractNode.DOCUMENT_POSITION_CONTAINS) {
            System.out.println("The svg element contains the g element.");
        } else {
            System.out.println("Something is wrong!");
        }
    }
}

Note that using these org.apache.batik.dom interfaces is only needed for the DOM Level 3 Core and Events interfaces. There were no earlier versions of the DOM XPath interfaces to conflict with, so these can be used directly (org.w3c.dom.xpath).

Of course, none of this matters if you are just using the DOM 3 functionality in ECMAScript, as the matter of interfaces is hidden from the scripting environment.

XML Binding Language for SVG (sXBL)

sXBL is supported in documents with version="1.2". However, the following issues exist:

  1. sXBL cannot be used for SVG resources or visual effects.
  2. The traitDef element is not implemented.
  3. The handling of CSS is probably not quite correct.

Two content selector languages are supported: XPath 1.0 Patterns and the drastically reduced XPath subset. XPath Patterns is the default language. To change the language used to the XPath subset, put an attribute batik:selectorLanguage="XPathSubset" on the xbl:content element or on the svg document element. (The batik extension namespace prefix should be declared with xmlns:batik="http://xml.apache.org/batik/ext".)

Note that sXBL is likely to be dropped in favor of XBL 2.0 in SVG 1.2 Full.

Flowing text and graphics

The flowRoot, flowRegion, flowDiv, flowPara, flowSpan, flowRegionBreak and flowLine elements from SVG 1.2 Full’s Flowing Text and Graphics chapter are supported in documents with version="1.2".

The more recent SVG 1.2 Tiny draft specifies a different syntax for (a more restricted version of) flowing text, and the full flowing text syntax is therefore likely to change.

Other SVG 1.2 features

The following other features from SVG 1.2 are supported: