KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > parser > PathParserTest


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.parser;
19
20 import java.io.*;
21
22 import org.apache.batik.test.*;
23
24 /**
25  * To test the path parser.
26  *
27  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: PathParserTest.java,v 1.3 2004/08/18 07:16:42 vhardy Exp $
29  */

30 public class PathParserTest extends AbstractTest {
31
32     protected String JavaDoc sourcePath;
33     protected String JavaDoc destinationPath;
34
35     protected StringBuffer JavaDoc buffer;
36     protected String JavaDoc resultPath;
37
38     /**
39      * Creates a new PathParserTest.
40      * @param spath The path to parse.
41      * @param dpath The path after serialization.
42      */

43     public PathParserTest(String JavaDoc spath, String JavaDoc dpath) {
44         sourcePath = spath;
45         destinationPath = dpath;
46     }
47
48     public TestReport runImpl() throws Exception JavaDoc {
49         PathParser pp = new PathParser();
50         pp.setPathHandler(new TestHandler());
51
52         try {
53             pp.parse(new StringReader(sourcePath));
54         } catch (ParseException e) {
55             DefaultTestReport report = new DefaultTestReport(this);
56             report.setErrorCode("parse.error");
57             report.addDescriptionEntry("exception.text", e.getMessage());
58             report.setPassed(false);
59             return report;
60         }
61
62         if (!destinationPath.equals(resultPath)) {
63             DefaultTestReport report = new DefaultTestReport(this);
64             report.setErrorCode("invalid.parsing.events");
65             report.addDescriptionEntry("expected.text", destinationPath);
66             report.addDescriptionEntry("generated.text", resultPath);
67             report.setPassed(false);
68             return report;
69         }
70
71         return reportSuccess();
72     }
73
74     class TestHandler extends DefaultPathHandler {
75         public TestHandler() {}
76
77         public void startPath() throws ParseException {
78             buffer = new StringBuffer JavaDoc();
79         }
80         
81         public void movetoRel(float x, float y) throws ParseException {
82             buffer.append('m');
83             buffer.append(x);
84             buffer.append(' ');
85             buffer.append(y);
86         }
87
88         public void movetoAbs(float x, float y) throws ParseException {
89             buffer.append('M');
90             buffer.append(x);
91             buffer.append(' ');
92             buffer.append(y);
93         }
94
95         public void endPath() throws ParseException {
96             resultPath = buffer.toString();
97         }
98
99         public void closePath() throws ParseException {
100             buffer.append('Z');
101         }
102
103         public void linetoRel(float x, float y) throws ParseException {
104             buffer.append('l');
105             buffer.append(x);
106             buffer.append(' ');
107             buffer.append(y);
108         }
109
110         public void linetoAbs(float x, float y) throws ParseException {
111             buffer.append('L');
112             buffer.append(x);
113             buffer.append(' ');
114             buffer.append(y);
115         }
116
117         public void linetoHorizontalRel(float x) throws ParseException {
118             buffer.append('h');
119             buffer.append(x);
120         }
121
122         public void linetoHorizontalAbs(float x) throws ParseException {
123             buffer.append('H');
124             buffer.append(x);
125         }
126
127         public void linetoVerticalRel(float y) throws ParseException {
128             buffer.append('v');
129             buffer.append(y);
130         }
131
132         public void linetoVerticalAbs(float y) throws ParseException {
133             buffer.append('V');
134             buffer.append(y);
135         }
136
137         public void curvetoCubicRel(float x1, float y1,
138                                     float x2, float y2,
139                                     float x, float y) throws ParseException {
140             buffer.append('c');
141             buffer.append(x1);
142             buffer.append(' ');
143             buffer.append(y1);
144             buffer.append(' ');
145             buffer.append(x2);
146             buffer.append(' ');
147             buffer.append(y2);
148             buffer.append(' ');
149             buffer.append(x);
150             buffer.append(' ');
151             buffer.append(y);
152         }
153
154         public void curvetoCubicAbs(float x1, float y1,
155                                     float x2, float y2,
156                                     float x, float y) throws ParseException {
157             buffer.append('C');
158             buffer.append(x1);
159             buffer.append(' ');
160             buffer.append(y1);
161             buffer.append(' ');
162             buffer.append(x2);
163             buffer.append(' ');
164             buffer.append(y2);
165             buffer.append(' ');
166             buffer.append(x);
167             buffer.append(' ');
168             buffer.append(y);
169         }
170
171         public void curvetoCubicSmoothRel(float x2, float y2,
172                                           float x, float y) throws ParseException {
173             buffer.append('s');
174             buffer.append(x2);
175             buffer.append(' ');
176             buffer.append(y2);
177             buffer.append(' ');
178             buffer.append(x);
179             buffer.append(' ');
180             buffer.append(y);
181         }
182
183         public void curvetoCubicSmoothAbs(float x2, float y2,
184                                           float x, float y) throws ParseException {
185             buffer.append('S');
186             buffer.append(x2);
187             buffer.append(' ');
188             buffer.append(y2);
189             buffer.append(' ');
190             buffer.append(x);
191             buffer.append(' ');
192             buffer.append(y);
193         }
194
195         public void curvetoQuadraticRel(float x1, float y1,
196                                         float x, float y) throws ParseException {
197             buffer.append('q');
198             buffer.append(x1);
199             buffer.append(' ');
200             buffer.append(y1);
201             buffer.append(' ');
202             buffer.append(x);
203             buffer.append(' ');
204             buffer.append(y);
205         }
206
207         public void curvetoQuadraticAbs(float x1, float y1,
208                                         float x, float y) throws ParseException {
209             buffer.append('Q');
210             buffer.append(x1);
211             buffer.append(' ');
212             buffer.append(y1);
213             buffer.append(' ');
214             buffer.append(x);
215             buffer.append(' ');
216             buffer.append(y);
217         }
218
219         public void curvetoQuadraticSmoothRel(float x, float y)
220             throws ParseException {
221             buffer.append('t');
222             buffer.append(x);
223             buffer.append(' ');
224             buffer.append(y);
225         }
226
227         public void curvetoQuadraticSmoothAbs(float x, float y)
228             throws ParseException {
229             buffer.append('T');
230             buffer.append(x);
231             buffer.append(' ');
232             buffer.append(y);
233         }
234
235         public void arcRel(float rx, float ry,
236                            float xAxisRotation,
237                            boolean largeArcFlag, boolean sweepFlag,
238                            float x, float y) throws ParseException {
239             buffer.append('a');
240             buffer.append(rx);
241             buffer.append(' ');
242             buffer.append(ry);
243             buffer.append(' ');
244             buffer.append(xAxisRotation);
245             buffer.append(' ');
246             buffer.append(largeArcFlag ? '1' : '0');
247             buffer.append(' ');
248             buffer.append(sweepFlag ? '1' : '0');
249             buffer.append(' ');
250             buffer.append(x);
251             buffer.append(' ');
252             buffer.append(y);
253         }
254
255         public void arcAbs(float rx, float ry,
256                            float xAxisRotation,
257                            boolean largeArcFlag, boolean sweepFlag,
258                            float x, float y) throws ParseException {
259             buffer.append('A');
260             buffer.append(rx);
261             buffer.append(' ');
262             buffer.append(ry);
263             buffer.append(' ');
264             buffer.append(xAxisRotation);
265             buffer.append(' ');
266             buffer.append(largeArcFlag ? '1' : '0');
267             buffer.append(' ');
268             buffer.append(sweepFlag ? '1' : '0');
269             buffer.append(' ');
270             buffer.append(x);
271             buffer.append(' ');
272             buffer.append(y);
273         }
274     }
275 }
276
Popular Tags