Project Report: fawkez

Packagesummary org.jcoderz.phoenix.sqlparser

org.jcoderz.phoenix.sqlparser.SqlToXml

LineHitsNoteSource
1  /*
2   * $Id: SqlToXml.java 1264 2008-12-12 13:32:27Z amandel $
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 (1)   public SqlToXml (String inFileName, String outFileName)
54100    {
55100       mInputFile = new File(inFileName);
56100       mOutputFile = new File(outFileName);
57100    }
58  
59 (2)   public void transformSqlToXml ()
60           throws FileNotFoundException, ParseException
61     {
62100       final ScannerInterface scanner
63              = new SqlScanner(new FileInputStream(mInputFile));
64100       final SqlParser parser = new SqlParser(scanner);
65100       final PrintWriter pw = new PrintWriter(new FileOutputStream(mOutputFile));
66        try
67        {
68100           pw.println("<tables>");
69  
70100           final List statements = parser.parse();
71100           for (final Iterator it = statements.iterator(); it.hasNext(); )
72            {
73100              final SqlStatement stmt = (SqlStatement) it.next();
74100(3)             if (stmt instanceof CreateTableStatement)
75               {
76100                 transformStatementToXml((CreateTableStatement) stmt, pw);
77               }
780              else if (stmt instanceof CreateSequenceStatement)
79               {
800                 transformStatementToXml((CreateSequenceStatement) stmt, pw);
81               }
82100           }
83  
84100           pw.println("</tables>");
85        }
86        finally
87        {
8850           IoUtil.close(pw);
89100       }
90100    }
91  
92     private void transformStatementToXml (CreateSequenceStatement stmt,
93             PrintWriter pw)
94     {
950(4)      pw.println("<sequence name=\"" + stmt.getName() + "\">");
960       pw.println(" <desc>");
970       pw.println(stmt.getAnnotation() != null ? stmt.getAnnotation() : "");
980       pw.println(" </desc>");
990       pw.println("</sequence>");
1000    }
101  
102 (5)(6)(7)   private void transformStatementToXml (CreateTableStatement stmt,
103             PrintWriter out)
104     {
105100       out.println("<table name=\"" + stmt.getTableName() + "\">");
106  
107100       if (stmt.getAnnotation() != null)
108        {
1090          out.println(stmt.getAnnotation());
110        }
111  
112100       for (final Iterator it = stmt.getColumns().iterator(); it.hasNext(); )
113        {
114100          final ColumnSpec column = (ColumnSpec) it.next();
115100          out.print(" <column "
116                 + "name=\"" + column.getColumnName() + "\" "
117                 + "type=\"" + column.getColumnType());
118100          StringBuffer sbuf = null;
119100          for (final Iterator it2 = column.getDatatypeAttributes().iterator();
120100                it2.hasNext(); )
121           {
122100             final ColumnAttribute attr = (ColumnAttribute) it2.next();
123100             if (sbuf == null)
124              {
125100                sbuf = new StringBuffer();
126100                sbuf.append('(');
127              }
1280             else if (attr instanceof StringAttribute)
129              {
1300                sbuf.append(' ');
131              }
132              else
133              {
1340                sbuf.append(',');
135              }
136100             sbuf.append(attr.getValue());
137100          }
138100          if (sbuf != null)
139           {
140100             sbuf.append(')');
141100             out.print(sbuf);
142           }
143  
144100          out.println("\">");
145100          if (column.getAnnotation() != null)
146           {
1470             out.println(column.getAnnotation());
148           }
149100          if (column.isNotNull())
150           {
151100             out.println(" <notnull/>");
152           }
153100          if (column.isPrimaryKey())
154           {
155100             out.println(" <primarykey/>");
156           }
157100          if (column.isUnique())
158           {
1590             out.println(" <unique/>");
160           }
161100          out.println(" </column>");
162100       }
163  
164100       for (final Iterator it = stmt.getFkConstraints().iterator();
165100           it.hasNext(); )
166        {
1670          final FkConstraint fk = (FkConstraint) it.next();
1680          out.println(" <fk name=\"" + fk.getName() + "\""
169                 + " references=\"" + fk.getRefTable() + "\">");
1700          out.println(" <columns>");
1710          for (final Iterator it2 = fk.getColumns().iterator(); it2.hasNext(); )
172           {
1730             out.println(" <col>" + it2.next() + "</col>");
174           }
1750          out.println(" </columns>");
1760          out.println(" <refcolumns>");
1770          for (final Iterator it2 = fk.getRefColumns().iterator();
1780              it2.hasNext(); )
179           {
1800             out.println(" <col>" + it2.next() + "</col>");
181           }
1820          out.println(" </refcolumns>");
1830          out.println(" </fk>");
1840       }
185  
186100       for (final Iterator it = stmt.getIndexes().iterator(); it.hasNext(); )
187        {
188100          final CreateIndexStatement idxStmt = (CreateIndexStatement) it.next();
189100          out.println(" <index name=\"" + idxStmt.getIndexName() + "\">");
190100          if (idxStmt.isUnique())
191           {
192100             out.println(" <unique/>");
193           }
194100          out.println(" <desc>");
19575          out.println(
196                 idxStmt.getAnnotation() != null ? idxStmt.getAnnotation() : "");
197100          out.println(" </desc>");
198100          for (final Iterator it2 = idxStmt.getColumnNames().iterator();
199100                it2.hasNext(); )
200           {
201100             out.println(" <column name=\"" + it2.next() + "\"/>");
202           }
203100          out.println(" </index>");
204100       }
205100       out.println("</table>");
206100    }
207  
208 (8)   public static void main (String[] args)
209     {
2100       String inputFile = null;
2110       String outputFile = null;
212  
2130       int i = 0;
214        try
215        {
2160          for (i = 0; i < args.length; i++)
217           {
2180(9)            if (args[i].equals("-i"))
219              {
2200                inputFile = args[++i];
221              }
2220(10)            else if (args[i].equals("-o"))
223              {
2240                outputFile = args[++i];
225              }
226           }
227  
228        }
2290       catch (ArrayIndexOutOfBoundsException x)
230        {
2310          System.err.println("Error: argument "
232              + args[i - 1] + " requires an option");
2330          usage();
2340       }
235  
2360       if (inputFile == null || outputFile == null)
237        {
2380          usage();
239        }
240  
2410       final SqlToXml transformer = new SqlToXml(inputFile, outputFile);
242        try
243        {
2440          transformer.transformSqlToXml();
245        }
2460       catch (Exception e)
247        {
2480          System.err.println("File transformation failed: " + e.getMessage());
2490          e.printStackTrace(System.err);
2500          System.exit(1);
2510       }
2520    }
253  
254     private static void usage ()
255     {
2560       System.err.println("Usage: SqlToXml -i <input_file> -o <output_file>");
2570(11)      System.exit(1);
2580    }
259  }

Findings in this File

f (12) System.out.print is used main class
f (13) System.out.print is used main class
f (14) System.out.print is used main class
c (1) 53 : 4 Missing a Javadoc comment.
c (2) 59 : 4 Missing a Javadoc comment.
w (3) 74 : 0 Method org.jcoderz.phoenix.sqlparser.SqlToXml.transformSqlToXml() uses instanceof on multiple types to arbitrate logic
i (4) 95 : 57 The String literal "\">" appears 5 times in this file; the first occurrence is on line 95
c (5) 102 : 4 Cyclomatic Complexity is 18 (max allowed is 12).
d (6) 102 : 4 Method length is 103 lines (max allowed is 100).
c (7) 102 : 12 The method transformStatementToXml() has an NPath complexity of 27090
c (8) 208 : 4 Missing a Javadoc comment.
i (9) 218 : 0 method org.jcoderz.phoenix.sqlparser.SqlToXml.main(String[]) makes literal string comparisons passing the literal as an argument
i (10) 222 : 0 method org.jcoderz.phoenix.sqlparser.SqlToXml.main(String[]) makes literal string comparisons passing the literal as an argument
i (11) 257 : 0 org.jcoderz.phoenix.sqlparser.SqlToXml.usage() invokes System.exit(...), which shuts down the entire virtual machine