1
2
3
4
5 package oscon2006.web;
6
7 import java.io.*;
8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
9 import org.apache.poi.hwpf.extractor.WordExtractor;
10 import com.healthmarketscience.jackcess.Database;
11 import au.com.bytecode.opencsv.*;
12 import wicket.markup.html.form.upload.*;
13 import wicket.util.lang.*;
14 import oscon2006.common.*;
15
16 public class FileUploadForm extends BaseForm
17 {
18 private FileUploadField fileField = new FileUploadField("file");
19
20 public FileUploadForm(String componentName)
21 {
22 super(componentName);
23
24 this.setMultiPart(true);
25 this.setMaxSize(Bytes.megabytes(100));
26
27 fileField.setRequired(true);
28
29 add(fileField);
30 }
31
32 public final void onSubmit()
33 {
34 FileUpload upload = this.fileField.getFileUpload();
35 if (upload != null)
36 {
37 processFileUpload(upload);
38 }
39 else
40 {
41 setResponsePage(FileUploadPage.class);
42 }
43 }
44
45 protected void processFileUpload(FileUpload u)
46 {
47 String clientFileName = u.getClientFileName();
48
49 String lowerFileName = clientFileName.toLowerCase();
50
51 if (lowerFileName.endsWith(".xls"))
52 {
53 processXlsFile(u);
54 }
55 else if (lowerFileName.endsWith(".csv"))
56 {
57 processCsvFile(u);
58 }
59 else if (lowerFileName.endsWith(".mdb"))
60 {
61 processMdbFile(u);
62 }
63 else if (lowerFileName.endsWith(".doc"))
64 {
65 processDocFile(u);
66 }
67 else
68 {
69 setResponsePage(FileUploadPage.class);
70 }
71
72 }
73
74 protected void processXlsFile(FileUpload u)
75 {
76 try
77 {
78 HSSFWorkbook w = new HSSFWorkbook(u.getInputStream());
79 DisplayExcelPage p = new DisplayExcelPage(u.getClientFileName(), w);
80 setResponsePage(p);
81 }
82 catch (IOException ex)
83 {
84 setResponsePage(FileUploadPage.class);
85 }
86 }
87
88 protected void processMdbFile(FileUpload u)
89 {
90
91 Database db = null;
92
93 try
94 {
95 File f = createTempFile(u.getInputStream());
96 db = Database.open(f);
97 setResponsePage(new DisplayMdbPage(u.getClientFileName(), db));
98 }
99 catch (Exception ex)
100 {
101 ex.printStackTrace();
102 }
103
104
105 }
106
107 protected void processCsvFile(FileUpload u)
108 {
109
110 InputStream input = null;
111
112 try
113 {
114 input = u.getInputStream();
115 InputStreamReader isr = new InputStreamReader(input);
116 CSVReader r = new CSVReader(isr);
117 XlsBuilder builder = new XlsBuilder();
118 builder.setData(r);
119 byte[] data = builder.build();
120 setResponse(data, "file.xls");
121 }
122 catch (IOException ex)
123 {
124 throw new RuntimeException(ex);
125 }
126 finally
127 {
128 try
129 {
130 input.close();
131 }
132 catch (IOException ex)
133 {
134
135 }
136 }
137 }
138
139 protected void processDocFile(FileUpload u)
140 {
141 InputStream input = null;
142
143 try
144 {
145 input = u.getInputStream();
146
147 WordExtractor extractor = new WordExtractor(input);
148
149 String docText = extractor.getText();
150
151 DisplayDocPage p = new DisplayDocPage(u.getClientFileName(), docText);
152
153 setResponsePage(p);
154 }
155 catch (IOException ex)
156 {
157 ex.printStackTrace();
158 setResponsePage(FileUploadPage.class);
159 }
160 }
161
162 private static File createTempFile(InputStream input)
163 throws IOException
164 {
165 File f = File.createTempFile("wicket", "mdb");
166 f.deleteOnExit();
167 FileOutputStream fos = new FileOutputStream(f);
168 byte[] buffer = new byte[4096];
169 int n = -1;
170 while ( (n = input.read(buffer)) > 0)
171 {
172 fos.write(buffer, 0, n);
173 }
174 fos.flush();
175 fos.close();
176 return f;
177 }
178 }