001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools.dbloader;
007:
008: import org.xml.sax.Attributes;
009: import org.xml.sax.ContentHandler;
010: import org.xml.sax.Locator;
011: import org.xml.sax.SAXException;
012:
013: /**
014: * Class for handling SAX events during parsing of tables.xml.
015: *
016: * @author Ken Weiner, kweiner@unicon.net
017: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
018: * @version $Revision: 36814 $
019: */
020: public class TableHandler implements ContentHandler {
021: private Configuration config = null;
022: private static final int UNSET = -1;
023: private static final int DROP = 0;
024: private static final int CREATE = 1;
025: private int mode = UNSET;
026: private StringBuffer stmtBuffer;
027: private String tableName = null;
028:
029: public TableHandler(Configuration config) {
030: this .config = config;
031: }
032:
033: public void startDocument() {
034: config.getLog().println();
035: }
036:
037: public void endDocument() {
038: config.getLog().println();
039: }
040:
041: public void startElement(String namespaceURI, String localName,
042: String qName, Attributes atts) {
043: if (qName.equals("statement")) {
044: stmtBuffer = new StringBuffer(1024);
045: String statementType = atts.getValue("type");
046: tableName = atts.getValue("name");
047:
048: if (mode == UNSET || mode == CREATE
049: && statementType != null
050: && statementType.equals("drop")) {
051: mode = DROP;
052:
053: if (config.getDropTables())
054: config.getLog().println("Dropping tables...");
055: else
056: config.getLog().println(
057: "Dropping tables...disabled.");
058: } else if (mode == UNSET || mode == DROP
059: && statementType != null
060: && statementType.equals("create")) {
061: mode = CREATE;
062: config.getLog().println();
063:
064: if (config.getCreateTables())
065: config.getLog().println("Creating tables...");
066: else
067: config.getLog().println(
068: "Creating tables...disabled.");
069: }
070: }
071: }
072:
073: public void endElement(String namespaceURI, String localName,
074: String qName) {
075: if (qName.equals("statement")) {
076: String statement = stmtBuffer.toString();
077:
078: switch (mode) {
079: case DROP:
080: if (config.getDropTables()) {
081: config.getLog().println(" " + tableName);
082: DbUtils.dropTable(config, statement);
083: }
084: if (config.getCreateScript()) {
085: config.getLog().println(" " + tableName);
086: DbUtils.dumpTableAction(config, statement);
087: }
088: break;
089: case CREATE:
090: if (config.getCreateTables()) {
091: config.getLog().println(" " + tableName);
092: DbUtils.createTable(config, statement);
093: }
094: if (config.getCreateScript()) {
095: config.getLog().println(" " + tableName);
096: DbUtils.dumpTableAction(config, statement);
097: }
098: break;
099: default:
100: break;
101: }
102: }
103: }
104:
105: public void characters(char ch[], int start, int length) {
106: stmtBuffer.append(ch, start, length);
107: }
108:
109: public void setDocumentLocator(Locator locator) {
110: }
111:
112: public void processingInstruction(String target, String data) {
113: }
114:
115: public void ignorableWhitespace(char[] ch, int start, int length) {
116: }
117:
118: public void startPrefixMapping(String prefix, String uri)
119: throws SAXException {
120: };
121:
122: public void endPrefixMapping(String prefix) throws SAXException {
123: };
124:
125: public void skippedEntity(String name) throws SAXException {
126: };
127: }
|