001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/warehouse/api-impl/src/java/org/theospi/portfolio/util/db/GenericTableHandler.java $
003: * $Id:GenericTableHandler.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.util.db;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.Locator;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * Generic table handler will work for most databases. If not,
031: * then you can create database specific handlers having this as the
032: * starting point.
033: *
034: * This code was inspired from the Dbloader.java from uPortal by JASIG
035: *
036: * @author <a href="felipeen@udel.edu">Luis F.C. Mendes</a> - University of Delaware
037: * @version $Revision 1.0 $
038: */
039: public class GenericTableHandler implements ContentHandler {
040:
041: private static final int UNSET = -1;
042: private static final int DROP = 0;
043: private static final int CREATE = 1;
044: private static final int ALTER = 2;
045: private static final int INDEX = 3;
046: private static int mode = UNSET;
047: private static StringBuffer stmtBuffer;
048: private int treeLevel;
049: private String tmpType = "";
050: private String tmpParm = "";
051: private DbLoader loader;
052:
053: protected final Log logger = LogFactory.getLog(getClass());
054:
055: public GenericTableHandler(DbLoader loader) {
056: this .loader = loader;
057: logger.debug("Generic table handler for "
058: + this .loader.getDbName() + " ...");
059: }
060:
061: public void startDocument() {
062: }
063:
064: public void endDocument() {
065: //System.out.println();
066: }
067:
068: public void startElement(String namespaceURI, String localName,
069: String qName, Attributes atts) {
070: if (qName.equals("statement")) {
071: stmtBuffer = new StringBuffer(1024);
072: String statementType = atts.getValue("type");
073:
074: if (mode == UNSET || mode != DROP && statementType != null
075: && statementType.equals("drop")) {
076: mode = DROP;
077:
078: logger.debug("Dropping tables...");
079:
080: if (!this .loader.isDropTables())
081: logger.debug("disabled.");
082: } else if (mode == UNSET || mode != CREATE
083: && statementType != null
084: && statementType.equals("create")) {
085: mode = CREATE;
086:
087: logger.debug("Creating tables...");
088:
089: if (!this .loader.isCreateTables())
090: logger.debug("disabled.");
091: } else if (mode == UNSET || mode != ALTER
092: && statementType != null
093: && statementType.equals("alter")) {
094: mode = ALTER;
095:
096: logger.debug("Altering tables...");
097:
098: if (!this .loader.isAlterTables())
099: logger.debug("disabled.");
100: } else if (mode == UNSET || mode != INDEX
101: && statementType != null
102: && statementType.equals("index")) {
103: mode = INDEX;
104:
105: logger.debug("Indexing tables...");
106:
107: if (!this .loader.isIndexTables())
108: logger.debug("disabled.");
109: }
110:
111: }
112:
113: if (qName.equals("column-type")) {
114: ++treeLevel;
115: tmpType = "";
116: }
117: if (qName.equals("type-param")) {
118: ++treeLevel;
119: tmpParm = "";
120: }
121: }
122:
123: public void endElement(String namespaceURI, String localName,
124: String qName) {
125: if (qName.equals("statement")) {
126: treeLevel = 0;
127: String statement = stmtBuffer.toString();
128:
129: switch (mode) {
130: case DROP:
131: if (this .loader.isDropTables())
132: this .loader.dropTable(Cascade
133: .cascadeConstraint(statement));
134: //System.out.println(statement);
135: break;
136: case CREATE:
137: if (this .loader.isCreateTables())
138: this .loader.createTable(statement);
139: //System.out.println(statement);
140: break;
141: case ALTER:
142: if (this .loader.isAlterTables())
143: this .loader.alterTable(statement);
144: //System.out.println(statement);
145: break;
146: case INDEX:
147: if (this .loader.isIndexTables())
148: this .loader.indexTable(statement);
149: //System.out.println(statement);
150: break;
151: default:
152: break;
153: }
154: }
155:
156: if (qName.equals("column-type"))
157: --treeLevel;
158: if (qName.equals("type-param"))
159: --treeLevel;
160: if (treeLevel == 0)
161: parseParamToDatabase();
162: }
163:
164: public void characters(char ch[], int start, int length) {
165: if (treeLevel == 0)
166: stmtBuffer.append(ch, start, length);
167: else if (treeLevel == 1)
168: tmpType += new String(ch, start, length);
169: else if (treeLevel == 2)
170: tmpParm += new String(ch, start, length);
171: //chmaurer 1/24/06
172: //Changed the tmpType and tmpParm to do a += due to some strange
173: // buffering issues. We were seeing data types being truncated
174: // if they fell at the end of the ch[] and were cut off.
175: }
176:
177: protected void parseParamToDatabase() {
178: if (tmpParm != null && tmpParm.length() > 0) {
179: stmtBuffer.append(tmpType.trim());
180: stmtBuffer.append("(" + tmpParm.trim() + ")");
181: } else if (tmpType != null && tmpType.length() > 0)
182: stmtBuffer.append(tmpType.trim());
183:
184: tmpParm = "";
185: tmpType = "";
186: }
187:
188: public void setDocumentLocator(Locator locator) {
189: }
190:
191: public void processingInstruction(String target, String data) {
192: }
193:
194: public void ignorableWhitespace(char[] ch, int start, int length) {
195: }
196:
197: public void startPrefixMapping(String prefix, String uri)
198: throws SAXException {
199: };
200:
201: public void endPrefixMapping(String prefix) throws SAXException {
202: };
203:
204: public void skippedEntity(String name) throws SAXException {
205: };
206: }
|