Project

General

Profile

Download (6.47 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.io.dwca.out;
11

    
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
14
import java.net.URI;
15
import java.util.List;
16

    
17
import javax.xml.stream.XMLStreamException;
18
import javax.xml.stream.XMLStreamWriter;
19

    
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.io.dwca.out.DwcaMetaDataRecord.FieldEntry;
25

    
26
/**
27
 * @author a.mueller
28
 * @since 20.04.2011
29
 */
30
@Component
31
public class DwcaMetaDataExport extends DwcaExportBase {
32

    
33
    private static final long serialVersionUID = -4033439569151252697L;
34

    
35
    private static final Logger logger = Logger.getLogger(DwcaMetaDataExport.class);
36

    
37
	protected static final String fileName = "meta.xml";
38

    
39
	/**
40
	 * Constructor
41
	 */
42
	public DwcaMetaDataExport() {
43
		super();
44
		this.ioName = this.getClass().getSimpleName();
45
	}
46

    
47
	/**
48
	 * {@inheritDoc}
49
	 */
50
	@Override
51
	protected void doInvoke(DwcaTaxExportState state){
52
		DwcaTaxExportConfigurator config = state.getConfig();
53

    
54
		DwcaMetaDataRecord metaDataRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, null);
55
		metaDataRecord.setMetaData(true);
56
		state.addMetaRecord(metaDataRecord);
57

    
58
		XMLStreamWriter writer = null;
59
		try {
60
			writer = createXmlStreamWriter(state, DwcaTaxExportFile.METADATA);
61

    
62
			String rootNamespace = "http://rs.tdwg.org/dwc/text/";
63
			String rootName = "archive";
64

    
65
			List<DwcaMetaDataRecord> metaRecords = state.getMetaRecords();
66

    
67
			// create header
68
			writer.writeStartDocument();
69
			writer.setDefaultNamespace(rootNamespace);
70

    
71
				// create root element
72
				writer.writeStartElement(rootName);
73
				writer.writeNamespace(null, rootNamespace);
74

    
75
				writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
76
				writer.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd");
77

    
78
				for (DwcaMetaDataRecord metaRecord : metaRecords){
79
					if (! metaRecord.isMetaData()){
80
						writeMetaDataRecord(writer, config, metaRecord);
81
					}
82
				}
83
				writer.writeEndElement();
84
			writer.writeEndDocument();
85
			writer.flush();
86
			writer.close();
87
		} catch (FileNotFoundException e) {
88
		    String message = "Metadata file could not be found";
89
			state.getResult().addException(e, message);
90
		} catch (XMLStreamException e) {
91
			if (e.getNestedException() != null){
92
			    String message = "Nested XMLStreamException while handling metadata";
93
			    state.getResult().addException((Exception)e.getNestedException(), message);
94
			}else{
95
			    String message = "XMLStreamException while handling metadata";
96
                state.getResult().addException(e, message);
97
			}
98
		} catch (IOException e) {
99
		    String message = "IOException while handling metadata";
100
            state.getResult().addException(e, message);
101
		} finally{
102
			closeWriter(writer, state);
103
		}
104

    
105
		return;
106
	}
107

    
108
	private void writeMetaDataRecord(XMLStreamWriter writer,
109
			DwcaTaxExportConfigurator config, DwcaMetaDataRecord metaRecord) throws XMLStreamException {
110
		if (! metaRecord.hasEntries()){
111
			return;
112
		}
113
		String encoding = config.getEncoding();
114
		String linesTerminatedBy = config.getLinesTerminatedBy();
115
		String fieldsEnclosedBy = config.getFieldsEnclosedBy();
116
		String ignoreHeaderLines = config.isHasHeaderLines()? "1":"0";
117
		String fieldsTerminatedBy= config.getFieldsTerminatedBy();
118

    
119
		// create core element
120
		String elementName = metaRecord.isCore()? "core": "extension";
121
		String rowType = metaRecord.getRowType();
122
		writeElementStart(writer, elementName, encoding, linesTerminatedBy,	fieldsEnclosedBy,
123
		        fieldsTerminatedBy, ignoreHeaderLines, rowType);
124
			String filename = metaRecord.getFileLocation();
125
			writeFiles(writer, filename );
126
			writeId(writer, metaRecord.isCore());
127

    
128
			List<FieldEntry> entryList = metaRecord.getEntries();
129
			for (FieldEntry fieldEntry : entryList){
130
				if (fieldEntry.index != 0){
131
					writeFieldLine(writer, fieldEntry.index, fieldEntry.term, fieldEntry.defaultValue);
132
				}
133
			}
134

    
135
		writer.writeEndElement();
136
	}
137

    
138
	private void writeFieldLine(XMLStreamWriter writer, int index, URI term, String defaultValue) throws XMLStreamException {
139
		writer.writeStartElement("field");
140
		if (StringUtils.isNotBlank(defaultValue)){
141
			writer.writeAttribute("default", defaultValue);
142
		}else{
143
			writer.writeAttribute("index", String.valueOf(index));
144
		}
145
		writer.writeAttribute("term", term.toString());
146
		writer.writeEndElement();
147

    
148
	}
149

    
150
	private void writeId(XMLStreamWriter writer, boolean isCore) throws XMLStreamException {
151
		String strId = isCore? "id" : "coreid";
152
		writer.writeStartElement(strId);
153
		writer.writeAttribute("index", "0");
154
		writer.writeEndElement();
155
	}
156

    
157

    
158
	private void writeFiles(XMLStreamWriter writer, String filename) throws XMLStreamException {
159
		writer.writeStartElement("files");
160
			writer.writeStartElement("location");
161
			writer.writeCharacters(filename);
162
			writer.writeEndElement();
163
		writer.writeEndElement();
164

    
165
	}
166

    
167
	/**
168
	 * @param writer
169
	 * @param encoding
170
	 * @param linesTerminatedBy
171
	 * @param fieldsEnclosedBy
172
	 * @param ignoreHeaderLines
173
	 * @param rowType
174
	 * @param elementName
175
	 * @throws XMLStreamException
176
	 */
177
	private void writeElementStart(XMLStreamWriter writer, String elementName, String encoding,
178
			String linesTerminatedBy, String fieldsEnclosedBy, String fieldsTerminatedBy,
179
			String ignoreHeaderLines, String rowType)
180
			throws XMLStreamException {
181
		writer.writeStartElement(elementName);
182
		writer.writeAttribute( "encoding", encoding );
183
		writer.writeAttribute( "linesTerminatedBy", linesTerminatedBy );
184
		writer.writeAttribute( "fieldsEnclosedBy", fieldsEnclosedBy );
185
		writer.writeAttribute("fieldsTerminatedBy", fieldsTerminatedBy);
186
		writer.writeAttribute("ignoreHeaderLines", ignoreHeaderLines);
187
		writer.writeAttribute("rowType", rowType);
188
	}
189

    
190

    
191

    
192
	@Override
193
	protected boolean doCheck(DwcaTaxExportState state) {
194
		boolean result = true;
195
		logger.warn("No check implemented for " + this.ioName);
196
		return result;
197
	}
198

    
199
	@Override
200
	protected boolean isIgnore(DwcaTaxExportState state) {
201
		return ! state.getConfig().isDoMetaData();
202
	}
203

    
204

    
205
}
(12-12/33)