KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2000-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 org.apache.batik.gvt.CompositeGraphicsNode;
21 import org.apache.batik.gvt.GraphicsNode;
22 import org.apache.batik.gvt.RootGraphicsNode;
23 import org.apache.batik.util.HaltingThread;
24 import org.apache.batik.util.SVGConstants;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  * This class is responsible for creating a GVT tree using an SVG DOM tree.
32  *
33  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
34  * @version $Id: GVTBuilder.java,v 1.30 2005/03/27 08:58:30 cam Exp $
35  */

36 public class GVTBuilder implements SVGConstants {
37
38     /**
39      * Constructs a new builder.
40      */

41     public GVTBuilder() { }
42
43     /**
44      * Builds using the specified bridge context the specified SVG document.
45      *
46      * @param ctx the bridge context
47      * @param document the SVG document to build
48      * @exception BridgeException if an error occured while constructing
49      * the GVT tree
50      */

51     public GraphicsNode build(BridgeContext ctx, Document JavaDoc document) {
52         // the bridge context is now associated to one document
53
ctx.setDocument(document);
54         ctx.initializeDocument(document);
55
56         // inform the bridge context the builder to use
57
ctx.setGVTBuilder(this);
58
59         // build the GVT tree
60
RootGraphicsNode rootNode = new RootGraphicsNode();
61         Element JavaDoc svgElement = document.getDocumentElement();
62         GraphicsNode topNode = null;
63         try {
64             // get the appropriate bridge according to the specified element
65
Bridge bridge = ctx.getBridge(svgElement);
66             if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) {
67                 return null;
68             }
69             // create the associated composite graphics node
70
GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge;
71             topNode = gnBridge.createGraphicsNode(ctx, svgElement);
72             if (topNode == null) {
73                 return null;
74             }
75             rootNode.getChildren().add(topNode);
76
77             buildComposite(ctx, svgElement, (CompositeGraphicsNode)topNode);
78             gnBridge.buildGraphicsNode(ctx, svgElement, topNode);
79         } catch (BridgeException ex) {
80             // update the exception with the missing parameters
81
ex.setGraphicsNode(rootNode);
82             Element JavaDoc errElement = ex.getElement();
83             ex.setLineNumber(ctx.getDocumentLoader().getLineNumber(errElement));
84             //ex.printStackTrace();
85
throw ex; // re-throw the udpated exception
86
}
87
88         // For cursor handling
89
if (ctx.isInteractive()) {
90             ctx.addUIEventListeners(document);
91
92             // register GVT listeners for AWT event support
93
BridgeEventSupport.addGVTListener(ctx, document);
94         }
95
96         // <!> FIXME: TO BE REMOVED
97
if (ctx.isDynamic()) {
98             // register DOM listeners for dynamic support
99
ctx.addDOMListeners();
100         }
101         return rootNode;
102     }
103
104     /**
105      * Builds using the specified bridge context the specified Element.
106      *
107      * @param ctx the bridge context
108      * @param e the element to build
109      * @exception BridgeException if an error occured while constructing
110      * the GVT tree
111      */

112     public GraphicsNode build(BridgeContext ctx, Element JavaDoc e) {
113         // get the appropriate bridge according to the specified element
114
Bridge bridge = ctx.getBridge(e);
115         if (bridge instanceof GenericBridge) {
116             // If it is a GenericBridge just handle it and return.
117
((GenericBridge) bridge).handleElement(ctx, e);
118             return null;
119         } else if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) {
120             return null;
121         }
122         // create the associated graphics node
123
GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge;
124         // check the display property
125
if (!gnBridge.getDisplay(e)) {
126             return null;
127         }
128         GraphicsNode gn = gnBridge.createGraphicsNode(ctx, e);
129         if (gn != null) {
130             if (gnBridge.isComposite()) {
131                 buildComposite(ctx, e, (CompositeGraphicsNode)gn);
132             } else {
133                 handleGenericBridges(ctx, e);
134             }
135             gnBridge.buildGraphicsNode(ctx, e, gn);
136         }
137         // <!> FIXME: see build(BridgeContext, Element)
138
// + may load the script twice (for example
139
// outside 'use' is ok versus local 'use' maybe wrong).
140
if (ctx.isDynamic()) {
141             //BridgeEventSupport.loadScripts(ctx, e);
142
}
143         return gn;
144     }
145
146     /**
147      * Builds a composite Element.
148      *
149      * @param ctx the bridge context
150      * @param e the element to build
151      * @param parentNode the composite graphics node, parent of the
152      * graphics node to build
153      * @exception BridgeException if an error occured while constructing
154      * the GVT tree
155      */

