001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wms/capabilities/Style.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2008 by:
006    EXSE, Department of Geography, University of Bonn
007    http://www.giub.uni-bonn.de/deegree/
008    lat/lon GmbH
009    http://www.lat-lon.de
010    
011    This library is free software; you can redistribute it and/or
012    modify it under the terms of the GNU Lesser General Public
013    License as published by the Free Software Foundation; either
014    version 2.1 of the License, or (at your option) any later version.
015    
016    This library is distributed in the hope that it will be useful,
017    but WITHOUT ANY WARRANTY; without even the implied warranty of
018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019    Lesser General Public License for more details.
020    
021    You should have received a copy of the GNU Lesser General Public
022    License along with this library; if not, write to the Free Software
023    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025    Contact:
026    
027    Andreas Poth
028    lat/lon GmbH
029    Aennchenstr. 19
030    53115 Bonn
031    Germany
032    E-Mail: poth@lat-lon.de
033    
034    Prof. Dr. Klaus Greve
035    Department of Geography
036    University of Bonn
037    Meckenheimer Allee 166
038    53115 Bonn
039    Germany
040    E-Mail: greve@giub.uni-bonn.de
041    
042                     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.ogcwebservices.wms.capabilities;
045    
046    import java.net.URL;
047    import java.util.HashMap;
048    import java.util.Map;
049    
050    import org.deegree.framework.xml.XMLParsingException;
051    import org.deegree.graphics.sld.AbstractStyle;
052    import org.deegree.graphics.sld.NamedLayer;
053    import org.deegree.graphics.sld.SLDFactory;
054    import org.deegree.graphics.sld.StyledLayerDescriptor;
055    import org.deegree.graphics.sld.UserLayer;
056    import org.deegree.graphics.sld.UserStyle;
057    
058    
059    /**
060     * Zero or more Styles may be advertised for a Layer or collection of layers
061     * using <Style> elements, each of which shall have <Name> and <Title> elements.
062     * The style's Name is used in the Map request STYLES parameter. The Title is a
063     * human-readable string. If only a single style is available, that style is
064     * known as the "default" style and need not be advertised by the server.<p>
065     * A Style may contain several other elements in the Capabilities XML DTD. In
066     * particular, &lt;Abstract&gt; provides a narrative description while &lt;LegendURL&gt;
067     * contains the location of an image of a map legend appropriate to the enclosing
068     * Style. A &lt;Format&gt; element in LegendURL indicates the MIME type of the logo
069     * image, and the attributes width and height state the size of the image in pixels.
070     * </p>
071     *
072     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp</a>
073     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
074     * @version $Revision: 9345 $
075     */
076    public class Style  {
077        static Map<String, UserStyle> styles = null;
078    
079        static {
080            styles = new HashMap<String, UserStyle>( 100 );
081            styles.put( "default", null );
082        }
083    
084        private String abstract_ = null;
085        private String name = null;
086        private String title = null;
087        private StyleSheetURL styleSheetURL = null;
088        private StyleURL styleURL = null;
089        private URL styleResource = null;
090        private LegendURL[] legendURLList = null;
091    
092        /**
093        * constructor initializing the class with the &lt;Style&gt;
094         * @param name 
095         * @param title 
096         * @param abstract_ 
097         * @param legendURLs 
098         * @param styleSheetURL 
099         * @param styleURL 
100         * @param styleResource 
101         * @throws XMLParsingException 
102        */
103        public Style( String name, String title, String abstract_, LegendURL[] legendURLs, 
104                    StyleSheetURL styleSheetURL, StyleURL styleURL, URL styleResource )
105            throws XMLParsingException {
106    
107            this.name = name;
108            this.title = title;
109            this.abstract_ = abstract_;
110            this.styleResource = styleResource;
111            this.styleSheetURL = styleSheetURL;
112            this.legendURLList = legendURLs;
113            this.styleURL = styleURL;
114            // only cache styles that belong to the local WMS
115            // for Remote WMS, styleResource is always null
116            if (styleResource != null) {
117                synchronized ( styles ) {
118                    if ( !styles.containsKey( name ) ) {
119                        loadStyles( styleResource );
120                    }
121                }
122            }
123        }
124    
125        /**
126         * loads style definitions from the submitted URL and writes them to the
127         * styles HashMap. The styleResource must provide style definitions in a
128         * SLD conform XML document.<p>
129         * @param styleResource resource of the style defintions
130         * @exception XMLParsingException thrown if the resource doesn't hold the style 
131         *            definitions in a SLD conform document
132         */
133        private static synchronized void loadStyles( URL styleResource ) throws XMLParsingException {
134            try {
135                StyledLayerDescriptor sld = SLDFactory.createSLD( styleResource );
136                // get styles from named layers
137                NamedLayer[] nl = sld.getNamedLayers();
138    
139                for ( int i = 0; i < nl.length; i++ ) {
140                    AbstractStyle[] sldStyles = nl[i].getStyles();
141    
142                    for ( int j = 0; j < sldStyles.length; j++ ) {
143                        if ( sldStyles[j] instanceof UserStyle ) {
144                            UserStyle us = (UserStyle)sldStyles[j];   
145                            styles.put( us.getName(), us );
146                        }
147                    }
148                }
149    
150                // get styles from user layers
151                UserLayer[] ul = sld.getUserLayers();
152    
153                for ( int i = 0; i < ul.length; i++ ) {
154                    AbstractStyle[] sldStyles = ul[i].getStyles();
155    
156                    for ( int j = 0; j < sldStyles.length; j++ ) {
157                        if ( sldStyles[j] instanceof UserStyle ) {
158                            UserStyle us = (UserStyle)sldStyles[j];
159                            styles.put( us.getName(), us );
160                        }
161                    }
162                }
163    
164            } catch ( Exception e ) {
165                e.printStackTrace();
166                throw new XMLParsingException( e.toString() );
167            }
168        }
169    
170        /**
171         * @return the name - machine code - of the style
172         */
173        public String getName() {
174            return name;
175        }
176    
177        /**
178         * @return the title (human readable) of the style
179         */
180        public String getTitle() {
181            return title;
182        }
183    
184        /**
185         * @return a short narrative description of the style
186         */
187        public String getAbstract() {
188            return abstract_;
189        }
190    
191        /**
192         * @return an array of LegendURL objects that defines the location, size
193         * and format of the legend graphics
194         */
195        public LegendURL[] getLegendURL() {
196            return legendURLList;
197        }
198    
199        /**
200         * @return StyleSheeetURL that provides symbology information for each style of a layer.
201         */
202        public StyleSheetURL getStyleSheetURL() {
203            return styleSheetURL;
204        }
205    
206        /**
207        * A Style element lists the name by which a style is requested and a
208        * human-readable title for pick lists, optionally (and ideally) provides a
209        * human-readable description, and optionally gives a style URL.
210        * 
211        * @return the style URL data
212        */
213        public StyleURL getStyleURL() {
214            return styleURL;
215        }
216    
217        /** 
218         * 
219         * @return the URL where to access the XML (SLD) document definingthe style
220         */
221        public URL getStyleResource() {
222            return styleResource;
223        }
224    
225        /** returns the content of the style as SLD style object
226         *
227         * @return instance of org.deegree.graphics.sld.Style
228         *
229         */
230        public UserStyle getStyleContent() {       
231            return styles.get( name );
232        }
233    
234        @Override
235        public String toString() {
236            String ret = null;
237            ret = "name = " + name + "\n";
238            ret += ( "title = " + title + "\n" );
239            ret += ( "abstract_ = " + abstract_ + "\n" );
240            ret += ( "styleSheetURL = " + styleSheetURL + "\n" );
241            ret += ( "styleURL = " + styleURL + "\n" );
242            ret += ( "legendURLList = " + legendURLList + "\n" );
243            return ret;
244        }
245    }