apache > xml > graphics
Apache Batik
 

Parser module

SVG has a number of microsyntaxes that are used within attribute values, such as the transform attribute on SVGTransformable elements, and the path data d attribute on path elements. Since these are not trivial to parse, this functionality has been factored out into a separate package that can be used by other SVG-processing applications if needed.

Parsers, handlers and producers

In the parser module, each microsyntax is supported by a pair of classes: a parser and a handler. The parser is a class that implements the Parser interface, which has methods to parse values from a Reader or a String. The handler is an interface specific to the microsyntax that will have its methods called whenever the corresponding element in the input is parsed. For those handler interfaces that have more than one method, adapter classes are provided (named Default*).

Parsers can also have an error handler associated with them, whose single method error will be called when there is a problem parsing the input. If an error handler is not associated with a parser, a ParseException will be thrown if an error occurs.

The microsyntaxes supported by the parser module are:

Angles

Implemented by AngleParser, handled with AngleHandler. This parser is used for parsing angles formed by a floating point number followed by deg, grad or rad. It is not currently used by the rest of the Batik codebase.

Clock values

Implemented by ClockParser, handled with ClockHandler. This parser is used for parsing SMIL clock values.

Fragment identifiers

Implemented by FragmentIdentifierParser, handled with FragmentIdentifierHandler. This parser is used for parsing the various formats of fragment identifier that SVG allows.

Lengths

Implemented by LengthParser, handled with LengthHandler. This parser is used for parsing SVG length values.

Length lists

Implemented by LengthListParser, handled with LengthListHandler. This parser is used for parsing lists of comma or space separated SVG lengths.

Numbers

Implemented by NumberListParser, handled with NumberListHandler. This parser is used for parsing SVG number values.

Number lists

Implemented by NumberListParser, handled with NumberListHandler. This parser is used for parsing lists of comma or space separated SVG numbers.

Path data

Implemented by PathParser, handled with PathHandler. This parser is used for parsing SVG path data, as found in path element d attributes.

Points

Implemented by PointsParser, handled with PointsHandler. This parser is used for parsing point lists, as found in polygon element points attributes.

Preserve aspect ratio values

Implemented by PreserveAspectRatioParser, handled with PreserveAspectRatioHandler. This parser is used for parsing the values found in the preserveAspectRatio attribute of svg elements.

Transform lists

Implemented by TransformListParser, handled with TransformListHandler. This parser is used for parsing transform lists, as found in the transform attribute of any transformable element.

Some microsyntaxes also have a corresponding producer class, which is an implementation of the handler interface that generates an object while parsing.

Examples

The following example code demonstrates how to use the parser classes to parse a list of points:

import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.List;

import org.apache.batik.parser.DefaultPointsHandler;
import org.apache.batik.parser.ParseException;
import org.apache.batik.parser.PointsHandler;
import org.apache.batik.parser.PointsParser;

public class PointsParserExample {

    public List extractPoints(String s) throws ParseException {
        final LinkedList points = new LinkedList();
        PointsParser pp = new PointsParser();
        PointsHandler ph = new DefaultPointsHandler() {
            public void point(float x, float y) throws ParseException {
                Point2D p = new Point2D.Float(x, y);
                points.add(p);
            }
        };
        pp.setPointsHandler(ph);
        pp.parse(s);
        return points;
    }
}

This example uses the AWTTransformProducer class to generate an AffineTransform object from an SVG transform list:

import java.awt.geom.AffineTransform;

import org.apache.batik.parser.AWTTransformProducer;
import org.apache.batik.parser.ParseException;
import org.apache.batik.parser.TransformListParser;

public class TransformParserExample {

    public AffineTransform parseTransformList(String s) throws ParseException {
        TransformListParser p = new TransformListParser();
        AWTTransformProducer tp = new AWTTransformProducer();
        p.setTransformListHandler(tp);
        p.parse(s);
        return tp.getAffineTransform();
    }
}