001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/datastore/SchemaAnnotator.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     Aennchenstraße 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.datastore;
044    
045    import java.io.File;
046    import java.io.FileInputStream;
047    import java.io.FileWriter;
048    import java.io.IOException;
049    import java.net.URL;
050    import java.util.Properties;
051    
052    import javax.xml.transform.OutputKeys;
053    import javax.xml.transform.TransformerException;
054    
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.framework.xml.XSLTDocument;
057    import org.xml.sax.SAXException;
058    
059    /**
060     * Annotates a plain GML application schema with (default) mapping information.
061     * 
062     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
063     * @author last edited by: $Author: apoth $
064     * 
065     * @version $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
066     */
067    public class SchemaAnnotator {
068    
069        private static final String XSL_FILE = "annotator.xsl";
070    
071        private static XSLTDocument xslSheet = new XSLTDocument();
072    
073        private static Properties featureTypeMappings = new Properties();
074    
075        private static Properties propertyMappings = new Properties();
076    
077        static {
078            URL sheetURL = SchemaAnnotator.class.getResource( XSL_FILE );
079            try {
080                xslSheet.load( sheetURL );
081            } catch ( Exception e ) {
082                e.printStackTrace();
083            }
084        }
085    
086        public static String getColumnName( String columnName ) {
087            columnName = columnName.toLowerCase();
088            String colName = (String) propertyMappings.get( columnName );
089            if ( colName == null ) {
090                System.out.println( "No field name -> column mapping for feature type '" + columnName
091                                    + "'. Using field name as column name." );
092                colName = columnName;
093            }
094            return colName;
095        }
096    
097        public static String getTableName( String featureTypeName ) {
098            featureTypeName = featureTypeName.toLowerCase();
099            String tableName = (String) featureTypeMappings.get( featureTypeName );
100            if ( tableName == null ) {
101                System.out.println( "No feature type -> table mapping for feature type '" + featureTypeName
102                                    + "'. Using feature type name as table name." );
103                tableName = featureTypeName;
104            }
105            return tableName;
106        }
107    
108        /**
109         * @param args
110         * @throws SAXException
111         * @throws IOException
112         * @throws TransformerException
113         */
114        public static void main( String[] args )
115                                throws IOException, SAXException, TransformerException {
116    
117            if ( args.length < 2 ) {
118                System.out.println( "Usage: SchemaAnnotator <input.xsd> <output.xsd> [tableName.properties] [columnName.properties]" );
119                System.exit( 0 );
120            }
121    
122            String tableNames = args.length < 4 ? null : args[3];
123            String columnNames = args.length < 3 ? null : args[2];
124            URL  outputFile = new File (args[1]).toURL();
125            URL inputFile = new File (args[0]).toURL();
126    
127            if ( tableNames != null ) {
128                System.out.println( "Loading feature type -> table name translations from file '" + tableNames + "'..." );
129                featureTypeMappings.load( new FileInputStream( new URL( tableNames ).getFile() ) );
130            }
131    
132            if ( columnNames != null ) {
133                System.out.println( "Loading property -> column name translations from file '" + columnNames + "'..." );
134                propertyMappings.load( new FileInputStream( new URL( columnNames ).getFile() ) );
135            }
136    
137            System.out.println( "Loading input schema file '" + inputFile + "'..." );
138            XMLFragment inputSchema = new XMLFragment();
139            inputSchema.load( inputFile );
140    
141            System.out.println( "Adding annotation information..." );
142            XMLFragment outputSchema = xslSheet.transform( inputSchema );
143    
144            System.out.println( "Writing annotated schema file '" + outputFile + "'..." );
145            FileWriter writer = new FileWriter( outputFile.getFile() );
146            Properties properties = new Properties();
147    
148            properties.setProperty( OutputKeys.ENCODING, "UTF-8" );
149            outputSchema.write( writer, properties );
150            writer.close();
151        }
152    }