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.BufferedReader; 027 import java.io.File; 028 import java.io.FileReader; 029 import java.io.IOException; 030 import java.io.InputStream; 031 import java.io.InputStreamReader; 032 import java.io.LineNumberReader; 033 import java.io.Reader; 034 035 import org.ujmp.core.Matrix; 036 import org.ujmp.core.MatrixFactory; 037 import org.ujmp.core.util.StringUtil; 038 039 public abstract class ImportMatrixM { 040 041 /** 042 * Creates a DefaultDenseStringMatrix2D from a given String. The string 043 * contains the rows of the matrix separated by semicolons or new lines. The 044 * columns of the matrix are separated by spaces or commas. All types of 045 * brackets are ignored. 046 * <p> 047 * Examples: "[1 2 3; 4 5 6]" or "(test1,test2);(test3,test4)" 048 * 049 * @param string 050 * the string to parse 051 * @return a StringMatrix with the desired values 052 */ 053 public static Matrix fromString(String string, Object... parameters) { 054 string = string.replaceAll(StringUtil.BRACKETS, ""); 055 String[] rows = string.split(StringUtil.SEMICOLONORNEWLINE); 056 String[] cols = rows[0].split(StringUtil.COLONORSPACES); 057 Object[][] values = new String[rows.length][cols.length]; 058 for (int r = 0; r < rows.length; r++) { 059 cols = rows[r].split(StringUtil.COLONORSPACES); 060 for (int c = 0; c < cols.length; c++) { 061 values[r][c] = cols[c]; 062 } 063 } 064 return MatrixFactory.linkToArray(values); 065 } 066 067 public static Matrix fromFile(File file, Object... parameters) throws IOException { 068 Reader reader = new BufferedReader(new FileReader(file)); 069 Matrix matrix = fromReader(reader, parameters); 070 reader.close(); 071 return matrix; 072 } 073 074 public static Matrix fromStream(InputStream stream, Object... parameters) throws IOException { 075 InputStreamReader r = new InputStreamReader(stream); 076 Matrix matrix = fromReader(r, parameters); 077 r.close(); 078 return matrix; 079 } 080 081 public static Matrix fromReader(Reader reader, Object... parameters) throws IOException { 082 String EOL = System.getProperty("line.separator"); 083 StringBuilder sb = new StringBuilder(); 084 LineNumberReader lr = new LineNumberReader(reader); 085 String line = null; 086 while ((line = lr.readLine()) != null) { 087 sb.append(line + EOL); 088 } 089 return fromString(sb.toString(), parameters); 090 } 091 092 }