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 ImportMatrixSPARSECSV {
044    
045            public static final Matrix fromString(String string, Object... parameters)
046                            throws MatrixException {
047                    StringReader sr = new StringReader(string);
048                    IntelligentFileReader r = new IntelligentFileReader(sr);
049                    Matrix m = fromReader(r);
050                    r.close();
051                    return m;
052            }
053    
054            public static final Matrix fromStream(InputStream stream, Object... parameters)
055                            throws MatrixException, IOException {
056                    InputStreamReader r = new InputStreamReader(stream);
057                    Matrix m = fromReader(r, parameters);
058                    r.close();
059                    return m;
060            }
061    
062            public static final Matrix fromFile(File file, Object... parameters) throws MatrixException,
063                            IOException {
064                    FileReader lr = new FileReader(file);
065                    Matrix m = fromReader(lr, parameters);
066                    m.setLabel(file.getAbsolutePath());
067                    lr.close();
068                    return m;
069            }
070    
071            public static final Matrix fromReader(Reader reader, Object... parameters)
072                            throws MatrixException {
073                    List<Long> rowData = new ArrayList<Long>();
074                    List<Long> colData = new ArrayList<Long>();
075                    List<String> data = new ArrayList<String>();
076    
077                    String separator = "[,;\t]";
078                    if (parameters.length == 1 && parameters[0] instanceof String) {
079                            separator = (String) parameters[0];
080                    }
081                    try {
082                            Pattern p = Pattern.compile(separator);
083                            IntelligentFileReader lr = new IntelligentFileReader(reader);
084                            long rows = 0;
085                            long cols = 0;
086                            String line = null;
087                            while ((line = lr.readLine()) != null) {
088                                    if (line.length() > 0) {
089                                            String[] fields = p.split(line);
090                                            if (fields.length == 3) {
091                                                    long row = Long.parseLong(fields[0]);
092                                                    long col = Long.parseLong(fields[1]);
093                                                    String s = fields[2];
094                                                    if (row > rows) {
095                                                            rows = row;
096                                                    }
097                                                    if (col > cols) {
098                                                            cols = col;
099                                                    }
100                                                    rowData.add(row);
101                                                    colData.add(col);
102                                                    data.add(s);
103                                            }
104                                    }
105                            }
106                            lr.close();
107                            Matrix m = MatrixFactory.sparse(ValueType.STRING, rows + 1, cols + 1);
108    
109                            for (int i = 0; i < data.size(); i++) {
110                                    long row = rowData.get(i);
111                                    long col = colData.get(i);
112                                    String s = data.get(i);
113                                    m.setAsString(s, row, col);
114                            }
115    
116                            return m;
117                    } catch (Exception e) {
118                            throw new MatrixException(e);
119                    }
120            }
121    }