/*

   Copyright 2001,2003  The Apache Software Foundation 

   Licensed 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.Vector;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This utility class converts a standard SVG document that uses
 * attribute into one that uses the CSS style attribute instead
 *
 * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
 * @version $Id$
 */
public class SVGCSSStyler implements SVGSyntax{
    static private String CSS_PROPERTY_VALUE_SEPARATOR = ":";
    static private String CSS_RULE_SEPARATOR = ";";
    static private String SPACE = " ";

    /**
     * Invoking this method removes all the styling attributes
     * (such as 'fill' or 'fill-opacity') from the input element
     * and its descendant and replaces them with their CSS2
     * property counterparts.
     * @param node SVG Node to be converted to use style
     *
     */
    public static void style(Node node){
        NamedNodeMap attributes = node.getAttributes();
        if (attributes != null){
            // Has to be an Element, as it has attributes
            // According to spec.
            Element element = (Element)node;
            StringBuffer styleAttrBuffer = new StringBuffer();
            int nAttr = attributes.getLength();
            Vector toBeRemoved = new Vector();
            for(int i=0; i<nAttr; i++){
                Attr attr = (Attr)attributes.item(i);
                if(SVGStylingAttributes.set.contains(attr.getName())){
                    // System.out.println("Found new style attribute");
                    styleAttrBuffer.append(attr.getName());
                    styleAttrBuffer.append(CSS_PROPERTY_VALUE_SEPARATOR);
                    styleAttrBuffer.append(attr.getValue());
                    styleAttrBuffer.append(CSS_RULE_SEPARATOR);
                    styleAttrBuffer.append(SPACE);
                    toBeRemoved.addElement(attr.getName());
                }
            }

            if(styleAttrBuffer.length() > 0){
                                // System.out.println("Setting style attribute on node: " + styleAttrBuffer.toString().trim());
                                // There were some styling attributes
                element.setAttributeNS(null,
                                       SVG_STYLE_ATTRIBUTE,
                                       styleAttrBuffer.toString().trim());

                int n = toBeRemoved.size();
                for(int i=0; i<n; i++)
                    element.removeAttribute((String)toBeRemoved.elementAt(i));
            }
            // else
            // System.out.println("NO STYLE PROPERTIES");
        }

        // Now, process child elements
        NodeList children = node.getChildNodes();
        int nChildren = children.getLength();
        for(int i=0; i<nChildren; i++){
            Node child = children.item(i);
            style(child);
        }

    }
}

@@ -19,16 +19,21 @@
 001,2003
+,2006
   The Ap
@@ -56,17 +56,16 @@
 undation
- 
 %0A%0A   Lic
@@ -665,22 +665,48 @@
 va.util.
-Vector
+List;%0Aimport java.util.ArrayList
 ;%0A%0Aimpor
@@ -973,16 +973,17 @@
  instead
+.
 %0A *%0A * @
@@ -1123,33 +1123,38 @@
 ax%7B%0A
+%0A
     
-static private String
+private static final char
  CSS
@@ -1185,38 +1185,42 @@
 R = 
-%22:%22
+':'
 ;%0A    
-static private String
+private static final char
  CSS
@@ -1241,32 +1241,25 @@
 R = 
-%22;%22
+';'
 ;%0A    
-static 
 private 
 Stri
@@ -1254,22 +1254,33 @@
 private 
-String
+static final char
  SPACE =
@@ -1284,11 +1284,11 @@
 E = 
-%22 %22
+' '
 ;%0A%0A 
@@ -1512,16 +1512,23 @@
 rparts.%0A
+     *%0A
      * @
@@ -1576,23 +1576,16 @@
 e style%0A
-     *%0A
      */%0A
@@ -1974,22 +1974,20 @@
         
-Vector
+List
  toBeRem
@@ -2001,14 +2001,17 @@
 new 
-Vector
+ArrayList
 ();%0A
@@ -2096,32 +2096,82 @@
 ibutes.item(i);%0A
+                String attrName = attr.getName();%0A
                 
@@ -2207,30 +2207,26 @@
 ontains(
+ 
 attr
-.get
 Name
-()
+ 
 ))%7B%0A    
@@ -2336,30 +2336,26 @@
 .append(
+ 
 attr
-.get
 Name
-()
+ 
 );%0A     
@@ -2638,30 +2638,19 @@
 .add
-Element(attr.get
+( attr
 Name
-()
+ 
 );%0A 
@@ -2735,32 +2735,70 @@
                 
+// There were some styling attributes%0A
                 
@@ -2897,78 +2897,8 @@
 ));%0A
-                                // There were some styling attributes%0A
     
@@ -3156,16 +3156,18 @@
 %3Cn; i++)
+ %7B
 %0A       
@@ -3227,22 +3227,36 @@
 ved.
-elementAt(i));
+get( i ));%0A                %7D
 %0A   

replace Vector by List/ArrayList