001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.Cursor;
022: import java.awt.Dimension;
023: import java.awt.Point;
024: import java.awt.geom.AffineTransform;
025: import java.awt.geom.Dimension2D;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: import org.apache.batik.gvt.event.EventDispatcher;
031: import org.apache.batik.gvt.text.Mark;
032: import org.apache.batik.util.ParsedURL;
033: import org.apache.batik.util.SVGConstants;
034: import org.apache.batik.util.XMLResourceDescriptor;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.svg.SVGAElement;
037: import org.w3c.dom.svg.SVGDocument;
038:
039: /**
040: * An abstract user agent adaptor implementation. It exists to simply
041: * the creation of UserAgent instances.
042: *
043: * @author <a href="mailto:thomas.deweese@kodak.com">Thomas DeWeese</a>
044: * @version $Id: UserAgentAdapter.java 475477 2006-11-15 22:44:28Z cam $
045: */
046: public class UserAgentAdapter implements UserAgent {
047: protected Set FEATURES = new HashSet();
048: protected Set extensions = new HashSet();
049:
050: /**
051: * The BridgeContext to use for error information.
052: */
053: protected BridgeContext ctx;
054:
055: /**
056: * Sets the BridgeContext to be used for error information.
057: */
058: public void setBridgeContext(BridgeContext ctx) {
059: this .ctx = ctx;
060: }
061:
062: /**
063: * Adds the standard SVG feature strings to the set of features supported
064: * by this user agent.
065: */
066: public void addStdFeatures() {
067: FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_FEATURE);
068: FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_LANG_FEATURE);
069: FEATURES.add(SVGConstants.SVG_ORG_W3C_SVG_STATIC_FEATURE);
070: }
071:
072: /**
073: * Returns the default size of this user agent (400x400).
074: */
075: public Dimension2D getViewportSize() {
076: return new Dimension(1, 1);
077: }
078:
079: /**
080: * Display the specified message.
081: */
082: public void displayMessage(String message) {
083: }
084:
085: /**
086: * Display the specified error message (forwards call to displayMessage).
087: */
088: public void displayError(String message) {
089: displayMessage(message);
090: }
091:
092: /**
093: * Display the specified error (forwards call to displayError(String))
094: */
095: public void displayError(Exception e) {
096: displayError(e.getMessage());
097: }
098:
099: /**
100: * Shows an alert dialog box.
101: */
102: public void showAlert(String message) {
103: }
104:
105: /**
106: * Shows a prompt dialog box.
107: */
108: public String showPrompt(String message) {
109: return null;
110: }
111:
112: /**
113: * Shows a prompt dialog box.
114: */
115: public String showPrompt(String message, String defaultValue) {
116: return null;
117: }
118:
119: /**
120: * Shows a confirm dialog box.
121: */
122: public boolean showConfirm(String message) {
123: return false;
124: }
125:
126: /**
127: * Returns the size of a px CSS unit in millimeters.
128: */
129: public float getPixelUnitToMillimeter() {
130: return 0.26458333333333333333333333333333f; // 96dpi
131: }
132:
133: /**
134: * Returns the size of a px CSS unit in millimeters.
135: * This will be removed after next release.
136: * @see #getPixelUnitToMillimeter()
137: */
138: public float getPixelToMM() {
139: return getPixelUnitToMillimeter();
140:
141: }
142:
143: /**
144: * Returns the default font family.
145: */
146: public String getDefaultFontFamily() {
147: return "Arial, Helvetica, sans-serif";
148: }
149:
150: /**
151: * Returns the medium font size.
152: */
153: public float getMediumFontSize() {
154: // 9pt (72pt = 1in)
155: return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
156: }
157:
158: /**
159: * Returns a lighter font-weight.
160: */
161: public float getLighterFontWeight(float f) {
162: return getStandardLighterFontWeight(f);
163: }
164:
165: /**
166: * Returns a bolder font-weight.
167: */
168: public float getBolderFontWeight(float f) {
169: return getStandardBolderFontWeight(f);
170: }
171:
172: /**
173: * Returns the user language "en" (english).
174: */
175: public String getLanguages() {
176: return "en";
177: }
178:
179: /**
180: * Returns this user agent's CSS media.
181: */
182: public String getMedia() {
183: return "all";
184: }
185:
186: /**
187: * Returns this user agent's alternate style-sheet title.
188: */
189: public String getAlternateStyleSheet() {
190: return null;
191: }
192:
193: /**
194: * Returns the user stylesheet
195: */
196: public String getUserStyleSheetURI() {
197: return null;
198: }
199:
200: /**
201: * Returns the XML parser to use
202: */
203: public String getXMLParserClassName() {
204: return XMLResourceDescriptor.getXMLParserClassName();
205: }
206:
207: /**
208: * Returns <tt>false</tt>. The XML parser is not in validation mode.
209: */
210: public boolean isXMLParserValidating() {
211: return false;
212: }
213:
214: /**
215: * Unsupported operation.
216: */
217: public EventDispatcher getEventDispatcher() {
218: return null;
219: }
220:
221: /**
222: * Unsupported operation.
223: */
224: public void openLink(SVGAElement elt) {
225: }
226:
227: /**
228: * Unsupported operation.
229: */
230: public void setSVGCursor(Cursor cursor) {
231: }
232:
233: /**
234: * This user agent doesn't display text selections.
235: */
236: public void setTextSelection(Mark start, Mark end) {
237: }
238:
239: /**
240: * This user agent doesn't display text selections so nothing to clear.
241: */
242: public void deselectAll() {
243: }
244:
245: /**
246: * Unsupported operation.
247: */
248: public void runThread(Thread t) {
249: }
250:
251: /**
252: * Unsupported operation.
253: */
254: public AffineTransform getTransform() {
255: return null;
256: }
257:
258: /**
259: * Unsupported operation.
260: */
261: public void setTransform(AffineTransform at) {
262: // Do nothing.
263: }
264:
265: /**
266: * Unsupported operation.
267: */
268: public Point getClientAreaLocationOnScreen() {
269: return new Point();
270: }
271:
272: /**
273: * Tells whether the given feature is supported by this
274: * user agent.
275: */
276: public boolean hasFeature(String s) {
277: return FEATURES.contains(s);
278: }
279:
280: /**
281: * Tells whether the given extension is supported by this
282: * user agent.
283: */
284: public boolean supportExtension(String s) {
285: return extensions.contains(s);
286: }
287:
288: /**
289: * Lets the bridge tell the user agent that the following
290: * ex tension is supported by the bridge.
291: */
292: public void registerExtension(BridgeExtension ext) {
293: Iterator i = ext.getImplementedExtensions();
294: while (i.hasNext())
295: extensions.add(i.next());
296: }
297:
298: /**
299: * Notifies the UserAgent that the input element
300: * has been found in the document. This is sometimes
301: * called, for example, to handle <a> or
302: * <title> elements in a UserAgent-dependant
303: * way.
304: */
305: public void handleElement(Element elt, Object data) {
306: }
307:
308: /**
309: * Returns the security settings for the given script
310: * type, script url and document url
311: *
312: * @param scriptType type of script, as found in the
313: * type attribute of the <script> element.
314: * @param scriptURL url for the script, as defined in
315: * the script's xlink:href attribute. If that
316: * attribute was empty, then this parameter should
317: * be null
318: * @param docURL url for the document into which the
319: * script was found.
320: */
321: public ScriptSecurity getScriptSecurity(String scriptType,
322: ParsedURL scriptURL, ParsedURL docURL) {
323: return new DefaultScriptSecurity(scriptType, scriptURL, docURL);
324: }
325:
326: /**
327: * This method throws a SecurityException if the script
328: * of given type, found at url and referenced from docURL
329: * should not be loaded.
330: *
331: * This is a convenience method to call checkLoadScript
332: * on the ScriptSecurity strategy returned by
333: * getScriptSecurity.
334: *
335: * @param scriptType type of script, as found in the
336: * type attribute of the <script> element.
337: * @param scriptURL url for the script, as defined in
338: * the script's xlink:href attribute. If that
339: * attribute was empty, then this parameter should
340: * be null
341: * @param docURL url for the document into which the
342: * script was found.
343: */
344: public void checkLoadScript(String scriptType, ParsedURL scriptURL,
345: ParsedURL docURL) throws SecurityException {
346: ScriptSecurity s = getScriptSecurity(scriptType, scriptURL,
347: docURL);
348: if (s != null) {
349: s.checkLoadScript();
350: }
351: }
352:
353: /**
354: * Returns the security settings for the given resource
355: * url and document url
356: *
357: * @param resourceURL url for the resource, as defined in
358: * the resource's xlink:href attribute. If that
359: * attribute was empty, then this parameter should
360: * be null
361: * @param docURL url for the document into which the
362: * resource was found.
363: */
364: public ExternalResourceSecurity getExternalResourceSecurity(
365: ParsedURL resourceURL, ParsedURL docURL) {
366: return new RelaxedExternalResourceSecurity(resourceURL, docURL);
367: }
368:
369: /**
370: * This method throws a SecurityException if the resource
371: * found at url and referenced from docURL
372: * should not be loaded.
373: *
374: * This is a convenience method to call checkLoadExternalResource
375: * on the ExternalResourceSecurity strategy returned by
376: * getExternalResourceSecurity.
377: *
378: * @param resourceURL url for the resource, as defined in
379: * the resource's xlink:href attribute. If that
380: * attribute was empty, then this parameter should
381: * be null
382: * @param docURL url for the document into which the
383: * resource was found.
384: */
385: public void checkLoadExternalResource(ParsedURL resourceURL,
386: ParsedURL docURL) throws SecurityException {
387: ExternalResourceSecurity s = getExternalResourceSecurity(
388: resourceURL, docURL);
389:
390: if (s != null) {
391: s.checkLoadExternalResource();
392: }
393: }
394:
395: /**
396: * Returns a lighter font-weight.
397: */
398: public static float getStandardLighterFontWeight(float f) {
399: // Round f to nearest 100...
400: int weight = ((int) ((f + 50) / 100)) * 100;
401: switch (weight) {
402: case 100:
403: return 100;
404: case 200:
405: return 100;
406: case 300:
407: return 200;
408: case 400:
409: return 300;
410: case 500:
411: return 400;
412: case 600:
413: return 400;
414: case 700:
415: return 400;
416: case 800:
417: return 400;
418: case 900:
419: return 400;
420: default:
421: throw new IllegalArgumentException("Bad Font Weight: " + f);
422: }
423: }
424:
425: /**
426: * Returns a bolder font-weight.
427: */
428: public static float getStandardBolderFontWeight(float f) {
429: // Round f to nearest 100...
430: int weight = ((int) ((f + 50) / 100)) * 100;
431: switch (weight) {
432: case 100:
433: return 600;
434: case 200:
435: return 600;
436: case 300:
437: return 600;
438: case 400:
439: return 600;
440: case 500:
441: return 600;
442: case 600:
443: return 700;
444: case 700:
445: return 800;
446: case 800:
447: return 900;
448: case 900:
449: return 900;
450: default:
451: throw new IllegalArgumentException("Bad Font Weight: " + f);
452: }
453: }
454:
455: /**
456: * This Implementation simply throws a BridgeException.
457: *
458: * @param e The <image> element that can't be loaded.
459: * @param url The resolved url that can't be loaded.
460: * @param message As best as can be determined the reason it can't be
461: * loaded (not available, corrupt, unknown format,...).
462: */
463: public SVGDocument getBrokenLinkDocument(Element e, String url,
464: String message) {
465: throw new BridgeException(ctx, e,
466: ErrorConstants.ERR_URI_IMAGE_BROKEN, new Object[] {
467: url, message });
468: }
469: }
|