1:
31:
32: package ;
33:
34: import ;
35: import ;
36: import ;
37: import ;
38:
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55:
60: public class HtmlZipExportTask implements Runnable
61: {
62: private ReportJob job;
63: private String dataDirectory;
64: private String exportMethod;
65: private String filename;
66: private File targetFile;
67: private String encoding;
68:
69: public HtmlZipExportTask(final ReportJob job)
70: throws ReportConfigurationException
71: {
72: if (job == null)
73: {
74: throw new NullPointerException();
75: }
76: this.job = job;
77:
78: final Configuration config = job.getConfiguration();
79: dataDirectory = config.getConfigProperty
80: ("org.jfree.report.modules.gui.common.html.zip.DataDirectory");
81: final String targetFileName = config.getConfigProperty
82: ("org.jfree.report.modules.gui.common.html.zip.TargetFileName");
83: exportMethod = config.getConfigProperty
84: ("org.jfree.report.modules.gui.common.html.zip.ExportMethod");
85: encoding = config.getConfigProperty
86: ("org.jfree.report.modules.gui.common.html.zip.Encoding", "ASCII");
87:
88: targetFile = new File(targetFileName);
89: filename = IOUtils.getInstance().stripFileExtension(targetFile.getName());
90:
91: if (targetFile.exists())
92: {
93:
94: if (targetFile.delete() == false)
95: {
96: throw new ReportConfigurationException
97: ("Target-File exists, but cannot be removed.");
98: }
99: }
100: }
101:
102:
112: public void run()
113: {
114: OutputStream fout = null;
115: try
116: {
117: fout = new BufferedOutputStream(new FileOutputStream(targetFile));
118: final ZipRepository zipRepository = new ZipRepository(fout);
119: final ContentLocation root = zipRepository.getRoot();
120: final ContentLocation data = RepositoryUtilities.createLocation
121: (zipRepository, RepositoryUtilities.split(dataDirectory, "/"));
122: final StreamingReportProcessor sp = new StreamingReportProcessor();
123:
124: final HtmlOutputProcessor outputProcessor = createOutputProcessor();
125: final HtmlPrinter printer = outputProcessor.getPrinter();
126:
127: printer.setContentWriter(root, new DefaultNameGenerator(root, filename));
128: printer.setDataWriter(data, new DefaultNameGenerator(data, "content"));
129: printer.setEncoding(encoding);
130:
131: sp.setOutputProcessor(outputProcessor);
132: sp.processReport(job);
133: }
134: catch(Exception e)
135: {
136: Log.error ("ZIP-Export failed. ", e);
137: }
138: finally
139: {
140: try
141: {
142: job.close();
143: job = null;
144: }
145: catch(Exception e)
146: {
147:
148: }
149:
150: if (fout != null)
151: {
152: try
153: {
154: fout.close();
155: }
156: catch(Exception e)
157: {
158:
159: }
160: }
161:
162: }
163: }
164:
165: protected HtmlOutputProcessor createOutputProcessor()
166: {
167: if ("pageable".equals(exportMethod))
168: {
169: return new PageableHtmlOutputProcessor(job.getConfiguration());
170: }
171: else if ("flow".equals(exportMethod))
172: {
173: return new FlowHtmlOutputProcessor(job.getConfiguration());
174: }
175: else
176: {
177: return new StreamingHtmlOutputProcessor(job.getConfiguration());
178: }
179: }
180: }