Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/dom/svg/AbstractSVGNumberList.java


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.NumberListHandler;
21  import org.apache.batik.parser.NumberListParser;
22  import org.apache.batik.parser.ParseException;
23  import org.w3c.dom.DOMException;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.svg.SVGException;
26  import org.w3c.dom.svg.SVGNumber;
27  import org.w3c.dom.svg.SVGNumberList;
28  
29  
30  /**
31   * This class is the implementation of
32   * <code>SVGNumberList</code>.
33   *
34   * @author  tonny@kiyut.com
35   */
36  public abstract class AbstractSVGNumberList extends AbstractSVGList implements SVGNumberList {
37      
38      /**
39       * Separator for a length list.
40       */
41      public final static String SVG_NUMBER_LIST_SEPARATOR
42          =" ";
43  
44      /**
45       * Return the separator between values in the list.
46       */
47      protected String getItemSeparator(){
48          return SVG_NUMBER_LIST_SEPARATOR;
49      }
50      
51      /**
52       * Create an SVGException when the checkItemType fails.
53       *
54       * @return SVGException
55       */
56      protected abstract SVGException createSVGException(short type,
57                                                         String key,
58                                                         Object[] args);
59  
60      /**
61       * return the element owning this SVGNumberList.
62       */
63      protected abstract Element getElement();
64      
65      /**
66       * Creates a new SVGNumberList.
67       */
68      protected AbstractSVGNumberList() {
69          super();
70      }
71  
72      /**
73       */
74      public SVGNumber initialize ( SVGNumber newItem )
75          throws DOMException, SVGException {
76  
77          return (SVGNumber)initializeImpl(newItem);
78      }
79  
80      /**
81       */
82      public SVGNumber getItem ( int index )
83          throws DOMException {
84  
85          return (SVGNumber)getItemImpl(index);
86      }
87      
88      /**
89       */
90      public SVGNumber insertItemBefore ( SVGNumber newItem, int index )
91          throws DOMException, SVGException {
92  
93          return (SVGNumber)insertItemBeforeImpl(newItem,index);
94      }
95  
96      /**
97       */
98      public SVGNumber replaceItem ( SVGNumber newItem, int index )
99          throws DOMException, SVGException {
100 
101         return (SVGNumber)replaceItemImpl(newItem,index);
102     }
103 
104     /**
105      */
106     public SVGNumber removeItem ( int index )
107         throws DOMException {
108 
109         return (SVGNumber)removeItemImpl(index);
110     }
111 
112     /**
113      */
114     public SVGNumber appendItem ( SVGNumber newItem )
115         throws DOMException, SVGException {
116 
117         return (SVGNumber) appendItemImpl(newItem);
118     }
119 
120     /**
121      */
122     protected SVGItem createSVGItem(Object newItem){
123         
124         SVGNumber l = (SVGNumber)newItem;
125 
126         return new SVGNumberItem(l.getValue());
127     }
128     
129     /**
130      * Parse the attribute associated with this SVGNumberList.
131      *
132      * @param value attribute value
133      * @param handler list handler
134      */
135     protected void doParse(String value, ListHandler handler)
136         throws ParseException{
137 
138         NumberListParser NumberListParser = new NumberListParser();
139         
140         NumberListBuilder builder = new NumberListBuilder(handler);
141         
142         NumberListParser.setNumberListHandler(builder);
143         NumberListParser.parse(value);
144         
145     }
146     
147     /**
148      * Check if the item is an SVGNumber
149      */
150     protected void checkItemType(Object newItem)
151         throws SVGException {
152         if ( !( newItem instanceof SVGNumber ) ){
153             createSVGException(SVGException.SVG_WRONG_TYPE_ERR,
154                                "expected SVGNumber",
155                                null);
156         }
157     }
158     
159     /**
160      * Representation of the item SVGNumber.
161      */
162     protected class SVGNumberItem 
163         extends AbstractSVGNumber 
164         implements SVGItem {
165 
166         /**
167          * Default Constructor.
168          */
169         public SVGNumberItem(float value){
170             super();
171             this.value = value;
172         }
173         
174         public String getValueAsString(){
175             return Float.toString(value);
176         }
177 
178         /**
179          * SVGNumberList this item belongs to.
180          */
181         protected AbstractSVGList parentList;
182 
183         /**
184          * Associates an item to an SVGXXXList
185          *
186          * @param list list the item belongs to.
187          */
188         public void setParent(AbstractSVGList list){
189             parentList = list;
190         }
191 
192         /**
193          * Return the list the item belongs to.
194          *
195          * @return list the item belongs to. This
196          *   could be if the item belongs to no list.
197          */
198         public AbstractSVGList getParent(){
199             return parentList;
200         }
201 
202         /**
203          * When the SVGLength changes, notify
204          * its parent.
205          */
206         protected void reset(){
207             if ( parentList != null ){
208                 parentList.itemChanged();
209             }
210         }
211         
212     }
213     
214     /**
215      * Helper class to interface the <code>NumberListParser</code>
216      * and the <code>NumberHandler</code>
217      */
218     protected class NumberListBuilder
219         implements NumberListHandler {
220 
221         /**
222          * list handler.
223          */
224         protected ListHandler listHandler;
225 
226         //current value being parsed
227         protected float currentValue;
228                 
229         /**
230          */
231         public NumberListBuilder(ListHandler listHandler){
232             this.listHandler = listHandler;
233         }
234 
235         /**
236          */
237         public void startNumberList() 
238             throws ParseException{
239 
240             listHandler.startList();
241         }
242         /**
243          * Implements {@link org.apache.batik.parser.NumberListHandler#startNumber()}.
244          */
245         public void startNumber() throws ParseException {
246             currentValue = 0.0f;
247         }
248 
249         /**
250          * Implements {@link org.apache.batik.parser.NumberListHandler#numberValue(float)}.
251          */
252         public void numberValue(float v) throws ParseException {
253             currentValue = v;
254         }
255         
256         /**
257          * Implements {@link org.apache.batik.parser.NumberListHandler#endNumber()}.
258          */
259         public void endNumber() throws ParseException {
260             listHandler.item(new SVGNumberItem(currentValue));
261         }
262         
263         /**
264          */
265         public void endNumberList() 
266             throws ParseException {
267             listHandler.endList();
268         }
269     }
270 }