001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wms/capabilities/Style.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.ogcwebservices.wms.capabilities;
037    
038    import java.net.URL;
039    import java.util.HashMap;
040    import java.util.Map;
041    
042    import org.deegree.framework.xml.XMLParsingException;
043    import org.deegree.graphics.sld.AbstractStyle;
044    import org.deegree.graphics.sld.NamedLayer;
045    import org.deegree.graphics.sld.SLDFactory;
046    import org.deegree.graphics.sld.StyledLayerDescriptor;
047    import org.deegree.graphics.sld.UserLayer;
048    import org.deegree.graphics.sld.UserStyle;
049    
050    
051    /**
052     * Zero or more Styles may be advertised for a Layer or collection of layers
053     * using <Style> elements, each of which shall have <Name> and <Title> elements.
054     * The style's Name is used in the Map request STYLES parameter. The Title is a
055     * human-readable string. If only a single style is available, that style is
056     * known as the "default" style and need not be advertised by the server.<p>
057     * A Style may contain several other elements in the Capabilities XML DTD. In
058     * particular, &lt;Abstract&gt; provides a narrative description while &lt;LegendURL&gt;
059     * contains the location of an image of a map legend appropriate to the enclosing
060     * Style. A &lt;Format&gt; element in LegendURL indicates the MIME type of the logo
061     * image, and the attributes width and height state the size of the image in pixels.
062     * </p>
063     *
064     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp</a>
065     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
066     * @version $Revision: 18195 $
067     */
068    public class Style  {
069        static Map<String, UserStyle> styles = null;
070    
071        static {
072            styles = new HashMap<String, UserStyle>( 100 );
073            styles.put( "default", null );
074        }
075    
076        private String abstract_ = null;
077        private String name = null;
078        private String title = null;
079        private StyleSheetURL styleSheetURL = null;
080        private StyleURL styleURL = null;
081        private URL styleResource = null;
082        private LegendURL[] legendURLList = null;
083    
084        /**
085        * constructor initializing the class with the &lt;Style&gt;
086         * @param name
087         * @param title
088         * @param abstract_
089         * @param legendURLs
090         * @param styleSheetURL
091         * @param styleURL
092         * @param styleResource
093         * @throws XMLParsingException
094        */
095        public Style( String name, String title, String abstract_, LegendURL[] legendURLs,
096                    StyleSheetURL styleSheetURL, StyleURL styleURL, URL styleResource )
097            throws XMLParsingException {
098    
099            this.name = name;
100            this.title = title;
101            this.abstract_ = abstract_;
102            this.styleResource = styleResource;
103            this.styleSheetURL = styleSheetURL;
104            this.legendURLList = legendURLs;
105            this.styleURL = styleURL;
106            // only cache styles that belong to the local WMS
107            // for Remote WMS, styleResource is always null
108            if (styleResource != null) {
109                synchronized ( styles ) {
110                    if ( !styles.containsKey( name ) ) {
111                        loadStyles( styleResource );
112                    }
113                }
114            }
115        }
116    
117        /**
118         * loads style definitions from the submitted URL and writes them to the
119         * styles HashMap. The styleResource must provide style definitions in a
120         * SLD conform XML document.<p>
121         * @param styleResource resource of the style defintions
122         * @exception XMLParsingException thrown if the resource doesn't hold the style
123         *            definitions in a SLD conform document
124         */
125        private static synchronized void loadStyles( URL styleResource ) throws XMLParsingException {
126            try {
127                StyledLayerDescriptor sld = SLDFactory.createSLD( styleResource );
128                // get styles from named layers
129                NamedLayer[] nl = sld.getNamedLayers();
130    
131                for ( int i = 0; i < nl.length; i++ ) {
132                    AbstractStyle[] sldStyles = nl[i].getStyles();
133    
134                    for ( int j = 0; j < sldStyles.length; j++ ) {
135                        if ( sldStyles[j] instanceof UserStyle ) {
136                            UserStyle us = (UserStyle)sldStyles[j];
137                            styles.put( us.getName(), us );
138                        }
139                    }
140                }
141    
142                // get styles from user layers
143                UserLayer[] ul = sld.getUserLayers();
144    
145                for ( int i = 0; i < ul.length; i++ ) {
146                    AbstractStyle[] sldStyles = ul[i].getStyles();
147    
148                    for ( int j = 0; j < sldStyles.length; j++ ) {
149                        if ( sldStyles[j] instanceof UserStyle ) {
150                            UserStyle us = (UserStyle)sldStyles[j];
151                            styles.put( us.getName(), us );
152                        }
153                    }
154                }
155    
156            } catch ( Exception e ) {
157                e.printStackTrace();
158                throw new XMLParsingException( e.toString() );
159            }
160        }
161    
162        /**
163         * @return the name - machine code - of the style
164         */
165        public String getName() {
166            return name;
167        }
168    
169        /**
170         * @return the title (human readable) of the style
171         */
172        public String getTitle() {
173            return title;
174        }
175    
176        /**
177         * @return a short narrative description of the style
178         */
179        public String getAbstract() {
180            return abstract_;
181        }
182    
183        /**
184         * @return an array of LegendURL objects that defines the location, size
185         * and format of the legend graphics
186         */
187        public LegendURL[] getLegendURL() {
188            return legendURLList;
189        }
190    
191        /**
192         * @return StyleSheeetURL that provides symbology information for each style of a layer.
193         */
194        public StyleSheetURL getStyleSheetURL() {
195            return styleSheetURL;
196        }
197    
198        /**
199        * A Style element lists the name by which a style is requested and a
200        * human-readable title for pick lists, optionally (and ideally) provides a
201        * human-readable description, and optionally gives a style URL.
202        *
203        * @return the style URL data
204        */
205        public StyleURL getStyleURL() {
206            return styleURL;
207        }
208    
209        /**
210         *
211         * @return the URL where to access the XML (SLD) document definingthe style
212         */
213        public URL getStyleResource() {
214            return styleResource;
215        }
216    
217        /** returns the content of the style as SLD style object
218         *
219         * @return instance of org.deegree.graphics.sld.Style
220         *
221         */
222        public UserStyle getStyleContent() {
223            return styles.get( name );
224        }
225    
226        @Override
227        public String toString() {
228            String ret = null;
229            ret = "name = " + name + "\n";
230            ret += ( "title = " + title + "\n" );
231            ret += ( "abstract_ = " + abstract_ + "\n" );
232            ret += ( "styleSheetURL = " + styleSheetURL + "\n" );
233            ret += ( "styleURL = " + styleURL + "\n" );
234            ret += ( "legendURLList = " + legendURLList + "\n" );
235            return ret;
236        }
237    }