001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/tools/srs/SRSInfo.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.tools.srs;
037    
038    import java.io.BufferedReader;
039    import java.io.BufferedWriter;
040    import java.io.File;
041    import java.io.FileWriter;
042    import java.io.IOException;
043    import java.io.InputStreamReader;
044    import java.util.ArrayList;
045    import java.util.Collections;
046    import java.util.HashMap;
047    import java.util.List;
048    
049    import org.deegree.crs.configuration.CRSConfiguration;
050    import org.deegree.crs.configuration.CRSProvider;
051    import org.deegree.crs.coordinatesystems.CoordinateSystem;
052    
053    /**
054     * A utility program to inform the callee about the availability (-isAvailable param) of a certain crs or to retrieve
055     * all available crs's from the deegree crs configuration.
056     *
057     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
058     * @author last edited by: $Author: mschneider $
059     *
060     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
061     */
062    public class SRSInfo {
063        private final CRSProvider provider;
064    
065        /**
066         * constructor creating a crs provider
067         */
068        SRSInfo() {
069            CRSConfiguration config = CRSConfiguration.getCRSConfiguration();
070            provider = config.getProvider();
071            if ( provider == null ) {
072                System.out.println( "Could not retrieve a deegree crs-provider instance, this may not be, please make sure your deegree installation uses a correct crs-configuration. Exiting!" );
073            }
074        }
075    
076        /**
077         * returns true if the the passed SRS is available in deegree
078         *
079         * @param srs
080         * @return <code>true</code> if the the passed SRS is available in deegree
081         */
082        private boolean isAvailable( String srs ) {
083            return provider.getCRSByID( srs ) != null;
084        }
085    
086        /**
087         * @return a list of crs's with following layout 1) crsid[0], crsid[1] ... etc.
088         */
089        private List<String> getAll( boolean verify, File exportFile ) {
090    
091            List<String> allCRSs = new ArrayList<String>();
092            if ( verify ) {
093                List<CoordinateSystem> avCRS = provider.getAvailableCRSs();
094                if ( avCRS != null && avCRS.size() > 0 ) {
095                    for ( CoordinateSystem crs : avCRS ) {
096                        String[] ids = crs.getIdentifiers();
097                        if ( ids != null ) {
098                            StringBuilder sb = new StringBuilder( 300 );
099                            for ( int i = 0; i < ids.length; ++i ) {
100                                sb.append( ids[i] );
101                                if ( i + 1 < ids.length ) {
102                                    sb.append( ", " );
103                                }
104                            }
105                            allCRSs.add( sb.toString() );
106                        }
107                    }
108                }
109                if ( exportFile != null ) {
110                    StringBuilder out = new StringBuilder( 20000000 );
111                    provider.export( out, avCRS );
112                    try {
113                        BufferedWriter bw = new BufferedWriter( new FileWriter( exportFile ) );
114                        bw.write( out.toString() );
115                        bw.flush();
116                        bw.close();
117                    } catch ( IOException e ) {
118                        System.out.println( e );
119                    }
120                }
121            } else {
122                allCRSs = provider.getAvailableCRSIds();
123            }
124            Collections.sort( allCRSs );
125            return allCRSs;
126        }
127    
128        /**
129         * @param args
130         *            following parameters are supported:
131         *            <ul>
132         *            <li>[-isAvailable srsName]</li>
133         *            <li>[-file outputfile]</li>
134         *            <li>[-verify]</li>
135         *            </ul>
136         */
137        public static void main( String[] args ) {
138    
139            SRSInfo srsinfo = new SRSInfo();
140    
141            HashMap<String, String> params = new HashMap<String, String>();
142            boolean verify = false;
143            for ( int i = 0; i < args.length; i++ ) {
144                String firstArgument = args[i];
145                if ( firstArgument != null && !"".equals( firstArgument.trim() ) ) {
146                    firstArgument = firstArgument.trim();
147                    if ( firstArgument.equalsIgnoreCase( "-?" ) || firstArgument.equalsIgnoreCase( "-h" ) ) {
148                        outputHelp();
149                    } else {
150                        if ( "-verify".equalsIgnoreCase( firstArgument ) ) {
151                            verify = true;
152                        } else {
153                            if ( i + 1 < args.length ) {
154                                String val = args[++i];
155                                if ( val != null && !"".equals( val.trim() ) ) {
156                                    params.put( firstArgument, val.trim() );
157                                } else {
158                                    System.out.println( "Invalid value for parameter: " + firstArgument );
159                                }
160                            } else {
161                                System.out.println( "No value for parameter: " + firstArgument );
162                            }
163                        }
164                    }
165                }
166            }
167            String availableCRS = params.get( "-isAvailable" );
168            if ( availableCRS != null && !"".equals( availableCRS.trim() ) ) {
169                System.out.println( "Coordinates System: " + availableCRS + " is "
170                                    + ( ( srsinfo.isAvailable( availableCRS.trim() ) ) ? "" : "not " )
171                                    + "available in deegree" );
172            } else {
173                File exportFile = null;//new File( "/dev/shm/crs-configuration.xml" );
174                List<String> availableCRSs = srsinfo.getAll( verify, exportFile );
175                if ( availableCRSs != null && availableCRSs.size() > 0 ) {
176                    String file = params.get( "-file" );
177                    if ( file != null && !"".equals( file.trim() ) ) {
178                        File f = new File( file );
179                        boolean overwrite = true;
180                        if( f.exists() ){
181                            System.out.print( "The file: "+ file + " already exsists, overwrite ([y]/n): " );
182                            BufferedReader read = new BufferedReader( new InputStreamReader( System.in ) );
183                            String s = "n";
184                            try {
185                                s = read.readLine();
186                            } catch ( IOException e ) {
187                                //nottin.
188                            }
189                            if( s != null && !"".equals( s.trim() ) && !"y".equalsIgnoreCase( s.trim() ) ){
190                                overwrite = false;
191                            }
192    
193    
194                        }
195                        if ( overwrite ) {
196                            System.out.println( "Writing to file: " + f.getAbsoluteFile() );
197                            try {
198                                FileWriter fw = new FileWriter( f );
199    
200                                int count = 1;
201                                for ( String crs : availableCRSs ) {
202                                    fw.write( ( count++ ) + ")" + crs + "\n");
203                                    //fw.write( crs );
204                                }
205                                fw.close();
206                            } catch ( IOException e ) {
207                                System.out.println( "An exception occurred while trying to write to file: "
208                                                    + f.getAbsoluteFile() + "\n message:\n" + e.getMessage() );
209                                e.printStackTrace();
210                            }
211    
212                            System.exit( 1 );
213                        } else {
214                            System.out.println( "Not overwriting file: " + f.getAbsoluteFile()
215                                                + ", outputting to standard out." );
216                        }
217                    } else {
218                        System.out.println( "No File given (-file param) writing to standard out." );
219                    }
220                    int count = 1;
221                    for ( String crs : availableCRSs ) {
222                        System.out.println( ( count++ ) + ")" + crs );
223                    }
224                } else {
225                    System.out.println( "No Coordinate Systems configured, this is very strange!" );
226                }
227            }
228        }
229    
230        private static void outputHelp() {
231            StringBuilder sb = new StringBuilder();
232            sb.append( "The SRSInfo program can be used to output all available crs's configured in deegree and\n" );
233            sb.append( "will give you an affirmation on an available crs.\n\n" );
234            sb.append( "Following parameters are supported:\n" );
235            sb.append( "[-isAvailable] crs_id -- will give you an affirmation if the given crs is available.\n" );
236            sb.append( "[-file] if [-isAvailable] is not given, the -file parameter can be used to write all configured crs_s to, if not given standard out will be used.\n" );
237            sb.append( "[-verify] if [-isAvailable] is not given, the -verify flag can be used to verify if the provider can create all configured crs_s, thus verifying if the configuration is correct.\n" );
238            sb.append( "-?|-h output this text\n" );
239            System.out.println( sb.toString() );
240            System.exit( 1 );
241        }
242    }