001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.io;
025    
026    import java.io.File;
027    import java.io.FileReader;
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.io.InputStreamReader;
031    import java.io.Reader;
032    import java.io.StringReader;
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.regex.Pattern;
036    
037    import org.ujmp.core.Matrix;
038    import org.ujmp.core.MatrixFactory;
039    import org.ujmp.core.enums.ValueType;
040    import org.ujmp.core.exceptions.MatrixException;
041    import org.ujmp.core.util.io.IntelligentFileReader;
042    
043    public abstract class ImportMatrixCSV {
044    
045            private static String fieldDelimiter = "[,;\t]";
046    
047            private static final boolean trimFields = true;
048    
049            private static final boolean ignoreQuotationMarks = true;
050    
051            private static final String quotation = "\"";
052    
053            public static final Matrix fromString(String string, Object... parameters)
054                            throws MatrixException {
055                    StringReader sr = new StringReader(string);
056                    IntelligentFileReader r = new IntelligentFileReader(sr);
057                    Matrix m = fromReader(r, parameters);
058                    r.close();
059                    return m;
060            }
061    
062            public static final Matrix fromStream(InputStream stream, Object... parameters)
063                            throws MatrixException, IOException {
064                    InputStreamReader r = new InputStreamReader(stream);
065                    Matrix m = fromReader(r, parameters);
066                    r.close();
067                    return m;
068            }
069    
070            public static final Matrix fromFile(File file, Object... parameters) throws MatrixException,
071                            IOException {
072                    FileReader lr = new FileReader(file);
073                    Matrix m = fromReader(lr, parameters);
074                    m.setLabel(file.getAbsolutePath());
075                    lr.close();
076                    return m;
077            }
078    
079            public static final Matrix fromReader(Reader reader, Object... parameters)
080                            throws MatrixException {
081                    List<String[]> rowData = new ArrayList<String[]>();
082    
083                    if (parameters.length == 1 && parameters[0] instanceof String) {
084                            fieldDelimiter = (String) parameters[0];
085                    } else {
086                            System.out
087                                            .println("You should specify the column separator to make sure that the file is parsed correctly.");
088                            System.out
089                                            .println("Example: MatrixFactory.importFromFile(FileFormat.CSV, file, \";\")");
090                    }
091                    try {
092                            Pattern p = Pattern.compile(fieldDelimiter);
093                            IntelligentFileReader lr = new IntelligentFileReader(reader);
094                            int rows = 0;
095                            int cols = 0;
096                            String line = null;
097                            while ((line = lr.readLine()) != null) {
098                                    if (line.length() > 0) {
099                                            String[] fields = p.split(line);
100                                            if (trimFields) {
101                                                    for (int i = 0; i < fields.length; i++) {
102                                                            fields[i] = fields[i].trim();
103                                                    }
104                                            }
105                                            if (ignoreQuotationMarks) {
106                                                    for (int i = 0; i < fields.length; i++) {
107                                                            String s = fields[i];
108                                                            if (s.length() > 1 && s.startsWith(quotation) && s.endsWith(quotation)) {
109                                                                    fields[i] = s.substring(1, s.length() - 2);
110                                                            }
111                                                    }
112                                            }
113                                            int lcols = fields.length;
114                                            rowData.add(fields);
115                                            if (lcols > cols) {
116                                                    cols = lcols;
117                                            }
118                                            rows++;
119                                    } else {
120                                            rowData.add(new String[] { "" });
121                                            rows++;
122                                    }
123                            }
124                            lr.close();
125                            Matrix m = MatrixFactory.zeros(ValueType.STRING, rows, cols);
126    
127                            int r = 0;
128                            for (String[] fields : rowData) {
129                                    for (int c = fields.length - 1; c != -1; c--) {
130                                            m.setAsString(fields[c], r, c);
131                                    }
132                                    r++;
133                            }
134    
135                            return m;
136                    } catch (Exception e) {
137                            throw new MatrixException(e);
138                    }
139            }
140    
141    }