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