001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/StyledLayerDescriptor.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.List;
040    
041    import org.deegree.framework.xml.Marshallable;
042    
043    /**
044     * StyledLayerDescriptor: This is a sequence of styled layers, represented at the first level by
045     * Layer and UserLayer elements. A "version" attribute has been added to allow the formatting of
046     * static-file
047     *
048     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp</a>
049     * @author last edited by: $Author: mschneider $
050     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
051     */
052    public class StyledLayerDescriptor implements Marshallable {
053        private List<AbstractLayer> layers = null;
054    
055        private String version = null;
056    
057        private String abstract_ = null;
058    
059        private String name = null;
060    
061        private String title = null;
062    
063        /**
064         * @param name
065         * @param title
066         * @param version
067         * @param abstract_
068         * @param layers
069         */
070        StyledLayerDescriptor( String name, String title, String version, String abstract_, AbstractLayer[] layers ) {
071            this.layers = new ArrayList<AbstractLayer>( layers.length );
072            setLayers( layers );
073            setVersion( version );
074            setAbstract( abstract_ );
075            setName( name );
076            setTitle( title );
077        }
078    
079        /**
080         * constructor initializing the class with the <StyledLayerDescriptor>
081         * @param layers
082         * @param version
083         */
084        public StyledLayerDescriptor( AbstractLayer[] layers, String version ) {
085            this.layers = new ArrayList<AbstractLayer>( layers.length );
086            setLayers( layers );
087            setVersion( version );
088        }
089    
090        /**
091         * @return the Layers as Array
092         */
093        public AbstractLayer[] getLayers() {
094            return layers.toArray( new AbstractLayer[layers.size()] );
095        }
096    
097        /**
098         * Sets Layers
099         *
100         * @param layers
101         *            the Layers as Array
102         */
103        public void setLayers( AbstractLayer[] layers ) {
104            this.layers.clear();
105    
106            if ( layers != null ) {
107                for ( int i = 0; i < layers.length; i++ ) {
108                    this.layers.add( layers[i] );
109                }
110            }
111        }
112    
113        /**
114         * adds the <Layer>
115         *
116         * @param layer
117         *            a Layer to add
118         */
119        public void addLayer( AbstractLayer layer ) {
120            layers.add( layer );
121        }
122    
123        /**
124         * removes the <Layer>
125         *
126         * @param layer
127         *            a Layer to remove
128         */
129        public void removeLayer( AbstractLayer layer ) {
130            if ( layers.indexOf( layer ) != -1 ) {
131                layers.remove( layers.indexOf( layer ) );
132            }
133        }
134    
135        /**
136         * A UserLayer can contain one or more UserStyles. A UserLayer may direct the WMS to a specified
137         * WFS source of feature data. Multiple feature types can be included in a UserLayer, since this
138         * is semantically equivalent to a Layer. All feature types of a UserLayer come from the same
139         * WFS. The WFS can be named explicitly with the "wfs" attribute or it can be implied by
140         * context.
141         *
142         * @return the UserLayers as Array
143         */
144        public UserLayer[] getUserLayers() {
145            List<UserLayer> list = new ArrayList<UserLayer>( layers.size() );
146            for ( int i = 0; i < layers.size(); i++ ) {
147                if ( layers.get( i ) instanceof UserLayer ) {
148                    list.add( (UserLayer) layers.get( i ) );
149                }
150            }
151            return list.toArray( new UserLayer[list.size()] );
152        }
153    
154        /**
155         * A NamedLayer uses the "name" attribute to identify a layer known to the WMS and can contain
156         * zero or more styles, either NamedStyles or UserStyles. In the absence of any styles the
157         * default style for the layer is used.
158         *
159         * @return the NamedLayers as Array
160         */
161        public NamedLayer[] getNamedLayers() {
162            List<NamedLayer> list = new ArrayList<NamedLayer>( layers.size() );
163            for ( int i = 0; i < layers.size(); i++ ) {
164                if ( layers.get( i ) instanceof NamedLayer ) {
165                    list.add( (NamedLayer) layers.get( i ) );
166                }
167            }
168            return list.toArray( new NamedLayer[list.size()] );
169        }
170    
171        /**
172         * The version attribute gives the SLD version of an SLD document, to facilitate backward
173         * compatibility with static documents stored in various different versions of the SLD spec. The
174         * string has the format x.y.z, the same as in other OpenGIS Web Server specs. For example, an
175         * SLD document stored according to this spec would have the version string 0.7.2.
176         *
177         * @return the version of the SLD as String
178         *
179         */
180        public String getVersion() {
181            return version;
182        }
183    
184        /**
185         * sets the <Version>
186         *
187         * @param version
188         *            the version of the SLD
189         *
190         */
191        public void setVersion( String version ) {
192            this.version = version;
193        }
194    
195        /**
196         * @return Returns the abstract_.
197         */
198        public String getAbstract() {
199            return abstract_;
200        }
201    
202        /**
203         * @param abstract_
204         *            The abstract_ to set.
205         */
206        public void setAbstract( String abstract_ ) {
207            this.abstract_ = abstract_;
208        }
209    
210        /**
211         * @return Returns the name.
212         *
213         */
214        public String getName() {
215            return name;
216        }
217    
218        /**
219         * @param name
220         *            The name to set.
221         *
222         */
223        public void setName( String name ) {
224            this.name = name;
225        }
226    
227        /**
228         * @return Returns the title.
229         *
230         */
231        public String getTitle() {
232            return title;
233        }
234    
235        /**
236         * @param title
237         *            The title to set.
238         *
239         */
240        public void setTitle( String title ) {
241            this.title = title;
242        }
243    
244        /**
245         * exports the content of the Font as XML formated String
246         *
247         * @return xml representation of the Font
248         */
249        public String exportAsXML() {
250    
251            StringBuffer sb = new StringBuffer( 50000 );
252            sb.append( "<?xml version='1.0' encoding='UTF-8'?>" );
253            sb.append( "<StyledLayerDescriptor version='" + version + "' " );
254            sb.append( "xmlns='http://www.opengis.net/sld' " );
255            sb.append( "xmlns:gml='http://www.opengis.net/gml' " );
256            sb.append( "xmlns:ogc='http://www.opengis.net/ogc' " );
257            sb.append( "xmlns:xlink='http://www.w3.org/1999/xlink' " );
258            sb.append( "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" );
259    
260            for ( int i = 0; i < layers.size(); i++ ) {
261                sb.append( ( (Marshallable) layers.get( i ) ).exportAsXML() );
262            }
263    
264            sb.append( "</StyledLayerDescriptor>" );
265    
266            return sb.toString();
267        }
268    
269    }