/*
 * Copyright 2015 Brian Hess
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.datastax.loader.parser;

import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.IndexOutOfBoundsException;
import java.io.StringReader;
import java.io.IOException;
import java.text.ParseException;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.InvalidTypeException;

public abstract class AbstractParser implements Parser {
    public abstract Object parse(String toparse) throws ParseException;
    public abstract String format(Row row, int index) throws IndexOutOfBoundsException, InvalidTypeException;

    public Object parse(IndexedLine il, String nullString, Character delim, 
			Character escape, Character quote, boolean last)
	throws IOException, ParseException {
	if (last)
	    return parse(il.remaining());
	return parse(getQuotedOrUnquoted(il, nullString, delim, escape, quote));
    }

    public String getQuotedOrUnquoted(IndexedLine il, String nullString,
				      Character delim, Character escape, 
				      Character quote) 
	throws IOException, ParseException {
	if (null == delim) {
	    return null;
	}
	char c = il.getNext();
	if (c == delim) {
	    return new String();
	}
	StringBuilder sb = new StringBuilder().append(c);
	String s = extractUntil(il, delim, escape, quote, (c == quote));
	if (null == s) {
	    return null;
	}
	String retstring = sb.append(s).toString();
	if (retstring.startsWith(quote.toString()) 
	    && retstring.endsWith(quote.toString()))
	    retstring = retstring.substring(1, retstring.length() - 1);
	retstring = retstring.trim();
	if (nullString.equalsIgnoreCase(retstring))
	    return null;
	return retstring;
    }

    public String extractUntil(IndexedLine il, Character delim, 
			       Character escape, Character quote,
			       boolean inquote)
	throws IOException, ParseException {
	if (null == delim) {
	    return null;
	}
	StringBuilder sb = new StringBuilder();
	char c;
	while (true) {
	    c = il.getNext();
	    if ((c == delim) && (!inquote)) {
		break;
	    }
	    sb.append(c);
	    if (null != quote) {
		if (c == quote) {
		    inquote = false;
		}
	    }
	    if (null != escape) {
		if (c == escape) {
		    c = il.getNext();
		    sb.append(Character.toChars(c));
		}
	    }
	}
	return sb.toString();
    }
}

@@ -1657,32 +1657,51 @@
 arseException %7B%0A
+%09String retstring;%0A
 %09if (null == del
@@ -1782,30 +1782,37 @@
  ret
-urn new String();%0A%09%7D%0A%09
+string = %22%22;%0A%09%7D%0A%09else %7B%0A%09    
 Stri
@@ -1858,16 +1858,20 @@
 nd(c);%0A%09
+    
 String s
@@ -1924,24 +1924,28 @@
 = quote));%0A%09
+    
 if (null == 
@@ -1946,28 +1946,25 @@
 ll == s) %7B%0A%09
-    
+%09
 return null;
@@ -1965,25 +1965,26 @@
  null;%0A%09
-%7D%0A%09String
+    %7D%0A%09   
  retstri
@@ -2009,24 +2009,27 @@
 toString();%0A
+%09%7D%0A
 %09if (retstri

empty string case