package org.intermine.util;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.commons.lang.text.StrMatcher;
import org.apache.commons.lang.text.StrTokenizer;

/**
 * Class for methods and util methods for parsing csv files and others.
 * @author Kim Rutherford
 * @author Jakub Kulaviak
 **/
public class FormattedTextParser
{

    /**
     * Return an Iterator over a tab delimited file.  Iterator.next() splits the current line at the
     * tabs and returns a String[] of the bits.  No attempt is made to deal with quoted tabs.
     * Lines beginning with # are ignored.
     * @param reader the Reader to read from
     * @return an Iterator over the lines of the Reader
     * @throws IOException if there is an error while reading from the Reader
     */
    public static Iterator parseTabDelimitedReader(final Reader reader) throws IOException {
        return parseDelimitedReader(reader, false, "\t");
    }
    
    /**
     * Return an Iterator over a comma delimited file.  Iterator.next() splits the current line at 
     * the commas and returns a String[] of the bits, stripped of all quotes.
     * Lines beginning with # are ignored.
     * @param reader the Reader to read from
     * @return an Iterator over the lines of the Reader
     * @throws IOException if there is an error while reading from the Reader
     */
    public static Iterator<String[]> parseCsvDelimitedReader(final Reader reader) 
    throws IOException {
        return parseDelimitedReader(reader, true, ",");
    }
    
    private static Iterator parseDelimitedReader(final Reader reader, final boolean stripQuotes, 
                                                 final String delim) 
    throws IOException {
        final BufferedReader bufferedReader = new BufferedReader(reader);

        return new Iterator() {
            String currentLine = null;

            {
                currentLine = getNextNonCommentLine();
            }

            public boolean hasNext() {
                return currentLine != null;
            }

            public Object next() {
                if (currentLine == null) {
                    throw new NoSuchElementException();
                } else {
                    String lastLine = currentLine;

                    try {
                        currentLine = getNextNonCommentLine();
                    } catch (IOException e) {
                        throw new RuntimeException("error while reading from " + reader, e);
                    }
                    
                    if (stripQuotes) {
                        StrMatcher delimMatcher = null;
                        
                        if (delim.equals(",")) {
                            delimMatcher = StrMatcher.commaMatcher();
                        } else {
                            delimMatcher = StrMatcher.tabMatcher();
                        }
                        StrTokenizer tokeniser
                        = new StrTokenizer(lastLine, delimMatcher, StrMatcher.doubleQuoteMatcher());
                        tokeniser.setEmptyTokenAsNull(false);
                        tokeniser.setIgnoreEmptyTokens(false);
                        return tokeniser.getTokenArray();
                    }                    
                    return StringUtil.split(lastLine, delim);
                }
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }

            private String getNextNonCommentLine() throws IOException {
                String line = null;

                while (true) {
                    line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    } else {
                        if (!line.startsWith("#")) {
                            break;
                        }
                    }
                }

                return line;
            }
        };
    }
}

@@ -1326,28 +1326,24 @@
 %5Ct%22);%0A    %7D%0A
-    
 %0A    /**%0A   
@@ -1437,17 +1437,16 @@
  line at
- 
 %0A     * 
@@ -1827,17 +1827,16 @@
  reader)
- 
 %0A    thr
@@ -1915,20 +1915,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -2012,17 +2012,16 @@
 pQuotes,
- 
 %0A       
@@ -2081,17 +2081,16 @@
 g delim)
- 
 %0A    thr
@@ -2583,36 +2583,25 @@
            %7D
- else %7B%0A    
+%0A
             
@@ -2656,22 +2656,14 @@
     
-    
 try %7B%0A
-    
     
@@ -2733,20 +2733,16 @@
         
-   
- 
 %7D catch 
@@ -2755,28 +2755,24 @@
 eption e) %7B%0A
-    
             
@@ -2868,39 +2868,11 @@
     
-    %7D%0A                    %0A    
+%7D%0A%0A
     
@@ -2922,20 +2922,16 @@
         
-    
 StrMatch
@@ -2958,35 +2958,9 @@
 ll;%0A
-                        %0A  
+%0A
     
@@ -2971,26 +2971,24 @@
             
-  
 if (delim.eq
@@ -2996,28 +2996,24 @@
 als(%22,%22)) %7B%0A
-    
             
@@ -3078,36 +3078,32 @@
                 
-    
 %7D else %7B%0A       
@@ -3111,36 +3111,32 @@
                 
- 
-   
 delimMatcher = S
@@ -3175,33 +3175,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -3201,17 +3201,16 @@
         
- 
 StrToken
@@ -3224,20 +3224,16 @@
 keniser%0A
-    
         
@@ -3333,36 +3333,32 @@
                 
-    
 tokeniser.setEmp
@@ -3391,36 +3391,32 @@
                 
-  
-  
 tokeniser.setIgn
@@ -3454,28 +3454,24 @@
             
-    
 return token
@@ -3488,28 +3488,24 @@
 kenArray();%0A
-    
             
@@ -3513,33 +3513,9 @@
    %7D
-                    %0A    
+%0A
     
@@ -3568,34 +3568,16 @@
 delim);%0A
-                %7D%0A
         
@@ -3974,36 +3974,25 @@
            %7D
- else %7B%0A    
+%0A
             
@@ -4056,45 +4056,15 @@
     
-    
 break;%0A
-                        %7D%0A
     
@@ -4091,33 +4091,32 @@
               %7D%0A
-%0A
                 

fix eclipse warning