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