View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/xmldb/DbTool.java,v 1.8 2004/08/18 12:26:11 hkollmann Exp $
3    * $Revision: 1.8 $
4    * $Date: 2004/08/18 12:26:11 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.xmldb;
25  
26  import java.io.*;
27  
28  import java.sql.*;
29  
30  import java.util.*;
31  
32  
33  
34  /***
35   * DOCUMENT ME!
36   *
37   * @version $Revision: 1.8 $
38   * @author $author$
39   */
40  public class DbTool {
41     private String dbURL;
42     private String driverClass;
43     private String outputFile;
44     private String propertyFile;
45  
46     /***
47      * Creates a new DbTool object.
48      *
49      * @param propertyFile DOCUMENT ME!
50      * @param outputFile DOCUMENT ME!
51      */
52     public DbTool(String propertyFile,
53                   String outputFile) {
54        this.propertyFile = propertyFile;
55        this.outputFile   = outputFile;
56     }
57  
58     /***
59      * DOCUMENT ME!
60      */
61     public void createXMLOutput() {
62        try {
63           StringBuffer result = new StringBuffer();
64           result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n\n<dbforms-config>\n");
65  
66           Connection con = createConnection();
67  
68           if (con == null) {
69              System.exit(1);
70           }
71  
72           DatabaseMetaData dbmd  = con.getMetaData();
73           String[]         types = {
74                                       "TABLE",
75                                       "VIEW"
76                                    };
77           ResultSet tablesRS = dbmd.getTables("", "", "", types);
78  
79           while (tablesRS.next()) {
80              String tableName = tablesRS.getString(3);
81  
82              result.append("\t<table name=\"");
83              result.append(tableName);
84              result.append("\">\n");
85  
86              ResultSet rsKeys = dbmd.getPrimaryKeys("", "", tableName);
87              Vector    keys = new Vector();
88  
89              while (rsKeys.next()) {
90                 String columnName = rsKeys.getString(4);
91                 keys.addElement(columnName);
92              }
93  
94              rsKeys.close();
95  
96              ResultSet rsFields = dbmd.getColumns("", "", tableName, null);
97  
98              while (rsFields.next()) {
99                 String columnName = rsFields.getString(4);
100                String typeName   = rsFields.getString(6);
101                int    columnSize = rsFields.getInt(7);
102 
103                result.append("\t\t<field name=\"");
104                result.append(columnName);
105                result.append("\" fieldType=\"");
106                result.append(typeName);
107                result.append("\" size=\"");
108                result.append(columnSize);
109                result.append("\"");
110 
111                if (keys.contains(columnName)) {
112                   result.append(" isKey=\"true\"");
113                }
114 
115                result.append("/>\n");
116             }
117 
118             rsFields.close();
119 
120             result.append("\n\t\t<!-- add \"granted-privileges\" element for security constraints -->\n\n\t</table>\n\n");
121          }
122 
123          tablesRS.close();
124 
125          result.append("\t<!-- ========== Connection =================================== -->\n");
126          result.append("\t<!--\n");
127          result.append("\tuncomment this if you have access to JNDI of an application server (see users guide for more info)\n");
128          result.append("\t<dbconnection\n");
129          result.append("\t\tname = \"jdbc/dbformstest\"\n");
130          result.append("\t\tisJndi = \"true\"\n");
131          result.append("\t/>\n");
132          result.append("\t-->\n\n");
133 
134          result.append("\t<dbconnection\n");
135          result.append("\t\tname   = \"" + dbURL + "\"\n");
136          result.append("\t\tisJndi = \"false\"\n");
137          result.append("\t\tconClass  = \"" + driverClass + "\"\n");
138          result.append("\t/>\n");
139          result.append("</dbforms-config>");
140 
141          FileOutputStream     os = new FileOutputStream(new File(outputFile));
142          ByteArrayInputStream is = new ByteArrayInputStream(result.toString().getBytes());
143 
144          byte[]               b    = new byte[1024];
145          int                  read;
146 
147          while ((read = is.read(b)) != -1) {
148             os.write(b, 0, read);
149          }
150 
151          os.close();
152          System.out.println("finished");
153       } catch (Exception e) {
154          System.out.println("Error:" + e.toString());
155          e.printStackTrace();
156       }
157    }
158 
159 
160    /***
161     * DOCUMENT ME!
162     *
163     * @param args DOCUMENT ME!
164     */
165    public static void main(String[] args) {
166       if (args.length != 2) {
167          System.out.println("usage: java DbTool propertyFile outputFile\n\nexample:\njava DbTool db.properties config.xml");
168          System.exit(1);
169       }
170 
171       new DbTool(args[0], args[1]).createXMLOutput();
172    }
173 
174 
175    private Connection createConnection()
176                                 throws SQLException, ClassNotFoundException, 
177                                        InstantiationException, IOException, 
178                                        IllegalAccessException {
179       Properties props = new Properties();
180       props.load(new FileInputStream(new File(propertyFile)));
181 
182       this.driverClass = props.getProperty("connection-class");
183       this.dbURL       = props.getProperty("connection-url");
184 
185       String dbUser    = props.getProperty("username");
186       String dbPwd = props.getProperty("password");
187 
188       System.out.println("driverClass=" + driverClass);
189       System.out.println("dbURL=" + dbURL);
190       System.out.println("dbUser=" + dbUser);
191       System.out.println("dbPwd=" + dbPwd);
192 
193       Class.forName(driverClass)
194            .newInstance();
195 
196       return DriverManager.getConnection(dbURL, dbUser, dbPwd);
197    }
198 }