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  import java.io.IOException;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  /**
22   * A "Proxy" writer which Provides a mecanism to skip empty row.
23   * 
24   * @author Olivier Godineau
25   * 
26   */
27  public class IgnoreNullWriter implements IWriter {
28  	/**
29  	 * the class logger.
30  	 */
31  	private static final Logger LOGGER = LoggerFactory.getLogger(IgnoreNullWriter.class);
32  	/**
33  	 * the original writer.
34  	 */
35  	private final IWriter writer;
36  	/**
37  	 * num of the next row to write.
38  	 */
39  	private int nextRowNum = 1;
40  
41  	/**
42  	 * 
43  	 * @param writer
44  	 *            a concret reader
45  	 */
46  	public IgnoreNullWriter(IWriter writer) {
47  		super();
48  		if (writer == null) {
49  			throw new IllegalArgumentException(IgnoreNullWriter.class.getSimpleName()
50  					+ " Constructor IWriter argument must not be null");
51  		}
52  		this.writer = writer;
53  
54  	}
55  
56  	/**
57  	 * Close the original writer.
58  	 */
59  	public void close() {
60  		try {
61  			writer.close();
62  		} catch (IOException e) {
63  			LOGGER.info("Error on closing reader", e);
64  		}
65  	}
66  
67  	/**
68  	 * writes a row only if not empty.
69  	 * 
70  	 * @param row
71  	 *            the row.
72  	 */
73  	public void addRow(Row row) {
74  
75  		if (isValid(row)) {
76  			Row copy = row.copy(nextRowNum++);
77  			writer.addRow(copy);
78  		}
79  	}
80  
81  	/**
82  	 * Write a row only if not empty.
83  	 * 
84  	 * @param values
85  	 *            the row.
86  	 */
87  	public void addLine(String[] values) {
88  		if (isValid(values)) {
89  			writer.addLine(values);
90  			nextRowNum++;
91  		}
92  
93  	}
94  
95  	/**
96  	 * @return the original withHeaders attribute.
97  	 */
98  	public boolean isWithHeaders() {
99  		return writer.isWithHeaders();
100 	}
101 
102 	/**
103 	 * Check if a row is not null and not empty.
104 	 * 
105 	 * @param row
106 	 *            the row
107 	 * @return true if the row is not null and not empty.
108 	 */
109 	private boolean isValid(Row row) {
110 		return (row != null && !row.isEmpty());
111 
112 	}
113 
114 	/**
115 	 * Check if a row is not null and not empty.
116 	 * 
117 	 * @param values
118 	 *            the row
119 	 * @return true if the row is not null and not empty.
120 	 */
121 	private boolean isValid(String[] values) {
122 		boolean retour = false;
123 		if (values != null) {
124 
125 			for (String value : values) {
126 				if (value != null) {
127 					retour = true;
128 					break;
129 				}
130 			}
131 		}
132 		return retour;
133 	}
134 
135 }