Project

General

Profile

1 14 jones
// No copyright, no warranty; use as you will.
2
// Written by Ronald Bourret, Technical University of Darmstadt, 1998-9
3
4
import de.tudarmstadt.ito.xmldbms.Map;
5
import de.tudarmstadt.ito.xmldbms.mapfactories.MapFactory_DTD;
6
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.OutputStream;
10
import java.sql.Connection;
11
import java.sql.DriverManager;
12
import java.util.Hashtable;
13
import oracle.xml.parser.XMLParser;
14
import org.xml.sax.InputSource;
15
import org.xml.sax.Parser;
16
17
/**
18
 * This application generates a map and a file of CREATE TABLE statements
19
 * from an input document. The input document can be a DTD, DDML document,
20
 * or XML document that has a DTD (including referencing an external DTD).
21
 * The document type is determined from the extension: .dtd for DTDs and
22
 * .ddm for DDML documents; everything else is assumed to be an XML document.
23
 * The documents have the same base name as the input document, but uses
24
 * extensions of .map and .sql.
25
 *
26
 * <P>The command line syntax for this application is:</P>
27
 * <PRE>
28
 *    java GenerateMap <input-file>
29
 * </PRE>
30
 */
31
32
public class GenerateMap
33
{
34
   static final byte[] RETURN = System.getProperty("line.separator").getBytes();
35
36
   public static void main (String[] argv)
37
   {
38
      try
39
      {
40
         if (argv.length != 1)
41
         {
42
            System.out.println("\nUsage: java GenerateMap <input-file>\n");
43
            return;
44
         }
45
46
         generateMap(argv[0]);
47
      }
48
      catch (Exception e)
49
      {
50
         e.printStackTrace();
51
      }
52
   }
53
54
   static void generateMap(String filename)
55
      throws Exception
56
   {
57
      Map              map;
58
      String           basename;
59
      InputSource      src;
60
61
      // Create an InputSource over the schema/DTD file.
62
63
      src = new InputSource(getFileURL(filename));
64
65
      // Get the basename of the file.
66
67
      basename = getBasename(filename);
68
69
      // Create and serialize the Map, then create the CREATE TABLE statements.
70
71
      map = createMap(src, filename);
72
      serializeMap(map, basename);
73
      createStatements(map, basename);
74
   }
75
76
   static Map createMap(InputSource src, String filename)
77
      throws Exception
78
   {
79
      MapFactory_DTD factory = new MapFactory_DTD();
80
      String         ext = getExtension(filename);
81
      Hashtable      namespaceURIs = null;
82
83
      // Determine the document type from its extension: .DDM for DDML and .DTD
84
      // for external subsets. Everything else is assumed to be an XML document
85
      // containing a DTD. Based on the document type, call the appropriate
86
      // functions to generate the Map.
87
88
      // If you are converting from a DTD and there are namespace prefixes
89
      // in that DTD, you need to declare them here, putting each one in
90
      // the namespaceURIs hashtable with the prefix as the key and the
91
      // namespace URI as the value. For example, the following
92
      // commented-out lines state that the DDML prefix corresponds to
93
      // the http://www.purl.org/NET/ddml/v1 URI. Note that the default
94
      // namespace can be declared by adding an empty string ("") as a key.
95
96
//      namespaceURIs = new Hashtable();
97
//      namespaceURIs.put("DDML", "http://www.purl.org/NET/ddml/v1");
98
99
      if (ext.equals("DDM"))
100
      {
101
         // DDML Document.
102
         // Note that this is hard-coded to use the Oracle version 1 parser.
103
         return factory.createMapFromSchema(src, MapFactory_DTD.SCHEMA_DDML, true, new XMLParser());
104
      }
105
      else if (ext.equals("DTD"))
106
      {
107
         // External subset (DTD)
108
         return factory.createMapFromDTD(src, MapFactory_DTD.DTD_EXTERNAL, true, namespaceURIs);
109
      }
110
      else
111
      {
112
         // Assume we have an XML document containing a DTD.
113
         return factory.createMapFromDTD(src, MapFactory_DTD.DTD_XMLDOCUMENT, true, namespaceURIs);
114
      }
115
   }
116
117
   static void serializeMap(Map map, String basename)
118
      throws Exception
119
   {
120
      FileOutputStream mapFile;
121
122
      // Serialize the map to a mapping document with the same base name.
123
124
      mapFile = new FileOutputStream(basename + ".map");
125
      map.serialize(mapFile, true, 3);
126
      mapFile.close();
127
   }
128
129
   static void createStatements(Map map, String basename)
130
      throws Exception
131
   {
132
      FileOutputStream sqlFile;
133
      String[]         createStrings;
134
      String           url = "jdbc:odbc:xmldbms",
135
                       driver = "sun.jdbc.odbc.JdbcOdbcDriver";
136
      Connection       conn;
137
138
      // Connect to the database and pass the JDBC Connection to the
139
      // Map object. The Map object needs this to retrieve information
140
      // about how to construct the CREATE TABLE statements.
141
142
      Class.forName(driver);
143
      conn = DriverManager.getConnection(url);
144
      map.setConnection(conn);
145
146
      // Create the output file.
147
148
      sqlFile = new FileOutputStream(basename + ".sql");
149
150
      // Get the CREATE TABLE strings from the Map object and write them to
151
      // the output file.
152
153
      createStrings = map.getCreateTableStrings();
154
      for (int i = 0; i < createStrings.length; i++)
155
      {
156
         sqlFile.write(createStrings[i].getBytes());
157
         sqlFile.write(RETURN);
158
      }
159
160
      // Close the output file.
161
162
      sqlFile.close();
163
   }
164
165
   static String getFileURL(String fileName)
166
   {
167
      File   file;
168
      String path, separator;
169
170
      // Construct a file URL for the file.
171
172
      file = new File(fileName);
173
      return "file:///" + file.getAbsolutePath();
174
   }
175
176
   static String getBasename(String filename)
177
   {
178
      int    period;
179
180
      // Get the basename of the file.
181
182
      period = filename.lastIndexOf('.', filename.length());
183
      if (period == -1)
184
      {
185
         return filename;
186
      }
187
      else
188
      {
189
         return filename.substring(0, period);
190
      }
191
   }
192
193
   static String getExtension(String filename)
194
   {
195
      int    period;
196
197
      // Get the file extension.
198
199
      period = filename.lastIndexOf('.', filename.length());
200
      if (period == -1)
201
      {
202
         return "";
203
      }
204
      else
205
      {
206
         return filename.substring(period + 1, filename.length()).toUpperCase();
207
      }
208
   }
209
}