Minor improvements; Changed location of xsl files
[cdmlib.git] / cdmlib-print / src / main / java / eu / etaxonomy / cdm / print / out / AbstractPublishOutputModule.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.print.out;
12
13 import java.io.File;
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;
19 import java.net.URL;
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;
25
26 import javax.imageio.stream.FileImageInputStream;
27
28 import org.apache.log4j.Logger;
29 import org.jdom.Document;
30
31 import eu.etaxonomy.cdm.common.CdmUtils;
32 import eu.etaxonomy.cdm.common.IProgressMonitor;
33
34 /**
35 * This abstract implementation of the {@link IPublishOutputModule} contains convenience methods for file
36 * path generation. Clients should consider extending this class.
37 *
38 * @author n.hoffmann
39 * @created Apr 20, 2010
40 * @version 1.0
41 */
42 public abstract class AbstractPublishOutputModule implements IPublishOutputModule {
43 private static final Logger logger = Logger
44 .getLogger(AbstractPublishOutputModule.class);
45
46 /**
47 * The date format used by {@link #generateFilenameWithDate(String, String)}
48 */
49 public static final String DATE_FORMAT_NOW = "yyyyMMdd-HHmm";
50
51 private FilenameFilter filter = new FilenameFilter() {
52
53 @Override
54 public boolean accept(File dir, String fileName) {
55 return fileName.substring(fileName.length() - 3).equals("xsl");
56 }
57 };
58
59 private File xslt;
60
61 /**
62 * Generates a string containing the current date followed by the given name.
63 *
64 * @param name a string.
65 * @return a string containing the current date followed by the given name.
66 */
67 protected String generateFilenameWithDate(String name){
68 StringBuffer buffer = new StringBuffer();
69
70 Calendar cal = Calendar.getInstance();
71 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
72 buffer.append(sdf.format(cal.getTime()));
73
74 buffer.append("-");
75
76 buffer.append(name);//"xml_export"
77 buffer.append("." + getOutputFileSuffix());
78
79 return buffer.toString();
80 }
81
82 /**
83 * Generates the complete path to the output file based on the given output folder.
84 *
85 * @param outputFolder the folder to store the output file in.
86 * @return a string containing the full path to the output file.
87 */
88 public String getFilePath(File outputFolder) {
89 return outputFolder.getPath() + File.separator + generateFilenameWithDate("output");
90 }
91
92 /* (non-Javadoc)
93 * @see eu.etaxonomy.printpublisher.out.IPublishOutputModule#output(org.jdom.Document, java.io.File)
94 */
95 public void output(Document document, File exportFolder, IProgressMonitor progressMonitor) {
96 if(progressMonitor == null){
97 throw new IllegalArgumentException("ProgressMonitor may not be null");
98 }
99
100 String message = "Running output module: " + this.getClass().getSimpleName();
101 logger.trace(message);
102 progressMonitor.subTask(message);
103 }
104
105
106 @Override
107 public List<File> getStylesheets() throws IOException {
108 List<File> stylesheets = new ArrayList<File>();
109
110 for(File directory : getStylesheetLocations()){
111 if(directory.exists() && directory.isDirectory()){
112 stylesheets.addAll(getStylesheetsByLocation(directory));
113 }else{
114 logger.info(String.format("Tried to read styleshets from '%s', but it does not exist or is not a directory", directory));
115 }
116 }
117
118 return stylesheets;
119
120 }
121
122 private List<File> getStylesheetLocations() throws IOException {
123 List<File> locationList = new ArrayList<File>();
124
125 String l = File.separator;
126
127 URL shippedStylesheetsResource = AbstractPublishOutputModule.class.getResource("/stylesheets/pdf/");
128 File shippedStylesheetsDir = new File(shippedStylesheetsResource.getFile());
129 locationList.add(shippedStylesheetsDir);
130
131 // TODO this should be configured in a central place, see #2387
132 String cdmlibHomeDir = CdmUtils.getHomeDir() + l + ".cdmLibrary";
133
134 File userdir = new File(cdmlibHomeDir + l + "stylesheets" + l + getOutputFileSuffix());
135 locationList.add(userdir);
136
137 return locationList;
138 }
139
140 @Override
141 public File getXslt() {
142 return xslt;
143 }
144
145 @Override
146 public void setXslt(File xslt){
147 this.xslt = xslt;
148 }
149
150 public InputStream getXsltInputStream(){
151 if(getXslt() == null){
152 return getDefaultXsltInputStream();
153 }
154
155 try {
156 return new FileInputStream(getXslt());
157 } catch (FileNotFoundException e) {
158 logger.error(e);
159 }
160 return null;
161 }
162
163 protected InputStream getDefaultXsltInputStream() {
164 return null;
165 }
166
167 public List<File> getStylesheetsByLocation(File stylesheetFolder){
168
169 File[] stylesheets = stylesheetFolder.listFiles(filter);
170
171 return Arrays.asList(stylesheets);
172 }
173 }