001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/wms/WMS2WMC.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.wms;
044    
045    import java.io.File;
046    import java.io.FileOutputStream;
047    import java.io.IOException;
048    import java.net.MalformedURLException;
049    import java.net.URL;
050    import java.util.Properties;
051    
052    import javax.xml.transform.TransformerException;
053    
054    import org.deegree.framework.xml.XMLFragment;
055    import org.deegree.framework.xml.XSLTDocument;
056    import org.xml.sax.SAXException;
057    
058    /**
059     * program for creating a Web Map Context document from a WMS capabilities file
060     * 
061     * 
062     * @version $Revision: 9346 $
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @author last edited by: $Author: apoth $
065     * 
066     * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
067     * 
068     * @since 2.0
069     */
070    public class WMS2WMC {
071    
072        private URL xsl = WMS2WMC.class.getResource( "wms2wmc.xsl" );
073    
074        private String inFile;
075    
076        private String outFile;
077        
078        private String wmsCapabilities;
079    
080        /**
081         * 
082         * @param inFile
083         * @param outFile
084         */
085        public WMS2WMC( String inFile, String outFile, String wmsCapabilities ) {
086            this.inFile = inFile;
087            this.outFile = outFile;
088            this.wmsCapabilities = wmsCapabilities;
089        }
090    
091        /**
092         * does all the work
093         * 
094         * @throws MalformedURLException
095         * @throws IOException
096         * @throws SAXException
097         * @throws TransformerException
098         */
099        public void run()
100                                throws MalformedURLException, IOException, SAXException, TransformerException {
101            // TODO
102            // consider already existing WMS capabilities
103            
104            File in = new File( inFile );
105            XMLFragment xml = new XMLFragment( in.toURL() );
106            XSLTDocument xslt = new XSLTDocument( xsl );
107            xml = xslt.transform( xml );
108    
109            FileOutputStream fos = new FileOutputStream( new File( outFile ) );
110            xml.write( fos );
111            fos.close();
112        }
113    
114        private static void validate( Properties map )
115                                throws Exception {
116            if ( map.get( "-inFile" ) == null ) {
117                throw new Exception( "-inFile parameter must be set" );
118            }
119            if ( map.get( "-outFile" ) == null ) {
120                throw new Exception( "-outFile parameter must be set" );
121            }
122            // TODO
123            // validate -WMSCapabilities target if parameter is available
124        }
125    
126        public static void main( String[] args )
127                                throws Exception {
128    
129            Properties map = new Properties();
130            for ( int i = 0; i < args.length; i += 2 ) {
131                System.out.println( args[i + 1] );
132                map.put( args[i], args[i + 1] );
133            }
134            try {
135                validate( map );
136            } catch ( Exception e ) {
137                System.out.println( "!!! E R R O R !!!" );
138                System.out.println( e.getMessage() );
139                return;
140            }
141    
142            WMS2WMC wms2wmc = new WMS2WMC( map.getProperty( "-inFile" ), 
143                                           map.getProperty( "-outFile" ),
144                                           map.getProperty( "-WMSCapabilities" ) );
145            wms2wmc.run();
146    
147        }
148    
149    }