View Javadoc
1   package org.csveed.row;
2   
3   import java.io.IOException;
4   import java.io.Writer;
5   import java.util.Collection;
6   import java.util.Iterator;
7   
8   import org.csveed.api.Header;
9   import org.csveed.api.Row;
10  import org.csveed.report.CsvException;
11  import org.csveed.report.GeneralError;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  public class RowWriterImpl implements RowWriter {
16  
17      private static final Logger LOG = LoggerFactory.getLogger(RowWriterImpl.class);
18  
19      private final Writer writer;
20  
21      private RowInstructions rowInstructions;
22  
23      private Header header;
24  
25      public RowWriterImpl(Writer writer) {
26          this(writer, new RowInstructionsImpl());
27      }
28  
29      public RowWriterImpl(Writer writer, RowInstructions rowInstructions) {
30          this.writer = writer;
31          this.rowInstructions = rowInstructions;
32      }
33  
34      @Override
35      public void writeRows(String[][] rows) {
36          for (String[] row : rows) {
37              writeRow(row);
38          }
39      }
40  
41      @Override
42      public void writeRows(Collection<Row> rows) {
43          for (Row row : rows) {
44              writeRow(row);
45          }
46      }
47  
48      @Override
49      public Row writeRow(String[] cells) {
50          Row row = new RowImpl(convertToLine(cells), header);
51          writeRow(row);
52          return row;
53      }
54  
55      @Override
56      public void writeRow(Row row) {
57          if (rowInstructions.isUseHeader() && this.header == null) {
58              throw new CsvException(new GeneralError("Header has not been set for this table. Make sure to write it or configure " +
59                      "it to be not used: .setUseHeader(false)"));
60          }
61          writeCells(row.iterator());
62      }
63  
64      @Override
65      public Header writeHeader(String[] headerNames) {
66          Header header = new HeaderImpl(convertToLine(headerNames));
67          writeHeader(header);
68          return header;
69      }
70  
71      @Override
72      public void writeHeader(Header header) {
73          this.header = header;
74          writeCells(header.iterator());
75      }
76  
77      @Override
78      public RowInstructions getRowInstructions() {
79          return this.rowInstructions;
80      }
81  
82      private void writeCells(Iterator<String> cells) {
83          int columnPosition = 1;
84          try {
85              while (cells.hasNext()) {
86                  String cell = cells.next();
87                  String nullSafeCell = cell != null ? cell : "";
88                  String headerValue = header != null ?  header.getName(columnPosition) : "";
89                  LOG.debug("Writing cell value [{}] in column position [{}], header value is [{}].", nullSafeCell, columnPosition, headerValue);
90                  if (columnPosition != 1) {
91                      writeSeparator();
92                  }
93                  if (rowInstructions.getQuotingEnabled()) {
94                      writeQuotedCell(nullSafeCell);
95                  } else {
96                      writeCell(nullSafeCell);
97                  }
98                  columnPosition++;
99              }
100             writeEOL();
101         } catch(IOException e){
102             LOG.trace("", e);
103             throw new CsvException(new GeneralError("Error in writing to the writer: " + e.getMessage()));
104         }
105     }
106 
107     private void writeEOL() throws IOException {
108         writer.write(rowInstructions.getEndOfLine());
109     }
110 
111     private void writeSeparator() throws IOException {
112         writer.write(rowInstructions.getSeparator());
113     }
114 
115     private void writeQuotedCell(String cell) throws IOException {
116         writer.write(rowInstructions.getQuote());
117         String searchString = Character.toString(rowInstructions.getQuote());
118         String replaceString = new String(new char[] { rowInstructions.getEscape(), rowInstructions.getQuote() } );
119         String replacedString = cell.replace(searchString, replaceString);
120         writeCell(replacedString);
121         writer.write(rowInstructions.getQuote());
122     }
123 
124     private void writeCell(String cell) throws IOException {
125         writer.write(cell);
126     }
127 
128     private LineWithInfo convertToLine(String[] cells) {
129         LineWithInfoneWithInfo">LineWithInfo line = new LineWithInfo();
130         for (String cell : cells) {
131             line.addCell(cell);
132         }
133         return line;
134     }
135 
136 }