Coverage report

  %line %branch
net.sf.infrared.web.util.sql.SQLFormatter
0% 
0% 

 1  
 /*
 2  
  *   Copyright (c)2005 Tavant Technologies
 3  
  *   All Rights Reserved.
 4  
  *
 5  
  *   This software is furnished under a license and may be used and copied
 6  
  *   only  in  accordance  with  the  terms  of such  license and with the
 7  
  *   inclusion of the above copyright notice. This software or  any  other
 8  
  *   copies thereof may not be provided or otherwise made available to any
 9  
  *   other person. No title to and ownership of  the  software  is  hereby
 10  
  *   transferred.
 11  
  *
 12  
  *   The information in this software is subject to change without  notice
 13  
  *   and  should  not be  construed as a commitment  by Tavant Technologies.
 14  
  */
 15  
 
 16  
 package net.sf.infrared.web.util.sql;
 17  
 
 18  
 
 19  
 //(c) Dan Meany - BSD license
 20  
 
 21  
 import java.util.StringTokenizer;
 22  
 import java.util.Vector;
 23  
 
 24  
 class SQLFormatter
 25  
 {
 26  
 
 27  
     private static boolean isName(String s)
 28  
     {
 29  0
         return !isIn(s, "|SELECT|FROM|WHERE|ORDER BY|GROUP BY|HAVING|UPDATE|SET|INSERT|INTO|VALUES|DELETE|UNION|ALL|MINUS|") && !isIn(s, "|COUNT|SUM|AVG|MIN|MAX|DISTINCT|AS|ANY|AND|OR|XOR|NOT|LIKE|IN|EXISTS|IS|NULL|");
 30  
     }
 31  
 
 32  
     private static boolean isFunction(String s)
 33  
     {
 34  0
         return isIn(s, "|COUNT|SUM|AVG|MIN|MAX|");
 35  
     }
 36  
 
 37  
     public void format()
 38  
     {
 39  0
         String s = sText;
 40  0
         Vector vector = new Vector();
 41  0
         for(int i = 0; i < s.length(); i++)
 42  
         {
 43  0
             for(int j = 0; j < sEscapes.length; j++)
 44  
             {
 45  0
                 String s1 = sEscapes[j][0];
 46  0
                 String s3 = sEscapes[j][1];
 47  0
                 String s5 = sEscapes[j][2];
 48  0
                 if(!s.regionMatches(i, s1, 0, s1.length()))
 49  0
                     continue;
 50  0
                 int j1 = i + s1.length();
 51  0
                 int k1 = s.indexOf(s3, j1);
 52  0
                 if(k1 == -1)
 53  
                 {
 54  0
                     k1 = s.indexOf("\n", j1);
 55  0
                     if(k1 == -1)
 56  
                     {
 57  0
                         k1 = s.indexOf("\r", j1);
 58  0
                         if(k1 == -1)
 59  0
                             k1 = s.length() - 1;
 60  
                     }
 61  0
                     k1++;
 62  
                 } else
 63  
                 {
 64  0
                     k1 += s3.length();
 65  
                 }
 66  0
                 String s6 = s.substring(i, k1);
 67  0
                 if(s5.equals("2"))
 68  0
                     s6 = "/*" + s6.trim().substring(2) + " */";
 69  0
                 vector.addElement(s6);
 70  0
                 String s7 = s.substring(0, i);
 71  
                 String s8;
 72  0
                 if(k1 < s.length())
 73  0
                     s8 = s.substring(k1);
 74  
                 else
 75  0
                     s8 = "";
 76  0
                 String s9 = "\001";
 77  0
                 if(!s5.equals(""))
 78  
                 {
 79  0
                     if(!s7.endsWith(" "))
 80  0
                         s9 = " " + s9;
 81  0
                     if(!s8.startsWith(" "))
 82  0
                         s9 = s9 + " ";
 83  
                 }
 84  0
                 s = s7 + s9 + s8;
 85  0
                 break;
 86  
             }
 87  
 
 88  
         }
 89  
 
 90  0
         Vector vector1 = new Vector();
 91  0
         for(StringTokenizer stringtokenizer = new StringTokenizer(s); stringtokenizer.hasMoreTokens();)
 92  
         {
 93  0
             String s2 = stringtokenizer.nextToken();
 94  0
             for(StringTokenizer stringtokenizer1 = new StringTokenizer(s2, "(),", true); stringtokenizer1.hasMoreTokens(); vector1.addElement(stringtokenizer1.nextToken()));
 95  
         }
 96  
 
 97  0
         for(int k = 0; k < vector1.size() - 1; k++)
 98  
         {
 99  0
             String s4 = (String)vector1.elementAt(k) + " " + (String)vector1.elementAt(k + 1);
 100  0
             if(isMajor(s4))
 101  
             {
 102  0
                 vector1.setElementAt(s4, k);
 103  0
                 vector1.removeElementAt(k + 1);
 104  
             }
 105  
         }
 106  
 
 107  0
         int l = vector1.size();
 108  0
         String as[] = new String[l += 2];
 109  0
         as[0] = "";
 110  0
         as[l - 1] = "";
 111  0
         for(int i1 = 0; i1 < vector1.size(); i1++)
 112  0
             as[i1 + 1] = (String)vector1.elementAt(i1);
 113  
 
 114  0
         int ai[] = new class="keyword">int[l];
 115  0
         int ai1[] = new class="keyword">int[l];
 116  0
         for(int l1 = 0; l1 < l; l1++)
 117  
         {
 118  0
             boolean flag = false;
 119  0
             if(isMajor(as[l1]))
 120  0
                 flag = bCapitalizeMajor;
 121  0
             if(isMinor(as[l1]))
 122  0
                 flag = bCapitalizeMinor;
 123  0
             if(isName(as[l1]))
 124  0
                 flag = bCapitalizeNames;
 125  0
             if(flag)
 126  0
                 as[l1] = as[l1].toUpperCase();
 127  
         }
 128  
 
 129  0
         for(int i2 = 1; i2 < l - 1; i2++)
 130  
         {
 131  0
             ai[i2] = 1;
 132  0
             if(isMajor(as[i2]))
 133  
             {
 134  0
                 ai[i2 - 1] = 2;
 135  0
                 ai[i2] = 2;
 136  
             } else
 137  0
             if(as[i2].equals(","))
 138  
             {
 139  0
                 ai[i2] = 2;
 140  0
                 ai[i2 - 1] = 0;
 141  
             } else
 142  0
             if(as[i2].equals("("))
 143  
             {
 144  0
                 ai[i2] = 0;
 145  0
                 if(isFunction(as[i2 - 1]) || isName(as[i2 - 1]))
 146  0
                     ai[i2 - 1] = 0;
 147  
             } else
 148  0
             if(as[i2].equals(")"))
 149  0
                 ai[i2 - 1] = 0;
 150  
             else
 151  0
             if(as[i2].equalsIgnoreCase("AND"))
 152  0
                 if(bNewLineBeforeAnd)
 153  0
                     ai[i2 - 1] = 2;
 154  
                 else
 155  0
                     ai[i2] = 2;
 156  
         }
 157  
 
 158  0
         ai[l - 2] = 2;
 159  0
         int j2 = 0;
 160  0
         int ai2[] = new class="keyword">int[16];
 161  0
         for(int k2 = 0; k2 < l; k2++)
 162  
         {
 163  0
             if(as[k2].equals(")"))
 164  0
                 if(ai2[j2] == 0)
 165  
                 {
 166  0
                     j2--;
 167  0
                     if(k2 > 0)
 168  0
                         ai[k2 - 1] = 2;
 169  
                 } else
 170  
                 {
 171  0
                     ai2[j2]--;
 172  
                 }
 173  0
             if(isMajor(as[k2]))
 174  0
                 ai1[k2] = j2 * 2;
 175  
             else
 176  0
                 ai1[k2] = j2 * 2 + 1;
 177  0
             if(as[k2].equals("("))
 178  0
                 if(isSubSelect(as[k2 + 1]))
 179  
                 {
 180  0
                     if(j2 < 16)
 181  0
                         j2++;
 182  0
                     ai2[j2] = 0;
 183  
                 } else
 184  
                 {
 185  0
                     ai2[j2]++;
 186  
                 }
 187  
         }
 188  
 
 189  0
         String as1[] = new String[3];
 190  0
         as1[0] = "";
 191  0
         as1[1] = " ";
 192  0
         as1[2] = sNewLine;
 193  0
         StringBuffer stringbuffer = new StringBuffer();
 194  0
         for(int l2 = 1; l2 < l - 1; l2++)
 195  
         {
 196  0
             if(ai[l2 - 1] == 2)
 197  0
                 stringbuffer.append(repeatString(sIndent, ai1[l2]));
 198  0
             stringbuffer.append(as[l2] + as1[ai[l2]]);
 199  
         }
 200  
 
 201  0
         s = stringbuffer.toString();
 202  0
         for(int i3 = 0; i3 < vector.size(); i3++)
 203  
         {
 204  0
             int j3 = s.indexOf("\001");
 205  0
             s = s.substring(0, j3) + (String)vector.elementAt(i3) + s.substring(j3 + 1);
 206  
         }
 207  
 
 208  0
         sText = s;
 209  0
         if(bDebug)
 210  
         {
 211  0
             StringBuffer stringbuffer1 = new StringBuffer();
 212  0
             stringbuffer1.append("Tokens:\r\n");
 213  0
             for(int k3 = 1; k3 < l - 1; k3++)
 214  0
                 stringbuffer1.append("" + ai1[k3] + " [" + as[k3] + "] " + ai[k3] + "\r\n");
 215  
 
 216  0
             stringbuffer1.append("Escapes:\r\n");
 217  0
             for(int l3 = 0; l3 < vector.size(); l3++)
 218  0
                 stringbuffer1.append((String)vector.elementAt(l3) + "\r\n");
 219  
 
 220  0
             sDebugString = stringbuffer1.toString();
 221  
         }
 222  0
     }
 223  
 
 224  
     public void setNewLineBeforeAnd(boolean flag)
 225  
     {
 226  0
         bNewLineBeforeAnd = flag;
 227  0
     }
 228  
 
 229  
     public String getDebugString()
 230  
     {
 231  0
         return sDebugString;
 232  
     }
 233  
 
 234  
     public void setNewLine(String s)
 235  
     {
 236  0
         for(int i = 0; i < sEscapes.length; i++)
 237  
         {
 238  0
             for(int j = 0; j < sEscapes[0].length; j++)
 239  0
                 if(sEscapes[i][j].equals(sNewLine))
 240  0
                     sEscapes[i][j] = s;
 241  
 
 242  
         }
 243  
 
 244  0
         sNewLine = s;
 245  0
     }
 246  
 
 247  
     SQLFormatter()
 248  0
     {
 249  0
         sText = "";
 250  0
         sNewLine = "\r\n";
 251  0
         sIndent = "   ";
 252  0
         sDebugString = "";
 253  0
         bCapitalizeMajor = false;
 254  0
         bCapitalizeMinor = false;
 255  0
         bCapitalizeNames = false;
 256  0
         bNewLineBeforeAnd = true;
 257  0
         bDebug = false;
 258  0
     }
 259  
 
 260  
     private static boolean isMinor(String s)
 261  
     {
 262  0
         return isIn(s, "|COUNT|SUM|AVG|MIN|MAX|DISTINCT|AS|ANY|AND|OR|XOR|NOT|LIKE|IN|EXISTS|IS|NULL|");
 263  
     }
 264  
 
 265  
     private static boolean isIn(String s, String s1)
 266  
     {
 267  0
         return s1.indexOf("|" + s.toUpperCase() + "|") > -1;
 268  
     }
 269  
 
 270  
     private static boolean isSubSelect(String s)
 271  
     {
 272  0
         return isIn(s, "|SELECT|");
 273  
     }
 274  
 
 275  
     public void setCapitalizeMajor(boolean flag)
 276  
     {
 277  0
         bCapitalizeMajor = flag;
 278  0
     }
 279  
 
 280  
     public void setCapitalizeNames(boolean flag)
 281  
     {
 282  0
         bCapitalizeNames = flag;
 283  0
     }
 284  
 
 285  
     public void setIndent(int i)
 286  
     {
 287  0
         if(i < 0)
 288  0
             sIndent = "\t";
 289  
         else
 290  0
             sIndent = repeatString(" ", i);
 291  0
     }
 292  
 
 293  
     public void setDebug(boolean flag)
 294  
     {
 295  0
         bDebug = flag;
 296  0
     }
 297  
 
 298  
     private String repeatString(String s, int i)
 299  
     {
 300  0
         if(i < 1)
 301  0
             return "";
 302  0
         StringBuffer stringbuffer = new StringBuffer(s.length() * i);
 303  0
         for(int j = 0; j < i; j++)
 304  0
             stringbuffer.append(s);
 305  
 
 306  0
         return stringbuffer.toString();
 307  
     }
 308  
 
 309  
     public void setCapitalizeMinor(boolean flag)
 310  
     {
 311  0
         bCapitalizeMinor = flag;
 312  0
     }
 313  
 
 314  
     private static boolean isMajor(String s)
 315  
     {
 316  0
         return isIn(s, "|SELECT|FROM|WHERE|ORDER BY|GROUP BY|HAVING|UPDATE|SET|INSERT|INTO|VALUES|DELETE|UNION|ALL|MINUS|");
 317  
     }
 318  
 
 319  
     public String getText()
 320  
     {
 321  0
         return sText;
 322  
     }
 323  
 
 324  
     public void setText(String s)
 325  
     {
 326  0
         sText = s;
 327  0
     }
 328  
 
 329  
     private static final String MAJOR_WORDS = "|SELECT|FROM|WHERE|ORDER BY|GROUP BY|HAVING|UPDATE|SET|INSERT|INTO|VALUES|DELETE|UNION|ALL|MINUS|";
 330  
     private static final String MINOR_WORDS = "|COUNT|SUM|AVG|MIN|MAX|DISTINCT|AS|ANY|AND|OR|XOR|NOT|LIKE|IN|EXISTS|IS|NULL|";
 331  
     private static final String FUNCTION_WORDS = "|COUNT|SUM|AVG|MIN|MAX|";
 332  
     private static final String SUB_SELECT = "|SELECT|";
 333  
     private static final String ESCAPE_TOKEN = "\001";
 334  
     private static final String DELIMITERS = "(),";
 335  
     private static final int MAX_INDENTS = 16;
 336  
     private static final int NOTHING = 0;
 337  
     private static final int SPACE = 1;
 338  
     private static final int NEW_LINE = 2;
 339  0
     private String sEscapes[][] = {
 340  
         {
 341  
             "'", "'", ""
 342  
         }, {
 343  
             "\"", "\"", ""
 344  
         }, {
 345  
             "/*", "*/", "1"
 346  
         }, {
 347  
             "--", "\r\n", "2"
 348  
         }
 349  
     };
 350  
     private String sText;
 351  
     private String sNewLine;
 352  
     private String sIndent;
 353  
     private String sDebugString;
 354  
     private boolean bCapitalizeMajor;
 355  
     private boolean bCapitalizeMinor;
 356  
     private boolean bCapitalizeNames;
 357  
     private boolean bNewLineBeforeAnd;
 358  
     private boolean bDebug;
 359  
 }
 360  
 

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.