1:
31:
32: package ;
33:
34: import ;
35:
36: import ;
37: import ;
38: import ;
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:
52:
57: public class HtmlFileExportTask implements Runnable
58: {
59: private ReportJob job;
60: private File dataDirectory;
61: private File targetDirectory;
62: private String exportMethod;
63: private String suffix;
64: private String filename;
65: private String encoding;
66:
67: public HtmlFileExportTask(final ReportJob job)
68: throws ReportConfigurationException
69: {
70: if (job == null)
71: {
72: throw new NullPointerException();
73: }
74: this.job = job;
75:
76: final Configuration config = job.getConfiguration();
77: final String dataDirectoryName = config.getConfigProperty
78: ("org.jfree.report.modules.gui.common.html.file.DataDirectory");
79: final String targetFileName = config.getConfigProperty
80: ("org.jfree.report.modules.gui.common.html.file.TargetFileName");
81: exportMethod = config.getConfigProperty
82: ("org.jfree.report.modules.gui.common.html.file.ExportMethod");
83: encoding = config.getConfigProperty
84: ("org.jfree.report.modules.gui.common.html.file.Encoding", "ASCII");
85:
86: final File targetFile = new File(targetFileName);
87: targetDirectory = targetFile.getParentFile();
88:
89: dataDirectory = new File(targetFile, dataDirectoryName);
90: if (dataDirectory.isDirectory() == false)
91: {
92: throw new ReportConfigurationException("DataDirectory is invalid: " + dataDirectory);
93: }
94:
95: suffix = IOUtils.getInstance().getFileExtension(targetFile.getName());
96: filename = IOUtils.getInstance().stripFileExtension(targetFile.getName());
97:
98: if (targetFile.exists())
99: {
100:
101: if (targetFile.delete() == false)
102: {
103: throw new ReportConfigurationException
104: ("Target-File exists, but cannot be removed.");
105: }
106: }
107: }
108:
109:
119: public void run()
120: {
121: try
122: {
123: final FileRepository targetRepository = new FileRepository(targetDirectory);
124: final ContentLocation targetRoot = targetRepository.getRoot();
125:
126: final FileRepository dataRepository = new FileRepository(dataDirectory);
127: final ContentLocation dataRoot = dataRepository.getRoot();
128:
129: final StreamingReportProcessor sp = new StreamingReportProcessor();
130: final HtmlOutputProcessor outputProcessor = createOutputProcessor();
131: final HtmlPrinter printer = outputProcessor.getPrinter();
132: printer.setContentWriter(targetRoot,
133: new DefaultNameGenerator(targetRoot, filename, suffix));
134: printer.setDataWriter(dataRoot, new DefaultNameGenerator(dataRoot, "content"));
135: printer.setEncoding(encoding);
136: printer.setUrlRewriter(new FileSystemURLRewriter());
137: sp.setOutputProcessor(outputProcessor);
138: sp.processReport(job);
139: }
140: catch(Exception e)
141: {
142: Log.error ("File-Export failed. ", e);
143: }
144: finally{
145: try
146: {
147: job.close();
148: job = null;
149: }
150: catch(Exception e)
151: {
152:
153: }
154: }
155: }
156:
157: protected HtmlOutputProcessor createOutputProcessor()
158: {
159: if ("pageable".equals(exportMethod))
160: {
161: return new PageableHtmlOutputProcessor(job.getConfiguration());
162: }
163: else if ("flow".equals(exportMethod))
164: {
165: return new FlowHtmlOutputProcessor(job.getConfiguration());
166: }
167: else
168: {
169: return new StreamingHtmlOutputProcessor(job.getConfiguration());
170: }
171: }
172:
173: }