001: /*
002: * DataPrinter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage;
013:
014: import java.io.IOException;
015: import java.io.PrintStream;
016: import java.io.PrintWriter;
017: import java.io.Writer;
018: import java.util.List;
019: import workbench.db.ColumnIdentifier;
020: import workbench.db.exporter.TextRowDataConverter;
021: import workbench.log.LogMgr;
022: import workbench.util.StrBuffer;
023: import workbench.util.StringUtil;
024:
025: /**
026: * A class to print the contents of a {@link DataStore} to a PrintStream
027: *
028: * @author support@sql-workbench.net
029: */
030: public class DataPrinter {
031: private DataStore data;
032: private TextRowDataConverter converter;
033:
034: public DataPrinter(DataStore source) {
035: this .data = source;
036: initConverter("\t", StringUtil.LINE_TERMINATOR, null, true);
037: }
038:
039: public DataPrinter(DataStore source, boolean includeHeaders) {
040: this .data = source;
041: initConverter("\t", StringUtil.LINE_TERMINATOR, null,
042: includeHeaders);
043: }
044:
045: public DataPrinter(DataStore source, String delimiter,
046: String lineEnd, List<ColumnIdentifier> columns,
047: boolean includeHeader) {
048: this .data = source;
049: initConverter(delimiter, lineEnd, columns, includeHeader);
050: }
051:
052: private void initConverter(String delimiter, String lineEnd,
053: List<ColumnIdentifier> columns, boolean includeHeader) {
054: converter = new TextRowDataConverter();
055: converter.setResultInfo(data.getResultInfo());
056: converter.setWriteBlobToFile(false);
057: converter.setWriteHeader(includeHeader);
058: converter.setLineEnding(lineEnd);
059: converter.setDelimiter(delimiter);
060: converter.setColumnsToExport(columns);
061: }
062:
063: public void printTo(PrintStream out) {
064: PrintWriter pw = new PrintWriter(out);
065: try {
066: writeDataString(pw, (int[]) null);
067: } catch (IOException e) {
068: LogMgr.logError("DataPrinter.printTo",
069: "Error when printing DataStore contents", e);
070: }
071: }
072:
073: public String getRowDataAsString(int row) {
074: RowData rowData = data.getRow(row);
075: StrBuffer line = converter.convertRowData(rowData, row);
076: if (line != null)
077: return line.toString();
078: return null;
079: }
080:
081: /**
082: * Write the contents of the DataStore into the writer but only the rows
083: * that have been passed in the rows[] parameter
084: */
085: public void writeDataString(Writer out, int[] rows)
086: throws IOException {
087: StrBuffer header = converter.getStart();
088: if (header != null) {
089: header.writeTo(out);
090: out.flush();
091: }
092:
093: int count = (rows == null ? data.getRowCount() : rows.length);
094:
095: for (int i = 0; i < count; i++) {
096: int row = (rows == null ? i : rows[i]);
097: RowData rowData = data.getRow(row);
098: StrBuffer line = converter.convertRowData(rowData, row);
099: line.writeTo(out);
100: out.flush();
101: }
102: }
103: }
|