Debian Codegraph @

documentation-sources/content/xdocs/using/parsers.xml

raw
1 <?xml version="1.0"?>
2
3 <!--
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 -->
21
22 <!-- ========================================================================= -->
23 <!-- author [email protected] -->
24 <!-- version $Id$ -->
25 <!-- ========================================================================= -->
26
27 <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
28 <document>
29 <header>
30 <title>Parser module</title>
31 </header>
32
33 <body>
34 <p>
35 SVG has a number of microsyntaxes that are used within attribute values,
36 such as the <code>transform</code> attribute on
37 <code>SVGTransformable</code> elements, and the path data <code>d</code>
38 attribute on <code>path</code> elements. Since these are not trivial to
39 parse, this functionality has been factored out into a separate package
40 that can be used by other SVG-processing applications if needed.
41 </p>
42
43 <section id="parsersHandlersAndProducers">
44 <title>Parsers, handlers and producers</title>
45
46 <p>
47 In the parser module, each microsyntax is supported by a pair of classes:
48 a parser and a handler. The parser is a class that implements the
49 <a class="class" href="../javadoc/org/apache/batik/parser/Parser.html">Parser</a>
50 interface, which has methods to parse values from a
51 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html">Reader</a>
52 or a
53 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html">String</a>.
54 The handler is an interface specific to the microsyntax that will have
55 its methods called whenever the corresponding element in the input is
56 parsed. For those handler interfaces that have more than one method,
57 adapter classes are provided (named <code>Default</code>*).
58 </p>
59 <p>
60 Parsers can also have an error handler associated with them, whose
61 single method <code>error</code> will be called when there is a
62 problem parsing the input. If an error handler is not associated with
63 a parser, a
64 <a class="class" href="../javadoc/org/apache/batik/parser/ParseException.html">ParseException</a>
65 will be thrown if an error occurs.
66 </p>
67 <p>
68 The microsyntaxes supported by the parser module are:
69 </p>
70 <dl>
71 <dt>Angles</dt>
72 <dd>
73 <p>
74 Implemented by
75 <a class="class" href="../javadoc/org/apache/batik/parser/AngleParser.html">AngleParser</a>,
76 handled with
77 <a class="class" href="../javadoc/org/apache/batik/parser/AngleHandler.html">AngleHandler</a>.
78 This parser is used for parsing angles formed by a floating point
79 number followed by <code>deg</code>, <code>grad</code> or
80 <code>rad</code>. It is not currently used by the rest of the Batik
81 codebase.
82 </p>
83 </dd>
84 <dt>Clock values</dt>
85 <dd>
86 <p>
87 Implemented by
88 <a class="class" href="../javadoc/org/apache/batik/parser/ClockParser.html">ClockParser</a>,
89 handled with
90 <a class="class" href="../javadoc/org/apache/batik/parser/ClockHandler.html">ClockHandler</a>.
91 This parser is used for parsing SMIL
92 <a href="http://www.w3.org/TR/smil-animation/#Timing-ClockValueSyntax">clock values</a>.
93 </p>
94 </dd>
95 <dt>Fragment identifiers</dt>
96 <dd>
97 <p>
98 Implemented by
99 <a class="class" href="../javadoc/org/apache/batik/parser/FragmentIdentifierParser.html">FragmentIdentifierParser</a>,
100 handled with
101 <a class="class" href="../javadoc/org/apache/batik/parser/FragmentIdentifierHandler.html">FragmentIdentifierHandler</a>.
102 This parser is used for parsing the various formats of
103 <a href="http://www.w3.org/TR/SVG11/linking.html#SVGFragmentIdentifiers">fragment
104 identifier</a> that SVG allows.
105 </p>
106 </dd>
107 <dt>Lengths</dt>
108 <dd>
109 <p>
110 Implemented by
111 <a class="class" href="../javadoc/org/apache/batik/parser/LengthParser.html">LengthParser</a>,
112 handled with
113 <a class="class" href="../javadoc/org/apache/batik/parser/LengthHandler.html">LengthHandler</a>.
114 This parser is used for parsing SVG length values.
115 </p>
116 </dd>
117 <dt>Length lists</dt>
118 <dd>
119 <p>
120 Implemented by
121 <a class="class" href="../javadoc/org/apache/batik/parser/LengthListParser.html">LengthListParser</a>,
122 handled with
123 <a class="class" href="../javadoc/org/apache/batik/parser/LengthListHandler.html">LengthListHandler</a>.
124 This parser is used for parsing lists of comma or space separated
125 SVG lengths.
126 </p>
127 </dd>
128 <dt>Numbers</dt>
129 <dd>
130 <p>
131 Implemented by
132 <a class="class" href="../javadoc/org/apache/batik/parser/NumberListParser.html">NumberListParser</a>,
133 handled with
134 <a class="class" href="../javadoc/org/apache/batik/parser/NumberListHandler.html">NumberListHandler</a>.
135 This parser is used for parsing SVG number values.
136 </p>
137 </dd>
138 <dt>Number lists</dt>
139 <dd>
140 <p>
141 Implemented by
142 <a class="class" href="../javadoc/org/apache/batik/parser/NumberListParser.html">NumberListParser</a>,
143 handled with
144 <a class="class" href="../javadoc/org/apache/batik/parser/NumberListHandler.html">NumberListHandler</a>.
145 This parser is used for parsing lists of comma or space separated
146 SVG numbers.
147 </p>
148 </dd>
149 <dt>Path data</dt>
150 <dd>
151 <p>
152 Implemented by
153 <a class="class" href="../javadoc/org/apache/batik/parser/PathParser.html">PathParser</a>,
154 handled with
155 <a class="class" href="../javadoc/org/apache/batik/parser/PathHandler.html">PathHandler</a>.
156 This parser is used for parsing SVG path data, as found in
157 <code>path</code> element <code>d</code> attributes.
158 </p>
159 </dd>
160 <dt>Points</dt>
161 <dd>
162 <p>
163 Implemented by
164 <a class="class" href="../javadoc/org/apache/batik/parser/PointsParser.html">PointsParser</a>,
165 handled with
166 <a class="class" href="../javadoc/org/apache/batik/parser/PointsHandler.html">PointsHandler</a>.
167 This parser is used for parsing point lists, as found in
168 <code>polygon</code> element <code>points</code> attributes.
169 </p>
170 </dd>
171 <dt>Preserve aspect ratio values</dt>
172 <dd>
173 <p>
174 Implemented by
175 <a class="class" href="../javadoc/org/apache/batik/parser/PreserveAspectRatioParser.html">PreserveAspectRatioParser</a>,
176 handled with
177 <a class="class" href="../javadoc/org/apache/batik/parser/PreserveAspectRatioHandler.html">PreserveAspectRatioHandler</a>.
178 This parser is used for parsing the values found in the
179 <code>preserveAspectRatio</code> attribute of <code>svg</code>
180 elements.
181 </p>
182 </dd>
183 <dt>Transform lists</dt>
184 <dd>
185 <p>
186 Implemented by
187 <a class="class" href="../javadoc/org/apache/batik/parser/TransformListParser.html">TransformListParser</a>,
188 handled with
189 <a class="class" href="../javadoc/org/apache/batik/parser/TransformListHandler.html">TransformListHandler</a>.
190 This parser is used for parsing transform lists, as found in the
191 <code>transform</code> attribute of any transformable element.
192 </p>
193 </dd>
194 </dl>
195 <p>
196 Some microsyntaxes also have a corresponding producer class, which is
197 an implementation of the handler interface that generates an object
198 while parsing.
199 </p>
200 </section>
201
202 <section id="examples">
203 <title>Examples</title>
204
205 <p>
206 The following example code demonstrates how to use the parser classes
207 to parse a list of points:
208 </p>
209
210 <source>import java.awt.geom.Point2D;
211 import java.util.LinkedList;
212 import java.util.List;
213
214 import org.apache.batik.parser.DefaultPointsHandler;
215 import org.apache.batik.parser.ParseException;
216 import org.apache.batik.parser.PointsHandler;
217 import org.apache.batik.parser.PointsParser;
218
219 public class PointsParserExample {
220
221 public List extractPoints(String s) throws ParseException {
222 final LinkedList points = new LinkedList();
223 PointsParser pp = new PointsParser();
224 PointsHandler ph = new DefaultPointsHandler() {
225 public void point(float x, float y) throws ParseException {
226 Point2D p = new Point2D.Float(x, y);
227 points.add(p);
228 }
229 };
230 pp.setPointsHandler(ph);
231 pp.parse(s);
232 return points;
233 }
234 }</source>
235
236 <p>
237 This example uses the
238 <a class="class" href="../javadoc/org/apache/batik/parser/AWTTransformProducer.html">AWTTransformProducer</a>
239 class to generate an
240 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.html">AffineTransform</a>
241 object from an SVG transform list:
242 </p>
243
244 <source>import java.awt.geom.AffineTransform;
245
246 import org.apache.batik.parser.AWTTransformProducer;
247 import org.apache.batik.parser.ParseException;
248 import org.apache.batik.parser.TransformListParser;
249
250 public class TransformParserExample {
251
252 public AffineTransform parseTransformList(String s) throws ParseException {
253 TransformListParser p = new TransformListParser();
254 AWTTransformProducer tp = new AWTTransformProducer();
255 p.setTransformListHandler(tp);
256 p.parse(s);
257 return tp.getAffineTransform();
258 }
259 }
260 </source>
261 </section>
262 </body>
263 </document>