Project

General

Profile

Download (5.45 KB) Statistics
| Branch: | Tag: | Revision:
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.monitor.IProgressMonitor;
29
import eu.etaxonomy.cdm.config.ConfigFileUtil;
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
	@Override
134
    public void output(Document document, File exportFolder,
135
			IProgressMonitor progressMonitor) {
136
		if (progressMonitor == null) {
137
			throw new IllegalArgumentException(
138
					"ProgressMonitor may not be null");
139
		}
140
		inputDocument=document;
141
		String message = "Running output module: "
142
				+ this.getClass().getSimpleName();
143
		logger.trace(message);
144
		progressMonitor.subTask(message);
145
	}
146

    
147
	@Override
148
	public List<File> getStylesheets() throws IOException {
149
		List<File> stylesheets = new ArrayList<File>();
150

    
151
		for (File directory : getStylesheetLocations()) {
152
			if (directory.exists() && directory.isDirectory()) {
153
				stylesheets.addAll(getStylesheetsByLocation(directory));
154
			} else {
155
				logger.info(String
156
						.format("Tried to read styleshets from '%s', but it does not exist or is not a directory",
157
								directory));
158
			}
159
		}
160

    
161
		return stylesheets;
162

    
163
	}
164

    
165
	private List<File> getStylesheetLocations() throws IOException {
166
		List<File> locationList = new ArrayList<File>();
167

    
168
		String l = File.separator;
169

    
170
		URL shippedStylesheetsResource = PublishOutputModuleBase.class
171
				.getResource("/stylesheets/pdf/");
172
		File shippedStylesheetsDir = new File(
173
				shippedStylesheetsResource.getFile());
174
		locationList.add(shippedStylesheetsDir);
175

    
176
		// TODO this should be configured in a central place, see #2387
177
		File userdir = new File(ConfigFileUtil.perUserCdmFolder() + l + "stylesheets"
178
				+ l + getOutputFileSuffix());
179
		locationList.add(userdir);
180

    
181
		return locationList;
182
	}
183

    
184
	@Override
185
	public File getXslt() {
186
		return xslt;
187
	}
188

    
189
	@Override
190
	public void setXslt(File xslt) {
191
		this.xslt = xslt;
192
	}
193

    
194
	public InputStream getXsltInputStream() {
195
		if (getXslt() == null) {
196
			return getDefaultXsltInputStream();
197
		}
198

    
199
		try {
200
			return new FileInputStream(getXslt());
201
		} catch (FileNotFoundException e) {
202
			logger.error(e);
203
		}
204
		return null;
205
	}
206

    
207
	protected InputStream getDefaultXsltInputStream() {
208
		return null;
209
	}
210

    
211
	public List<File> getStylesheetsByLocation(File stylesheetFolder) {
212

    
213
		File[] stylesheets = stylesheetFolder.listFiles(filter);
214

    
215
		return Arrays.asList(stylesheets);
216
	}
217
}
(2-2/2)