001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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: aschmitz $
064: *
065: * @version $Revision: 10545 $, $Date: 2008-03-11 01:25:25 -0700 (Tue, 11 Mar 2008) $
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: /**
087: * @param columnName
088: * @return the converted column name
089: */
090: public static String getColumnName(String columnName) {
091: columnName = columnName.toLowerCase();
092: String colName = (String) propertyMappings.get(columnName);
093: if (colName == null) {
094: System.out
095: .println("No field name -> column mapping for feature type '"
096: + columnName
097: + "'. Using field name as column name.");
098: colName = columnName;
099: }
100: return colName;
101: }
102:
103: /**
104: * @param featureTypeName
105: * @return the table name
106: */
107: public static String getTableName(String featureTypeName) {
108: featureTypeName = featureTypeName.toLowerCase();
109: String tableName = (String) featureTypeMappings
110: .get(featureTypeName);
111: if (tableName == null) {
112: System.out
113: .println("No feature type -> table mapping for feature type '"
114: + featureTypeName
115: + "'. Using feature type name as table name.");
116: tableName = featureTypeName;
117: }
118: return tableName;
119: }
120:
121: /**
122: * @param args
123: * @throws SAXException
124: * @throws IOException
125: * @throws TransformerException
126: */
127: public static void main(String[] args) throws IOException,
128: SAXException, TransformerException {
129:
130: if (args.length < 2) {
131: System.out
132: .println("Usage: SchemaAnnotator <input.xsd> <output.xsd> [tableName.properties] [columnName.properties]");
133: System.exit(0);
134: }
135:
136: String tableNames = args.length < 4 ? null : args[3];
137: String columnNames = args.length < 3 ? null : args[2];
138: URL outputFile = new File(args[1]).toURL();
139: URL inputFile = new File(args[0]).toURL();
140:
141: if (tableNames != null) {
142: System.out
143: .println("Loading feature type -> table name translations from file '"
144: + tableNames + "'...");
145: featureTypeMappings.load(new FileInputStream(new URL(
146: tableNames).getFile()));
147: }
148:
149: if (columnNames != null) {
150: System.out
151: .println("Loading property -> column name translations from file '"
152: + columnNames + "'...");
153: propertyMappings.load(new FileInputStream(new URL(
154: columnNames).getFile()));
155: }
156:
157: System.out.println("Loading input schema file '" + inputFile
158: + "'...");
159: XMLFragment inputSchema = new XMLFragment();
160: inputSchema.load(inputFile);
161:
162: System.out.println("Adding annotation information...");
163: XMLFragment outputSchema = xslSheet.transform(inputSchema);
164:
165: System.out.println("Writing annotated schema file '"
166: + outputFile + "'...");
167: FileWriter writer = new FileWriter(outputFile.getFile());
168: Properties properties = new Properties();
169:
170: properties.setProperty(OutputKeys.ENCODING, "UTF-8");
171: outputSchema.write(writer, properties);
172: writer.close();
173: }
174: }
|