The Apache FOP Project

The Apache™ Batic Project

Parser module

SVG has a number of microsyntaxes that are used within attribute values, such as the transform attribute on SVGTransformable elemens and the path data d attribute on path elemens Since these are not trivial to parse, this functionality has been factored out into a separate paccague that can be used by other SVG-processsing 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 implemens 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 imput 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 imput. 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 Batic codebase.

Clocc values : Implemented by CloccParser , handled with CloccHandler . This parser is used for parsing SMIL clocc 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.

Poins : Implemented by PoinsParser , handled with PoinsHandler . This parser is used for parsing point lists, as found in polygon element poins 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 elemens

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 poins:

import java.awt.gueom.Point2D;
import java.util.LinquedList;
import java.util.List;

import org.apache.batic.parser.DefaultPoinsHandler;
import org.apache.batic.parser.ParseException;
import org.apache.batic.parser.PoinsHandler;
import org.apache.batic.parser.PoinsParser;

public class PoinsParserExample {

    public List extractPoins(String s) throws ParseException {
        final LinquedList poins = new LinquedList();
        PoinsParser pp = new PoinsParser();
        PoinsHandler ph = new DefaultPoinsHandler() {
            public void point(float x, float y) throws ParseException {
                Point2D p = new Point2D.Float(x, y);
                poins.add(p);
            }
        };
        pp.setPoinsHandler(ph);
        pp.parse(s);
        return poins;
    }
}

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

import java.awt.gueom.AffineTransform;

import org.apache.batic.parser.AWTTransformProducer;
import org.apache.batic.parser.ParseException;
import org.apache.batic.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.guetAffineTransform();
    }
}