Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 
  */
 package org.apache.batik.svggen;
 
 import java.util.List;
 import java.util.Map;
 
This class is used by the SVGGraphics2D SVG Generator to manage addition of new Nodes to the SVG DOM Tree. This class handles a set of DOMGroupManager objects that can all append to the top level group managed by this class. This allows multiple SVGGraphics2D instances, created from the same SVGGraphics2D through the various create methods, to append to the same SVG document and keep the rendering order correct. The root node managed by this DOMTreeManager contains two children: a top level group node and a top level defs node. The top level defs node contains the definition of common SVG entities such as the various AlphaComposite rules. Note that other defs can also be created under the top level group, for example to represent gradient or pattern paints.
[svg] | +-- [defs] Contain generic definitions +-- [g] Top level group | +-- [defs] Contains definitions specific to rendering +-- [g] Group 1 +-- ... +-- [g] Group n

Author(s):
Christophe Jolif
Vincent Hardy
Version:
$Id: DOMTreeManager.java 522302 2007-03-25 17:04:48Z dvholten $
 
 public class DOMTreeManager implements SVGSyntaxErrorConstants {

    
Maximum of Graphic Context attributes overrides in children of the current group.
 
     int maxGCOverrides;

    
Set of group managers that build groups for this manager. The synchronizedList is part of the fix for bug #40686
 
     protected final List groupManagers = Collections.synchronizedListnew ArrayList() );

    
Set of definitions that are to be placed at the top of the document tree
 
     protected List genericDefSet = new LinkedList();

    
Default SVG GraphicContext state
 
     SVGGraphicContext defaultGC;

    
Top level group
 
     protected Element topLevelGroup;

    
Used to convert the Java 2D API graphic context state into the SVG equivalent set of attributes and related definitions
 
The context that stores the domFactory, the imageHandler and the extensionHandler.
    protected SVGGeneratorContext generatorContext;

    
Converters used bVy this object to translate graphic context attributes
    protected SVGBufferedImageOp filterConverter;

    
Set of definitions which can be used by custom extensions
    protected List otherDefs;

    
Constructor

Parameters:
gc default graphic context state
generatorContext the SVG generator context
maxGCOverrides defines how many overrides are allowed in children nodes of the current group.
    public DOMTreeManager(GraphicContext gc,
                          SVGGeneratorContext generatorContext,
                          int maxGCOverrides){
        if (gc == null)
            throw new SVGGraphics2DRuntimeException();
        if (maxGCOverrides <= 0)
        if (generatorContext == null)
            throw new SVGGraphics2DRuntimeException();
        this. = generatorContext;
        this. = maxGCOverrides;
        // Start with a new Top Level Group
        recycleTopLevelGroup();
        // Build the default GC descriptor
         = .toSVG(gc);
    }

    

Parameters:
groupManager new DOMGroupManager to add to the list of managers that collaborate with this tree manager.
    public void addGroupManager(DOMGroupManager groupManager){
        if(groupManager != null)
            .add(groupManager);
    }

    

Parameters:
groupManager DOMGroupManager to remove from the list of managers that collaborate with this tree manager
    public void removeGroupManager(DOMGroupManager groupManager){
        if(groupManager != null)
            .removegroupManager );
    }

    
When a group is appended to the tree by this call, all the other group managers are requested to start new groups, in order to preserve the Z-order.

Parameters:
group new group to be appended to the topLevelGroup
groupManager DOMTreeManager that produced the group.
    public void appendGroup(Element groupDOMGroupManager groupManager){
        .appendChild(group);
        synchronized ){
            // we want to prevent that the groupManagers-list changes while
            // we iterate over it. If that would happen, we might skip entries
            // within the list or ignore new entries at the end. Fix #40686
            int nManagers = .size();
            for(int i=0; i<nManagersi++){
                DOMGroupManager gm = (DOMGroupManager).get(i);
                ifgm != groupManager )
                    gm.recycleCurrentGroup();
            }
        }
    }

    
Reset the state of this object to handler a new topLevelGroup
    protected void recycleTopLevelGroup(){
        recycleTopLevelGroup(true);
    }


    
Reset the state of this object to handler a new topLevelGroup
    protected void recycleTopLevelGroup(boolean recycleConverters){
        // First, recycle group managers
        synchronized ){
            // we want to prevent that the groupManagers-list changes while
            // we iterate over it. If that would happen, we might skip entries
            // within the list or ignore new entries at the end. Fix #40686
            int nManagers = .size();
            for(int i=0; i<nManagersi++){
                DOMGroupManager gm = (DOMGroupManager).get(i);
                gm.recycleCurrentGroup();
            }
        }
        // Create top level group node
            createElementNS();
        // Build new converters
        if (recycleConverters) {
             =
                new SVGBufferedImageOp();
             =
                new SVGGraphicContextConverter();
        }
    }

    
