001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 16/11/2005 18:42:42
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.install;
044:
045: import java.io.BufferedReader;
046: import java.io.FileReader;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: import net.jforum.exceptions.ForumException;
051:
052: /**
053: * @author Rafael Steil
054: * @version $Id: ParseDBStructFile.java,v 1.7 2007/07/28 14:17:13 rafaelsteil Exp $
055: */
056: public class ParseDBStructFile {
057: public static List parse(String filename) {
058: List statements = new ArrayList();
059:
060: BufferedReader reader = null;
061:
062: try {
063: reader = new BufferedReader(new FileReader(filename));
064: StringBuffer sb = new StringBuffer(512);
065:
066: boolean processing = false;
067: char delimiter = ';';
068: String[] creators = { "CREATE INDEX", "CREATE TABLE",
069: "CREATE SEQUENCE", "DROP TABLE", "IF EXISTS",
070: "DROP SEQUENCE", "DROP INDEX" };
071:
072: String line;
073: while ((line = reader.readLine()) != null) {
074: if (line.length() == 0) {
075: continue;
076: }
077:
078: char charAt = line.charAt(0);
079:
080: // Ignore comments
081: if (charAt == '-' || charAt == '#') {
082: continue;
083: }
084:
085: if (processing) {
086: sb.append(line);
087:
088: if (line.indexOf(delimiter) > -1) {
089: sb.delete(sb.length() - 1, sb.length());
090: statements.add(sb.toString());
091: processing = false;
092: }
093: } else {
094: for (int i = 0; i < creators.length; i++) {
095: if (line.indexOf(creators[i]) > -1) {
096: sb.delete(0, sb.length());
097:
098: if (line.indexOf(delimiter) > -1) {
099: if (line.indexOf(';') > -1) {
100: line = line.replace(';', ' ');
101: }
102:
103: statements.add(line);
104: } else {
105: sb.append(line);
106: processing = true;
107: }
108:
109: break;
110: }
111: }
112: }
113: }
114: } catch (Exception e) {
115: throw new ForumException(e);
116: } finally {
117: if (reader != null) {
118: try {
119: reader.close();
120: } catch (Exception e) {
121: // catch close BufferedReader
122: }
123: }
124: }
125:
126: return statements;
127: }
128: }
|