KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > AbstractSVGPointList


1 /*
2
3    Copyright 2003 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.dom.svg;
19
20 import org.apache.batik.parser.ParseException;
21 import org.apache.batik.parser.PointsHandler;
22 import org.apache.batik.parser.PointsParser;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.svg.SVGException;
25 import org.w3c.dom.svg.SVGMatrix;
26 import org.w3c.dom.svg.SVGPoint;
27 import org.w3c.dom.svg.SVGPointList;
28
29
30
31 /**
32  * This class is the implementation of
33  * <code>SVGPointList</code>.
34  *
35  * @author <a HREF="mailto:nicolas.socheleau@bitflash.com">Nicolas Socheleau</a>
36  * @version $Id: AbstractSVGPointList.java,v 1.4 2004/08/18 07:13:13 vhardy Exp $
37  */

38 public abstract class AbstractSVGPointList
39     extends AbstractSVGList
40     implements SVGPointList {
41
42     /**
43      * Separator for a point list.
44      */

45     public final static String JavaDoc SVG_POINT_LIST_SEPARATOR
46         =" ";
47
48     /**
49      * Return the separator between points in the list.
50      */

51     protected String JavaDoc getItemSeparator(){
52         return SVG_POINT_LIST_SEPARATOR;
53     }
54
55     /**
56      * Create an SVGException when the checkItemType fails.
57      *
58      * @return SVGException
59      */

60     protected abstract SVGException createSVGException(short type,
61                                                        String JavaDoc key,
62                                                        Object JavaDoc[] args);
63
64     /**
65      * Creates a new SVGPointList.
66      */

67     protected AbstractSVGPointList() {
68         super();
69     }
70
71     /**
72      */

73     public SVGPoint initialize ( SVGPoint newItem )
74         throws DOMException JavaDoc, SVGException {
75
76         return (SVGPoint)initializeImpl(newItem);
77     }
78
79     /**
80      */

81     public SVGPoint getItem ( int index )
82         throws DOMException JavaDoc {
83
84         return (SVGPoint)getItemImpl(index);
85     }
86
87     /**
88      */

89     public SVGPoint insertItemBefore ( SVGPoint newItem, int index )
90         throws DOMException JavaDoc, SVGException {
91
92         return (SVGPoint)insertItemBeforeImpl(newItem,index);
93     }
94
95     /**
96      */

97     public SVGPoint replaceItem ( SVGPoint newItem, int index )
98         throws DOMException JavaDoc, SVGException {
99
100         return (SVGPoint)replaceItemImpl(newItem,index);
101     }
102
103     /**
104      */

105     public SVGPoint removeItem ( int index )
106         throws DOMException JavaDoc {
107
108         return (SVGPoint)removeItemImpl(index);
109     }
110
111     /**
112      */

113     public SVGPoint appendItem ( SVGPoint newItem )
114         throws DOMException JavaDoc, SVGException {
115
116         return (SVGPoint) appendItemImpl(newItem);
117     }
118
119     /**
120      */

121     protected SVGItem createSVGItem(Object JavaDoc newItem){
122         
123         SVGPoint point= (SVGPoint)newItem;
124
125         return new SVGPointItem(point.getX(), point.getY());
126     }
127     
128     /**
129      * Parse the 'points' attribute.
130      *
131      * @param value 'points' attribute value
132      * @param handler : list handler
133      */

134     protected void doParse(String JavaDoc value, ListHandler handler)
135         throws ParseException{
136
137         PointsParser pointsParser = new PointsParser();
138         
139         PointsListBuilder builder = new PointsListBuilder(handler);
140         
141         pointsParser.setPointsHandler(builder);
142         pointsParser.parse(value);
143         
144     }
145
146     /**
147      * Check if the item is an SVGPoint.
148      */

149     protected void checkItemType(Object JavaDoc newItem)
150         throws SVGException {
151         if ( !( newItem instanceof SVGPoint ) ){
152             createSVGException(SVGException.SVG_WRONG_TYPE_ERR,
153                                "expected SVGPoint",
154                                null);
155         }
156     }
157
158     /**
159      * Representation of the item SVGPoint.
160      */

161     protected class SVGPointItem
162         extends AbstractSVGItem
163         implements SVGPoint {
164
165         ///x-axis value
166
protected float x;
167         ///yaxis value
168
protected float y;
169
170         /**
171          * Default contructor.
172          * @param x x-axis value
173          * @param y y-axis value
174          */

175         public SVGPointItem(float x, float y){
176             this.x = x;
177             this.y = y;
178         }
179
180         /**
181          * Return a String representation of
182          * on SVGPoint in a SVGPointList.
183          *
184          * @return String representation of the item
185          */

186         protected String JavaDoc getStringValue(){
187             StringBuffer JavaDoc value = new StringBuffer JavaDoc();
188             value.append(x);
189             value.append(',');
190             value.append(y);
191
192             return value.toString();
193         }
194
195         /**
196          */

197         public float getX(){
198             return x;
199         }
200         /**
201          */

202         public float getY(){
203             return y;
204         }
205         /**
206          */

207         public void setX(float x){
208             this.x = x;
209             resetAttribute();
210         }
211         /**
212          */

213         public void setY(float y){
214             this.y = y;
215             resetAttribute();
216         }
217         /**
218          */

219         public SVGPoint matrixTransform ( SVGMatrix matrix ){
220             throw new RuntimeException JavaDoc(" !!! TODO: matrixTransform ( SVGMatrix matrix )");
221         }
222     }
223
224     /**
225      * Helper class to interface the <code>PointsParser</code>
226      * and the <code>ListHandler</code>
227      */

228     protected class PointsListBuilder
229         implements PointsHandler {
230
231         /**
232          * list handler.
233          */

234         protected ListHandler listHandler;
235         
236         public PointsListBuilder(ListHandler listHandler){
237             this.listHandler = listHandler;
238         }
239
240         public void startPoints()
241             throws ParseException{
242
243             listHandler.startList();
244         }
245         /**
246          * Create SVGPoint item and motifies
247          * the list handler is new item was created.
248          */

249         public void point(float x, float y)
250             throws ParseException {
251
252             listHandler.item(new SVGPointItem(x,y));
253         }
254
255         public void endPoints()
256             throws ParseException {
257             listHandler.endList();
258         }
259     }
260    
261 }
262
Popular Tags