| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.sqlparser; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.io.FileInputStream; |
|---|
| 37 | import java.io.FileNotFoundException; |
|---|
| 38 | import java.io.FileOutputStream; |
|---|
| 39 | import java.io.PrintWriter; |
|---|
| 40 | import java.util.Iterator; |
|---|
| 41 | import java.util.List; |
|---|
| 42 | |
|---|
| 43 | import org.jcoderz.commons.util.IoUtil; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * @author Albrecht Messner |
|---|
| 47 | */ |
|---|
| 48 | public class SqlToXml |
|---|
| 49 | { |
|---|
| 50 | private final File mInputFile; |
|---|
| 51 | private final File mOutputFile; |
|---|
| 52 | |
|---|
| 53 | public SqlToXml (String inFileName, String outFileName) |
|---|
| 54 | { |
|---|
| 55 | mInputFile = new File(inFileName); |
|---|
| 56 | mOutputFile = new File(outFileName); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public void transformSqlToXml () |
|---|
| 60 | throws FileNotFoundException, ParseException |
|---|
| 61 | { |
|---|
| 62 | final ScannerInterface scanner |
|---|
| 63 | = new SqlScanner(new FileInputStream(mInputFile)); |
|---|
| 64 | final SqlParser parser = new SqlParser(scanner); |
|---|
| 65 | final PrintWriter pw = new PrintWriter(new FileOutputStream(mOutputFile)); |
|---|
| 66 | try |
|---|
| 67 | { |
|---|
| 68 | pw.println("<tables>"); |
|---|
| 69 | |
|---|
| 70 | final List statements = parser.parse(); |
|---|
| 71 | for (final Iterator it = statements.iterator(); it.hasNext(); ) |
|---|
| 72 | { |
|---|
| 73 | final SqlStatement stmt = (SqlStatement) it.next(); |
|---|
| 74 | if (stmt instanceof CreateTableStatement) |
|---|
| 75 | { |
|---|
| 76 | transformStatementToXml((CreateTableStatement) stmt, pw); |
|---|
| 77 | } |
|---|
| 78 | else if (stmt instanceof CreateSequenceStatement) |
|---|
| 79 | { |
|---|
| 80 | transformStatementToXml((CreateSequenceStatement) stmt, pw); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | pw.println("</tables>"); |
|---|
| 85 | } |
|---|
| 86 | finally |
|---|
| 87 | { |
|---|
| 88 | IoUtil.close(pw); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | private void transformStatementToXml (CreateSequenceStatement stmt, |
|---|
| 93 | PrintWriter pw) |
|---|
| 94 | { |
|---|
| 95 | pw.println("<sequence name=\"" + stmt.getName() + "\">"); |
|---|
| 96 | pw.println(" <desc>"); |
|---|
| 97 | pw.println(stmt.getAnnotation() != null ? stmt.getAnnotation() : ""); |
|---|
| 98 | pw.println(" </desc>"); |
|---|
| 99 | pw.println("</sequence>"); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | private void transformStatementToXml (CreateTableStatement stmt, |
|---|
| 103 | PrintWriter out) |
|---|
| 104 | { |
|---|
| 105 | out.println("<table name=\"" + stmt.getTableName() + "\">"); |
|---|
| 106 | |
|---|
| 107 | if (stmt.getAnnotation() != null) |
|---|
| 108 | { |
|---|
| 109 | out.println(stmt.getAnnotation()); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | for (final Iterator it = stmt.getColumns().iterator(); it.hasNext(); ) |
|---|
| 113 | { |
|---|
| 114 | final ColumnSpec column = (ColumnSpec) it.next(); |
|---|
| 115 | out.print(" <column " |
|---|
| 116 | + "name=\"" + column.getColumnName() + "\" " |
|---|
| 117 | + "type=\"" + column.getColumnType()); |
|---|
| 118 | StringBuffer sbuf = null; |
|---|
| 119 | for (final Iterator it2 = column.getDatatypeAttributes().iterator(); |
|---|
| 120 | it2.hasNext(); ) |
|---|
| 121 | { |
|---|
| 122 | final ColumnAttribute attr = (ColumnAttribute) it2.next(); |
|---|
| 123 | if (sbuf == null) |
|---|
| 124 | { |
|---|
| 125 | sbuf = new StringBuffer(); |
|---|
| 126 | sbuf.append('('); |
|---|
| 127 | } |
|---|
| 128 | else if (attr instanceof StringAttribute) |
|---|
| 129 | { |
|---|
| 130 | sbuf.append(' '); |
|---|
| 131 | } |
|---|
| 132 | else |
|---|
| 133 | { |
|---|
| 134 | sbuf.append(','); |
|---|
| 135 | } |
|---|
| 136 | sbuf.append(attr.getValue()); |
|---|
| 137 | } |
|---|
| 138 | if (sbuf != null) |
|---|
| 139 | { |
|---|
| 140 | sbuf.append(')'); |
|---|
| 141 | out.print(sbuf); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | out.println("\">"); |
|---|
| 145 | if (column.getAnnotation() != null) |
|---|
| 146 | { |
|---|
| 147 | out.println(column.getAnnotation()); |
|---|
| 148 | } |
|---|
| 149 | if (column.isNotNull()) |
|---|
| 150 | { |
|---|
| 151 | out.println(" <notnull/>"); |
|---|
| 152 | } |
|---|
| 153 | if (column.isPrimaryKey()) |
|---|
| 154 | { |
|---|
| 155 | out.println(" <primarykey/>"); |
|---|
| 156 | } |
|---|
| 157 | if (column.isUnique()) |
|---|
| 158 | { |
|---|
| 159 | out.println(" <unique/>"); |
|---|
| 160 | } |
|---|
| 161 | out.println(" </column>"); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | for (final Iterator it = stmt.getFkConstraints().iterator(); |
|---|
| 165 | it.hasNext(); ) |
|---|
| 166 | { |
|---|
| 167 | final FkConstraint fk = (FkConstraint) it.next(); |
|---|
| 168 | out.println(" <fk name=\"" + fk.getName() + "\"" |
|---|
| 169 | + " references=\"" + fk.getRefTable() + "\">"); |
|---|
| 170 | out.println(" <columns>"); |
|---|
| 171 | for (final Iterator it2 = fk.getColumns().iterator(); it2.hasNext(); ) |
|---|
| 172 | { |
|---|
| 173 | out.println(" <col>" + it2.next() + "</col>"); |
|---|
| 174 | } |
|---|
| 175 | out.println(" </columns>"); |
|---|
| 176 | out.println(" <refcolumns>"); |
|---|
| 177 | for (final Iterator it2 = fk.getRefColumns().iterator(); |
|---|
| 178 | it2.hasNext(); ) |
|---|
| 179 | { |
|---|
| 180 | out.println(" <col>" + it2.next() + "</col>"); |
|---|
| 181 | } |
|---|
| 182 | out.println(" </refcolumns>"); |
|---|
| 183 | out.println(" </fk>"); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | for (final Iterator it = stmt.getIndexes().iterator(); it.hasNext(); ) |
|---|
| 187 | { |
|---|
| 188 | final CreateIndexStatement idxStmt = (CreateIndexStatement) it.next(); |
|---|
| 189 | out.println(" <index name=\"" + idxStmt.getIndexName() + "\">"); |
|---|
| 190 | if (idxStmt.isUnique()) |
|---|
| 191 | { |
|---|
| 192 | out.println(" <unique/>"); |
|---|
| 193 | } |
|---|
| 194 | out.println(" <desc>"); |
|---|
| 195 | out.println( |
|---|
| 196 | idxStmt.getAnnotation() != null ? idxStmt.getAnnotation() : ""); |
|---|
| 197 | out.println(" </desc>"); |
|---|
| 198 | for (final Iterator it2 = idxStmt.getColumnNames().iterator(); |
|---|
| 199 | it2.hasNext(); ) |
|---|
| 200 | { |
|---|
| 201 | out.println(" <column name=\"" + it2.next() + "\"/>"); |
|---|
| 202 | } |
|---|
| 203 | out.println(" </index>"); |
|---|
| 204 | } |
|---|
| 205 | out.println("</table>"); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | public static void main (String[] args) |
|---|
| 209 | { |
|---|
| 210 | String inputFile = null; |
|---|
| 211 | String outputFile = null; |
|---|
| 212 | |
|---|
| 213 | int i = 0; |
|---|
| 214 | try |
|---|
| 215 | { |
|---|
| 216 | for (i = 0; i < args.length; i++) |
|---|
| 217 | { |
|---|
| 218 | if (args[i].equals("-i")) |
|---|
| 219 | { |
|---|
| 220 | inputFile = args[++i]; |
|---|
| 221 | } |
|---|
| 222 | else if (args[i].equals("-o")) |
|---|
| 223 | { |
|---|
| 224 | outputFile = args[++i]; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | catch (ArrayIndexOutOfBoundsException x) |
|---|
| 230 | { |
|---|
| 231 | System.err.println("Error: argument " |
|---|
| 232 | + args[i - 1] + " requires an option"); |
|---|
| 233 | usage(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | if (inputFile == null || outputFile == null) |
|---|
| 237 | { |
|---|
| 238 | usage(); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | final SqlToXml transformer = new SqlToXml(inputFile, outputFile); |
|---|
| 242 | try |
|---|
| 243 | { |
|---|
| 244 | transformer.transformSqlToXml(); |
|---|
| 245 | } |
|---|
| 246 | catch (Exception e) |
|---|
| 247 | { |
|---|
| 248 | System.err.println("File transformation failed: " + e.getMessage()); |
|---|
| 249 | e.printStackTrace(System.err); |
|---|
| 250 | System.exit(1); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | private static void usage () |
|---|
| 255 | { |
|---|
| 256 | System.err.println("Usage: SqlToXml -i <input_file> -o <output_file>"); |
|---|
| 257 | System.exit(1); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|