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.taxonx;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.jdom.Content;
|
16
|
import org.jdom.Element;
|
17
|
import org.jdom.Namespace;
|
18
|
import org.jdom.Text;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
22
|
import eu.etaxonomy.cdm.io.common.CdmIoBase;
|
23
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
24
|
import eu.etaxonomy.cdm.model.agent.Person;
|
25
|
import eu.etaxonomy.cdm.model.agent.Team;
|
26
|
import eu.etaxonomy.cdm.model.common.Marker;
|
27
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
28
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
29
|
import eu.etaxonomy.cdm.model.reference.IPublicationBase;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
32
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
33
|
|
34
|
|
35
|
/**
|
36
|
* @author a.mueller
|
37
|
* @created 29.07.2008
|
38
|
* @version 1.0
|
39
|
*/
|
40
|
@Component
|
41
|
public class TaxonXModsImport extends CdmIoBase<TaxonXImportState> implements ICdmIO<TaxonXImportState> {
|
42
|
private static final Logger logger = Logger.getLogger(TaxonXModsImport.class);
|
43
|
|
44
|
@SuppressWarnings("unused")
|
45
|
private static int modCount = 10000;
|
46
|
public TaxonXModsImport(){
|
47
|
super();
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public boolean doCheck(TaxonXImportState state){
|
52
|
boolean result = true;
|
53
|
logger.warn("Checking for TaxonXMods not yet implemented");
|
54
|
//result &= checkArticlesWithoutJournal(bmiConfig);
|
55
|
|
56
|
return result;
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
public void doInvoke(TaxonXImportState state){
|
61
|
logger.info("start make mods reference ...");
|
62
|
TaxonXImportConfigurator config = state.getConfig();
|
63
|
Element root = config.getSourceRoot();
|
64
|
Namespace nsTaxonx = root.getNamespace();
|
65
|
Namespace nsMods = Namespace.getNamespace("mods", "http://www.loc.gov/mods/v3");
|
66
|
|
67
|
state.setModsReference(null); //delete old reference
|
68
|
Element elTaxonHeader = root.getChild("taxonxHeader", nsTaxonx);
|
69
|
if (elTaxonHeader != null){
|
70
|
Element elMods = elTaxonHeader.getChild("mods", nsMods);
|
71
|
if (elMods != null){
|
72
|
Reference ref = ReferenceFactory.newGeneric();
|
73
|
//TitleInfo
|
74
|
Element elTitleInfo = elMods.getChild("titleInfo", nsMods);
|
75
|
if (elTitleInfo != null){
|
76
|
makeTitleInfo(elTitleInfo, ref);
|
77
|
}else{
|
78
|
logger.warn("TitleInfo element is missing in " + state.getConfig().getSource());
|
79
|
}
|
80
|
//mods name
|
81
|
Element elModsName = elMods.getChild("name", nsMods);
|
82
|
makeModsName(elModsName, ref);
|
83
|
//origin info
|
84
|
Element elOriginInfo = elMods.getChild("originInfo", nsMods);
|
85
|
makeOriginInfo(elOriginInfo, ref);
|
86
|
|
87
|
//publish
|
88
|
if (state.getConfig().isPublishReferences()){
|
89
|
boolean publish = false;
|
90
|
ref.addMarker(Marker.NewInstance(MarkerType.IN_BIBLIOGRAPHY(), publish));
|
91
|
}
|
92
|
|
93
|
//save
|
94
|
state.setModsReference(ref);
|
95
|
|
96
|
}
|
97
|
}
|
98
|
|
99
|
logger.info("end make mods reference ...");
|
100
|
return;
|
101
|
}
|
102
|
|
103
|
|
104
|
/**
|
105
|
* @param contentList
|
106
|
* @return
|
107
|
*/
|
108
|
private void removeEmptyContent(List<Content> contentList) {
|
109
|
List<Content> removeList = new ArrayList<Content>();
|
110
|
for (Content content: contentList){
|
111
|
if (content instanceof Text){
|
112
|
if ( CdmUtils.isEmpty(((Text) content).getTextNormalize())){
|
113
|
removeList.add(content);
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
contentList.removeAll(removeList);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* @param elModsName
|
122
|
* @param ref
|
123
|
*/
|
124
|
private void makeOriginInfo(Element elOriginInfo, Reference ref) {
|
125
|
Namespace nsMods = elOriginInfo.getNamespace();
|
126
|
List<Content> contentList = elOriginInfo.getContent();
|
127
|
|
128
|
//dateIssued
|
129
|
Element elDateIssued = elOriginInfo.getChild("dateIssued", nsMods);
|
130
|
if (elDateIssued != null){
|
131
|
String dateIssued = elDateIssued.getTextNormalize();
|
132
|
contentList.remove(elDateIssued);
|
133
|
|
134
|
TimePeriod datePublished = TimePeriodParser.parseString(dateIssued);
|
135
|
if (ref.getType().isPublication()){
|
136
|
((IPublicationBase)ref).setDatePublished(datePublished );
|
137
|
}else{
|
138
|
logger.warn("Reference has issue date but is not of type publication base. Date was not set");
|
139
|
}
|
140
|
}
|
141
|
|
142
|
//dateIssued
|
143
|
Element elPublisher = elOriginInfo.getChild("publisher", nsMods);
|
144
|
if (elPublisher != null){
|
145
|
String publisher = elPublisher.getTextNormalize();
|
146
|
contentList.remove(elPublisher);
|
147
|
|
148
|
if (ref.getType().isPublication()){
|
149
|
((IPublicationBase)ref).setPublisher(publisher);
|
150
|
}else{
|
151
|
logger.warn("Reference has publisher but is not of type publication base. Publisher was not set");
|
152
|
}
|
153
|
}
|
154
|
|
155
|
removeEmptyContent(contentList);
|
156
|
for (Content o: contentList){
|
157
|
logger.warn(o + " (in mods:originInfo) not yet implemented for mods import");
|
158
|
}
|
159
|
return;
|
160
|
}
|
161
|
|
162
|
|
163
|
/**
|
164
|
* @param elModsName
|
165
|
* @param ref
|
166
|
*/
|
167
|
//TODO
|
168
|
//THIS implementation is against the mods semantics but supports the current
|
169
|
//format for palmae taxonX files
|
170
|
//The later has to be changed and this part has to be adapted
|
171
|
private void makeModsName(Element elModsName, Reference ref) {
|
172
|
int UNPARSED = 0;
|
173
|
int PARSED = 1;
|
174
|
Namespace nsMods = elModsName.getNamespace();
|
175
|
List<Content> contentList = elModsName.getContent();
|
176
|
Team authorship = Team.NewInstance();
|
177
|
|
178
|
//name
|
179
|
List<Element> elNameParts = elModsName.getChildren("namePart", nsMods);
|
180
|
int mode = UNPARSED;
|
181
|
if (elNameParts.size() > 0){
|
182
|
if (elNameParts.get(0).getAttributes().size() > 0){
|
183
|
mode = PARSED;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
if (mode == 0){
|
188
|
Element elNamePart = elNameParts.get(0);
|
189
|
if (elNamePart != null){
|
190
|
String namePart = elNamePart.getTextNormalize();
|
191
|
contentList.remove(elNamePart);
|
192
|
authorship.setTitleCache(namePart, true);
|
193
|
}
|
194
|
if (elNameParts.size()> 1){
|
195
|
logger.warn("Multiple nameparts of unexpected type");
|
196
|
}
|
197
|
}else{
|
198
|
|
199
|
Person lastTeamMember = Person.NewInstance();
|
200
|
List<Element> tmpNamePartList = new ArrayList<Element>();
|
201
|
tmpNamePartList.addAll(elNameParts);
|
202
|
for (Element elNamePart: tmpNamePartList){
|
203
|
if (elNamePart.getAttributeValue("type").equals("family")){
|
204
|
lastTeamMember = Person.NewInstance();
|
205
|
authorship.addTeamMember(lastTeamMember);
|
206
|
lastTeamMember.setLastname(elNamePart.getTextNormalize());
|
207
|
}else if (elNamePart.getAttributeValue("type").equals("given")){
|
208
|
lastTeamMember.setFirstname(elNamePart.getTextNormalize());
|
209
|
}else{
|
210
|
logger.warn("Unsupport name part type");
|
211
|
}
|
212
|
contentList.remove(elNamePart);
|
213
|
}
|
214
|
}
|
215
|
ref.setAuthorship(authorship);
|
216
|
|
217
|
removeEmptyContent(contentList);
|
218
|
for (Content o: contentList){
|
219
|
logger.warn(o + " (in mods:name) not yet implemented for mods import");
|
220
|
}
|
221
|
return;
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* @param elTitleInfo
|
226
|
* @param ref
|
227
|
*/
|
228
|
private void makeTitleInfo(Element elTitleInfo, Reference ref) {
|
229
|
Namespace nsMods = elTitleInfo.getNamespace();
|
230
|
List<Content> contentList = elTitleInfo.getContent();
|
231
|
|
232
|
//title
|
233
|
Element elTitle = elTitleInfo.getChild("title", nsMods);
|
234
|
if (elTitle != null){
|
235
|
String title = elTitle.getTextNormalize();
|
236
|
contentList.remove(elTitle);
|
237
|
ref.setTitle(title);
|
238
|
}
|
239
|
removeEmptyContent(contentList);
|
240
|
for (Content o: contentList){
|
241
|
logger.warn(o + " (in titleInfo) not yet implemented for mods import");
|
242
|
}
|
243
|
return;
|
244
|
}
|
245
|
|
246
|
/* (non-Javadoc)
|
247
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
248
|
*/
|
249
|
@Override
|
250
|
protected boolean isIgnore(TaxonXImportState state){
|
251
|
return ! state.getConfig().isDoMods();
|
252
|
}
|
253
|
|
254
|
}
|