KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > svg > SVGUserAgentAdapter


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.swing.svg;
19
20 import org.apache.batik.util.XMLResourceDescriptor;
21
22 import org.apache.batik.bridge.ExternalResourceSecurity;
23 import org.apache.batik.bridge.RelaxedExternalResourceSecurity;
24 import org.apache.batik.bridge.RelaxedScriptSecurity;
25 import org.apache.batik.bridge.ScriptSecurity;
26 import org.apache.batik.util.ParsedURL;
27 import org.w3c.dom.Element JavaDoc;
28
29 /*
30 import org.apache.batik.bridge.DefaultExternalResourceSecurity;
31 import org.apache.batik.bridge.DefaultScriptSecurity;
32 import org.apache.batik.bridge.EmbededExternalResourceSecurity;
33 import org.apache.batik.bridge.EmbededScriptSecurity;
34 import org.apache.batik.bridge.ExternalResourceSecurity;
35 import org.apache.batik.bridge.NoLoadExternalResourceSecurity;
36 import org.apache.batik.bridge.NoLoadScriptSecurity;
37 */

38
39 /**
40  * This Implements the SVGUserAgent interface to provide a very simple
41  * version of client services to the JSVGComponent.
42  *
43  * This implementation does not require any GUI interaction to work.
44  * This implementation is particularly bad about user interaction
45  * most of the alert,prompt,etc methods are totally useless.
46  * In a GUI environment you probably want to use SVGUserAgentGUIAdapter.
47  *
48  * @author <a HREF="mailto:deweese@apache.org">deweese</a>
49  * @version $Id: SVGUserAgentAdapter.java,v 1.3 2005/03/27 08:58:36 cam Exp $
50  */

