Coverage Report - org.apache.torque.engine.sql.SQLScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
SQLScanner
0%
0/78
0%
0/60
5.143
 
 1  
 package org.apache.torque.engine.sql;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 5  
  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 7  
  * License. You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 12  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 13  
  * specific language governing permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.io.Reader;
 18  
 import java.util.List;
 19  
 import java.util.ArrayList;
 20  
 
 21  
 /**
 22  
  * A simple Scanner implementation that scans an sql file into usable tokens. Used by SQLToAppData.
 23  
  * 
 24  
  * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
 25  
  * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
 26  
  * @author <a href="mailto:andyhot@di.uoa.gr">Andreas Andreou</a>
 27  
  * @version $Id: SQLScanner.java,v 1.1 2007-10-21 07:57:27 abyrne Exp $
 28  
  */
 29  
 public class SQLScanner {
 30  
     /** white spaces */
 31  
     private static final String WHITE = "\f\r\t\n ";
 32  
     /** alphabetic characters */
 33  
     private static final String ALFA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 34  
     /** numbers */
 35  
     private static final String NUMER = "0123456789";
 36  
     /** alphanumeric */
 37  
     private static final String ALFANUM = ALFA + NUMER;
 38  
     /** special characters */
 39  
     private static final String SPECIAL = ";(),'";
 40  
     /** comment */
 41  
     private static final char COMMENT_POUND = '#';
 42  
     /** comment */
 43  
     private static final char COMMENT_SLASH = '/';
 44  
     /** comment */
 45  
     private static final char COMMENT_STAR = '*';
 46  
     /** comment */
 47  
     private static final char COMMENT_DASH = '-';
 48  
 
 49  
     /** the input reader */
 50  
     private Reader in;
 51  
     /** character */
 52  
     private int chr;
 53  
     /** token */
 54  
     private String token;
 55  
     /** list of tokens */
 56  
     private List tokens;
 57  
     /** line */
 58  
     private int line;
 59  
     /** column */
 60  
     private int col;
 61  
 
 62  
     /**
 63  
      * Creates a new scanner with no Reader
 64  
      */
 65  
     public SQLScanner() {
 66  0
         this(null);
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Creates a new scanner with an Input Reader
 71  
      * 
 72  
      * @param input
 73  
      *            the input reader
 74  
      */
 75  0
     public SQLScanner(Reader input) {
 76  0
         setInput(input);
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Set the Input
 81  
      * 
 82  
      * @param input
 83  
      *            the input reader
 84  
      */
 85  
     public void setInput(Reader input) {
 86  0
         in = input;
 87  0
     }
 88  
 
 89  
     /**
 90  
      * Reads the next character and increments the line and column counters.
 91  
      * 
 92  
      * @throws IOException
 93  
      *             If an I/O error occurs
 94  
      */
 95  
     private void readChar() throws IOException {
 96  0
         boolean wasLine = (char) chr == '\r';
 97  0
         chr = in.read();
 98  0
         if ((char) chr == '\n' || (char) chr == '\r' || (char) chr == '\f') {
 99  0
             col = 0;
 100  0
             if (!wasLine || (char) chr != '\n') {
 101  0
                 line++;
 102  
             }
 103  
         } else {
 104  0
             col++;
 105  
         }
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Scans an identifier.
 110  
      * 
 111  
      * @throws IOException
 112  
      *             If an I/O error occurs
 113  
      */
 114  
     private void scanIdentifier() throws IOException {
 115  0
         token = "";
 116  0
         char c = (char) chr;
 117  0
         while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1) {
 118  0
             token = token + (char) chr;
 119  0
             readChar();
 120  0
             c = (char) chr;
 121  
         }
 122  0
         int start = col - token.length();
 123  0
         tokens.add(new Token(token, line, start));
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Scans an identifier which had started with the negative sign.
 128  
      * 
 129  
      * @throws IOException
 130  
      *             If an I/O error occurs
 131  
      */
 132  
     private void scanNegativeIdentifier() throws IOException {
 133  0
         token = "-";
 134  0
         char c = (char) chr;
 135  0
         while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1) {
 136  0
             token = token + (char) chr;
 137  0
             readChar();
 138  0
             c = (char) chr;
 139  
         }
 140  0
         int start = col - token.length();
 141  0
         tokens.add(new Token(token, line, start));
 142  0
     }
 143  
 
 144  
     /**
 145  
      * Scan the input Reader and returns a list of tokens.
 146  
      * 
 147  
      * @return a list of tokens
 148  
      * @throws IOException
 149  
      *             If an I/O error occurs
 150  
      */
 151  
     public List scan() throws IOException {
 152  0
         line = 1;
 153  0
         col = 0;
 154  0
         boolean inComment = false;
 155  0
         boolean inCommentSlashStar = false;
 156  0
         boolean inCommentDash = false;
 157  
 
 158  
         boolean inNegative;
 159  
 
 160  0
         tokens = new ArrayList();
 161  0
         readChar();
 162  0
         while (chr != -1) {
 163  0
             char c = (char) chr;
 164  0
             inNegative = false;
 165  
 
 166  0
             if (c == COMMENT_DASH) {
 167  0
                 readChar();
 168  0
                 if ((char) chr == COMMENT_DASH) {
 169  0
                     inCommentDash = true;
 170  
                 } else {
 171  0
                     inNegative = true;
 172  0
                     c = (char) chr;
 173  
                 }
 174  
             }
 175  
 
 176  0
             if (inCommentDash) {
 177  0
                 if (c == '\n' || c == '\r') {
 178  0
                     inCommentDash = false;
 179  
                 }
 180  0
                 readChar();
 181  0
             } else if (c == COMMENT_POUND) {
 182  0
                 inComment = true;
 183  0
                 readChar();
 184  0
             } else if (c == COMMENT_SLASH) {
 185  0
                 readChar();
 186  0
                 if ((char) chr == COMMENT_STAR) {
 187  0
                     inCommentSlashStar = true;
 188  
                 }
 189  0
             } else if (inComment || inCommentSlashStar) {
 190  0
                 if (c == '*') {
 191  0
                     readChar();
 192  0
                     if ((char) chr == COMMENT_SLASH) {
 193  0
                         inCommentSlashStar = false;
 194  
                     }
 195  0
                 } else if (c == '\n' || c == '\r') {
 196  0
                     inComment = false;
 197  
                 }
 198  0
                 readChar();
 199  0
             } else if (ALFANUM.indexOf(c) >= 0) {
 200  0
                 if (inNegative) {
 201  0
                     scanNegativeIdentifier();
 202  
                 } else {
 203  0
                     scanIdentifier();
 204  
                 }
 205  0
             } else if (SPECIAL.indexOf(c) >= 0) {
 206  0
                 tokens.add(new Token("" + c, line, col));
 207  0
                 readChar();
 208  
             } else {
 209  0
                 readChar();
 210  
             }
 211  0
         }
 212  0
         return tokens;
 213  
     }
 214  
 }