KickJava   Java API By Example, From Geeks To Geeks.

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


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.LengthListHandler;
21 import org.apache.batik.parser.LengthListParser;
22 import org.apache.batik.parser.ParseException;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.svg.SVGException;
26 import org.w3c.dom.svg.SVGLength;
27 import org.w3c.dom.svg.SVGLengthList;
28
29
30 /**
31  * This class is the implementation of
32  * <code>SVGLengthList</code>.
33  *
34  * @author <a HREF="mailto:nicolas.socheleau@bitflash.com">Nicolas Socheleau</a>
35  * @version $Id: AbstractSVGLengthList.java,v 1.6 2004/08/18 07:13:13 vhardy Exp $
36  */

37 public abstract class AbstractSVGLengthList
38     extends AbstractSVGList
39     implements SVGLengthList {
40
41
42     /**
43      * This length list's direction.
44      */

45     protected short direction;
46
47     /**
48      * Separator for a length list.
49      */

50     public final static String JavaDoc SVG_LENGTH_LIST_SEPARATOR
51         =" ";
52
53     /**
54      * Return the separator between values in the list.
55      */

56     protected String JavaDoc getItemSeparator(){
57         return SVG_LENGTH_LIST_SEPARATOR;
58     }
59
60     /**
61      * Create an SVGException when the checkItemType fails.
62      *
63      * @return SVGException
64      */

65     protected abstract SVGException createSVGException(short type,
66                                                        String JavaDoc key,
67                                                        Object JavaDoc[] args);
68
69     /**
70      * return the element owning this SVGLengthList.
71      */

72     protected abstract Element JavaDoc getElement();
73
74     /**
75      * Creates a new SVGLengthList.
76      */

77     protected AbstractSVGLengthList(short direction) {
78         super();
79         this.direction = direction;
80     }
81
82     /**
83      */

84     public SVGLength initialize ( SVGLength newItem )
85         throws DOMException JavaDoc, SVGException {
86
87         return (SVGLength)initializeImpl(newItem);
88     }
89
90     /**
91      */

92     public SVGLength getItem ( int index )
93         throws DOMException JavaDoc {
94
95         return (SVGLength)getItemImpl(index);
96     }
97
98     /**
99      */

100     public SVGLength insertItemBefore ( SVGLength newItem, int index )
101         throws DOMException JavaDoc, SVGException {
102
103         return (SVGLength)insertItemBeforeImpl(newItem,index);
104     }
105
106     /**
107      */

108     public SVGLength replaceItem ( SVGLength newItem, int index )
109         throws DOMException JavaDoc, SVGException {
110
111         return (SVGLength)replaceItemImpl(newItem,index);
112     }
113
114     /**
115      */

116     public SVGLength removeItem ( int index )
117         throws DOMException JavaDoc {
118
119         return (SVGLength)removeItemImpl(index);
120     }
121
122     /**
123      */

124     public SVGLength appendItem ( SVGLength newItem )
125         throws DOMException JavaDoc, SVGException {
126
127         return (SVGLength) appendItemImpl(newItem);
128     }
129
130     /**
131      */

132     protected SVGItem createSVGItem(Object JavaDoc newItem){
133         
134         SVGLength l = (SVGLength)newItem;
135
136         return new SVGLengthItem(l.getUnitType(), l.getValueInSpecifiedUnits(),direction);
137     }
138     
139     /**
140      * Parse the attribute associated with this SVGLengthList.
141      *
142      * @param value attribute value
143      * @param handler list handler
144      */

145     protected void doParse(String JavaDoc value, ListHandler handler)
146         throws ParseException{
147
148         LengthListParser lengthListParser = new LengthListParser();
149         
150         LengthListBuilder builder = new LengthListBuilder(handler);
151         
152         lengthListParser.setLengthListHandler(builder);
153         lengthListParser.parse(value);
154         
155     }
156
157     /**
158      * Check if the item is an SVGLength.
159      */

160     protected void checkItemType(Object JavaDoc newItem)
161         throws SVGException {
162         if ( !( newItem instanceof SVGLength ) ){
163             createSVGException(SVGException.SVG_WRONG_TYPE_ERR,
164                                "expected SVGLength",
165                                null);
166         }
167     }
168
169     /**
170      * Representation of the item SVGLength.
171      */

172     protected class SVGLengthItem
173         extends AbstractSVGLength
174         implements SVGItem {
175
176         /**
177          * Default Constructor.
178          */

179         public SVGLengthItem(short type, float value,short direction){
180             super(direction);
181             this.unitType = type;
182             this.value = value;
183         }
184
185         /**
186          */

187         protected SVGOMElement getAssociatedElement(){
188             return (SVGOMElement)AbstractSVGLengthList.this.getElement();
189         }
190
191         /**
192          * SVGLengthList this item belongs to.
193          */

194         protected AbstractSVGList parentList;
195
196         /**
197          * Associates an item to an SVGXXXList
198          *
199          * @param list list the item belongs to.
200          */

201         public void setParent(AbstractSVGList list){
202             parentList = list;
203         }
204
205         /**
206          * Return the list the item belongs to.
207          *
208          * @return list the item belongs to. This
209          * could be if the item belongs to no list.
210          */

211         public AbstractSVGList getParent(){
212             return parentList;
213         }
214
215         /**
216          * When the SVGLength changes, notify
217          * its parent.
218          */

219         protected void reset(){
220             if ( parentList != null ){
221                 parentList.itemChanged();
222             }
223         }
224         
225     }
226
227     /**
228      * Helper class to interface the <code>LengthListParser</code>
229      * and the <code>ListHandler</code>
230      */

231     protected class LengthListBuilder
232         implements LengthListHandler {
233
234         /**
235          * list handler.
236          */

237         protected ListHandler listHandler;
238
239         //current value being parsed
240
protected float currentValue;
241         //current type being parsed
242
protected short currentType;
243         
244         /**
245          */

246         public LengthListBuilder(ListHandler listHandler){
247             this.listHandler = listHandler;
248         }
249
250         /**
251          */

252         public void startLengthList()
253             throws ParseException{
254
255             listHandler.startList();
256         }
257         /**
258          * Implements {@link org.apache.batik.parser.LengthHandler#startLength()}.
259          */

260         public void startLength() throws ParseException {
261             currentType = SVGLength.SVG_LENGTHTYPE_NUMBER;
262             currentValue = 0.0f;
263         }
264
265         /**
266          * Implements {@link org.apache.batik.parser.LengthHandler#lengthValue(float)}.
267          */

268         public void lengthValue(float v) throws ParseException {
269             currentValue = v;
270         }
271         
272         /**
273          * Implements {@link org.apache.batik.parser.LengthHandler#em()}.
274          */

275         public void em() throws ParseException {
276             currentType = SVGLength.SVG_LENGTHTYPE_EMS;
277         }
278
279         /**
280          * Implements {@link org.apache.batik.parser.LengthHandler#ex()}.
281          */

282         public void ex() throws ParseException {
283             currentType = SVGLength.SVG_LENGTHTYPE_EXS;
284         }
285
286         /**
287          * Implements {@link org.apache.batik.parser.LengthHandler#in()}.
288          */

289         public void in() throws ParseException {
290             currentType = SVGLength.SVG_LENGTHTYPE_IN;
291         }
292         
293         /**
294          * Implements {@link org.apache.batik.parser.LengthHandler#cm()}.
295          */

296         public void cm() throws ParseException {
297             currentType = SVGLength.SVG_LENGTHTYPE_CM;
298         }
299         
300         /**
301          * Implements {@link org.apache.batik.parser.LengthHandler#mm()}.
302          */

303         public void mm() throws ParseException {
304             currentType = SVGLength.SVG_LENGTHTYPE_MM;
305         }
306         
307         /**
308          * Implements {@link org.apache.batik.parser.LengthHandler#pc()}.
309          */

310         public void pc() throws ParseException {
311             currentType = SVGLength.SVG_LENGTHTYPE_PC;
312         }
313
314         /**
315          * Implements {@link org.apache.batik.parser.LengthHandler#pt()}.
316          */

317         public void pt() throws ParseException {
318             currentType = SVGLength.SVG_LENGTHTYPE_EMS;
319         }
320
321         /**
322          * Implements {@link org.apache.batik.parser.LengthHandler#px()}.
323          */

324         public void px() throws ParseException {
325             currentType = SVGLength.SVG_LENGTHTYPE_PX;
326         }
327
328         /**
329          * Implements {@link org.apache.batik.parser.LengthHandler#percentage()}.
330          */

331         public void percentage() throws ParseException {
332             currentType = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
333         }
334
335         /**
336          * Implements {@link org.apache.batik.parser.LengthHandler#endLength()}.
337          */

338         public void endLength() throws ParseException {
339             listHandler.item(new SVGLengthItem(currentType,currentValue,direction));
340         }
341         
342         /**
343          */

344         public void endLengthList()
345             throws ParseException {
346             listHandler.endList();
347         }
348     }
349    
350 }
351
Popular Tags