1
2
3
4
5
6
7
8
9
10
11
12 package org.displaytag.export;
13
14 import java.io.IOException;
15 import java.io.Writer;
16 import java.util.Iterator;
17
18 import javax.servlet.jsp.JspException;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.displaytag.model.Column;
24 import org.displaytag.model.ColumnIterator;
25 import org.displaytag.model.HeaderCell;
26 import org.displaytag.model.Row;
27 import org.displaytag.model.RowIterator;
28 import org.displaytag.model.TableModel;
29
30
31
32
33
34
35
36
37
38
39
40
41 public abstract class BaseExportView implements TextExportView
42 {
43
44
45
46
47 private static Log log = LogFactory.getLog(BaseExportView.class);
48
49
50
51
52 private TableModel model;
53
54
55
56
57 private boolean exportFull;
58
59
60
61
62 private boolean header;
63
64
65
66
67 private boolean decorated;
68
69
70
71
72 public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
73 boolean decorateValues)
74 {
75 this.model = tableModel;
76 this.exportFull = exportFullList;
77 this.header = includeHeader;
78 this.decorated = decorateValues;
79 }
80
81
82
83
84
85 protected String getRowStart()
86 {
87 return null;
88 }
89
90
91
92
93
94 protected String getRowEnd()
95 {
96 return null;
97 }
98
99
100
101
102
103 protected String getCellStart()
104 {
105 return null;
106 }
107
108
109
110
111
112 protected abstract String getCellEnd();
113
114
115
116
117
118 protected String getDocumentStart()
119 {
120 return null;
121 }
122
123
124
125
126
127 protected String getDocumentEnd()
128 {
129 return null;
130 }
131
132
133
134
135
136 protected abstract boolean getAlwaysAppendCellEnd();
137
138
139
140
141
142 protected abstract boolean getAlwaysAppendRowEnd();
143
144
145
146
147
148
149 protected abstract String escapeColumnValue(Object value);
150
151
152
153
154
155 protected String doHeaders()
156 {
157
158 final String ROW_START = getRowStart();
159 final String ROW_END = getRowEnd();
160 final String CELL_START = getCellStart();
161 final String CELL_END = getCellEnd();
162 final boolean ALWAYS_APPEND_CELL_END = getAlwaysAppendCellEnd();
163
164 StringBuffer buffer = new StringBuffer(1000);
165
166 Iterator iterator = this.model.getHeaderCellList().iterator();
167
168
169 if (ROW_START != null)
170 {
171 buffer.append(ROW_START);
172 }
173
174 while (iterator.hasNext())
175 {
176 HeaderCell headerCell = (HeaderCell) iterator.next();
177
178 String columnHeader = headerCell.getTitle();
179
180 if (columnHeader == null)
181 {
182 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
183 }
184
185 columnHeader = escapeColumnValue(columnHeader);
186
187 if (CELL_START != null)
188 {
189 buffer.append(CELL_START);
190 }
191
192 if (columnHeader != null)
193 {
194 buffer.append(columnHeader);
195 }
196
197 if (CELL_END != null && (ALWAYS_APPEND_CELL_END || iterator.hasNext()))
198 {
199 buffer.append(CELL_END);
200 }
201 }
202
203
204 if (ROW_END != null)
205 {
206 buffer.append(ROW_END);
207 }
208
209 return buffer.toString();
210
211 }
212
213
214
215
216 public void doExport(Writer out) throws IOException, JspException
217 {
218 if (log.isDebugEnabled())
219 {
220 log.debug(getClass().getName());
221 }
222
223 final String DOCUMENT_START = getDocumentStart();
224 final String DOCUMENT_END = getDocumentEnd();
225 final String ROW_START = getRowStart();
226 final String ROW_END = getRowEnd();
227 final String CELL_START = getCellStart();
228 final String CELL_END = getCellEnd();
229 final boolean ALWAYS_APPEND_CELL_END = getAlwaysAppendCellEnd();
230 final boolean ALWAYS_APPEND_ROW_END = getAlwaysAppendRowEnd();
231
232
233 write(out, DOCUMENT_START);
234
235 if (this.header)
236 {
237 write(out, doHeaders());
238 }
239
240
241 RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
242
243
244 while (rowIterator.hasNext())
245 {
246 Row row = rowIterator.next();
247
248 if (this.model.getTableDecorator() != null)
249 {
250
251 String stringStartRow = this.model.getTableDecorator().startRow();
252 write(out, stringStartRow);
253 }
254
255
256 ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
257
258 write(out, ROW_START);
259
260 while (columnIterator.hasNext())
261 {
262 Column column = columnIterator.nextColumn();
263
264
265 String value = escapeColumnValue(column.getValue(this.decorated));
266
267 write(out, CELL_START);
268
269 write(out, value);
270
271 if (ALWAYS_APPEND_CELL_END || columnIterator.hasNext())
272 {
273 write(out, CELL_END);
274 }
275
276 }
277 if (ALWAYS_APPEND_ROW_END || rowIterator.hasNext())
278 {
279 write(out, ROW_END);
280 }
281 }
282
283
284 write(out, DOCUMENT_END);
285
286 }
287
288
289
290
291
292
293
294 private void write(Writer out, String string) throws IOException
295 {
296 if (string != null)
297 {
298 out.write(string);
299 }
300 }
301
302
303
304
305 public boolean outputPage()
306 {
307 return false;
308 }
309 }