51 public class SVGUserAgentAdapter implements SVGUserAgent {
52     public SVGUserAgentAdapter() { }
53
54     /**
55      * Displays an error message.
56      */

57     public void displayError(String JavaDoc message) {
58         System.err.println(message);
59     }
60
61     /**
62      * Displays an error resulting from the specified Exception.
63      */

64     public void displayError(Exception JavaDoc ex) {
65         ex.printStackTrace();
66     }
67
68     /**
69      * Displays a message in the User Agent interface.
70      * The given message is typically displayed in a status bar.
71      */

72     public void displayMessage(String JavaDoc message) {
73         System.out.println(message);
74     }
75
76     /**
77      * Shows an alert dialog box.
78      */

79     public void showAlert(String JavaDoc message) {
80         System.err.println(message);
81     }
82
83     /**
84      * Shows a prompt dialog box.
85      */

86     public String JavaDoc showPrompt(String JavaDoc message) {
87         return "";
88     }
89
90     /**
91      * Shows a prompt dialog box.
92      */

93     public String JavaDoc showPrompt(String JavaDoc message, String JavaDoc defaultValue) {
94         return defaultValue;
95     }
96
97     /**
98      * Shows a confirm dialog box.
99      */

100     public boolean showConfirm(String JavaDoc message) {
101         return false;
102     }
103
104     /**
105      * Returns the size of a px CSS unit in millimeters.
106      */

107     public float getPixelUnitToMillimeter() {
108         return 0.26458333333333333333333333333333f; // 96dpi
109
}
110         
111     /**
112      * Returns the size of a px CSS unit in millimeters.
113      * This will be removed after next release.
114      * @see #getPixelUnitToMillimeter()
115      */

116     public float getPixelToMM() {
117         return getPixelUnitToMillimeter();
118             
119     }
120
121     /**
122      * Returns the default font family.
123      */

124     public String JavaDoc getDefaultFontFamily() {
125         return "Serif";
126     }
127
128     /**
129      * Returns the medium font size.
130      */

131     public float getMediumFontSize() {
132         // 9pt (72pt == 1in)
133
return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
134     }
135
136     /**
137      * Returns a lighter font-weight.
138      */

139     public float getLighterFontWeight(float f) {
140         // Round f to nearest 100...
141
int weight = ((int)((f+50)/100))*100;
142         switch (weight) {
143         case 100: return 100;
144         case 200: return 100;
145         case 300: return 200;
146         case 400: return 300;
147         case 500: return 400;
148         case 600: return 400;
149         case 700: return 400;
150         case 800: return 400;
151         case 900: return 400;
152         default:
153             throw new IllegalArgumentException JavaDoc("Bad Font Weight: " + f);
154         }
155     }
156
157     /**
158      * Returns a bolder font-weight.
159      */

160     public float getBolderFontWeight(float f) {
161         // Round f to nearest 100...
162
int weight = ((int)((f+50)/100))*100;
163         switch (weight) {
164         case 100: return 600;
165         case 200: return 600;
166         case 300: return 600;
167         case 400: return 600;
168         case 500: return 600;
169         case 600: return 700;
170         case 700: return 800;
171         case 800: return 900;
172         case 900: return 900;
173         default:
174             throw new IllegalArgumentException JavaDoc("Bad Font Weight: " + f);
175         }
176     }
177
178
179     /**
180      * Returns the language settings.
181      */

182     public String JavaDoc getLanguages() {
183         return "en";
184     }
185
186     /**
187      * Returns the user stylesheet uri.
188      * @return null if no user style sheet was specified.
189      */

190     public String JavaDoc getUserStyleSheetURI() {
191         return null;
192     }
193
194     /**
195      * Returns the class name of the XML parser.
196      */

197     public String JavaDoc getXMLParserClassName() {
198         return XMLResourceDescriptor.getXMLParserClassName();
199     }
200
201     /**
202      * Returns true if the XML parser must be in validation mode, false
203      * otherwise.
204      */

205     public boolean isXMLParserValidating() {
206         return false;
207     }
208
209     /**
210      * Returns this user agent's CSS media.
211      */

212     public String JavaDoc getMedia() {
213         return "screen";
214     }
215
216     /**
217      * Returns this user agent's alternate style-sheet title.
218      */

219     public String JavaDoc getAlternateStyleSheet() {
220         return null;
221     }
222
223     /**
224      * Opens a link.
225      * @param uri The document URI.
226      * @param newc Whether the link should be activated in a new component.
227      */

228     public void openLink(String JavaDoc uri, boolean newc) {
229     }
230
231     /**
232      * Tells whether the given extension is supported by this
233      * user agent.
234      */

235     public boolean supportExtension(String JavaDoc s) {
236         return false;
237     }
238
239     public void handleElement(Element JavaDoc elt, Object JavaDoc data){
240     }
241
242     /**
243      * Returns the security settings for the given script
244      * type, script url and document url
245      *
246      * @param scriptType type of script, as found in the
247      * type attribute of the &lt;script&gt; element.
248      * @param scriptURL url for the script, as defined in
249      * the script's xlink:href attribute. If that
250      * attribute was empty, then this parameter should
251      * be null
252      * @param docURL url for the document into which the
253      * script was found.
254      */

255     public ScriptSecurity getScriptSecurity(String JavaDoc scriptType,
256                                             ParsedURL scriptURL,
257                                             ParsedURL docURL){
258         return new RelaxedScriptSecurity(scriptType,
259                                          scriptURL,
260                                          docURL);
261         /*
262         return new DefaultScriptSecurity(scriptType,
263                                          scriptURL,
264                                          docURL);
265         return new EmbededScriptSecurity(scriptType,
266                                          scriptURL,
267                                          docURL);
268         return new NoLoadScriptSecurity(scriptType);
269         */

270     }
271
272     /**
273      * This method throws a SecurityException if the script
274      * of given type, found at url and referenced from docURL
275      * should not be loaded.
276      *
277      * This is a convenience method to call checkLoadScript
278      * on the ScriptSecurity strategy returned by
279      * getScriptSecurity.
280      *
281      * @param scriptType type of script, as found in the
282      * type attribute of the &lt;script&gt; element.
283      * @param scriptURL url for the script, as defined in
284      * the script's xlink:href attribute. If that
285      * attribute was empty, then this parameter should
286      * be null
287      * @param docURL url for the document into which the
288      * script was found.
289      */

290     public void checkLoadScript(String JavaDoc scriptType,
291                                 ParsedURL scriptURL,
292                                 ParsedURL docURL) throws SecurityException JavaDoc {
293         ScriptSecurity s = getScriptSecurity(scriptType,
294                                              scriptURL,
295                                              docURL);
296
297         if (s != null) {
298             s.checkLoadScript();
299         }
300     }
301
302     /**
303      * Returns the security settings for the given
304      * resource url and document url
305      *
306      * @param resourceURL url for the resource, as defined in
307      * the resource's xlink:href attribute. If that
308      * attribute was empty, then this parameter should
309      * be null
310      * @param docURL url for the document into which the
311      * resource was found.
312      */

313     public ExternalResourceSecurity
314         getExternalResourceSecurity(ParsedURL resourceURL,
315                                     ParsedURL docURL){
316         return new RelaxedExternalResourceSecurity(resourceURL,
317                                                    docURL);
318         /*
319         return new DefaultExternalResourceSecurity(resourceURL,
320                                                    docURL);
321         return new EmbededExternalResourceSecurity(resourceURL);
322         return new NoLoadExternalResourceSecurity();
323         */

324     }
325
326     /**
327      * This method throws a SecurityException if the resource
328      * found at url and referenced from docURL
329      * should not be loaded.
330      *
331      * This is a convenience method to call checkLoadExternalResource
332      * on the ExternalResourceSecurity strategy returned by
333      * getExternalResourceSecurity.
334      *
335      * @param resourceURL url for the resource, as defined in
336      * the resource's xlink:href attribute. If that
337      * attribute was empty, then this parameter should
338      * be null
339      * @param docURL url for the document into which the
340      * resource was found.
341      */

342     public void
343         checkLoadExternalResource(ParsedURL resourceURL,
344                                   ParsedURL docURL) throws SecurityException JavaDoc {
345         ExternalResourceSecurity s
346             = getExternalResourceSecurity(resourceURL, docURL);
347             
348         if (s != null) {
349             s.checkLoadExternalResource();
350         }
351     }
352 };
353
Popular Tags