KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > database > xml > TableCreator


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.database.xml;
20
21 import org.lucane.common.Logging;
22 import org.lucane.server.database.DatabaseAbstractionLayer;
23
24 import javax.xml.parsers.*;
25 import org.w3c.dom.*;
26
27 import java.sql.*;
28
29 public abstract class TableCreator
30 {
31     private DatabaseAbstractionLayer dbLayer;
32     
33     /**
34      * Set the database layer
35      */

36     public void setDbLayer(DatabaseAbstractionLayer dbLayer)
37     {
38         this.dbLayer = dbLayer;
39     }
40     
41     /**
42      * Get the database layer
43      */

44     public DatabaseAbstractionLayer getDbLayer()
45     {
46         return this.dbLayer;
47     }
48     
49     /**
50      * Create tables from a xml file describing a database subset
51      *
52      * @param xmlfile the path to the xml description
53      */

54     public void createFromXml(String JavaDoc xmlfile)
55     throws Exception JavaDoc
56     {
57         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
58         Document document = builder.parse(xmlfile);
59         
60         //-- root element
61
Node node = document.getFirstChild();
62         while(node != null && node.getNodeType() != Node.ELEMENT_NODE)
63             node = node.getNextSibling();
64         
65         if(node == null || !node.getNodeName().equals("database"))
66             throw new Exception JavaDoc("root element is different from 'database'");
67         
68         
69         //-- tables
70
Connection c = getDbLayer().getConnection();
71         node = node.getFirstChild();
72         while(node != null)
73         {
74             if(node.getNodeName().equals("table"))
75                 createTableFromXmlNode(c, node);
76             
77             node = node.getNextSibling();
78         }
79         c.close();
80         
81         Logging.getLogger().info("Created tables from '" +xmlfile + "'.");
82     }
83     
84     /**
85      * Create only one table from a xml file
86      *
87      * @param xmlfile the path to the xml description
88      * @param table the table to create
89      */

90     public void createFromXml(String JavaDoc xmlfile, String JavaDoc table)
91     throws Exception JavaDoc
92     {
93         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
94         Document document = builder.parse(xmlfile);
95         
96         //-- root element
97
Node node = document.getFirstChild();
98         while(node != null && node.getNodeType() != Node.ELEMENT_NODE)
99             node = node.getNextSibling();
100         
101         if(node == null || !node.getNodeName().equals("database"))
102             throw new Exception JavaDoc("root element is different from 'database'");
103         
104         
105         //-- tables
106
Connection c = getDbLayer().getConnection();
107         node = node.getFirstChild();
108         while(node != null)
109         {
110             if(node.getNodeName().equals("table"))
111             {
112                 String JavaDoc tableName = node.getAttributes().getNamedItem("name").getNodeValue();
113                 if(tableName.equals(table))
114                     createTableFromXmlNode(c, node);
115             }
116             node = node.getNextSibling();
117         }
118         c.close();
119         
120         Logging.getLogger().info("Created tables from '" +xmlfile + "'.");
121     }
122     
123     /**
124      * Create a table from a xml node
125      *
126      * @param connection an already opened connection
127      * @param node the node describing the table
128      */

129     public abstract void createTableFromXmlNode(Connection connection, Node node) throws Exception JavaDoc;
130 }
131
Popular Tags