001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/wms/util/ClientHelper.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.portal.standard.wms.util;
037    
038    import org.deegree.framework.util.StringTools;
039    import org.deegree.i18n.Messages;
040    import org.deegree.ogcwebservices.getcapabilities.MetadataURL;
041    import org.deegree.ogcwebservices.wms.capabilities.Layer;
042    
043    /**
044     * 
045     * 
046     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
047     * @author last edited by: $Author: jwilden $
048     * 
049     * @version $Revision: 25889 $, $Date: 2010-08-16 07:43:37 +0200 (Mo, 16 Aug 2010) $
050     */
051    public class ClientHelper {
052    
053        /**
054         * 
055         * @param root
056         * @return String representation of Layers as tree
057         */
058        public static String getLayersAsTree( Layer root ) {
059            StringBuffer sb = new StringBuffer( 10000 );
060            sb.append( Messages.getMessage( "IGEO_STD_WMSLAYERSELECT_LAYERTREE_HEADER", root.getTitle() ) );
061            Layer[] layers = root.getLayer();
062            int indent = 0;
063            for ( int i = 0; i < layers.length; i++ ) {
064                appendLayer( layers[i], indent, sb );
065            }
066            return sb.toString();
067        }
068    
069        private static void appendLayer( Layer layer, int indent, StringBuffer target ) {
070            indent++;
071            String s = "";
072            for ( int i = 0; i < indent; i++ ) {
073                s = s + "&nbsp;";
074            }
075            target.append( s );
076            if ( layer.getName() != null ) {
077    
078                // get MetadataURL
079                String metadataUrl = null;
080                if ( layer.getMetadataURL() != null ) {
081                    MetadataURL[] mdURLs = layer.getMetadataURL();
082                    MetadataURL mdUrl = null;
083                    if ( mdURLs != null && mdURLs.length > 0 ) {
084                        mdUrl = (MetadataURL) mdURLs[0];
085                        metadataUrl = mdUrl.getOnlineResource().toExternalForm();
086                    }
087                }
088    
089                target.append( s ).append( "<input name='LAYER' type='checkbox' " );
090                target.append( "value='" );
091                appendHTMLEntityEncode( layer.getName(), target ); // value_0
092                target.append( '|' );
093                appendHTMLEntityEncode( layer.getTitle(), target ); // value_1
094                target.append( '|' );
095                target.append( layer.isQueryable() ); // value_2
096                target.append( '|' );
097                // TODO test new stuff (value_3, value_4, value_5):
098                if ( metadataUrl != null ) {
099                    metadataUrl = StringTools.replace( metadataUrl, "&", "&amp;", true );
100                    target.append( metadataUrl ); // value_3
101                }
102                target.append( '|' );
103                if ( layer.getScaleHint() != null ) {
104                    target.append( layer.getScaleHint().getMin() ).append( '|' ); // value_4
105                    target.append( layer.getScaleHint().getMax() ); // value_5
106                } else {
107                    // no scaleHint provided in Layer
108                    target.append( '0' ).append( '|' ).append( Double.MAX_VALUE );
109                }
110                target.append( "' >" );
111                appendHTMLEntityEncode( layer.getTitle(), target );
112                target.append( "<br/>\n" );
113            } else {
114                target.append( "<b>" );
115                appendHTMLEntityEncode( layer.getTitle(), target );
116                target.append( "</b><br/>" );
117                Layer[] layers = layer.getLayer();
118                for ( int i = 0; i < layers.length; i++ ) {
119                    appendLayer( layers[i], indent, target );
120                }
121                target.append( "<br/>" );
122            }
123        }
124    
125        /**
126         * @param s
127         * @param sb
128         * @return the new string
129         */
130        public static String appendHTMLEntityEncode( String s, StringBuffer sb ) {
131            StringBuffer buf = new StringBuffer();
132            for ( int i = 0; i < s.length(); i++ ) {
133                char c = s.charAt( i );
134                if ( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' ) {
135                    buf.append( c );
136                } else {
137                    buf.append( "&#" + (int) c + ";" );
138                }
139            }
140    
141            return sb.append( buf ).toString();
142        }
143    
144    }