KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > UserAgentAdapter


1 /*
2
3    Copyright 2001-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.bridge;
19
20 import java.awt.Cursor JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Point JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.geom.Dimension2D JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.batik.gvt.event.EventDispatcher;
30 import org.apache.batik.gvt.text.Mark;
31 import org.apache.batik.util.ParsedURL;
32 import org.apache.batik.util.SVGConstants;
33 import org.apache.batik.util.XMLResourceDescriptor;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.svg.SVGAElement;
36
37 /**
38  * An abstract user agent adaptor implementation. It exists to simply
39  * the creation of UserAgent instances.
40  *
41  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas DeWeese</a>
42  * @version $Id: UserAgentAdapter.java,v 1.20 2005/03/27 08:58:30 cam Exp $
43  */

44 public class UserAgentAdapter implements UserAgent {
45     protected Set JavaDoc FEATURES = new HashSet JavaDoc();
46     protected Set JavaDoc extensions = new HashSet JavaDoc();
47
48     public UserAgentAdapter() {
49     }
50
51     public void addStdFeatures() {
52         FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_FEATURE);
53         FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_LANG_FEATURE);
54         FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_STATIC_FEATURE);
55     }
56
57
58     /**
59      * Returns the default size of this user agent (400x400).
60      */

61     public Dimension2D JavaDoc getViewportSize() {
62         return new Dimension JavaDoc(1, 1);
63     }
64
65     /**
66      * Display the specified message.
67      */

68     public void displayMessage(String JavaDoc message) {
69     }
70
71     /**
72      * Display the specified error message (forwards call to displayMessage).
73      */

74     public void displayError(String JavaDoc message) {
75         displayMessage(message);
76     }
77
78     /**
79      * Display the specified error (forwards call to displayError(String))
80      */

81     public void displayError(Exception JavaDoc e) {
82         displayError(e.getMessage());
83     }
84
85     /**
86      * Shows an alert dialog box.
87      */

88     public void showAlert(String JavaDoc message) {
89     }
90
91     /**
92      * Shows a prompt dialog box.
93      */

94     public String JavaDoc showPrompt(String JavaDoc message) {
95         return null;
96     }
97
98     /**
99      * Shows a prompt dialog box.
100      */

101     public String JavaDoc showPrompt(String JavaDoc message, String JavaDoc defaultValue) {
102         return null;
103     }
104
105     /**
106      * Shows a confirm dialog box.
107      */

108     public boolean showConfirm(String JavaDoc message) {
109         return false;
110     }
111
112     /**
113      * Returns the size of a px CSS unit in millimeters.
114      */

115     public float getPixelUnitToMillimeter() {
116         return 0.26458333333333333333333333333333f; // 96dpi
117
}
118
119     /**
120      * Returns the size of a px CSS unit in millimeters.
121      * This will be removed after next release.
122      * @see #getPixelUnitToMillimeter()
123      */

124     public float getPixelToMM() {
125         return getPixelUnitToMillimeter();
126             
127     }
128
129     /**
130      * Returns the default font family.
131      */

132     public String JavaDoc getDefaultFontFamily() {
133         return "Arial, Helvetica, sans-serif";
134     }
135
136     /**
137      * Returns the medium font size.
138      */

139     public float getMediumFontSize() {
140         // 9pt (72pt = 1in)
141
return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
142     }
143
144     /**
145      * Returns a lighter font-weight.
146      */

147     public float getLighterFontWeight(float f) {
148         return getStandardLighterFontWeight(f);
149     }
150
151     /**
152      * Returns a bolder font-weight.
153      */

154     public float getBolderFontWeight(float f) {
155         return getStandardBolderFontWeight(f);
156     }
157
158
159     /**
160      * Returns the user language "en" (english).
161      */

162     public String JavaDoc getLanguages() {
163         return "en";
164     }
165
166     /**
167      * Returns this user agent's CSS media.
168      */

169     public String JavaDoc getMedia() {
170         return "all";
171     }
172
173     /**
174      * Returns this user agent's alternate style-sheet title.
175      */

176     public String JavaDoc getAlternateStyleSheet() {
177         return null;
178     }
179
180     /**
181      * Returns the user stylesheet
182      */

183     public String JavaDoc getUserStyleSheetURI() {
184         return null;
185     }
186
187     /**
188      * Returns the XML parser to use
189      */

190     public String JavaDoc getXMLParserClassName() {
191         return XMLResourceDescriptor.getXMLParserClassName();
192     }
193
194     /**
195      * Returns <tt>false</tt>. The XML parser is not in validation mode.
196      */

197     public boolean isXMLParserValidating() {
198         return false;
199     }
200
201     /**
202      * Unsupported operation.
203      */

204     public EventDispatcher getEventDispatcher() {
205         return null;
206     }
207
208     /**
209      * Unsupported operation.
210      */

211     public void openLink(SVGAElement elt) { }
212
213     /**
214      * Unsupported operation.
215      */

216     public void setSVGCursor(Cursor JavaDoc cursor) { }
217
218     /**
219      * This user agent doesn't display text selections.
220      */

221     public void setTextSelection(Mark start, Mark end) { }
222
223     /**
224      * This user agent doesn't display text selections so nothing to clear.
225      */

226     public void deselectAll() { }
227
228     /**
229      * Unsupported operation.
230      */

231     public void runThread(Thread JavaDoc t) { }
232
233     /**
234      * Unsupported operation.
235      */

236     public AffineTransform JavaDoc getTransform() {
237         return null;
238     }
239
240     /**
241      * Unsupported operation.
242      */