156     protected void buildComposite(BridgeContext ctx,
157                                   Element JavaDoc e,
158                                   CompositeGraphicsNode parentNode) {
159         for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
160             if (n.getNodeType() == Node.ELEMENT_NODE) {
161                 buildGraphicsNode(ctx, (Element JavaDoc)n, parentNode);
162             }
163         }
164     }
165
166     /**
167      * Builds a 'leaf' Element.
168      *
169      * @param ctx the bridge context
170      * @param e the element to build
171      * @param parentNode the composite graphics node, parent of the
172      * graphics node to build
173      * @exception BridgeException if an error occured while constructing
174      * the GVT tree
175      */

176     protected void buildGraphicsNode(BridgeContext ctx,
177                                      Element JavaDoc e,
178                                      CompositeGraphicsNode parentNode) {
179         // Check If we should halt early.
180
if (HaltingThread.hasBeenHalted()) {
181             throw new InterruptedBridgeException();
182         }
183         // get the appropriate bridge according to the specified element
184
Bridge bridge = ctx.getBridge(e);
185         if (bridge instanceof GenericBridge) {
186             // If it is a GenericBridge just handle it and return.
187
((GenericBridge) bridge).handleElement(ctx, e);
188             return;
189         } else if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) {
190             return;
191         }
192         // check the display property
193
if (!CSSUtilities.convertDisplay(e)) {
194             return;
195         }
196         GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge;
197         try {
198             // create the associated graphics node
199
GraphicsNode gn = gnBridge.createGraphicsNode(ctx, e);
200             if (gn != null) {
201                 // attach the graphics node to the GVT tree now !
202
parentNode.getChildren().add(gn);
203                 // check if the element has children to build
204
if (gnBridge.isComposite()) {
205                     buildComposite(ctx, e, (CompositeGraphicsNode)gn);
206                 } else {
207                     // if not then still handle the GenericBridges
208
handleGenericBridges(ctx, e);
209                 }
210                 gnBridge.buildGraphicsNode(ctx, e, gn);
211             }
212         } catch (BridgeException ex) {
213             // some bridge may decide that the node in error can be
214
// displayed (e.g. polyline, path...)
215
// In this case, the exception contains the GraphicsNode
216
GraphicsNode errNode = ex.getGraphicsNode();
217             if (errNode != null) {
218                 parentNode.getChildren().add(errNode);
219                 gnBridge.buildGraphicsNode(ctx, e, errNode);
220                 ex.setGraphicsNode(null);
221             }
222             //ex.printStackTrace();
223
throw ex;
224         }
225     }
226
227     /**
228      * Handles any GenericBridge elements which are children of the
229      * specified element.
230      * @param ctx the bridge context
231      * @param e the element whose child elements should be handled
232      */

233     protected void handleGenericBridges(BridgeContext ctx, Element JavaDoc e) {
234         for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
235             if (n instanceof Element JavaDoc) {
236                 Element JavaDoc e2 = (Element JavaDoc) n;
237                 Bridge b = ctx.getBridge(e2);
238                 if (b instanceof GenericBridge) {
239                     ((GenericBridge) b).handleElement(ctx, e2);
240                 }
241             }
242         }
243     }
244 }
245
Popular Tags