001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/srs/SRSInfo.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     53177 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    package org.deegree.tools.srs;
044    
045    import java.io.File;
046    import java.io.FileWriter;
047    import java.io.IOException;
048    import java.util.ArrayList;
049    import java.util.HashMap;
050    import java.util.List;
051    
052    import org.deegree.crs.configuration.CRSConfiguration;
053    import org.deegree.crs.configuration.CRSProvider;
054    import org.deegree.crs.coordinatesystems.CoordinateSystem;
055    
056    /**
057     * A utility program to inform the callee about the availability (-isAvailable param) of a certain crs or to retrieve
058     * all available crs's from the deegree crs configuration.
059     * 
060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     * @author last edited by: $Author: rbezema $
062     * 
063     * @version $Revision: 9648 $, $Date: 2008-01-21 13:21:52 +0100 (Mo, 21 Jan 2008) $
064     */
065    public class SRSInfo {
066        private final CRSProvider 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() {
090            List<CoordinateSystem> avCRS = provider.getAvailableCRSs();
091            List<String> allCRSs = new ArrayList<String>();
092            if ( avCRS != null && avCRS.size() > 0 ) {
093                int count = 1;
094                for ( CoordinateSystem crs : avCRS ) {
095                    String[] ids = crs.getIdentifiers();
096                    if ( ids != null ) {
097                        StringBuilder sb = new StringBuilder( 300 );
098                        sb.append( ( count++ ) + ") " );
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                        sb.append( "\n" );
106                        allCRSs.add( sb.toString() );
107                    }
108                }
109            }
110            return allCRSs;
111        }
112    
113        /**
114         * @param args
115         *            following parameters are supported:
116         *            <ul>
117         *            <li>[-isAvailable srsName]</li>
118         *            <li>[-file outputfile]</li>
119         *            </ul>
120         */
121        public static void main( String[] args ) {
122    
123            SRSInfo srsinfo = new SRSInfo();
124    
125            HashMap<String, String> params = new HashMap<String, String>();
126            for ( int i = 0; i < args.length; i++ ) {
127                String arg = args[i];
128                if ( args != null && !"".equals( arg.trim() ) ) {
129                    arg = arg.trim();
130                    if ( arg.equalsIgnoreCase( "-?" ) || arg.equalsIgnoreCase( "-h" ) ) {
131                        outputHelp();
132                    } else {
133                        if ( i + 1 < args.length ) {
134                            String val = args[++i];
135                            if ( val != null && !"".equals( val.trim() ) ) {
136                                params.put( arg, val.trim() );
137                            } else {
138                                System.out.println( "Invalid value for parameter: " + arg );
139                            }
140                        } else {
141                            System.out.println( "No value for parameter: " + arg );
142                        }
143                    }
144                }
145            }
146            String availableCRS = params.get( "-isAvailable" );
147            if ( availableCRS != null && !"".equals( availableCRS.trim() ) ) {
148                System.out.println( "Coordinates System: " + availableCRS
149                                    + " is "
150                                    + ( ( srsinfo.isAvailable( availableCRS.trim() ) ) ? "" : "not " )
151                                    + "available in deegree" );
152            } else {
153                List<String> availableCRSs = srsinfo.getAll();
154                if ( availableCRSs != null && availableCRSs.size() > 0 ) {
155                    String file = params.get( "-file" );
156                    if ( file != null && !"".equals( file.trim() ) ) {
157                        File f = new File( file );
158                        if ( !f.exists() && f.canWrite() ) {
159                            System.out.println( "Writing to file: " + f.getAbsoluteFile() );
160                            try {
161                                FileWriter fw = new FileWriter( f );
162    
163                                for ( String crs : availableCRSs ) {
164                                    fw.write( crs );
165                                }
166                                fw.close();
167                            } catch ( IOException e ) {
168                                System.out.println( "An exception occurred while trying to write to file: " + f.getAbsoluteFile()
169                                                    + "\n message:\n"
170                                                    + e.getMessage() );
171                                e.printStackTrace();
172                            }
173    
174                            System.exit( 1 );
175                        } else {
176                            System.out.println( "The given file: " + f.getAbsoluteFile()
177                                                + " could not be written to, outputting to standard out." );
178                        }
179                    } else {
180                        System.out.println( "No File given (-file param) writing to standard out." );
181                    }
182                    for ( String crs : availableCRSs ) {
183                        System.out.println( crs );
184                    }
185                } else {
186                    System.out.println( "No Coordinate Systems configured, this is very strange!" );
187                }
188            }
189        }
190    
191        private static void outputHelp() {
192            StringBuilder sb = new StringBuilder();
193            sb.append( "The SRSInfo program can be used to output all available crs's configured in deegree and\n" );
194            sb.append( "will give you an affirmation on an available crs.\n\n" );
195            sb.append( "Following parameters are supported:\n" );
196            sb.append( "[-isAvailable] crs_id -- will give you an affirmation if the given crs is available.\n" );
197            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" );
198            sb.append( "-?|-h output this text\n" );
199            System.out.println( sb.toString() );
200            System.exit( 1 );
201        }
202    }