KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > piratepete > util > DefaultDBConfig


1 package com.piratepete.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileWriter JavaDoc;
5 import org.jdom.Comment;
6 import org.jdom.Document;
7 import org.jdom.Element;
8 import org.jdom.output.XMLOutputter;
9
10 /**
11  * DefaultDBConfig Class
12  * Copyright (C) 2003 Jeremy Whitlock<p>
13  * <a HREF="mailto:jcscoobyrs@developerkb.com">jcscoobyrs@developerkb.com</a>
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.<p>
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.<p>
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.<p>
28  *
29  * <a HREF="http://www.piratepetesoftware.com">piratepetesoftware.com</a><br>
30  * <a HREF="mailto:dlwhitehurst@comcast.net">dlwhitehurst@comcast.net</a>
31  *
32  * @author Jeremy Whitlock
33  * @version 1.0a
34  *
35  */

36 public class DefaultDBConfig
37 {
38     static Document xmldoc;
39     static Element xmlroot;
40     static String JavaDoc sfile = "";
41
42     /** called to create an example of db.conf with the filename
43      * of db.conf.example
44      * @param args args[0] should be the only paramater passed
45      * and it's value should be the full path to where the
46      * db.conf.example file will be created
47      */

48     public static void main(String JavaDoc[] args)
49     {
50         sfile = args[0];
51         
52         createXMLDoc();
53         addDatabases();
54         writeXMLDoc();
55     }
56     
57     /** creates the document and adds the root node and comment
58      */

59     public static void createXMLDoc()
60     {
61         xmldoc = new Document();
62         Comment nchg = new Comment("Example Database Configuratin File");
63         xmldoc.addContent(nchg);
64         xmlroot = new Element("databases");
65         xmldoc.setRootElement(xmlroot);
66     }
67     
68     /** adds the proper XML to db.conf.example for each database supported by
69      * DBPirate to give the users an example of the proper xml
70      */

71     public static void addDatabases()
72     {
73         Comment mysqlc = new Comment("MySQL Database Configuration");
74         Comment oraclec = new Comment("Oracle Database Configuration");
75         Comment postgresqlc = new Comment("PostgreSQL Database Configuration");
76         
77         Element name = new Element("name");
78         Element classpath = new Element("classpath");
79         Element url = new Element("url");
80         Element database;
81         
82         //Adds the structure to db.conf.example for MySQL Database
83
xmlroot.addContent(mysqlc);
84         database = new Element("database");
85         name = new Element("name").addContent("MySQL");
86         classpath = new Element("classpath").addContent("com.mysql.jdbc.Driver");
87         url = new Element("url").addContent("jdbc:mysql://#host#/#sid#?user=#user#^password=#pass#");
88         database.addContent(name);
89         database.addContent(classpath);
90         database.addContent(url);
91         xmlroot.addContent(database);
92         
93         //Adds the structure to db.conf.example for Oracle Database
94
xmlroot.addContent(oraclec);
95         database = new Element("database");
96         name = new Element("name").addContent("Oracle");
97         classpath = new Element("classpath").addContent("oracle.jdbc.OracleDriver");
98         url = new Element("url").addContent("jdbc:oracle:thin@#host#:1521:#sid#:#user#:#pass#");
99         database.addContent(name);
100         database.addContent(classpath);
101         database.addContent(url);
102         xmlroot.addContent(database);
103         
104         //Adds the structure to db.conf.example for PostgreSQL Database
105
xmlroot.addContent(postgresqlc);
106         database = new Element("database");
107         name = new Element("name").addContent("PostgreSQL");
108         classpath = new Element("classpath").addContent("org.postgresql.Driver");
109         url = new Element("url").addContent("jdbc:postgresql://#host#/#sid#?user=#user#^password=#pass#");
110         database.addContent(name);
111         database.addContent(classpath);
112         database.addContent(url);
113         xmlroot.addContent(database);
114     }
115     
116     /** called to write the XML file to the proper location
117      */

118     public static void writeXMLDoc()
119     {
120         File JavaDoc nconf = new File JavaDoc(sfile);
121     
122         try
123         {
124             XMLOutputter outputter = new XMLOutputter(" ", true);
125             FileWriter JavaDoc writer = new FileWriter JavaDoc(nconf);
126
127             outputter.output(xmldoc, writer);
128             //outputter.output(xmldoc, System.out);
129
writer.close();
130         }
131         catch (java.io.IOException JavaDoc e)
132         {
133             e.printStackTrace();
134         }
135     }
136 }
137
Popular Tags