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
|
package eu.etaxonomy.cdm.io.dwca.out;
|
10
|
|
11
|
import java.io.ByteArrayOutputStream;
|
12
|
import java.io.File;
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.io.FileOutputStream;
|
15
|
import java.io.IOException;
|
16
|
import java.io.OutputStream;
|
17
|
import java.io.PrintWriter;
|
18
|
import java.io.UnsupportedEncodingException;
|
19
|
|
20
|
import javax.xml.stream.XMLOutputFactory;
|
21
|
import javax.xml.stream.XMLStreamException;
|
22
|
import javax.xml.stream.XMLStreamWriter;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
|
26
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
27
|
import eu.etaxonomy.cdm.io.common.ICdmExport;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
29
|
|
30
|
/**
|
31
|
* @author a.mueller
|
32
|
* @date 18.04.2011
|
33
|
*
|
34
|
*/
|
35
|
public abstract class DwcaExportBase
|
36
|
extends CdmExportBase<DwcaTaxExportConfigurator, DwcaTaxExportState, IExportTransformer, File>
|
37
|
implements ICdmExport<DwcaTaxExportConfigurator, DwcaTaxExportState>{
|
38
|
|
39
|
private static final long serialVersionUID = -3214410418410044139L;
|
40
|
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = Logger.getLogger(DwcaExportBase.class);
|
43
|
|
44
|
protected static final boolean IS_CORE = true;
|
45
|
|
46
|
protected DwcaTaxExportFile file;
|
47
|
|
48
|
|
49
|
/**
|
50
|
* @param config
|
51
|
* @return
|
52
|
* @throws IOException
|
53
|
* @throws FileNotFoundException
|
54
|
*/
|
55
|
protected FileOutputStream createFileOutputStream(DwcaTaxExportConfigurator config, String thisFileName) throws IOException, FileNotFoundException {
|
56
|
String filePath = config.getDestinationNameString();
|
57
|
String fileName = filePath + File.separatorChar + thisFileName;
|
58
|
File f = new File(fileName);
|
59
|
if (!f.exists()){
|
60
|
f.createNewFile();
|
61
|
}
|
62
|
FileOutputStream fos = new FileOutputStream(f);
|
63
|
return fos;
|
64
|
}
|
65
|
|
66
|
|
67
|
/**
|
68
|
* @param state
|
69
|
* @param table
|
70
|
* @return
|
71
|
* @throws IOException
|
72
|
* @throws FileNotFoundException
|
73
|
* @throws XMLStreamException
|
74
|
*/
|
75
|
protected XMLStreamWriter createXmlStreamWriter(DwcaTaxExportState state, DwcaTaxExportFile table)
|
76
|
throws IOException, FileNotFoundException, XMLStreamException {
|
77
|
|
78
|
XMLOutputFactory factory = XMLOutputFactory.newInstance();
|
79
|
OutputStream os;
|
80
|
boolean useZip = state.isZip();
|
81
|
if (useZip){
|
82
|
os = state.getZipStream(table.getTableName());
|
83
|
}else if(state.getConfig().getDestination() != null){
|
84
|
os = createFileOutputStream(state.getConfig(), table.getTableName());
|
85
|
}else{
|
86
|
os = new ByteArrayOutputStream();
|
87
|
state.getProcessor().put(table, (ByteArrayOutputStream)os);
|
88
|
}
|
89
|
XMLStreamWriter writer = factory.createXMLStreamWriter(os);
|
90
|
return writer;
|
91
|
}
|
92
|
|
93
|
|
94
|
/**
|
95
|
* @param state
|
96
|
* @param file
|
97
|
* @return
|
98
|
* @throws IOException
|
99
|
* @throws FileNotFoundException
|
100
|
* @throws UnsupportedEncodingException
|
101
|
*/
|
102
|
protected PrintWriter createPrintWriter(DwcaTaxExportState state, DwcaTaxExportFile file)
|
103
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
104
|
|
105
|
PrintWriter writer = state.getWriter(file);
|
106
|
if (writer == null){
|
107
|
boolean useZip = state.isZip();
|
108
|
OutputStream os;
|
109
|
if (useZip){
|
110
|
os = state.getZipStream(file.getTableName());
|
111
|
}else if(state.getConfig().getDestination() != null){
|
112
|
os = createFileOutputStream(state.getConfig(), file.getTableName());
|
113
|
}else{
|
114
|
os = new ByteArrayOutputStream();
|
115
|
state.getProcessor().put(file, (ByteArrayOutputStream)os);
|
116
|
}
|
117
|
writer = new PrintWriter(os);
|
118
|
|
119
|
state.putWriter(file, writer);
|
120
|
}
|
121
|
return writer;
|
122
|
}
|
123
|
|
124
|
|
125
|
/**
|
126
|
* flushes the writer for the according file if exists.
|
127
|
*/
|
128
|
protected void flushWriter(DwcaTaxExportState state, DwcaTaxExportFile file) {
|
129
|
PrintWriter writer = state.getWriter(file);
|
130
|
if (writer != null){
|
131
|
writer.flush();
|
132
|
}
|
133
|
}
|
134
|
|
135
|
|
136
|
/**
|
137
|
* Closes the writer
|
138
|
* @param file
|
139
|
* @param state
|
140
|
*/
|
141
|
protected void closeWriter(DwcaTaxExportState state) {
|
142
|
PrintWriter writer = state.getWriter(file);
|
143
|
if (writer != null && state.isZip() == false){
|
144
|
writer.close();
|
145
|
}
|
146
|
}
|
147
|
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Closes the writer.
|
152
|
* Note: XMLStreamWriter does not close the underlying stream.
|
153
|
* @param writer
|
154
|
* @param state
|
155
|
*/
|
156
|
protected void closeWriter(XMLStreamWriter writer, DwcaTaxExportState state) {
|
157
|
if (writer != null && state.isZip() == false){
|
158
|
try {
|
159
|
writer.close();
|
160
|
} catch (XMLStreamException e) {
|
161
|
throw new RuntimeException(e);
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
}
|