001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/StyleUtils.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.graphics.sld;
037    
038    import java.util.ArrayList;
039    import java.util.Iterator;
040    import java.util.List;
041    import java.util.Map;
042    
043    import org.deegree.io.datastore.PropertyPathResolvingException;
044    import org.deegree.model.filterencoding.Expression;
045    import org.deegree.model.filterencoding.Filter;
046    import org.deegree.model.filterencoding.FilterTools;
047    import org.deegree.ogcbase.PropertyPath;
048    
049    /**
050     * Collects all property names used by a list of styles. E.g. this can be used to optimze GetFeature
051     * requests from a WMS against a WFS.
052     *
053     * @version $Revision: 18195 $
054     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
055     * @author last edited by: $Author: mschneider $
056     *
057     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
058     *
059     * @since 2.0
060     */
061    public class StyleUtils {
062    
063        /**
064         * @return a list of all
065         *
066         * @param styles
067         * @param scaleDen
068         * @throws PropertyPathResolvingException
069         */
070        public static List<PropertyPath> extractRequiredProperties( List<UserStyle> styles, double scaleDen )
071                                throws PropertyPathResolvingException {
072            List<PropertyPath> pp = new ArrayList<PropertyPath>();
073    
074            for ( int i = 0; i < styles.size(); i++ ) {
075                FeatureTypeStyle[] fts = styles.get( i ).getFeatureTypeStyles();
076                for ( int j = 0; j < fts.length; j++ ) {
077                    Rule[] rules = fts[j].getRules();
078                    for ( int k = 0; k < rules.length; k++ ) {
079                        double minScale = rules[k].getMinScaleDenominator();
080                        double maxScale = rules[k].getMaxScaleDenominator();
081                        if ( minScale <= scaleDen && maxScale > scaleDen ) {
082                            Filter filter = rules[k].getFilter();
083                            List<PropertyPath> list = FilterTools.extractPropertyPaths( filter );
084                            pp.addAll( list );
085                            Symbolizer[] sym = rules[k].getSymbolizers();
086                            for ( int d = 0; d < sym.length; d++ ) {
087                                if ( sym[d] instanceof PointSymbolizer ) {
088                                    pp = extractPPFromPointSymbolizer( (PointSymbolizer) sym[d], pp );
089                                } else if ( sym[d] instanceof LineSymbolizer ) {
090                                    pp = extractPPFromLineSymbolizer( (LineSymbolizer) sym[d], pp );
091                                } else if ( sym[d] instanceof PolygonSymbolizer ) {
092                                    pp = extractPPFromPolygonSymbolizer( (PolygonSymbolizer) sym[d], pp );
093                                } else if ( sym[d] instanceof TextSymbolizer ) {
094                                    pp = extractPPFromTextSymbolizer( (TextSymbolizer) sym[d], pp );
095                                }
096                            }
097                        }
098                    }
099                }
100            }
101    
102            List<PropertyPath> tmp = new ArrayList<PropertyPath>( pp.size() );
103            for ( int i = 0; i < pp.size(); i++ ) {
104                if ( !tmp.contains( pp.get( i ) ) ) {
105                    tmp.add( pp.get( i ) );
106                }
107            }
108    
109            return tmp;
110        }
111    
112        private static List<PropertyPath> extractPPFromTextSymbolizer( TextSymbolizer symbolizer, List<PropertyPath> pp )
113                                throws PropertyPathResolvingException {
114    
115            ParameterValueType[] bbox = symbolizer.getBoundingBox();
116            if ( bbox != null ) {
117                pp = extractPPFromParamValueType( bbox[0], pp );
118                pp = extractPPFromParamValueType( bbox[1], pp );
119                pp = extractPPFromParamValueType( bbox[2], pp );
120                pp = extractPPFromParamValueType( bbox[3], pp );
121            }
122    
123            Map<?, ?> css = null;
124            if ( symbolizer.getFill() != null ) {
125                css = symbolizer.getFill().getCssParameters();
126                pp = extractPPFromCssParameter( css, pp );
127            }
128            if ( symbolizer.getFont() != null ) {
129                css = symbolizer.getFont().getCssParameters();
130                pp = extractPPFromCssParameter( css, pp );
131            }
132            if ( symbolizer.getGeometry() != null ) {
133                pp.add( symbolizer.getGeometry().getPropertyPath() );
134            }
135            Halo halo = symbolizer.getHalo();
136            ParameterValueType pvt = null;
137            if ( halo != null ) {
138                pvt = halo.getRadius();
139                pp = extractPPFromParamValueType( pvt, pp );
140    
141                if ( halo.getFill() != null ) {
142                    css = halo.getFill().getCssParameters();
143                    pp = extractPPFromCssParameter( css, pp );
144                }
145                if ( halo.getStroke() != null ) {
146                    css = halo.getStroke().getCssParameters();
147                    pp = extractPPFromCssParameter( css, pp );
148                }
149            }
150            pvt = symbolizer.getLabel();
151            pp = extractPPFromParamValueType( pvt, pp );
152    
153            // collect property names from line placement
154            LinePlacement lp = symbolizer.getLabelPlacement().getLinePlacement();
155            if ( lp != null ) {
156                pvt = lp.getGap();
157                pp = extractPPFromParamValueType( pvt, pp );
158                pvt = lp.getLineWidth();
159                pp = extractPPFromParamValueType( pvt, pp );
160                pvt = lp.getPerpendicularOffset();
161                pp = extractPPFromParamValueType( pvt, pp );
162            }
163    
164            // collect property names from line placement
165            PointPlacement ppl = symbolizer.getLabelPlacement().getPointPlacement();
166            if ( ppl != null ) {
167                pvt = ppl.getRotation();
168                pp = extractPPFromParamValueType( pvt, pp );
169                ParameterValueType[] pvta = ppl.getAnchorPoint();
170                if ( pvta != null ) {
171                    for ( int i = 0; i < pvta.length; i++ ) {
172                        pp = extractPPFromParamValueType( pvta[i], pp );
173                    }
174                }
175                pvta = ppl.getDisplacement();
176                if ( pvta != null ) {
177                    for ( int i = 0; i < pvta.length; i++ ) {
178                        pp = extractPPFromParamValueType( pvta[i], pp );
179                    }
180                }
181            }
182    
183            return pp;
184        }
185    
186        private static List<PropertyPath> extractPPFromPolygonSymbolizer( PolygonSymbolizer symbolizer,
187                                                                          List<PropertyPath> pp )
188                                throws PropertyPathResolvingException {
189    
190            Map<?, ?> css = null;
191            if ( symbolizer != null ) {
192                if ( symbolizer.getFill() != null ) {
193                    css = symbolizer.getFill().getCssParameters();
194                    pp = extractPPFromCssParameter( css, pp );
195                }
196    
197                if ( symbolizer.getGeometry() != null ) {
198                    pp.add( symbolizer.getGeometry().getPropertyPath() );
199                }
200    
201                if ( symbolizer.getStroke() != null ) {
202                    css = symbolizer.getStroke().getCssParameters();
203                    pp = extractPPFromCssParameter( css, pp );
204                }
205            }
206    
207            return pp;
208        }
209    
210        private static List<PropertyPath> extractPPFromLineSymbolizer( LineSymbolizer symbolizer, List<PropertyPath> pp )
211                                throws PropertyPathResolvingException {
212    
213            if ( symbolizer.getGeometry() != null ) {
214                pp.add( symbolizer.getGeometry().getPropertyPath() );
215            }
216    
217            Map<?, ?> css = symbolizer.getStroke().getCssParameters();
218            pp = extractPPFromCssParameter( css, pp );
219    
220            return pp;
221        }
222    
223        private static List<PropertyPath> extractPPFromPointSymbolizer( PointSymbolizer symbolizer, List<PropertyPath> pp )
224                                throws PropertyPathResolvingException {
225    
226            Graphic graphic = symbolizer.getGraphic();
227            if ( graphic != null ) {
228                if ( graphic.getOpacity() != null ) {
229                    pp = extractPPFromParamValueType( graphic.getOpacity(), pp );
230                }
231    
232                if ( graphic.getRotation() != null ) {
233                    pp = extractPPFromParamValueType( graphic.getRotation(), pp );
234                }
235    
236                if ( graphic.getSize() != null ) {
237                    pp = extractPPFromParamValueType( graphic.getSize(), pp );
238                }
239    
240                if ( symbolizer.getGeometry() != null ) {
241                    pp.add( symbolizer.getGeometry().getPropertyPath() );
242                }
243            }
244    
245            return pp;
246        }
247    
248        private static List<PropertyPath> extractPPFromCssParameter( Map<?, ?> css, List<PropertyPath> pp )
249                                throws PropertyPathResolvingException {
250            if ( css != null ) {
251                Iterator<?> iter = css.values().iterator();
252                while ( iter.hasNext() ) {
253                    ParameterValueType pvt = ( (CssParameter) iter.next() ).getValue();
254                    pp = extractPPFromParamValueType( pvt, pp );
255                }
256            }
257            return pp;
258        }
259    
260        private static List<PropertyPath> extractPPFromParamValueType( ParameterValueType pvt, List<PropertyPath> pp )
261                                throws PropertyPathResolvingException {
262            if ( pvt != null ) {
263                Object[] components = pvt.getComponents();
264                for ( int i = 0; i < components.length; i++ ) {
265                    if ( components[i] instanceof Expression ) {
266                        pp = FilterTools.extractPropertyPaths( (Expression) components[i], pp );
267                    }
268                }
269            }
270            return pp;
271        }
272    
273    }