1 18 package org.apache.batik.svggen; 19 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Vector ; 25 26 import org.apache.batik.ext.awt.g2d.GraphicContext; 27 import org.w3c.dom.Comment ; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.NodeList ; 31 32 62 public class DOMTreeManager implements SVGSyntax, ErrorConstants { 63 67 int maxGCOverrides; 68 69 73 protected Vector groupManagers = new Vector (); 74 75 79 protected List genericDefSet = new LinkedList (); 80 81 84 SVGGraphicContext defaultGC; 85 86 89 protected Element topLevelGroup; 90 91 96 SVGGraphicContextConverter gcConverter; 97 98 102 protected SVGGeneratorContext generatorContext; 103 104 108 protected SVGBufferedImageOp filterConverter; 109 110 113 protected List otherDefs; 114 115 122 public DOMTreeManager(GraphicContext gc, 123 SVGGeneratorContext generatorContext, 124 int maxGCOverrides){ 125 if (gc == null) 126 throw new SVGGraphics2DRuntimeException(ERR_GC_NULL); 127 128 if (maxGCOverrides <= 0) 129 throw new SVGGraphics2DRuntimeException(ERR_MAXGCOVERRIDES_OUTOFRANGE); 130 131 if (generatorContext == null) 132 throw new SVGGraphics2DRuntimeException(ERR_CONTEXT_NULL); 133 134 this.generatorContext = generatorContext; 135 this.maxGCOverrides = maxGCOverrides; 136 137 recycleTopLevelGroup(); 139 140 defaultGC = gcConverter.toSVG(gc); 142 } 143 144 148 public void addGroupManager(DOMGroupManager groupManager){ 149 if(groupManager != null) 150 groupManagers.addElement(groupManager); 151 } 152 153 157 public void removeGroupManager(DOMGroupManager groupManager){ 158 if(groupManager != null) 159 groupManagers.removeElement(groupManager); 160 } 161 162 170 public void appendGroup(Element group, DOMGroupManager groupManager){ 171 topLevelGroup.appendChild(group); 172 int nManagers = groupManagers.size(); 173 for(int i=0; i<nManagers; i++){ 174 DOMGroupManager gm = (DOMGroupManager)groupManagers.elementAt(i); 175 if( gm != groupManager ) 176 gm.recycleCurrentGroup(); 177 } 178 } 179 180 183 protected void recycleTopLevelGroup(){ 184 recycleTopLevelGroup(true); 185 } 186 187 188 191 protected void recycleTopLevelGroup(boolean recycleConverters){ 192 int nManagers = groupManagers.size(); 194 for(int i=0; i<nManagers; i++){ 195 DOMGroupManager gm = (DOMGroupManager)groupManagers.elementAt(i); 196 gm.recycleCurrentGroup(); 197 } 198 199 topLevelGroup = generatorContext.domFactory. 201 createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG); 202 203 if (recycleConverters) { 205 filterConverter = 206 new SVGBufferedImageOp(generatorContext); 207 gcConverter = 208 new SVGGraphicContextConverter(generatorContext); 209 } 210 } 211 212 217 public void setTopLevelGroup(Element topLevelGroup){ 218 if(topLevelGroup == null) 219 throw new SVGGraphics2DRuntimeException(ERR_TOP_LEVEL_GROUP_NULL); 220 221 if(!SVG_G_TAG.equalsIgnoreCase(topLevelGroup.getTagName())) 222 throw new SVGGraphics2DRuntimeException(ERR_TOP_LEVEL_GROUP_NOT_G); 223 224 recycleTopLevelGroup(false); 225 this.topLevelGroup = topLevelGroup; 226 } 227 228 232 public Element getRoot(){ 233 return getRoot(null); 234 } 235 236 240 public Element getRoot(Element svgElement){ 241 Element svg = svgElement; 242 243 if (svg == null) { 244 svg = generatorContext.domFactory. 245 createElementNS(SVG_NAMESPACE_URI, SVG_SVG_TAG); 246 } 247 248 if (gcConverter.getCompositeConverter(). 250 getAlphaCompositeConverter().requiresBackgroundAccess()) 251 svg.setAttributeNS 252 (null, SVG_ENABLE_BACKGROUND_ATTRIBUTE, SVG_NEW_VALUE); 253 254 if (generatorContext.generatorComment != null) { 255 Comment generatorComment = generatorContext.domFactory. 256 createComment(generatorContext.generatorComment); 257 svg.appendChild(generatorComment); 258 } 259 260 applyDefaultRenderingStyle(svg); 262 263 svg.appendChild(getGenericDefinitions()); 264 svg.appendChild(getTopLevelGroup()); 265 266 return svg; 267 } 268 269 public void applyDefaultRenderingStyle(Element element) { 270 Map groupDefaults = defaultGC.getGroupContext(); 271 generatorContext.styleHandler.setStyle(element, groupDefaults, generatorContext); 272 } 273 274 278 public Element getGenericDefinitions() { 279 Element genericDefs = 282 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 283 SVG_DEFS_TAG); 284 Iterator iter = genericDefSet.iterator(); 285 while (iter.hasNext()) { 286 genericDefs.appendChild((Element )iter.next()); 287 } 288 289 genericDefs.setAttributeNS(null, ATTR_ID, ID_PREFIX_GENERIC_DEFS); 290 return genericDefs; 291 } 292 293 296 public ExtensionHandler getExtensionHandler(){ 297 return generatorContext.getExtensionHandler(); 298 } 299 300 305 void setExtensionHandler(ExtensionHandler extensionHandler) { 306 generatorContext.setExtensionHandler(extensionHandler); 307 } 308 309 314 public List getDefinitionSet(){ 315 List defSet = gcConverter.getDefinitionSet(); 320 defSet.removeAll(genericDefSet); 321 defSet.addAll(filterConverter.getDefinitionSet()); 322 if (otherDefs != null){ 323 defSet.addAll(otherDefs); 324 otherDefs = null; 325 } 326 327 filterConverter = new SVGBufferedImageOp(generatorContext); 329 gcConverter = new SVGGraphicContextConverter(generatorContext); 330 331 return defSet; 332 } 333 334 338 public void addOtherDef(Element definition){ 339 if (otherDefs == null){ 340 otherDefs = new LinkedList (); 341 } 342 343 otherDefs.add(definition); 344 } 345 346 353 public Element getTopLevelGroup(){ 354 boolean includeDefinitionSet = true; 355 return getTopLevelGroup(includeDefinitionSet); 356 } 357 358 368 public Element getTopLevelGroup(boolean includeDefinitionSet){ 369 Element topLevelGroup = this.topLevelGroup; 370 371 if(includeDefinitionSet){ 375 List defSet = getDefinitionSet(); 376 if(defSet.size() > 0){ 377 Element defElement = null; 378 379 NodeList defsElements = 380 topLevelGroup.getElementsByTagName(SVG_DEFS_TAG); 381 if (defsElements.getLength() > 0) 382 defElement = (Element )defsElements.item(0); 383 384 if (defElement == null) { 385 defElement = 386 generatorContext.domFactory. 387 createElementNS(SVG_NAMESPACE_URI, 388 SVG_DEFS_TAG); 389 defElement. 390 setAttributeNS(null, ATTR_ID, 391 generatorContext.idGenerator. 392 generateID(ID_PREFIX_DEFS)); 393 topLevelGroup.insertBefore(defElement, 394 topLevelGroup.getFirstChild()); 395 } 396 397 Iterator iter = defSet.iterator(); 398 while(iter.hasNext()) 399 defElement.appendChild((Element )iter.next()); 400 } 401 } 402 403 recycleTopLevelGroup(false); 407 408 return topLevelGroup; 409 } 410 411 public SVGBufferedImageOp getFilterConverter() { 412 return filterConverter; 413 } 414 415 public SVGGraphicContextConverter getGraphicContextConverter() { 416 return gcConverter; 417 } 418 419 SVGGeneratorContext getGeneratorContext() { 420 return generatorContext; 421 } 422 423 Document getDOMFactory() { 424 return generatorContext.domFactory; 425 } 426 427 StyleHandler getStyleHandler() { 428 return generatorContext.styleHandler; 429 } 430 } 431 | Popular Tags |