Android Open Source - AquaBase C S V Parser






From Project

Back to project page AquaBase.

License

The source code is released under:

GNU General Public License

If you think the Android project AquaBase listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  This file is part of AquaBase./*from ww  w. jav  a2s  .c o  m*/
 *
 *  AquaBase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AquaBase is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AquaBase.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (c) 2014 Cdric Bosdonnat <cedric@bosdonnat.fr>
 */
package org.aquabase.data;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CSVParser {

    public class Result {
        private String[] mColumns;
        private List<String[]> mRows;

        public String[] getColumns() {
            return mColumns;
        }
        public List<String[]> getRows() {
            return mRows;
        }
    }

    private InputStream mStream;

    public CSVParser(InputStream stream) {
        mStream = stream;
    }

    public Result parse() throws IOException {
        Result result = new Result();

        int chr = mStream.read();
        int nextChar = mStream.read();

        boolean inQuote = false;
        boolean inComment = false;
        ArrayList<String[]> rows = new ArrayList<String[]>();
        ArrayList<String> row = new ArrayList<String>();
        StringBuffer current = new StringBuffer();

        while (chr != -2) {

            switch (chr) {
                case '"':
                    if (!inComment) {
                        if (!inQuote) {
                            inQuote = true;
                        } else {
                            if (nextChar == ';' || nextChar == '\n' ||
                                    nextChar == -1) {
                                inQuote = false;
                            } else {
                                current.append((char)chr);
                            }
                        }
                    }
                    break;
                case -1:
                case '\n':
                    if (inComment) {
                        inComment = false;
                    } else if (inQuote) {
                        current.append((char)chr);
                    } else {
                        if (current.toString().length() > 0 || row.size() > 0) {
                            // Finish field
                            row.add(current.toString());
                            current = new StringBuffer();

                            // Finish row
                            String[] rowArr = row.toArray(new String[row.size()]);
                            if (result.mColumns == null) {
                                result.mColumns = rowArr;
                            } else {
                                rows.add(rowArr);
                            }
                            row.clear();
                        }
                    }
                    break;
                case ',':   // This one is just to workaround a bug in some csv file header
                case ';':
                    if (inQuote) {
                        current.append((char)chr);
                    } else if (!inComment) {
                        // Finish field
                        row.add(current.toString());
                        current = new StringBuffer();
                    }
                    break;
                case '#':
                    inComment = true;
                    break;
                default:
                    if (!inComment) {
                        current.append((char)chr);
                    }
            }

            chr = nextChar;
            if (chr != -1) {
                nextChar = mStream.read();
            } else {
                nextChar = -2;
            }
        }

        result.mRows = rows;

        return result;
    }

}




Java Source Code List

org.aquabase.DetailsActivity.java
org.aquabase.DetailsFragment.java
org.aquabase.MainActivity.java
org.aquabase.NavigationDrawerFragment.java
org.aquabase.SpeciesListFragment.java
org.aquabase.data.AquabaseContentProvider.java
org.aquabase.data.CSVParser.java
org.aquabase.data.CrustaceanTableHelper.java
org.aquabase.data.CsvMapping.java
org.aquabase.data.DatabaseHelper.java
org.aquabase.data.FishTableHelper.java
org.aquabase.data.TableHelper.java