Project

General

Profile

Download (6.31 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.util.List;
15

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

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

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

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

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

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

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

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

    
48
	@Override
49
	protected void doInvoke(DwcaTaxExportState state){
50
		DwcaTaxExportConfigurator config = state.getConfig();
51

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

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

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

    
63
			List<DwcaMetaDataRecord> metaRecords = state.getMetaRecords();
64

    
65
			// create header
66
			writer.writeStartDocument();
67
			writer.setDefaultNamespace(rootNamespace);
68

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

    
73
				writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
74
				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");
75

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

    
103
		return;
104
	}
105

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

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

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

    
133
		writer.writeEndElement();
134
	}
135

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

    
146
	}
147

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

    
155

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

    
163
	}
164

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

    
188

    
189

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

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

    
202

    
203
}
(12-12/33)