Project

General

Profile

1
// 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.domutils.DF_Oracle;
5
import de.tudarmstadt.ito.domutils.NQ_Oracle;
6
import de.tudarmstadt.ito.xmldbms.DBMSToDOM;
7
import de.tudarmstadt.ito.xmldbms.DOMToDBMS;
8
import de.tudarmstadt.ito.xmldbms.Map;
9
import de.tudarmstadt.ito.xmldbms.helpers.KeyGeneratorImpl;
10
import de.tudarmstadt.ito.xmldbms.mapfactories.MapFactory_MapDocument;
11

    
12
import java.io.File;
13
import java.io.FileOutputStream;
14
import java.io.OutputStream;
15
import java.sql.Connection;
16
import java.sql.DriverManager;
17
import oracle.xml.parser.XMLParser;
18
import oracle.xml.parser.XMLDocument;
19
import org.xml.sax.InputSource;
20
import org.xml.sax.Parser;
21
import org.w3c.dom.Document;
22

    
23
/**
24
 * This application accepts a map document name, an XML document name, and
25
 * (in the case of transferring data from the database to an XML document) a
26
 * table name and key value. It transfers data in the specified direction.
27
 *
28
 * <P>The command line syntax for this application is:</P>
29
 * <PRE>
30
 *    java Transfer {-todbms|-toxml} <map-file> <xml-file> [<table-name> <key-value>...]
31
 * </PRE>
32
 *
33
 * <P>where:</P>
34
 *
35
 *    -todbms | -toxml indicates the direction of data transfer
36
 *    <map-file> is the name of the XML-DBMS map document
37
 *    <xml-file> is the name of the input or output XML document
38
 *    <table-name> is the name of the table containing the data to retrieve
39
 *       (-toxml direction only)
40
 *    <key-value>... is one or more values in a (multi-part) key
41
 *       (-toxml direction only)
42
 *
43
 * <P>For this application to work, there must be an ODBC data source named
44
 * "xmldbms" and the Oracle parser must be installed. The tables used to store
45
 * data must have already been created in the data source. If the map document
46
 * specifies that keys are to be generated, the default KeyGenerator
47
 * implementation is used; for more information, see
48
 * de.tudarmstadt.ito.xmldbms.helper.KeyGeneratorImpl.</P>
49
 */
50

    
51
public class Transfer
52
{
53
   public static void main (String[] argv)
54
   {
55
      String   mapFilename = null,
56
               xmlFilename = null,
57
               url = "jdbc:odbc:xmldbms",
58
               tableName = null;
59
      Object[] key = null;
60
      boolean  toxml;
61

    
62
      try
63
      {
64
         if (argv.length < 3) throw new IllegalArgumentException();
65

    
66
         if ((!argv[0].equals("-todbms")) && (!argv[0].equals("-toxml")))
67
            throw new IllegalArgumentException();
68

    
69
         toxml = (argv[0].equals("-toxml"));
70
         mapFilename = argv[1];
71
         xmlFilename = argv[2];
72
         if (toxml)
73
         {
74
            if (argv.length < 5) throw new IllegalArgumentException();
75

    
76
            tableName = argv[3];
77

    
78
            key = new Object[argv.length - 4];
79
            for (int i = 4; i < argv.length; i++)
80
            {
81
               key[i - 4] = argv[i];
82
            }
83
         }
84

    
85
         if (toxml)
86
         {
87
            toXML(mapFilename, xmlFilename, url, tableName, key);
88
         }
89
         else
90
         {
91
            toDBMS(mapFilename, xmlFilename, url);
92
         }
93
      }
94
      catch (IllegalArgumentException iae)
95
      {
96
         System.out.println("\nUsage: java Transfer {-todbms|-toxml} <map-file> <xml-file> [<table-name> <key-value>...]\n<table-name> and <key-value> are required when -toxml is chosen.\n");
97
      }
98
      catch (Exception e)
99
      {
100
         e.printStackTrace();
101
      }
102
   }
103

    
104
   static void toDBMS(String mapFilename, String xmlFilename, String url)
105
      throws Exception
106
   {
107
      Connection       conn1, conn2;
108
      Map              map;
109
      Document         doc;
110
      DOMToDBMS        domToDBMS;
111
      KeyGeneratorImpl keyGenerator = null;
112

    
113
      // Connect to the database
114
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
115
      conn1 = DriverManager.getConnection(url);
116
      conn2 = DriverManager.getConnection(url);
117

    
118
      // Create and initialize a key generator
119
      keyGenerator = new KeyGeneratorImpl(conn1);
120
      keyGenerator.initialize();
121

    
122
      // Create the map, open the XML document, and transfer the data
123
      // to the database
124
      map = createMap(mapFilename, conn2);
125
      doc = openDocument(xmlFilename);
126
      domToDBMS = new DOMToDBMS(map, keyGenerator, new NQ_Oracle());
127
      domToDBMS.storeDocument(doc);
128
   }
129

    
130
   static void toXML(String mapFilename, String xmlFilename, String url, String tableName, Object[]key)
131
      throws Exception
132
   {
133
      Connection       conn;
134
      Map              map;
135
      DBMSToDOM        dbmsToDOM;
136
      Document         doc;
137
      FileOutputStream xmlFile;
138

    
139
      // Connect to the database
140
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
141
      conn = DriverManager.getConnection(url);
142

    
143
      // Create the Map and transfer the data to a DOM tree.
144
      map = createMap(mapFilename, conn);
145
      dbmsToDOM = new DBMSToDOM(map, new DF_Oracle());
146
      doc = dbmsToDOM.retrieveDocument(tableName, key);
147

    
148
      // Write the DOM tree to a file.
149
      // WARNING! This code is specific to the Oracle parser.
150
      xmlFile = new FileOutputStream(xmlFilename);
151
      ((XMLDocument)doc).print((OutputStream)xmlFile);
152
      xmlFile.close();
153
   }
154

    
155
   static Document openDocument(String xmlFilename) throws Exception
156
   {
157
      XMLParser parser;
158

    
159
      // WARNING! This code is specific to the Oracle parser.
160

    
161
      // Instantiate the parser and set various options.
162
      parser = new XMLParser();
163
      parser.showWarnings(true);
164

    
165
      // Parse the input file
166
      parser.parse(new InputSource(getFileURL(xmlFilename)));
167

    
168
      // Return the DOM tree
169
      return parser.getDocument();
170
   }
171

    
172
   static Map createMap(String mapFilename, Connection conn) throws Exception
173
   {
174
      MapFactory_MapDocument factory;
175
      XMLParser              parser;
176

    
177
      // Instantiate the parser and set various options
178
      // WARNING! This code is specific to the Oracle parser.
179
      parser = new XMLParser();
180
      parser.setValidationMode(true);
181
      parser.showWarnings(true);
182

    
183
      // Create a new map factory and create the Map. This code is not specific
184
      // to the Oracle parser -- any SAX Parser will do.
185
      factory = new MapFactory_MapDocument(conn, parser);
186
      return factory.createMap(new InputSource(getFileURL(mapFilename)));
187
   }
188

    
189
   static String getFileURL(String fileName)
190
   {
191
      File   file;
192
      String path, separator;
193

    
194
      file = new File(fileName);
195
      return "file:///" + file.getAbsolutePath();
196
   }
197
}
198

    
(6-6/14)