001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.sld.bindings;
017:
018: import org.picocontainer.MutablePicoContainer;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import java.util.Iterator;
022: import java.util.StringTokenizer;
023: import javax.xml.namespace.QName;
024: import org.geotools.filter.Expression;
025: import org.geotools.filter.Filters;
026: import org.geotools.sld.CssParameter;
027: import org.geotools.styling.Graphic;
028: import org.geotools.styling.Stroke;
029: import org.geotools.styling.StyleBuilder;
030: import org.geotools.styling.StyleFactory;
031: import org.geotools.xml.*;
032:
033: /**
034: * Binding object for the element http://www.opengis.net/sld:Stroke.
035: *
036: * <p>
037: * <pre>
038: * <code>
039: * <xsd:element name="Stroke">
040: * <xsd:annotation>
041: * <xsd:documentation> A "Stroke" specifies the
042: * appearance of a linear geometry. It is defined in
043: * parallel with SVG strokes. The following CssParameters
044: * may be used: "stroke" (color),
045: * "stroke-opacity", "stroke-width",
046: * "stroke-linejoin", "stroke-linecap",
047: * "stroke-dasharray", and
048: * "stroke-dashoffset". </xsd:documentation>
049: * </xsd:annotation>
050: * <xsd:complexType>
051: * <xsd:sequence>
052: * <xsd:choice minOccurs="0">
053: * <xsd:element ref="sld:GraphicFill"/>
054: * <xsd:element ref="sld:GraphicStroke"/>
055: * </xsd:choice>
056: * <xsd:element ref="sld:CssParameter" minOccurs="0" maxOccurs="unbounded"/>
057: * </xsd:sequence>
058: * </xsd:complexType>
059: * </xsd:element>
060: *
061: * </code>
062: * </pre>
063: * </p>
064: *
065: * @generated
066: */
067: public class SLDStrokeBinding extends AbstractComplexBinding {
068: StyleFactory styleFactory;
069:
070: public SLDStrokeBinding(StyleFactory styleFactory) {
071: this .styleFactory = styleFactory;
072: }
073:
074: /**
075: * @generated
076: */
077: public QName getTarget() {
078: return SLD.STROKE;
079: }
080:
081: /**
082: * <!-- begin-user-doc -->
083: * <!-- end-user-doc -->
084: *
085: * @generated modifiable
086: */
087: public int getExecutionMode() {
088: return AFTER;
089: }
090:
091: /**
092: * <!-- begin-user-doc -->
093: * <!-- end-user-doc -->
094: *
095: * @generated modifiable
096: */
097: public Class getType() {
098: return Stroke.class;
099: }
100:
101: /**
102: * <!-- begin-user-doc -->
103: * <!-- end-user-doc -->
104: *
105: * @generated modifiable
106: */
107: public void initialize(ElementInstance instance, Node node,
108: MutablePicoContainer context) {
109: }
110:
111: /**
112: * <!-- begin-user-doc -->
113: * <!-- end-user-doc -->
114: *
115: * @generated modifiable
116: */
117: public Object parse(ElementInstance instance, Node node,
118: Object value) throws Exception {
119: //The following CssParameters may be used:
120: //"stroke" (color),
121: //"stroke-opacity"
122: //"stroke-width",
123: //"stroke-linejoin"
124: //"stroke-linecap",
125: //"stroke-dasharray",
126: //"stroke-dashoffset".
127: Expression color = null;
128:
129: //The following CssParameters may be used:
130: //"stroke" (color),
131: //"stroke-opacity"
132: //"stroke-width",
133: //"stroke-linejoin"
134: //"stroke-linecap",
135: //"stroke-dasharray",
136: //"stroke-dashoffset".
137: Expression opacity = null;
138:
139: //The following CssParameters may be used:
140: //"stroke" (color),
141: //"stroke-opacity"
142: //"stroke-width",
143: //"stroke-linejoin"
144: //"stroke-linecap",
145: //"stroke-dasharray",
146: //"stroke-dashoffset".
147: Expression width = null;
148:
149: //The following CssParameters may be used:
150: //"stroke" (color),
151: //"stroke-opacity"
152: //"stroke-width",
153: //"stroke-linejoin"
154: //"stroke-linecap",
155: //"stroke-dasharray",
156: //"stroke-dashoffset".
157: Expression lineJoin = null;
158: Expression lineCap = null;
159: Expression dashArray = null;
160: Expression dashOffset = null;
161:
162: for (Iterator i = node.getChildValues(CssParameter.class)
163: .iterator(); i.hasNext();) {
164: CssParameter css = (CssParameter) i.next();
165:
166: if (css.getExpressions().isEmpty()) {
167: continue;
168: }
169:
170: if ("stroke".equals(css.getName())) {
171: color = (Expression) css.getExpressions().get(0);
172: } else if ("stroke-opacity".equals(css.getName())) {
173: opacity = (Expression) css.getExpressions().get(0);
174: } else if ("stroke-width".equals(css.getName())) {
175: width = (Expression) css.getExpressions().get(0);
176: } else if ("stroke-linejoin".equals(css.getName())) {
177: lineJoin = (Expression) css.getExpressions().get(0);
178: } else if ("stroke-linecap".equals(css.getName())) {
179: lineCap = (Expression) css.getExpressions().get(0);
180: } else if ("stroke-dasharray".equals(css.getName())) {
181: dashArray = (Expression) css.getExpressions().get(0);
182: } else if ("stroke-dashoffset".equals(css.getName())) {
183: dashOffset = (Expression) css.getExpressions().get(0);
184: }
185: }
186:
187: float[] dash = null;
188:
189: if (dashArray != null) {
190: String[] string = Filters.asString(dashArray).split(" +");
191: dash = new float[string.length];
192:
193: for (int i = 0; i < string.length; i++) {
194: dash[i] = Float.parseFloat(string[i]);
195: }
196: }
197:
198: //<xsd:choice minOccurs="0">
199: // <xsd:element ref="sld:GraphicFill"/>
200: // <xsd:element ref="sld:GraphicStroke"/>
201: //</xsd:choice>
202: Graphic graphicFill = (Graphic) node
203: .getChildValue("GraphicFill");
204: Graphic graphicStroke = (Graphic) node
205: .getChildValue("GraphicStroke");
206:
207: return styleFactory.createStroke(color, width, opacity,
208: lineJoin, lineCap, dash, dashOffset, graphicFill,
209: graphicStroke);
210: }
211: }
|