001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.database.xml;
020:
021: import org.lucane.common.Logging;
022: import org.lucane.server.database.DatabaseAbstractionLayer;
023:
024: import javax.xml.parsers.*;
025: import org.w3c.dom.*;
026:
027: import java.sql.*;
028:
029: public abstract class TableCreator {
030: private DatabaseAbstractionLayer dbLayer;
031:
032: /**
033: * Set the database layer
034: */
035: public void setDbLayer(DatabaseAbstractionLayer dbLayer) {
036: this .dbLayer = dbLayer;
037: }
038:
039: /**
040: * Get the database layer
041: */
042: public DatabaseAbstractionLayer getDbLayer() {
043: return this .dbLayer;
044: }
045:
046: /**
047: * Create tables from a xml file describing a database subset
048: *
049: * @param xmlfile the path to the xml description
050: */
051: public void createFromXml(String xmlfile) throws Exception {
052: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
053: .newDocumentBuilder();
054: Document document = builder.parse(xmlfile);
055:
056: //-- root element
057: Node node = document.getFirstChild();
058: while (node != null && node.getNodeType() != Node.ELEMENT_NODE)
059: node = node.getNextSibling();
060:
061: if (node == null || !node.getNodeName().equals("database"))
062: throw new Exception(
063: "root element is different from 'database'");
064:
065: //-- tables
066: Connection c = getDbLayer().getConnection();
067: node = node.getFirstChild();
068: while (node != null) {
069: if (node.getNodeName().equals("table"))
070: createTableFromXmlNode(c, node);
071:
072: node = node.getNextSibling();
073: }
074: c.close();
075:
076: Logging.getLogger().info(
077: "Created tables from '" + xmlfile + "'.");
078: }
079:
080: /**
081: * Create only one table from a xml file
082: *
083: * @param xmlfile the path to the xml description
084: * @param table the table to create
085: */
086: public void createFromXml(String xmlfile, String table)
087: throws Exception {
088: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
089: .newDocumentBuilder();
090: Document document = builder.parse(xmlfile);
091:
092: //-- root element
093: Node node = document.getFirstChild();
094: while (node != null && node.getNodeType() != Node.ELEMENT_NODE)
095: node = node.getNextSibling();
096:
097: if (node == null || !node.getNodeName().equals("database"))
098: throw new Exception(
099: "root element is different from 'database'");
100:
101: //-- tables
102: Connection c = getDbLayer().getConnection();
103: node = node.getFirstChild();
104: while (node != null) {
105: if (node.getNodeName().equals("table")) {
106: String tableName = node.getAttributes().getNamedItem(
107: "name").getNodeValue();
108: if (tableName.equals(table))
109: createTableFromXmlNode(c, node);
110: }
111: node = node.getNextSibling();
112: }
113: c.close();
114:
115: Logging.getLogger().info(
116: "Created tables from '" + xmlfile + "'.");
117: }
118:
119: /**
120: * Create a table from a xml node
121: *
122: * @param connection an already opened connection
123: * @param node the node describing the table
124: */
125: public abstract void createTableFromXmlNode(Connection connection,
126: Node node) throws Exception;
127: }
|