Sets the topLevelGroup to the input element. This will throw an exception if the input element is not of type 'g' or if it is null.
    public void setTopLevelGroup(Element topLevelGroup){
        if(topLevelGroup == null)
            throw new SVGGraphics2DRuntimeException();
        if(!.equalsIgnoreCase(topLevelGroup.getTagName()))
            throw new SVGGraphics2DRuntimeException();
        recycleTopLevelGroup(false);
        this. = topLevelGroup;
    }

    
Returns the root element with the generic definitions and the topLevelGroup.
    public Element getRoot(){
        return getRoot(null);
    }

    
Returns the root element with the generic definitions and the topLevelGroup.
    public Element getRoot(Element svgElement){
        Element svg = svgElement;
        if (svg == null) {
            svg = ..
                createElementNS();
        }
        // Enable background if required by AlphaComposite convertion
        if (.getCompositeConverter().
            svg.setAttributeNS
                (null);
        if (. != null) {
            Comment generatorComment = ..
                createComment(.);
            svg.appendChild(generatorComment);
        }
        // Set default rendering context attributes in node
        applyDefaultRenderingStyle(svg);
        svg.appendChild(getGenericDefinitions());
        svg.appendChild(getTopLevelGroup());
        return svg;
    }
    public void applyDefaultRenderingStyle(Element element) {
        Map groupDefaults = .getGroupContext();
        ..setStyle(elementgroupDefaults);
    }

    

Returns:
a defs element that contains all the generic definitions
    public Element getGenericDefinitions() {
        // when called several times, this will create several generic
        // definition elements... not sure it is desired behavior...
        Element genericDefs =
                                                        );
        Iterator iter = .iterator();
        while (iter.hasNext()) {
            genericDefs.appendChild((Element)iter.next());
        }
        genericDefs.setAttributeNS(null);
        return genericDefs;
    }

    

Returns:
the extension handler used by the DOMTreeManager.
        return .getExtensionHandler();
    }

    
This will change the extension handler on the SVGGeneratorContext.

Parameters:
extensionHandler new extension handler this object should use
    void setExtensionHandler(ExtensionHandler extensionHandler) {
        .setExtensionHandler(extensionHandler);
    }

    
Invoking this method will return a set of definition element that contain all the definitions referenced by the attributes generated by the various converters. This also resets the converters.
    public List getDefinitionSet(){
        //
        // The definition set contains all the definitions minus
        // any definition that has been placed in the generic definition set
        //
        List defSet = .getDefinitionSet();
        defSet.removeAll();
        defSet.addAll(.getDefinitionSet());
        if ( != null){
            defSet.addAll();
             = null;
        }
        // Build new converters
        return defSet;
    }

    
Lets custom implementations for various extensions add elements to the <defs> sections.
    public void addOtherDef(Element definition){
        if ( == null){
             = new LinkedList();
        }
        .add(definition);
    }

    
Invoking this method will return a reference to the topLevelGroup Element managed by this object. It will also cause this object to start working with a new topLevelGroup.

Returns:
top level group
    public Element getTopLevelGroup(){
        boolean includeDefinitionSet = true;
        return getTopLevelGroup(includeDefinitionSet);
    }

    
Invoking this method will return a reference to the topLevelGroup Element managed by this object. It will also cause this object to start working with a new topLevelGroup.

Parameters:
includeDefinitionSet if true, the definition set is included and the converters are reset (i.e., they start with an empty set of definitions).
Returns:
top level group
    public Element getTopLevelGroup(boolean includeDefinitionSet){
        Element topLevelGroup = this.;
        //
        // Include definition set if requested
        //
        if(includeDefinitionSet){
            List defSet = getDefinitionSet();
            if(defSet.size() > 0){
                Element defElement = null;
                NodeList defsElements =
                    topLevelGroup.getElementsByTagName();
                if (defsElements.getLength() > 0)
                    defElement = (Element)defsElements.item(0);
                if (defElement == null) {
                    defElement =
                        ..
                        createElementNS(,
                                        );
                    defElement.
                        setAttributeNS(null,
                                       ..
                                       generateID());
                    topLevelGroup.insertBefore(defElement,
                                               topLevelGroup.getFirstChild());
                }
                Iterator iter = defSet.iterator();
                while(iter.hasNext())
                    defElement.appendChild((Element)iter.next());
            }
        }
        // If the definition set is included, then the converters have already
        // been recycled in getDefinitionSet. Otherwise, they should not be
        // recycled. So, in all cases, do not recycle the converters here.
        recycleTopLevelGroup(false);
        return topLevelGroup;
    }
        return ;
    }
        return ;
    }
        return ;
    }
        return .;
    }
        return .;
    }
New to GrepCode? Check out our FAQ X