001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.framework.data; 021 022 import java.io.IOException; 023 import java.io.StringWriter; 024 import java.io.Writer; 025 import java.util.ArrayList; 026 import java.util.Arrays; 027 import java.util.HashMap; 028 import java.util.List; 029 import java.util.Map; 030 031 public class DataTable { 032 private List<String> columns = new ArrayList<String>(); 033 private List<DataRow> rows = new ArrayList<DataRow>(); 034 private Map<String, Integer> columnIndexMap = new HashMap<String, Integer>(); 035 public void setColumns(List<String> columns) { 036 this.columns = new ArrayList<String>(); 037 columnIndexMap.clear(); 038 for (String col : columns) { 039 addColumn(col); 040 } 041 } 042 public void addColumn(String column) { 043 columnIndexMap.put(column, new Integer(columns.size())); 044 columns.add(column); 045 } 046 public int getColumnCount() { 047 return columns.size(); 048 } 049 public String getColumn(int index) { 050 return columns.get(index); 051 } 052 public Map<String, String> getRowData(int rowIndex) { 053 Map<String, String> map = new HashMap<String, String>(); 054 int colCount = getColumnCount(); 055 for (int j = 0; j < colCount; j++) { 056 String col = getColumn(j); 057 map.put(col, getValue(rowIndex, col)); 058 } 059 return map; 060 } 061 public void addRow(List<String> cellValues) { 062 rows.add(new DataRow(cellValues)); 063 } 064 public void addRow(String[] cellValues) { 065 rows.add(new DataRow(Arrays.asList(cellValues))); 066 } 067 public int getRowCount() { 068 return rows.size(); 069 } 070 public String getValue(int row, String columnName) { 071 Integer index = columnIndexMap.get(columnName); 072 if (index == null) 073 return null; 074 return getValue(row, index.intValue()); 075 } 076 public String getValue(int row, int column) { 077 if (row >= rows.size()) 078 return null; 079 return rows.get(row).getValue(column); 080 } 081 public static class DataRow { 082 private List<String> values = new ArrayList<String>(); 083 public DataRow() { 084 } 085 public DataRow(List<String> values) { 086 this.values = new ArrayList<String>(values); 087 } 088 public String getValue(int colIndex) { 089 if (colIndex >= values.size()) 090 return null; 091 return values.get(colIndex); 092 } 093 } 094 public String toString() { 095 StringWriter sw = new StringWriter(); 096 try { 097 writeAsCsv(sw); 098 } catch (IOException e) { 099 e.printStackTrace(); 100 } 101 return sw.toString(); 102 } 103 public void writeAsHtml(Writer out) throws IOException { 104 out.write("\n<table>\n"); 105 out.write("<tr>"); 106 for (String col : columns) { 107 out.write("<td>"); 108 out.write(col); 109 out.write("</td>"); 110 } 111 out.write("</tr>\n"); 112 for (DataRow row : rows) { 113 out.write("<tr>"); 114 for (String val : row.values) { 115 out.write("<td>"); 116 out.write(val); 117 out.write("</td>"); 118 } 119 out.write("</tr>\n"); 120 } 121 out.write("</table>\n<br/><br/>"); 122 } 123 public void writeAsCsv(Writer out) throws IOException { 124 for (int i = 0; i < columns.size(); i++) { 125 out.write(columns.get(i)); 126 if (i < columns.size() - 1) 127 out.write(";"); 128 } 129 for (DataRow row : rows) { 130 out.write("\n"); 131 for (int i = 0; i < row.values.size(); i++) { 132 out.write(row.values.get(i)); 133 if (i < row.values.size() - 1) 134 out.write(";"); 135 } 136 } 137 out.write("\n"); 138 } 139 }