KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2004 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 java.util.StringTokenizer JavaDoc;
21
22 import org.apache.batik.util.SVGConstants;
23 import org.apache.batik.parser.ParseException;
24 import org.apache.batik.parser.DefaultPreserveAspectRatioHandler;
25 import org.apache.batik.parser.PreserveAspectRatioHandler;
26 import org.apache.batik.parser.PreserveAspectRatioParser;
27
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.svg.SVGPreserveAspectRatio;
30
31 /**
32  * Abstract implementation for SVGPreservAspectRatio
33  *
34  * This is the base implementation for SVGPreservAspectRatio
35  *
36  * @author Tonny Kohar
37  */

38 public abstract class AbstractSVGPreserveAspectRatio
39     implements SVGPreserveAspectRatio {
40     
41     /**
42      * align property by default the value is
43      * SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID
44      */

45     protected short align =
46         SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID;
47     
48     /**
49      * meetOrSlice property
50      * by default the value is SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET;
51      */

52     protected short meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET;
53     
54     /** Creates a new instance of AbstractSVGPreserveAspectRatio */
55     public AbstractSVGPreserveAspectRatio() {
56     }
57     
58     public short getAlign() {
59         return this.align;
60     }
61     
62     public short getMeetOrSlice() {
63         return this.meetOrSlice;
64     }
65     
66     public void setAlign(short align) {
67         this.align = align;
68         setAttributeValue(getValueAsString());
69     }
70     
71     public void setMeetOrSlice(short meetOrSlice) {
72         this.meetOrSlice = meetOrSlice;
73         setAttributeValue(getValueAsString());
74     }
75     
76     public void reset() {
77         align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID;
78         meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET;
79         //setAttributeValue(getValueAsString());
80
}
81     
82     protected abstract void setAttributeValue(String JavaDoc value)
83         throws DOMException JavaDoc;
84
85     protected abstract DOMException JavaDoc createDOMException(short type, String JavaDoc key,
86                                                        Object JavaDoc[] args);
87     
88     protected void setValueAsString(String JavaDoc value) throws DOMException JavaDoc {
89         PreserveAspectRatioParserHandler ph;
90         ph = new PreserveAspectRatioParserHandler();
91         try {
92             PreserveAspectRatioParser p = new PreserveAspectRatioParser();
93             p.setPreserveAspectRatioHandler(ph);
94             p.parse(value);
95             align = ph.getAlign();
96             meetOrSlice = ph.getMeetOrSlice();
97         } catch (ParseException ex) {
98             throw createDOMException(SVG_PRESERVEASPECTRATIO_UNKNOWN,
99                                      "invalid value for preserveAspectRatio",
100                                      null);
101         }
102     }
103     
104     /** Return the value of String to be used on setAttributeNS, in
105      * other word the mapping of align meetOrSlice to representation
106      * string use by SVG
107      */

108     protected String JavaDoc getValueAsString() {
109         String JavaDoc value = null;
110         
111         switch (align) {
112         case SVG_PRESERVEASPECTRATIO_NONE:
113             value = SVGConstants.SVG_NONE_VALUE;
114             return value; // if none ignore the rest
115
case SVG_PRESERVEASPECTRATIO_XMINYMIN:
116             value = SVGConstants.SVG_XMINYMIN_VALUE;
117             break;
118         case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
119             value = SVGConstants.SVG_XMIDYMIN_VALUE;
120             break;
121         case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
122             value = SVGConstants.SVG_XMAXYMIN_VALUE;
123             break;
124         case SVG_PRESERVEASPECTRATIO_XMINYMID:
125             value = SVGConstants.SVG_XMINYMID_VALUE;
126             break;
127         case SVG_PRESERVEASPECTRATIO_XMIDYMID:
128             value = SVGConstants.SVG_XMIDYMID_VALUE;
129             break;
130         case SVG_PRESERVEASPECTRATIO_XMAXYMID:
131             value = SVGConstants.SVG_XMAXYMID_VALUE;
132             break;
133         case SVG_PRESERVEASPECTRATIO_XMINYMAX:
134             value = SVGConstants.SVG_XMINYMAX_VALUE;
135             break;
136         case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
137             value = SVGConstants.SVG_XMIDYMAX_VALUE;
138             break;
139         case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
140             value = SVGConstants.SVG_XMAXYMAX_VALUE;
141             break;
142         default:
143             throw createDOMException
144                 (SVG_PRESERVEASPECTRATIO_UNKNOWN,
145                  "invalid value for preserveAspectRatio",null);
146             //break;
147
}
148         
149         switch (meetOrSlice) {
150         case SVG_MEETORSLICE_MEET:
151             value = value + " " + SVGConstants.SVG_MEET_VALUE;
152             break;
153         case SVG_MEETORSLICE_SLICE:
154             value = value + " " + SVGConstants.SVG_SLICE_VALUE;
155             break;
156         default:
157             throw createDOMException(SVG_MEETORSLICE_UNKNOWN,
158                                      "invalid value for preserveAspectRatio",
159                                      null);
160             //break;
161
}
162         
163         return value;
164     }
165     
166     protected class PreserveAspectRatioParserHandler
167         extends DefaultPreserveAspectRatioHandler {
168         public short align =
169             SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID;
170         public short meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET;
171         
172         public short getAlign() {
173             return align;
174         }
175         
176         public short getMeetOrSlice() {
177             return meetOrSlice;
178         }
179         
180         /**
181          * Invoked when 'none' been parsed.
182          * @exception ParseException if an error occured while processing
183          * the transform
184          */

185         public void none() throws ParseException {
186             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE;
187         }
188
189         /**
190          * Invoked when 'xMaxYMax' has been parsed.
191          * @exception ParseException if an error occured while processing
192          * the transform
193          */

194         public void xMaxYMax() throws ParseException {
195             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMAX;
196         }
197
198         /**
199          * Invoked when 'xMaxYMid' has been parsed.
200          * @exception ParseException if an error occured while processing
201          * the transform
202          */

203         public void xMaxYMid() throws ParseException {
204             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMID;
205         }
206
207         /**
208          * Invoked when 'xMaxYMin' has been parsed.
209          * @exception ParseException if an error occured while processing
210          * the transform
211          */

212         public void xMaxYMin() throws ParseException {
213             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMIN;
214         }
215
216         /**
217          * Invoked when 'xMidYMax' has been parsed.
218          * @exception ParseException if an error occured while processing
219          * the transform
220          */

221         public void xMidYMax() throws ParseException {
222             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX;
223         }
224
225         /**
226          * Invoked when 'xMidYMid' has been parsed.
227          * @exception ParseException if an error occured while processing
228          * the transform
229          */

230         public void xMidYMid() throws ParseException {
231             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID;
232         }
233
234         /**
235          * Invoked when 'xMidYMin' has been parsed.
236          * @exception ParseException if an error occured while processing
237          * the transform
238          */

239         public void xMidYMin() throws ParseException {
240             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMIN;
241         }
242
243         /**
244          * Invoked when 'xMinYMax' has been parsed.
245          * @exception ParseException if an error occured while processing
246          * the transform
247          */

248         public void xMinYMax() throws ParseException {
249             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMAX;
250         }
251
252         /**
253          * Invoked when 'xMinYMid' has been parsed.
254          * @exception ParseException if an error occured while processing
255          * the transform
256          */

257         public void xMinYMid() throws ParseException {
258             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMID;
259         }
260
261         /**
262          * Invoked when 'xMinYMin' has been parsed.
263          * @exception ParseException if an error occured while processing
264          * the transform
265          */

266         public void xMinYMin() throws ParseException {
267             align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN;
268         }
269
270         /**
271          * Invoked when 'meet' has been parsed.
272          * @exception ParseException if an error occured while processing
273          * the transform
274          */

275         public void meet() throws ParseException {
276             meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_MEET;
277         }
278
279         /**
280          * Invoked when 'slice' has been parsed.
281          * @exception ParseException if an error occured while processing
282          * the transform
283          */

284         public void slice() throws ParseException {
285             meetOrSlice = SVGPreserveAspectRatio.SVG_MEETORSLICE_SLICE;
286         }
287     }
288 }
289
Popular Tags