KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001-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.bridge;
19
20 import java.net.URL JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.batik.dom.util.DOMUtilities;
25 import org.apache.batik.ext.awt.image.renderable.Filter;
26 import org.apache.batik.ext.awt.image.spi.DefaultBrokenLinkProvider;
27 import org.apache.batik.gvt.GraphicsNode;
28 import org.apache.batik.gvt.filter.GraphicsNodeRable8Bit;
29 import org.apache.batik.util.SVGConstants;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.svg.SVGDocument;
33 /**
34  * This interface is to be used to provide alternate ways of
35  * generating a placeholder image when the ImageTagRegistry
36  * fails to handle a given reference.
37  */

38 public class SVGBrokenLinkProvider
39     extends DefaultBrokenLinkProvider
40     implements ErrorConstants {
41
42     public final static String JavaDoc SVG_BROKEN_LINK_DOCUMENT_PROPERTY =
43         "org.apache.batik.bridge.BrokenLinkDocument";
44
45     UserAgent userAgent;
46     DocumentLoader loader;
47     BridgeContext ctx;
48     GraphicsNode gvtRoot = null;
49     SVGDocument svgDoc;
50     
51     public SVGBrokenLinkProvider() {
52         userAgent = new UserAgentAdapter();
53         loader = new DocumentLoader(userAgent);
54         ctx = new BridgeContext(userAgent, loader);
55
56         Class JavaDoc cls = SVGBrokenLinkProvider.class;
57         URL JavaDoc blURL = cls.getResource("BrokenLink.svg");
58         if (blURL == null) return;
59
60         GVTBuilder builder = new GVTBuilder();
61         try {
62             svgDoc = (SVGDocument)loader.loadDocument(blURL.toString());
63             gvtRoot = builder.build(ctx, svgDoc);
64         } catch (Exception JavaDoc ex) {
65             // t.printStackTrace();
66
}
67     }
68
69     /**
70      * This method is responsible for constructing an image that will
71      * represent the missing image in the document. This method
72      * recives information about the reason a broken link image is
73      * being requested in the <tt>code</tt> and <tt>params</tt>
74      * parameters. These parameters may be used to generate nicely
75      * localized messages for insertion into the broken link image, or
76      * for selecting the broken link image returned.
77      *
78      * @param code This is the reason the image is unavailable should
79      * be taken from ErrorConstants.
80      * @param params This is more detailed information about
81      * the circumstances of the failure. */

82     public Filter getBrokenLinkImage(Object JavaDoc base, String JavaDoc code,
83                                      Object JavaDoc[] params) {
84         if (gvtRoot == null)
85             return null;
86
87         String JavaDoc message = formatMessage(base, code, params);
88         Document JavaDoc doc = getBrokenLinkDocument(message);
89         Map JavaDoc props = new HashMap JavaDoc();
90         props.put(BROKEN_LINK_PROPERTY, message);
91         props.put(SVG_BROKEN_LINK_DOCUMENT_PROPERTY, doc);
92         
93         return new GraphicsNodeRable8Bit(gvtRoot, props);
94     }
95
96     public SVGDocument getBrokenLinkDocument(Object JavaDoc base,
97                                           String JavaDoc code, Object JavaDoc [] params) {
98         String JavaDoc message = formatMessage(base, code, params);
99         return getBrokenLinkDocument(message);
100     }
101
102     public SVGDocument getBrokenLinkDocument(String JavaDoc message) {
103         SVGDocument doc = (SVGDocument)DOMUtilities.deepCloneDocument
104             (svgDoc, svgDoc.getImplementation());
105         Element JavaDoc infoE = doc.getElementById("__More_About");
106         Element JavaDoc title = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI,
107                                             SVGConstants.SVG_TITLE_TAG);
108         title.appendChild(doc.createTextNode
109                           (Messages.formatMessage
110                            (MSG_BROKEN_LINK_TITLE, null)));
111         Element JavaDoc desc = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI,
112                                            SVGConstants.SVG_DESC_TAG);
113         desc.appendChild(doc.createTextNode(message));
114         infoE.insertBefore(desc, infoE.getFirstChild());
115         infoE.insertBefore(title, desc);
116         return doc;
117     }
118 }
119
Popular Tags