PageRenderTime 35ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/files/TextDatFile.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 286 lines | 177 code | 31 blank | 78 comment | 13 complexity | e30fe800da0a00e6572f633eb3302bfa MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.files;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.print.DocFlavor;
  27. import javax.swing.JTable;
  28. import javax.swing.table.DefaultTableModel;
  29. import mpv5.db.common.DatabaseObject;
  30. import mpv5.logging.Log;
  31. import mpv5.utils.arrays.ArrayUtilities;
  32. import mpv5.utils.jobs.Waitable;
  33. /**
  34. * This is a helper class for reading and writing csv files
  35. *
  36. */
  37. public class TextDatFile extends File implements Waitable {
  38. private static final long serialVersionUID = 2059941918698508985L;
  39. private FileReaderWriter rw;
  40. private String fieldSeparator = ";";
  41. private String[][] data;
  42. private String[] header;
  43. private int mode;
  44. private DefaultTableModel model;
  45. private JTable table;
  46. /**
  47. * Constructs a new text file
  48. */
  49. public TextDatFile() {
  50. super(FileDirectoryHandler.getTempFile().getPath());
  51. rw = new FileReaderWriter(this);
  52. }
  53. /**
  54. * Constructs a new temporary text file
  55. * @param data The data to write to the file
  56. * @param header
  57. */
  58. public TextDatFile(String[][] data, String[] header) {
  59. super(FileDirectoryHandler.getTempFile().getPath());
  60. rw = new FileReaderWriter(this);
  61. this.data = data;
  62. this.header = header;
  63. mode = 0;
  64. this.deleteOnExit();
  65. }
  66. /**
  67. * Constructs a new text file
  68. * @param data The data to write to the file
  69. * @param header
  70. * @param name The name/path of the file
  71. */
  72. public TextDatFile(String[][] data, String[] header, String name) {
  73. super(name);
  74. rw = new FileReaderWriter(this);
  75. this.data = data;
  76. this.header = header;
  77. mode = 0;
  78. }
  79. /**
  80. * Constructs a new file and sets 'read' mode
  81. * @param file The file (to read from)
  82. */
  83. public TextDatFile(File file) {
  84. super(file.getPath());
  85. rw = new FileReaderWriter(this);
  86. mode = 1;
  87. }
  88. /**
  89. * Constructs a new file and sets 'read' mode
  90. * @param file The file (to read from)
  91. * @param table The table to retrieve the data
  92. */
  93. public TextDatFile(File file, JTable table) {
  94. super(file.getPath());
  95. rw = new FileReaderWriter(this);
  96. this.table = table;
  97. mode = 2;
  98. }
  99. public void parse(ArrayList<DatabaseObject> dbobjarr) {
  100. String[] headers;
  101. Method[] headerx;
  102. if (dbobjarr.size() > 0) {
  103. headerx = dbobjarr.get(0).getVars().values().toArray(new Method[]{});
  104. headers = new String[headerx.length];
  105. for (int i = 0; i < headerx.length; i++) {
  106. Method method = headerx[i];
  107. headers[i] = method.getName().substring(5);
  108. }
  109. data = new String[dbobjarr.size()][dbobjarr.get(0).getVars().size()];
  110. for (int i = 0; i < dbobjarr.size(); i++) {
  111. DatabaseObject databaseObject = dbobjarr.get(i);
  112. List<String[]> temp = databaseObject.getValues();
  113. for (int j = 0; j < temp.size(); j++) {
  114. String[] strings = temp.get(j);
  115. data[i][j] = strings[1];
  116. }
  117. }
  118. setData(data);
  119. setHeader(headers);
  120. }
  121. }
  122. @Override
  123. public Exception waitFor() {
  124. switch (mode) {
  125. case 0:
  126. print();
  127. break;
  128. case 1:
  129. read();
  130. break;
  131. case 2:
  132. readToTable();
  133. break;
  134. }
  135. return null;
  136. }
  137. /**
  138. * Write the file to disk
  139. */
  140. public void print() {
  141. if (header != null) {
  142. rw.writeLine(header, getFieldSeparator());
  143. }
  144. for (int i = 0; i < getData().length; i++) {
  145. String[] strings = getData()[i];
  146. String line = "";
  147. for (int j = 0; j < strings.length; j++) {
  148. String string = strings[j];
  149. line += string + getFieldSeparator();
  150. }
  151. line = (line.substring(0, line.length() - getFieldSeparator().length())).replaceAll("[\\r\\n]", "");
  152. // Log.Debug(this, line);
  153. rw.write(line);
  154. }
  155. }
  156. /**
  157. * Reads the file, no headers given
  158. * @return The data from file
  159. */
  160. public String[][] read() {
  161. @SuppressWarnings("unchecked")
  162. ArrayList<String[]> arr = new ArrayList();
  163. String[] line = rw.readLines();
  164. Log.Debug(this, "Reading file: " + this.getPath() + " (" + line.length + " lines)");
  165. for (int i = 0; i < line.length; i++) {
  166. arr.add(line[i].split(getFieldSeparator()));
  167. line = rw.readLines();
  168. // Log.Debug(this, "Line.. " + i);
  169. }
  170. data = ArrayUtilities.listToStringArrayArray(arr);
  171. model = new DefaultTableModel(data, header);
  172. return data;
  173. }
  174. /**
  175. * Reads the file, assuming the first line contains the header
  176. * @return The data from file
  177. */
  178. public DefaultTableModel readToTable() {
  179. @SuppressWarnings("unchecked")
  180. ArrayList<String[]> arr = new ArrayList();
  181. String[] line = rw.readLines();
  182. Log.Debug(this, "Reading file: " + this.getPath() + " (" + line.length + " lines)");
  183. header = line[0].split(getFieldSeparator());
  184. for (int i = 1; i < line.length; i++) {
  185. arr.add(line[i].split(getFieldSeparator()));
  186. // Log.Debug(this, "Line.. " + arr.get(i-1));
  187. }
  188. data = ArrayUtilities.listToStringArrayArray(arr);
  189. model = new DefaultTableModel(data, header);
  190. if (table != null) {
  191. this.table.setModel(model);
  192. }
  193. return model;
  194. }
  195. public DocFlavor getFlavor() {
  196. return DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
  197. }
  198. public File getFile() {
  199. return this;
  200. }
  201. /**
  202. *
  203. * @return the field separating char
  204. */
  205. public String getFieldSeparator() {
  206. return fieldSeparator;
  207. }
  208. /**
  209. * Set the field separating char (default: ';')
  210. * @param fieldSeparator
  211. */
  212. public void setFieldSeparator(String fieldSeparator) {
  213. this.fieldSeparator = fieldSeparator;
  214. }
  215. /**
  216. *
  217. * @return The data
  218. */
  219. public String[][] getData() {
  220. return data;
  221. }
  222. /**
  223. *
  224. * @param data Set the data of the file
  225. */
  226. public void setData(String[][] data) {
  227. this.data = data;
  228. }
  229. public String[] getHeader() {
  230. return header;
  231. }
  232. public void setHeader(String[] header) {
  233. this.header = header;
  234. }
  235. public DefaultTableModel getModel() {
  236. return model;
  237. }
  238. /**
  239. * Create a new file
  240. * @param filename
  241. * @return
  242. */
  243. public File createFile(String filename) {
  244. File f = FileDirectoryHandler.getTempFile(filename, "csv");
  245. try {
  246. print();
  247. FileDirectoryHandler.copyFile(this, f);
  248. } catch (FileNotFoundException ex) {
  249. mpv5.logging.Log.Debug(ex);//Logger.getLogger(TextDatFile.class.getName()).log(Level.SEVERE, null, ex);
  250. } catch (IOException ex) {
  251. mpv5.logging.Log.Debug(ex);//Logger.getLogger(TextDatFile.class.getName()).log(Level.SEVERE, null, ex);
  252. }
  253. return f;
  254. }
  255. }