3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.print
.out
;
14 import java
.io
.FileInputStream
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.FilenameFilter
;
17 import java
.io
.IOException
;
18 import java
.io
.InputStream
;
20 import java
.text
.SimpleDateFormat
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.Calendar
;
24 import java
.util
.List
;
26 import javax
.imageio
.stream
.FileImageInputStream
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.jdom
.Document
;
31 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
32 import eu
.etaxonomy
.cdm
.common
.IProgressMonitor
;
35 * This abstract implementation of the {@link IPublishOutputModule} contains convenience methods for file
36 * path generation. Clients should consider extending this class.
39 * @created Apr 20, 2010
42 public abstract class AbstractPublishOutputModule
implements IPublishOutputModule
{
43 private static final Logger logger
= Logger
44 .getLogger(AbstractPublishOutputModule
.class);
47 * The date format used by {@link #generateFilenameWithDate(String, String)}
49 public static final String DATE_FORMAT_NOW
= "yyyyMMdd-HHmm";
51 private FilenameFilter filter
= new FilenameFilter() {
54 public boolean accept(File dir
, String fileName
) {
55 return fileName
.substring(fileName
.length() - 3).equals("xsl");
62 * Generates a string containing the current date followed by the given name.
64 * @param name a string.
65 * @return a string containing the current date followed by the given name.
67 protected String
generateFilenameWithDate(String name
){
68 StringBuffer buffer
= new StringBuffer();
70 Calendar cal
= Calendar
.getInstance();
71 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
72 buffer
.append(sdf
.format(cal
.getTime()));
76 buffer
.append(name
);//"xml_export"
77 buffer
.append("." + getOutputFileSuffix());
79 return buffer
.toString();
83 * Generates the complete path to the output file based on the given output folder.
85 * @param outputFolder the folder to store the output file in.
86 * @return a string containing the full path to the output file.
88 public String
getFilePath(File outputFolder
) {
89 return outputFolder
.getPath() + File
.separator
+ generateFilenameWithDate("output");
93 * @see eu.etaxonomy.printpublisher.out.IPublishOutputModule#output(org.jdom.Document, java.io.File)
95 public void output(Document document
, File exportFolder
, IProgressMonitor progressMonitor
) {
96 if(progressMonitor
== null){
97 throw new IllegalArgumentException("ProgressMonitor may not be null");
100 String message
= "Running output module: " + this.getClass().getSimpleName();
101 logger
.trace(message
);
102 progressMonitor
.subTask(message
);
107 public List
<File
> getStylesheets() throws IOException
{
108 List
<File
> stylesheets
= new ArrayList
<File
>();
110 for(File directory
: getStylesheetLocations()){
111 if(directory
.exists() && directory
.isDirectory()){
112 stylesheets
.addAll(getStylesheetsByLocation(directory
));
114 logger
.info(String
.format("Tried to read styleshets from '%s', but it does not exist or is not a directory", directory
));
122 private List
<File
> getStylesheetLocations() throws IOException
{
123 List
<File
> locationList
= new ArrayList
<File
>();
125 String l
= File
.separator
;
127 URL shippedStylesheetsResource
= AbstractPublishOutputModule
.class.getResource("/stylesheets/pdf/");
128 File shippedStylesheetsDir
= new File(shippedStylesheetsResource
.getFile());
129 locationList
.add(shippedStylesheetsDir
);
131 // TODO this should be configured in a central place, see #2387
132 String cdmlibHomeDir
= CdmUtils
.getHomeDir() + l
+ ".cdmLibrary";
134 File userdir
= new File(cdmlibHomeDir
+ l
+ "stylesheets" + l
+ getOutputFileSuffix());
135 locationList
.add(userdir
);
141 public File
getXslt() {
146 public void setXslt(File xslt
){
150 public InputStream
getXsltInputStream(){
151 if(getXslt() == null){
152 return getDefaultXsltInputStream();
156 return new FileInputStream(getXslt());
157 } catch (FileNotFoundException e
) {
163 protected InputStream
getDefaultXsltInputStream() {
167 public List
<File
> getStylesheetsByLocation(File stylesheetFolder
){
169 File
[] stylesheets
= stylesheetFolder
.listFiles(filter
);
171 return Arrays
.asList(stylesheets
);