View Javadoc
1   /*
2    * Copyright 2012 Olivier Godineau
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package olg.csv.base.ods;
15  
16  import java.util.List;
17  
18  import olg.csv.base.AbstractSheetSettings;
19  import olg.csv.base.ods.ODSReader.ODSCellFormatter;
20  import olg.csv.base.ods.ODSWriter.ODSParser;
21  import olg.csv.bean.formatter.Formatter;
22  import olg.csv.bean.parser.AbstractParser;
23  
24  import org.odftoolkit.odfdom.doc.table.OdfTableCell;
25  
26  /**
27   * Settings Class for ODS document reading and writing.
28   * 
29   * @author Olivier Godineau
30   * 
31   * @see ODSWriter
32   * @see ODSReader
33   */
34  public class ODSSettings extends AbstractSheetSettings<ODSSettings> {
35  	/**
36  	 * Default Emptying row value.
37  	 * <p>
38  	 * There's two ways to rewrite an ODS file when rows which are passed are
39  	 * non-consecutive :
40  	 * <ul>
41  	 * <li>Intermediate lines are left as</li>
42  	 * <li>Intermediate lines are emptied (default behavior)</li>
43  	 * </ul>
44  	 * </p>
45  	 * 
46  	 * @see #setEmptyingRow(boolean)
47  	 */
48  	public static final boolean DEFAULT_EMPTYINGROW = true;
49  
50  	/**
51  	 * Default Emptying cell value.
52  	 * <p>
53  	 * There's two ways to rewrite an ODS file when cells which are passed on a
54  	 * row are non-consecutive :
55  	 * <ul>
56  	 * <li>Intermediate cells are left as</li>
57  	 * <li>Intermediate cells are emptied</li>
58  	 * </ul>
59  	 * </p>
60  	 * 
61  	 * @see #setEmptyingCell(boolean)
62  	 */
63  	public static final boolean DEFAULT_EMPTYINGCELL = true;
64  
65  	/**
66  	 * 
67  	 */
68  	private Formatter<OdfTableCell> cellFormatter = null;
69  	/**
70  	 * 
71  	 */
72  	private AbstractParser<List<String>> stringParser = null;
73  	/**
74  	 * Emptying row value. If true intermediate rows will be emptied.
75  	 */
76  	private boolean emptyingRow = DEFAULT_EMPTYINGROW;
77  	/**
78  	 * Emptying cell value. If true intermediate cells will be emptied.
79  	 */
80  	private boolean emptyingCell = DEFAULT_EMPTYINGCELL;
81  
82  	/**
83  	 * Purpose default settings.
84  	 * <ul>
85  	 * <li>sheetNum = 0</li>
86  	 * <li>withHeaders = true</li>
87  	 * <li>beginAtRow = 1</li>
88  	 * <li>endAtRow = null</li>
89  	 * <li>beginAtColumn=null</li>
90  	 * <li><endAtColumn = null</li>
91  	 * <li>cellFormatter = ODSCellFormatter(\n)</li>
92  	 * </ul>
93  	 * 
94  	 * @see ODSReader.ODSCellFormatter
95  	 */
96  	public ODSSettings() {
97  		super();
98  		this.cellFormatter = new ODSCellFormatter("\n");
99  		this.stringParser = new ODSParser();
100 
101 	}
102 
103 	/**
104 	 * Defines settings for writing process.
105 	 * 
106 	 * @param sheetName
107 	 *            The name of the Spreadsheet to write on. If the file on which
108 	 *            to write exists and contains a sheet with the specified name,
109 	 *            the writer will write on it otherwise the writer will write on
110 	 *            a new sheet. If the file doesn't exist, the writer create it
111 	 *            and its specified sheet.
112 	 * @param beginAtRow
113 	 *            specify the first line to begin writing. May be
114 	 *            <code>null</code>
115 	 * @param beginAtColumn
116 	 *            specify the first column where to begin writing. May be
117 	 *            <code>null</code>
118 	 * @see ODSReader.ODSCellFormatter ODS Cell Formatter for reading process
119 	 *      which use'\n' character as line separator
120 	 * @see ODSParser String Parser used for writing process. Define how to
121 	 *      interpret a value as a ODS Cell content. For each line returned by
122 	 *      this parser a paragraph will be created into the cell.
123 	 */
124 	public ODSSettings(String sheetName, Integer beginAtRow, String beginAtColumn) {
125 		super(0, ODSSettings.DEFAULT_WITHHEADERS, beginAtRow, null, beginAtColumn, null, sheetName);
126 		this.cellFormatter = new ODSCellFormatter("\n");
127 		this.stringParser = new ODSParser();
128 	}
129 
130 	/**
131 	 * Defines settings for writing process.
132 	 * 
133 	 * @param sheetName
134 	 *            The name of the Spreadsheet to write on. If the file on which
135 	 *            to write exists and contains a sheet with the specified name,
136 	 *            the writer will write on it otherwise the writer will write on
137 	 *            a new sheet. If the file doesn't exist, the writer create it
138 	 *            and its specified sheet.
139 	 * @see ODSReader.ODSCellFormatter ODS Cell Formatter for reading process
140 	 *      which use'\n' character as line separator
141 	 * @see ODSParser String Parser used for writing process. Define how to
142 	 *      interpret a value as a ODS Cell content. For each line returned by
143 	 *      this parser a paragraph will be created into the cell.
144 	 */
145 	public ODSSettings(String sheetName) {
146 		super(0, ODSSettings.DEFAULT_WITHHEADERS, null, null, null, null, sheetName);
147 		this.cellFormatter = new ODSCellFormatter("\n");
148 		this.stringParser = new ODSParser();
149 	}
150 
151 	/**
152 	 * Defines settings for Reading process.
153 	 * 
154 	 * @param sheetNum
155 	 *            the table name in the document.First is 0.
156 	 * @param beginAtRow
157 	 *            specify the first line to begin reading. May be
158 	 *            <code>null</code>
159 	 * @param endAtRow
160 	 *            specify the last line to read. May be <code>null</code>
161 	 * @param beginAtColumn
162 	 *            specify the first column where to begin reading. May be
163 	 *            <code>null</code>
164 	 * @param endAtColumn
165 	 *            specify the last column where to end traitment. May be
166 	 *            <code>null</code>
167 	 * @param withHeaders
168 	 *            indicate if the table has headers.
169 	 * @see ODSCellFormatter
170 	 * @see ODSParser
171 	 */
172 	public ODSSettings(int sheetNum, Integer beginAtRow, Integer endAtRow, String beginAtColumn, String endAtColumn,
173 			boolean withHeaders) {
174 		super(sheetNum, withHeaders, beginAtRow, endAtRow, beginAtColumn, endAtColumn, null);
175 		this.cellFormatter = new ODSCellFormatter("\n");
176 		this.stringParser = new ODSParser();
177 	}
178 
179 	/**
180 	 * Defines settings for Reading process.
181 	 * 
182 	 * @param sheetNum
183 	 *            the table name in the document.First is 0.
184 	 * @param withHeaders
185 	 *            indicate if the table has headers.
186 	 * @see ODSCellFormatter
187 	 * @see ODSParser
188 	 */
189 	public ODSSettings(int sheetNum, boolean withHeaders) {
190 		super(sheetNum, withHeaders, null, null, null, null, null);
191 		this.cellFormatter = new ODSCellFormatter("\n");
192 		this.stringParser = new ODSParser();
193 	}
194 
195 	/**
196 	 * Returns a cell formatter. This formatter defines how extract cell content
197 	 * as a string. Especially when cell is composed with paragraphs or line
198 	 * breaks; Only for reading.
199 	 * 
200 	 * @see ODSReader.ODSCellFormatter
201 	 * @return the cell formatter.
202 	 */
203 	public Formatter<OdfTableCell> getCellFormatter() {
204 		return cellFormatter;
205 	}
206 
207 	/**
208 	 * Sets the cell formatter to use. This formatter define how extract cell
209 	 * content as a string. Especially when cell is composed with paragraphs or
210 	 * line breaks; Only for reading.
211 	 * 
212 	 * @param cellFormatter
213 	 *            the formatter
214 	 * @return this instance
215 	 */
216 	public ODSSettings setCellFormatter(Formatter<OdfTableCell> cellFormatter) {
217 		this.cellFormatter = cellFormatter;
218 		return this;
219 	}
220 
221 	/**
222 	 * Returns the String parser. This parser defines how to interpret breakline
223 	 * characters found into a string and returns lines extracted from this
224 	 * string. {@link ODSWriter} uses this parser to create for each returned
225 	 * line a paragraph in the ODS Cell(where line breaks are not interpreted),
226 	 * target of the original string. Only for writing
227 	 * 
228 	 * @see ODSWriter.ODSParser
229 	 * @return the string parser
230 	 */
231 	public AbstractParser<List<String>> getStringParser() {
232 		return stringParser;
233 	}
234 
235 	/**
236 	 * Set the String parser. This parser defines how to interpret breakline
237 	 * characters found into a string and returns lines extracted from this
238 	 * string. {@link ODSWriter} uses this parser to create for each returned
239 	 * line a paragraph in the ODS Cell target of the original string. Only for
240 	 * writing
241 	 * 
242 	 * @param stringParser
243 	 *            the parser.
244 	 * @return this instance
245 	 * @see ODSWriter.ODSParser
246 	 */
247 	public ODSSettings setStringParser(AbstractParser<List<String>> stringParser) {
248 		this.stringParser = stringParser;
249 		return this;
250 	}
251 
252 	/**
253 	 * Returns the policy on rows rewriting.
254 	 * 
255 	 * @return true if intermediate lines are emptied
256 	 * @see #setEmptyingRow(boolean)
257 	 */
258 	public boolean isEmptyingRow() {
259 		return emptyingRow;
260 	}
261 
262 	/**
263 	 * Sets the policy on rows rewriting. There's two ways to rewrite an ODS
264 	 * file when rows which are passed are non-consecutive :
265 	 * <ul>
266 	 * <li>Intermediate lines are left as</li>
267 	 * <li>Intermediate lines are emptied (default behavior)</li>
268 	 * </ul>
269 	 * Applied only when an existing file is overridden.
270 	 * 
271 	 * @param emptyingRow
272 	 *            the value to set.
273 	 * @return the ODSSettings instance.
274 	 * 
275 	 */
276 	public ODSSettings setEmptyingRow(boolean emptyingRow) {
277 		this.emptyingRow = emptyingRow;
278 		return this;
279 	}
280 
281 	/**
282 	 * Returns the policy on cells rewriting.
283 	 * 
284 	 * @return true if intermediate cells are emptied
285 	 * @see #setEmptyingCell(boolean)
286 	 */
287 	public boolean isEmptyingCell() {
288 		return emptyingCell;
289 	}
290 
291 	/**
292 	 * Sets the policy on cells rewriting. There's two ways to rewrite an ODS
293 	 * file when cells which are passed on a row are non-consecutive :
294 	 * <ul>
295 	 * <li>Intermediate cells are left as</li>
296 	 * <li>Intermediate cells are emptied (default behavior)</li>
297 	 * </ul>
298 	 * Applied only when an existing file is overridden.
299 	 * 
300 	 * @param emptyingCell
301 	 *            the value to set.
302 	 * @return this instance
303 	 * 
304 	 */
305 	public ODSSettings setEmptyingCell(boolean emptyingCell) {
306 		this.emptyingCell = emptyingCell;
307 		return this;
308 	}
309 
310 }