KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.geom.Rectangle2D JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.batik.dom.svg.SVGOMDocument;
30 import org.apache.batik.dom.util.XLinkSupport;
31 import org.apache.batik.ext.awt.image.PadMode;
32 import org.apache.batik.ext.awt.image.renderable.Filter;
33 import org.apache.batik.ext.awt.image.renderable.FilterChainRable;
34 import org.apache.batik.ext.awt.image.renderable.FilterChainRable8Bit;
35 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
36 import org.apache.batik.gvt.GraphicsNode;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39
40 /**
41  * Bridge class for the <filter> element.
42  *
43  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
44  * @version $Id: SVGFilterElementBridge.java,v 1.20 2004/11/18 01:46:53 deweese Exp $
45  */

46 public class SVGFilterElementBridge extends AbstractSVGBridge
47     implements FilterBridge, ErrorConstants {
48
49     /**
50      * Constructs a new bridge for the &lt;filter> element.
51      */

52     public SVGFilterElementBridge() {}
53
54     /**
55      * Returns 'filter'.
56      */

57     public String JavaDoc getLocalName() {
58         return SVG_FILTER_TAG;
59     }
60
61     /**
62      * Creates a <tt>Filter</tt> according to the specified parameters.
63      *
64      * @param ctx the bridge context to use
65      * @param filterElement the element that defines the filter
66      * @param filteredElement the element that references the filter element
67      * @param filteredNode the graphics node to filter
68      */

69     public Filter createFilter(BridgeContext ctx,
70                                Element JavaDoc filterElement,
71                                Element JavaDoc filteredElement,
72                                GraphicsNode filteredNode) {
73
74         // get filter chain region
75
Rectangle2D JavaDoc filterRegion = SVGUtilities.convertFilterChainRegion
76             (filterElement, filteredElement, filteredNode, ctx);
77
78         // make the initial source as a RenderableImage
79
Filter sourceGraphic = filteredNode.getGraphicsNodeRable(true);
80         // Pad out to filterRegion
81
sourceGraphic = new PadRable8Bit(sourceGraphic, filterRegion,
82                                          PadMode.ZERO_PAD);
83
84         // build a FilterChainRable8Bit
85
FilterChainRable filterChain
86             = new FilterChainRable8Bit(sourceGraphic, filterRegion);
87
88         // 'filterRes' attribute - default is implementation specific
89
float [] filterRes = SVGUtilities.convertFilterRes(filterElement, ctx);
90         filterChain.setFilterResolutionX((int)filterRes[0]);
91         filterChain.setFilterResolutionY((int)filterRes[1]);
92
93         // Create a map for filter nodes to advertise themselves as
94
// named source
95
Map JavaDoc filterNodeMap = new HashMap JavaDoc(11);
96         filterNodeMap.put(SVG_SOURCE_GRAPHIC_VALUE, sourceGraphic);
97
98
99         Filter in = buildFilterPrimitives(filterElement,
100                                           filterRegion,
101                                           filteredElement,
102                                           filteredNode,
103                                           sourceGraphic,
104                                           filterNodeMap,
105                                           ctx);
106         if ((in == null) || (in == sourceGraphic)) {
107             return null; // no filter primitives found, disable the filter.
108
} else {
109             filterChain.setSource(in);
110             return filterChain;
111         }
112     }
113
114     /**
115      * Builds the filter primitives of filter chain of the specified
116      * filter element and returns the last filter primitive
117      * created. Filter primitives can be children of the filter or
118      * defined on one of its 'ancestor' (linked with the xlink:href
119      * attribute).
120      *
121      * @param filterElement the filter element
122      * @param filterRegion the filter chain region
123      * @param filteredElement the filtered element
124      * @param filteredNode the filtered node
125      * @param in the input Filter
126      * @param filterNodeMap the map used by named filter primitives
127      * @param ctx the bridge context
128      * @return the last filter primitive created
129      */

130     protected static Filter buildFilterPrimitives(Element JavaDoc filterElement,
131                                                   Rectangle2D JavaDoc filterRegion,
132                                                   Element JavaDoc filteredElement,
133                                                   GraphicsNode filteredNode,
134                                                   Filter in,
135                                                   Map JavaDoc filterNodeMap,
136                                                   BridgeContext ctx) {
137
138         List JavaDoc refs = new LinkedList JavaDoc();
139         for (;;) {
140             Filter newIn = buildLocalFilterPrimitives(filterElement,
141                                                       filterRegion,
142                                                       filteredElement,
143                                                       filteredNode,
144                                                       in,
145                                                       filterNodeMap,
146                                                       ctx);
147             if (newIn != in) {
148                 return newIn; // filter primitives found, exit
149
}
150             String JavaDoc uri = XLinkSupport.getXLinkHref(filterElement);
151             if (uri.length() == 0) {
152                 return in; // no xlink:href found, exit
153
}
154             // check if there is circular dependencies
155
SVGOMDocument doc = (SVGOMDocument)filterElement.getOwnerDocument();
156             URL JavaDoc url;
157             try {
158                 url = new URL JavaDoc(doc.getURLObject(), uri);
159             } catch (MalformedURLException JavaDoc ex) {
160                 throw new BridgeException(filterElement,
161                                           ERR_URI_MALFORMED,
162                                           new Object JavaDoc[] {uri});
163
164             }
165             if (contains(refs, url)) {
166                 throw new BridgeException(filterElement,
167                                           ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES,
168                                           new Object JavaDoc[] {uri});
169             }
170             refs.add(url);
171             filterElement = ctx.getReferencedElement(filterElement, uri);
172         }
173     }
174
175     /**
176      * Builds the filter primitives of filter chain of the specified
177      * filter element and returns the last filter primitive
178      * created or 'in' if no filter primitive has been specified.
179      *
180      * @param filterElement the filter element
181      * @param filterRegion the filter chain region
182      * @param filteredElement the filtered element
183      * @param filteredNode the filtered node
184      * @param in the input Filter
185      * @param filterNodeMap the map used by named filter primitives
186      * @param ctx the bridge context
187      * @return the last filter primitive created or 'in'
188      */

189     protected static
190         Filter buildLocalFilterPrimitives(Element JavaDoc filterElement,
191                                           Rectangle2D JavaDoc filterRegion,
192                                           Element JavaDoc filteredElement,
193                                           GraphicsNode filteredNode,
194                                           Filter in,
195                                           Map JavaDoc filterNodeMap,
196                                           BridgeContext ctx) {
197
198         for (Node n = filterElement.getFirstChild();
199              n != null;
200              n = n.getNextSibling()) {
201
202             if (n.getNodeType() != Node.ELEMENT_NODE) {
203                 continue; // skip node that is not an Element
204
}
205             Element JavaDoc e = (Element JavaDoc)n;
206             Bridge bridge = ctx.getBridge(e);
207             if (bridge == null || !(bridge instanceof FilterPrimitiveBridge)) {
208                 continue;
209             }
210             FilterPrimitiveBridge filterBridge = (FilterPrimitiveBridge)bridge;
211             Filter filterNode = filterBridge.createFilter(ctx,
212                                                           e,
213                                                           filteredElement,
214                                                           filteredNode,
215                                                           in,
216                                                           filterRegion,
217                                                           filterNodeMap);
218             if (filterNode == null) {
219                 return null; // disable the filter if a primitive is null
220
} else {
221                 in = filterNode;
222             }
223         }
224         return in;
225     }
226
227     /**
228      * Returns true if the specified list of URLs contains the specified url.
229      *
230      * @param urls the list of URLs
231      * @param key the url to search for
232      */

233     private static boolean contains(List JavaDoc urls, URL JavaDoc key) {
234         Iterator JavaDoc iter = urls.iterator();
235         while (iter.hasNext()) {
236             URL JavaDoc url = (URL JavaDoc)iter.next();
237             if (url.sameFile(key) && url.getRef().equals(key.getRef())) {
238                 return true;
239             }
240         }
241         return false;
242     }
243 }
244
Popular Tags