2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.dwca
.out
;
11 import java
.io
.FileNotFoundException
;
12 import java
.io
.IOException
;
13 import java
.util
.List
;
15 import javax
.xml
.stream
.XMLStreamException
;
16 import javax
.xml
.stream
.XMLStreamWriter
;
18 import org
.apache
.commons
.lang
.StringUtils
;
19 import org
.apache
.logging
.log4j
.LogManager
;
20 import org
.apache
.logging
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.common
.URI
;
24 import eu
.etaxonomy
.cdm
.io
.dwca
.out
.DwcaMetaDataRecord
.FieldEntry
;
31 public class DwcaMetaDataExport
extends DwcaExportBase
{
33 private static final long serialVersionUID
= -4033439569151252697L;
35 private static final Logger logger
= LogManager
.getLogger();
37 protected static final String fileName
= "meta.xml";
42 public DwcaMetaDataExport() {
44 this.ioName
= this.getClass().getSimpleName();
48 protected void doInvoke(DwcaTaxExportState state
){
49 DwcaTaxExportConfigurator config
= state
.getConfig();
51 DwcaMetaDataRecord metaDataRecord
= new DwcaMetaDataRecord(! IS_CORE
, fileName
, null);
52 metaDataRecord
.setMetaData(true);
53 state
.addMetaRecord(metaDataRecord
);
55 XMLStreamWriter writer
= null;
57 writer
= createXmlStreamWriter(state
, DwcaTaxExportFile
.METADATA
);
59 String rootNamespace
= "http://rs.tdwg.org/dwc/text/";
60 String rootName
= "archive";
62 List
<DwcaMetaDataRecord
> metaRecords
= state
.getMetaRecords();
65 writer
.writeStartDocument();
66 writer
.setDefaultNamespace(rootNamespace
);
68 // create root element
69 writer
.writeStartElement(rootName
);
70 writer
.writeNamespace(null, rootNamespace
);
72 writer
.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
73 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 for (DwcaMetaDataRecord metaRecord
: metaRecords
){
76 if (! metaRecord
.isMetaData()){
77 writeMetaDataRecord(writer
, config
, metaRecord
);
80 writer
.writeEndElement();
81 writer
.writeEndDocument();
84 } catch (FileNotFoundException e
) {
85 String message
= "Metadata file could not be found";
86 state
.getResult().addException(e
, message
);
87 } catch (XMLStreamException e
) {
88 if (e
.getNestedException() != null){
89 String message
= "Nested XMLStreamException while handling metadata";
90 state
.getResult().addException((Exception
)e
.getNestedException(), message
);
92 String message
= "XMLStreamException while handling metadata";
93 state
.getResult().addException(e
, message
);
95 } catch (IOException e
) {
96 String message
= "IOException while handling metadata";
97 state
.getResult().addException(e
, message
);
99 closeWriter(writer
, state
);
105 private void writeMetaDataRecord(XMLStreamWriter writer
,
106 DwcaTaxExportConfigurator config
, DwcaMetaDataRecord metaRecord
) throws XMLStreamException
{
107 if (! metaRecord
.hasEntries()){
110 String encoding
= config
.getEncoding();
111 String linesTerminatedBy
= config
.getLinesTerminatedBy();
112 String fieldsEnclosedBy
= config
.getFieldsEnclosedBy();
113 String ignoreHeaderLines
= config
.isHasHeaderLines()?
"1":"0";
114 String fieldsTerminatedBy
= config
.getFieldsTerminatedBy();
116 // create core element
117 String elementName
= metaRecord
.isCore()?
"core": "extension";
118 String rowType
= metaRecord
.getRowType();
119 writeElementStart(writer
, elementName
, encoding
, linesTerminatedBy
, fieldsEnclosedBy
,
120 fieldsTerminatedBy
, ignoreHeaderLines
, rowType
);
121 String filename
= metaRecord
.getFileLocation();
122 writeFiles(writer
, filename
);
123 writeId(writer
, metaRecord
.isCore());
125 List
<FieldEntry
> entryList
= metaRecord
.getEntries();
126 for (FieldEntry fieldEntry
: entryList
){
127 if (fieldEntry
.index
!= 0){
128 writeFieldLine(writer
, fieldEntry
.index
, fieldEntry
.term
, fieldEntry
.defaultValue
);
131 writer
.writeEndElement();
134 private void writeFieldLine(XMLStreamWriter writer
, int index
, URI term
, String defaultValue
) throws XMLStreamException
{
135 writer
.writeStartElement("field");
136 if (StringUtils
.isNotBlank(defaultValue
)){
137 writer
.writeAttribute("default", defaultValue
);
139 writer
.writeAttribute("index", String
.valueOf(index
));
141 writer
.writeAttribute("term", term
.toString());
142 writer
.writeEndElement();
145 private void writeId(XMLStreamWriter writer
, boolean isCore
) throws XMLStreamException
{
146 String strId
= isCore?
"id" : "coreid";
147 writer
.writeStartElement(strId
);
148 writer
.writeAttribute("index", "0");
149 writer
.writeEndElement();
152 private void writeFiles(XMLStreamWriter writer
, String filename
) throws XMLStreamException
{
153 writer
.writeStartElement("files");
154 writer
.writeStartElement("location");
155 writer
.writeCharacters(filename
);
156 writer
.writeEndElement();
157 writer
.writeEndElement();
160 private void writeElementStart(XMLStreamWriter writer
, String elementName
, String encoding
,
161 String linesTerminatedBy
, String fieldsEnclosedBy
, String fieldsTerminatedBy
,
162 String ignoreHeaderLines
, String rowType
)
163 throws XMLStreamException
{
164 writer
.writeStartElement(elementName
);
165 writer
.writeAttribute( "encoding", encoding
);
166 writer
.writeAttribute( "linesTerminatedBy", linesTerminatedBy
);
167 writer
.writeAttribute( "fieldsEnclosedBy", fieldsEnclosedBy
);
168 writer
.writeAttribute("fieldsTerminatedBy", fieldsTerminatedBy
);
169 writer
.writeAttribute("ignoreHeaderLines", ignoreHeaderLines
);
170 writer
.writeAttribute("rowType", rowType
);
174 protected boolean doCheck(DwcaTaxExportState state
) {
175 boolean result
= true;
176 logger
.warn("No check implemented for " + this.ioName
);
181 protected boolean isIgnore(DwcaTaxExportState state
) {
182 return ! state
.getConfig().isDoMetaData();