1 package org.tigris.scarab.screens; 2 3 48 49 import java.io.Writer ; 50 import java.io.IOException ; 51 import java.io.PrintWriter ; 52 import java.io.OutputStreamWriter ; 53 import java.util.List ; 54 55 import org.apache.commons.lang.StringUtils; 56 57 import org.apache.turbine.Turbine; 58 import org.apache.turbine.RunData; 59 import org.apache.turbine.TemplateContext; 60 61 import org.tigris.scarab.util.ScarabUtil; 62 import org.tigris.scarab.util.export.ExportFormat; 63 64 65 87 class DataExport extends Default 88 { 89 93 protected static final String NO_CONTENT = ""; 94 95 100 public void doBuildTemplate(RunData data, TemplateContext context) 101 throws Exception 102 { 103 super.doBuildTemplate(data, context); 104 String format = ScarabUtil.findValue(data, ExportFormat.KEY_NAME); 105 106 if (ExportFormat.EXCEL_FORMAT.equalsIgnoreCase(format)) 111 { 112 data.getResponse().setContentType("application/vnd.ms-excel"); 113 } 114 else 115 { 116 String encoding = getEncodingForExport(data); 119 String contentType = "text/plain"; 120 if (encoding != null && !encoding.equals("")) 121 { 122 contentType = contentType + "; charset=" + encoding; 123 } 124 data.getResponse().setContentType(contentType); 125 } 126 130 135 data.setTarget(null); 137 } 138 139 144 protected String getEncodingForExport(RunData data) 145 { 146 String encoding = Turbine.getConfiguration() 147 .getString("scarab.dataexport.encoding"); 148 return encoding; 149 } 150 151 156 protected Writer getWriter(RunData data) 157 throws IOException 158 { 159 Writer writer = null; 160 String encoding = getEncodingForExport(data); 161 if (encoding != null && !encoding.equals("")) 162 { 163 writer = 164 new OutputStreamWriter (data.getResponse().getOutputStream(), 165 encoding); 166 } 167 else 168 { 169 writer = data.getResponse().getWriter(); 170 } 171 return writer; 172 } 173 174 180 protected String escapeCommas(String s) 181 { 182 return quote(s); 185 } 186 187 protected final boolean containsElements(List l) 188 { 189 return l != null && !l.isEmpty(); 190 } 191 192 198 private static String quote(String s) 199 { 200 return '"' + StringUtils.replace(s, "\"", "\"\"") + '"'; 201 } 202 203 210 protected class TSVPrinter 211 { 212 215 private boolean lineStart = true; 216 217 220 private PrintWriter printer = null; 221 222 229 public TSVPrinter(Writer writer) 230 { 231 if (writer == null) 232 { 233 throw new IllegalArgumentException 236 ("TSVPrinter constructor requires a non-null writer"); } 238 239 if (writer instanceof PrintWriter ) 240 { 241 this.printer = (PrintWriter ) writer; 242 } 243 else 244 { 245 this.printer = new PrintWriter (writer); 246 } 247 } 248 249 252 public void print(String s) 253 { 254 if (!lineStart) 255 { 256 printer.print('\t'); 258 } 259 lineStart = false; 260 261 if (StringUtils.isNotEmpty(s)) 262 { 263 printer.print(escape(s)); 264 } 265 } 266 267 271 public void println() 272 { 273 printer.println(); 274 printer.flush(); 275 lineStart = true; 276 } 277 278 284 protected String quote(String s) 285 { 286 return DataExport.quote(s); 287 } 288 289 297 private String escape(String s) 298 { 299 if (StringUtils.isNotEmpty(s)) 300 { 301 for (int i = 0; i < s.length(); i++) 302 { 303 char c = s.charAt(i); 304 if (c == '"' || c == '\t' || c == '\n' || c == '\r') 305 { 306 s = quote(s); 307 break; 308 } 309 } 310 } 311 312 return s; 313 } 314 } 315 } 316 | Popular Tags |