Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19a591995c7cbe4dec1fb7265ff766816d63bec7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*********************************************************************
 * Copyright (c) 2004, 2007 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

package org.eclipse.osee.framework.jdk.core.util.io.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;

/**
 * @author Ryan D. Brooks
 */
public abstract class AbstractSheetWriter implements ISheetWriter {
   private boolean startRow;
   private int implicitCellIndex;

   public AbstractSheetWriter() {
      startRow = true;
      implicitCellIndex = 0;
   }

   @Override
   public void startSheet(String worksheetName, int columnCount, File file) throws IOException {
      startSheet(worksheetName, columnCount, file == null ? null : new BufferedWriter(new FileWriter(file)));
   }

   /**
    * must be called by subclasses in their implementations of writeCell(String data, int cellIndex)
    */
   protected void startRowIfNecessary() throws IOException {
      if (startRow) {
         startRow();
         startRow = false;
      }
   }

   @Override
   public void writeRow(Collection<?> row) throws IOException {
      writeRow(row.toArray(new Object[row.size()]));
   }

   @Override
   public void writeRow(Object... row) throws IOException {
      for (int i = 0; i < row.length; i++) {
         writeCell(row[i], implicitCellIndex);
      }

      endRow();
   }

   /**
    * when calling writeCell with an index, the implicit index will be set to one greater than the given index
    */
   @Override
   public void writeCell(Object data, int cellIndex) throws IOException {
      startRowIfNecessary();
      implicitCellIndex = cellIndex + 1;
      writeCellText(data, cellIndex);
   }

   @Override
   public void endRow() throws IOException {
      startRowIfNecessary();
      startRow = true;
      implicitCellIndex = 0;
      writeEndRow();
   }

   /**
    * every time you call writeCell, the implicit index will be incremented
    */
   @Override
   public void writeCell(Object cellData) throws IOException {
      writeCell(cellData, implicitCellIndex);
   }

   @Override
   public void writeEmptyCell() throws IOException {
      writeCell(null);
   }

   protected abstract void startRow() throws IOException;

   protected abstract void writeEndRow() throws IOException;

   protected abstract void writeCellText(Object data, int cellIndex) throws IOException;

}

Back to the top