/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2002-2006, GeoTools Project Managment Committee (PMC)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.sld.bindings;

import org.picocontainer.MutablePicoContainer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.xml.namespace.QName;
import org.geotools.filter.Expression;
import org.geotools.filter.Filters;
import org.geotools.sld.CssParameter;
import org.geotools.styling.Graphic;
import org.geotools.styling.Stroke;
import org.geotools.styling.StyleBuilder;
import org.geotools.styling.StyleFactory;
import org.geotools.xml.*;


/**
 * Binding object for the element http://www.opengis.net/sld:Stroke.
 *
 * <p>
 *        <pre>
 *         <code>
 *  &lt;xsd:element name="Stroke"&gt;
 *      &lt;xsd:annotation&gt;
 *          &lt;xsd:documentation&gt;         A &quot;Stroke&quot; specifies the
 *              appearance of a linear geometry.  It is         defined in
 *              parallel with SVG strokes.  The following CssParameters
 *              may be used: &quot;stroke&quot; (color),
 *              &quot;stroke-opacity&quot;, &quot;stroke-width&quot;,
 *              &quot;stroke-linejoin&quot;, &quot;stroke-linecap&quot;,
 *              &quot;stroke-dasharray&quot;, and
 *              &quot;stroke-dashoffset&quot;.       &lt;/xsd:documentation&gt;
 *      &lt;/xsd:annotation&gt;
 *      &lt;xsd:complexType&gt;
 *          &lt;xsd:sequence&gt;
 *              &lt;xsd:choice minOccurs="0"&gt;
 *                  &lt;xsd:element ref="sld:GraphicFill"/&gt;
 *                  &lt;xsd:element ref="sld:GraphicStroke"/&gt;
 *              &lt;/xsd:choice&gt;
 *              &lt;xsd:element ref="sld:CssParameter" minOccurs="0" maxOccurs="unbounded"/&gt;
 *          &lt;/xsd:sequence&gt;
 *      &lt;/xsd:complexType&gt;
 *  &lt;/xsd:element&gt;
 *
 *          </code>
 *         </pre>
 * </p>
 *
 * @generated
 */
public class SLDStrokeBinding extends AbstractComplexBinding {
    StyleFactory styleFactory;

    public SLDStrokeBinding(StyleFactory styleFactory) {
        this.styleFactory = styleFactory;
    }

    /**
     * @generated
     */
    public QName getTarget() {
        return SLD.STROKE;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     *
     * @generated modifiable
     */
    public int getExecutionMode() {
        return AFTER;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     *
     * @generated modifiable
     */
    public Class getType() {
        return Stroke.class;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     *
     * @generated modifiable
     */
    public void initialize(ElementInstance instance, Node node, MutablePicoContainer context) {
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     *
     * @generated modifiable
     */
    public Object parse(ElementInstance instance, Node node, Object value)
        throws Exception {
        //The following CssParameters may be used: 
        //&quot;stroke&quot; (color),
        //&quot;stroke-opacity&quot;
        //&quot;stroke-width&quot;,
        //&quot;stroke-linejoin&quot; 
        //&quot;stroke-linecap&quot;,
        //&quot;stroke-dasharray&quot;, 
        //&quot;stroke-dashoffset&quot;.
        Expression color = null;

        //The following CssParameters may be used: 
        //&quot;stroke&quot; (color),
        //&quot;stroke-opacity&quot;
        //&quot;stroke-width&quot;,
        //&quot;stroke-linejoin&quot; 
        //&quot;stroke-linecap&quot;,
        //&quot;stroke-dasharray&quot;, 
        //&quot;stroke-dashoffset&quot;.
        Expression opacity = null;

        //The following CssParameters may be used: 
        //&quot;stroke&quot; (color),
        //&quot;stroke-opacity&quot;
        //&quot;stroke-width&quot;,
        //&quot;stroke-linejoin&quot; 
        //&quot;stroke-linecap&quot;,
        //&quot;stroke-dasharray&quot;, 
        //&quot;stroke-dashoffset&quot;.
        Expression width = null;

        //The following CssParameters may be used: 
        //&quot;stroke&quot; (color),
        //&quot;stroke-opacity&quot;
        //&quot;stroke-width&quot;,
        //&quot;stroke-linejoin&quot; 
        //&quot;stroke-linecap&quot;,
        //&quot;stroke-dasharray&quot;, 
        //&quot;stroke-dashoffset&quot;.
        Expression lineJoin = null;
        Expression lineCap = null;
        Expression dashArray = null;
        Expression dashOffset = null;

        for (Iterator i = node.getChildValues(CssParameter.class).iterator(); i.hasNext();) {
            CssParameter css = (CssParameter) i.next();

            if (css.getExpressions().isEmpty()) {
                continue;
            }

            if ("stroke".equals(css.getName())) {
                color = (Expression) css.getExpressions().get(0);
            } else if ("stroke-opacity".equals(css.getName())) {
                opacity = (Expression) css.getExpressions().get(0);
            } else if ("stroke-width".equals(css.getName())) {
                width = (Expression) css.getExpressions().get(0);
            } else if ("stroke-linejoin".equals(css.getName())) {
                lineJoin = (Expression) css.getExpressions().get(0);
            } else if ("stroke-linecap".equals(css.getName())) {
                lineCap = (Expression) css.getExpressions().get(0);
            } else if ("stroke-dasharray".equals(css.getName())) {
                dashArray = (Expression) css.getExpressions().get(0);
            } else if ("stroke-dashoffset".equals(css.getName())) {
                dashOffset = (Expression) css.getExpressions().get(0);
            }
        }

        float[] dash = null;

        if (dashArray != null) {
            String[] string = Filters.asString(dashArray).split(" +");
            dash = new float[string.length];

            for (int i = 0; i < string.length; i++) {
                dash[i] = Float.parseFloat(string[i]);
            }
        }

        //&lt;xsd:choice minOccurs="0"&gt;
        //   &lt;xsd:element ref="sld:GraphicFill"/&gt;
        //   &lt;xsd:element ref="sld:GraphicStroke"/&gt;
        //&lt;/xsd:choice&gt;
        Graphic graphicFill = (Graphic) node.getChildValue("GraphicFill");
        Graphic graphicStroke = (Graphic) node.getChildValue("GraphicStroke");

        return styleFactory.createStroke(color, width, opacity, lineJoin, lineCap, dash,
            dashOffset, graphicFill, graphicStroke);
    }
}