243     public void setTransform(AffineTransform JavaDoc at) {
244         // Do nothing.
245
}
246
247     /**
248      * Unsupported operation.
249      */

250     public Point JavaDoc getClientAreaLocationOnScreen() {
251         return new Point JavaDoc();
252     }
253
254     /**
255      * Tells whether the given feature is supported by this
256      * user agent.
257      */

258     public boolean hasFeature(String JavaDoc s) {
259         return FEATURES.contains(s);
260     }
261
262     /**
263      * Tells whether the given extension is supported by this
264      * user agent.
265      */

266     public boolean supportExtension(String JavaDoc s) {
267         return extensions.contains(s);
268     }
269
270     /**
271      * Lets the bridge tell the user agent that the following
272      * ex tension is supported by the bridge.
273      */

274     public void registerExtension(BridgeExtension ext) {
275         Iterator JavaDoc i = ext.getImplementedExtensions();
276         while (i.hasNext())
277             extensions.add(i.next());
278     }
279
280
281     /**
282      * Notifies the UserAgent that the input element
283      * has been found in the document. This is sometimes
284      * called, for example, to handle &lt;a&gt; or
285      * &lt;title&gt; elements in a UserAgent-dependant
286      * way.
287      */

288     public void handleElement(Element JavaDoc elt, Object JavaDoc data){
289     }
290
291     /**
292      * Returns the security settings for the given script
293      * type, script url and document url
294      *
295      * @param scriptType type of script, as found in the
296      * type attribute of the &lt;script&gt; element.
297      * @param scriptURL url for the script, as defined in
298      * the script's xlink:href attribute. If that
299      * attribute was empty, then this parameter should
300      * be null
301      * @param docURL url for the document into which the
302      * script was found.
303      */

304     public ScriptSecurity getScriptSecurity(String JavaDoc scriptType,
305                                             ParsedURL scriptURL,
306                                             ParsedURL docURL){
307         return new DefaultScriptSecurity(scriptType, scriptURL, docURL);
308     }
309     
310     /**
311      * This method throws a SecurityException if the script
312      * of given type, found at url and referenced from docURL
313      * should not be loaded.
314      *
315      * This is a convenience method to call checkLoadScript
316      * on the ScriptSecurity strategy returned by
317      * getScriptSecurity.
318      *
319      * @param scriptType type of script, as found in the
320      * type attribute of the &lt;script&gt; element.
321      * @param scriptURL url for the script, as defined in
322      * the script's xlink:href attribute. If that
323      * attribute was empty, then this parameter should
324      * be null
325      * @param docURL url for the document into which the
326      * script was found.
327      */

328     public void checkLoadScript(String JavaDoc scriptType,
329                                 ParsedURL scriptURL,
330                                 ParsedURL docURL) throws SecurityException JavaDoc {
331         ScriptSecurity s = getScriptSecurity(scriptType,
332                                              scriptURL,
333                                              docURL);
334         if (s != null) {
335             s.checkLoadScript();
336         }
337     }
338
339     /**
340      * Returns the security settings for the given resource
341      * url and document url
342      *
343      * @param resourceURL url for the resource, as defined in
344      * the resource's xlink:href attribute. If that
345      * attribute was empty, then this parameter should
346      * be null
347      * @param docURL url for the document into which the
348      * resource was found.
349      */

350     public ExternalResourceSecurity
351         getExternalResourceSecurity(ParsedURL resourceURL,
352                                     ParsedURL docURL) {
353         return new RelaxedExternalResourceSecurity(resourceURL, docURL);
354     }
355     
356     /**
357      * This method throws a SecurityException if the resource
358      * found at url and referenced from docURL
359      * should not be loaded.
360      *
361      * This is a convenience method to call checkLoadExternalResource
362      * on the ExternalResourceSecurity strategy returned by
363      * getExternalResourceSecurity.
364      *
365      * @param resourceURL url for the resource, as defined in
366      * the resource's xlink:href attribute. If that
367      * attribute was empty, then this parameter should
368      * be null
369      * @param docURL url for the document into which the
370      * resource was found.
371      */

372     public void
373         checkLoadExternalResource(ParsedURL resourceURL,
374                                   ParsedURL docURL) throws SecurityException JavaDoc {
375         ExternalResourceSecurity s
376             = getExternalResourceSecurity(resourceURL, docURL);
377
378         if (s != null) {
379             s.checkLoadExternalResource();
380         }
381     }
382
383     /**
384      * Returns a lighter font-weight.
385      */

386     public static float getStandardLighterFontWeight(float f) {
387         // Round f to nearest 100...
388
int weight = ((int)((f+50)/100))*100;
389         switch (weight) {
390         case 100: return 100;
391         case 200: return 100;
392         case 300: return 200;
393         case 400: return 300;
394         case 500: return 400;
395         case 600: return 400;
396         case 700: return 400;
397         case 800: return 400;
398         case 900: return 400;
399         default:
400             throw new IllegalArgumentException JavaDoc("Bad Font Weight: " + f);
401         }
402     }
403
404     /**
405      * Returns a bolder font-weight.
406      */

407     public static float getStandardBolderFontWeight(float f) {
408         // Round f to nearest 100...
409
int weight = ((int)((f+50)/100))*100;
410         switch (weight) {
411         case 100: return 600;
412         case 200: return 600;
413         case 300: return 600;
414         case 400: return 600;
415         case 500: return 600;
416         case 600: return 700;
417         case 700: return 800;
418         case 800: return 900;
419         case 900: return 900;
420         default:
421             throw new IllegalArgumentException JavaDoc("Bad Font Weight: " + f);
422         }
423     }
424
425 }
426
427
Popular Tags