Coverage Report - org.jtheque.core.managers.schema.HSQLFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
HSQLFileReader
0 %
0/41
0 %
0/22
2.429
 
 1  
 package org.jtheque.core.managers.schema;
 2  
 
 3  
 import org.jtheque.core.managers.Managers;
 4  
 import org.jtheque.core.managers.log.ILoggingManager;
 5  
 import org.jtheque.utils.StringUtils;
 6  
 import org.jtheque.utils.io.FileUtils;
 7  
 
 8  
 import java.io.File;
 9  
 import java.io.FileInputStream;
 10  
 import java.io.FileNotFoundException;
 11  
 import java.nio.channels.FileChannel;
 12  
 import java.util.ArrayList;
 13  
 import java.util.Collection;
 14  
 import java.util.Scanner;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * A reader for HSQL Script file and log files.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  
 final class HSQLFileReader {
 38  
     /**
 39  
      * Utility class, cannot be instantiated.
 40  
      */
 41  
     private HSQLFileReader() {
 42  0
         super();
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Read the file and get off all the inserts.
 47  
      *
 48  
      * @param file The file to parse.
 49  
      * @return The List of all the inserts of the file.
 50  
      */
 51  
     public static Collection<Insert> readFile(File file) {
 52  0
         Collection<Insert> inserts = new ArrayList<Insert>(100);
 53  
 
 54  0
         FileChannel in = null;
 55  
         try {
 56  0
             in = new FileInputStream(file).getChannel();
 57  
 
 58  0
             Scanner scanner = new Scanner(in);
 59  
 
 60  0
             while (scanner.hasNextLine()) {
 61  0
                 String line = scanner.nextLine().trim();
 62  
 
 63  0
                 if (line.startsWith("INSERT INTO")) {
 64  0
                     inserts.add(readInsert(line));
 65  
                 }
 66  0
             }
 67  0
         } catch (FileNotFoundException e) {
 68  0
             Managers.getManager(ILoggingManager.class).getLogger(HSQLFileReader.class).error(e);
 69  
         } finally {
 70  0
             FileUtils.close(in);
 71  0
         }
 72  
 
 73  0
         return inserts;
 74  
     }
 75  
 
 76  
     /**
 77  
      * Read the insert.
 78  
      *
 79  
      * @param line The line to read the insert from.
 80  
      * @return The read Insert.
 81  
      */
 82  
     private static Insert readInsert(String line) {
 83  0
         Insert insert = new Insert();
 84  
 
 85  0
         insert.setTable(line.substring(12, line.indexOf(" VALUES(")));
 86  
 
 87  0
         insert.setValues(extractValues(line));
 88  
 
 89  0
         return insert;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Extract The values from the line.
 94  
      *
 95  
      * @param line The line to read.
 96  
      * @return A collection containing all the values.
 97  
      */
 98  
     private static Collection<String> extractValues(String line) {
 99  0
         String valuesStr = line.substring(line.indexOf('(') + 1, line.lastIndexOf(')') + 1);
 100  
 
 101  0
         valuesStr = valuesStr.replace(",'',", ",NULL,");
 102  
 
 103  0
         return parseStrValues(valuesStr);
 104  
     }
 105  
 
 106  
     /**
 107  
      * Parse the values string to a Collection of values.
 108  
      *
 109  
      * @param valuesStr The String values.
 110  
      * @return A collection containing all the values.
 111  
      */
 112  
     private static Collection<String> parseStrValues(String valuesStr) {
 113  0
         Collection<String> values = new ArrayList<String>(10);
 114  
 
 115  0
         StringBuilder current = new StringBuilder(100);
 116  
 
 117  0
         boolean surroundStart = false;
 118  0
         char last = ' ';
 119  
 
 120  0
         for (char c : valuesStr.toCharArray()) {
 121  0
             if (isEndOfValue(surroundStart, c)) {
 122  0
                 values.add(StringUtils.removeUnicode(current.toString()));
 123  
 
 124  0
                 current.setLength(0);
 125  0
             } else if (isEscapedChar(last, c)) {
 126  0
                 current.append('\'');
 127  0
                 surroundStart ^= true;
 128  0
             } else if (c == '\'') {
 129  0
                 surroundStart ^= true;
 130  
             } else {
 131  0
                 current.append(c);
 132  
             }
 133  
 
 134  0
             last = c;
 135  
         }
 136  
 
 137  0
         return values;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Indicate if the two chars form an escaped char or not.
 142  
      *
 143  
      * @param last The last char.
 144  
      * @param c    The current char.
 145  
      * @return true if the two chars form an escaped char else false.
 146  
      */
 147  
     private static boolean isEscapedChar(char last, char c) {
 148  0
         return last == '\'' && c == '\'';
 149  
     }
 150  
 
 151  
     /**
 152  
      * Indicate if the char c indicate the end of the current value or not.
 153  
      *
 154  
      * @param surroundStart Indicate the surround started or not.
 155  
      * @param c             The current char.
 156  
      * @return true if this is the end of the value else false.
 157  
      */
 158  
     private static boolean isEndOfValue(boolean surroundStart, char c) {
 159  0
         return !surroundStart && (c == ',' || c == ')');
 160  
     }
 161  
 }