1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.print.out;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.FilenameFilter;
|
16
|
import java.io.IOException;
|
17
|
import java.io.InputStream;
|
18
|
import java.net.URL;
|
19
|
import java.text.SimpleDateFormat;
|
20
|
import java.util.ArrayList;
|
21
|
import java.util.Arrays;
|
22
|
import java.util.Calendar;
|
23
|
import java.util.List;
|
24
|
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.jdom.Document;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
29
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
30
|
|
31
|
/**
|
32
|
* This abstract implementation of the {@link IPublishOutputModule} contains
|
33
|
* convenience methods for file path generation. Clients should consider
|
34
|
* extending this class.
|
35
|
*
|
36
|
* @author n.hoffmann
|
37
|
* @since Apr 20, 2010
|
38
|
* @version 1.0
|
39
|
*/
|
40
|
public abstract class PublishOutputModuleBase implements IPublishOutputModule {
|
41
|
private static final Logger logger = Logger
|
42
|
.getLogger(PublishOutputModuleBase.class);
|
43
|
|
44
|
/**
|
45
|
* The date format used by {@link #generateFilenameWithDate(String, String)}
|
46
|
*/
|
47
|
public static final String DATE_FORMAT_NOW = "yyyyMMdd-HHmm";
|
48
|
|
49
|
private FilenameFilter filter = new FilenameFilter() {
|
50
|
|
51
|
@Override
|
52
|
public boolean accept(File dir, String fileName) {
|
53
|
return fileName.substring(fileName.length() - 3).equals("xsl");
|
54
|
}
|
55
|
};
|
56
|
|
57
|
private File xslt;
|
58
|
private String filePath = null;
|
59
|
private Document inputDocument=null;
|
60
|
|
61
|
/**
|
62
|
* Generates a string containing the current date followed by the given
|
63
|
* name.
|
64
|
*
|
65
|
* @param name
|
66
|
* a string.
|
67
|
* @return a string containing the current date followed by the given name.
|
68
|
*/
|
69
|
protected String generateFilenameWithDate(String name) {
|
70
|
StringBuffer buffer = new StringBuffer();
|
71
|
|
72
|
Calendar cal = Calendar.getInstance();
|
73
|
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
|
74
|
buffer.append(sdf.format(cal.getTime()));
|
75
|
|
76
|
buffer.append("-");
|
77
|
|
78
|
buffer.append(name);// "xml_export"
|
79
|
buffer.append("." + getOutputFileSuffix());
|
80
|
|
81
|
return buffer.toString();
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* return the complete path to the output file based on the given output
|
86
|
* folder. if it was not created before getNewFilePath() is called to create
|
87
|
* it
|
88
|
* @param outputFolder
|
89
|
* the folder to store the output file in.
|
90
|
* @return a string containing the full path to the output file.
|
91
|
*/
|
92
|
public String getFilePath(File outputFolder) {
|
93
|
/*
|
94
|
* @author s.buers: changed to: the filepath is only created once
|
95
|
*/
|
96
|
if (filePath == null) {
|
97
|
filePath = getNewFilePath(outputFolder);
|
98
|
}
|
99
|
return filePath;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @return the complete path to the output file
|
104
|
* @author s.buers
|
105
|
*/
|
106
|
public String getFilePath() {
|
107
|
return filePath;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* creates new name for the output file based on the given output
|
112
|
* folder and actual timestamp.
|
113
|
* @author s.buers
|
114
|
* @param outputFolder
|
115
|
* @return
|
116
|
*/
|
117
|
public String getNewFilePath(File outputFolder) {
|
118
|
return outputFolder.getPath() + File.separator
|
119
|
+ generateFilenameWithDate("output");
|
120
|
}
|
121
|
|
122
|
public Document getInputDocument() {
|
123
|
return inputDocument;
|
124
|
}
|
125
|
|
126
|
/*
|
127
|
* (non-Javadoc)
|
128
|
*
|
129
|
* @see
|
130
|
* eu.etaxonomy.printpublisher.out.IPublishOutputModule#output(org.jdom.
|
131
|
* Document, java.io.File)
|
132
|
*/
|
133
|
public void output(Document document, File exportFolder,
|
134
|
IProgressMonitor progressMonitor) {
|
135
|
if (progressMonitor == null) {
|
136
|
throw new IllegalArgumentException(
|
137
|
"ProgressMonitor may not be null");
|
138
|
}
|
139
|
inputDocument=document;
|
140
|
String message = "Running output module: "
|
141
|
+ this.getClass().getSimpleName();
|
142
|
logger.trace(message);
|
143
|
progressMonitor.subTask(message);
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public List<File> getStylesheets() throws IOException {
|
148
|
List<File> stylesheets = new ArrayList<File>();
|
149
|
|
150
|
for (File directory : getStylesheetLocations()) {
|
151
|
if (directory.exists() && directory.isDirectory()) {
|
152
|
stylesheets.addAll(getStylesheetsByLocation(directory));
|
153
|
} else {
|
154
|
logger.info(String
|
155
|
.format("Tried to read styleshets from '%s', but it does not exist or is not a directory",
|
156
|
directory));
|
157
|
}
|
158
|
}
|
159
|
|
160
|
return stylesheets;
|
161
|
|
162
|
}
|
163
|
|
164
|
private List<File> getStylesheetLocations() throws IOException {
|
165
|
List<File> locationList = new ArrayList<File>();
|
166
|
|
167
|
String l = File.separator;
|
168
|
|
169
|
URL shippedStylesheetsResource = PublishOutputModuleBase.class
|
170
|
.getResource("/stylesheets/pdf/");
|
171
|
File shippedStylesheetsDir = new File(
|
172
|
shippedStylesheetsResource.getFile());
|
173
|
locationList.add(shippedStylesheetsDir);
|
174
|
|
175
|
// TODO this should be configured in a central place, see #2387
|
176
|
File userdir = new File(CdmUtils.PER_USER_CDM_FOLDER + l + "stylesheets"
|
177
|
+ l + getOutputFileSuffix());
|
178
|
locationList.add(userdir);
|
179
|
|
180
|
return locationList;
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public File getXslt() {
|
185
|
return xslt;
|
186
|
}
|
187
|
|
188
|
@Override
|
189
|
public void setXslt(File xslt) {
|
190
|
this.xslt = xslt;
|
191
|
}
|
192
|
|
193
|
public InputStream getXsltInputStream() {
|
194
|
if (getXslt() == null) {
|
195
|
return getDefaultXsltInputStream();
|
196
|
}
|
197
|
|
198
|
try {
|
199
|
return new FileInputStream(getXslt());
|
200
|
} catch (FileNotFoundException e) {
|
201
|
logger.error(e);
|
202
|
}
|
203
|
return null;
|
204
|
}
|
205
|
|
206
|
protected InputStream getDefaultXsltInputStream() {
|
207
|
return null;
|
208
|
}
|
209
|
|
210
|
public List<File> getStylesheetsByLocation(File stylesheetFolder) {
|
211
|
|
212
|
File[] stylesheets = stylesheetFolder.listFiles(filter);
|
213
|
|
214
|
return Arrays.asList(stylesheets);
|
215
|
}
|
216
|
}
|