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;
15  
16  /**
17   * Abstract class for SpreadSheet document writer. Embeds some Sheet writer
18   * characteristics. Not a great interest : Provides a settings validation on
19   * instanciation.
20   * 
21   * @author Olivier Godineau
22   * 
23   */
24  public abstract class AbstractSheetWriter implements IWriter {
25  
26  	/**
27  	 * the sheet name on which write.
28  	 */
29  	protected String sheetName;
30  
31  	/**
32  	 * Index from which begin the writing.
33  	 */
34  	protected Integer beginAtRow = 1;
35  	/**
36  	 * Index from which begin the row writing.
37  	 */
38  	protected Integer beginAtColumn = 0;
39  	/**
40  	 * Indicate if an header row must be written.
41  	 */
42  	protected boolean withHeaders;
43  
44  	/**
45  	 * 
46  	 * @param settings
47  	 *            the settings.
48  	 */
49  	protected AbstractSheetWriter(AbstractSheetSettings<? extends AbstractSheetSettings<?>> settings) {
50  		if (settings == null) {
51  			throw new IllegalArgumentException("SheetWriter Constructor settings argument must not be null");
52  		}
53  		if (settings.getSheetName() == null) {
54  			throw new IllegalArgumentException(
55  					"SheetWriter constructor settings argument : must have not null sheetName attribut");
56  		}
57  
58  		this.sheetName = settings.getSheetName();
59  		if (settings.getBeginAtRow() != null) {
60  			this.beginAtRow = settings.getBeginAtRow();
61  			if (this.beginAtRow < 1) {
62  				throw new IllegalArgumentException("SheetWriter Constructor ettings argument :beginAtRow parameter ["
63  						+ beginAtRow
64  						+ "] must be a positive number or null (in this last case reading begins at the first row)");
65  
66  			}
67  		}
68  
69  		if (settings.getBeginAtColumn() != null) {
70  			this.beginAtColumn = Cell.fromSheetCellNumber(settings.getBeginAtColumn());
71  		}
72  		this.withHeaders = settings.isWithHeaders();
73  	}
74  
75  	/**
76  	 * Returns the sheet name.
77  	 * 
78  	 * @return the sheet name.
79  	 */
80  	public String getSheetName() {
81  		return sheetName;
82  	}
83  
84  	/**
85  	 * Indicates if a header row must be written.
86  	 * 
87  	 * @return if a header row must be written
88  	 */
89  	public boolean isWithHeaders() {
90  
91  		return withHeaders;
92  	}
93  
94  }