1
|
/**
|
2
|
* Copyright (C) 2009 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.markup;
|
11
|
|
12
|
import java.net.MalformedURLException;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.net.URL;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.Arrays;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.LinkedList;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Queue;
|
23
|
import java.util.Set;
|
24
|
import java.util.UUID;
|
25
|
|
26
|
import javax.xml.stream.FactoryConfigurationError;
|
27
|
import javax.xml.stream.Location;
|
28
|
import javax.xml.stream.XMLEventReader;
|
29
|
import javax.xml.stream.XMLStreamException;
|
30
|
import javax.xml.stream.events.Attribute;
|
31
|
import javax.xml.stream.events.StartElement;
|
32
|
import javax.xml.stream.events.XMLEvent;
|
33
|
|
34
|
import org.apache.commons.lang.StringUtils;
|
35
|
import org.apache.log4j.Logger;
|
36
|
import org.springframework.beans.factory.annotation.Autowired;
|
37
|
import org.springframework.security.access.PermissionEvaluator;
|
38
|
import org.springframework.security.authentication.AuthenticationManager;
|
39
|
import org.springframework.security.core.Authentication;
|
40
|
import org.springframework.stereotype.Component;
|
41
|
|
42
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
43
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
|
44
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
|
45
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
46
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
47
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
48
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
49
|
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
|
50
|
import eu.etaxonomy.cdm.model.agent.Team;
|
51
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
52
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
53
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
54
|
import eu.etaxonomy.cdm.model.common.AnnotationType;
|
55
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
56
|
import eu.etaxonomy.cdm.model.common.Extension;
|
57
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
58
|
import eu.etaxonomy.cdm.model.common.Figure;
|
59
|
import eu.etaxonomy.cdm.model.common.Language;
|
60
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
61
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
62
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
63
|
import eu.etaxonomy.cdm.model.description.Feature;
|
64
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
65
|
import eu.etaxonomy.cdm.model.description.TextData;
|
66
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
67
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
68
|
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
|
69
|
import eu.etaxonomy.cdm.model.media.Media;
|
70
|
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
|
71
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
72
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
|
73
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
74
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
75
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
76
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
77
|
import eu.etaxonomy.cdm.model.name.Rank;
|
78
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
79
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
80
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
81
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
|
82
|
import eu.etaxonomy.cdm.model.occurrence.Specimen;
|
83
|
import eu.etaxonomy.cdm.model.reference.IArticle;
|
84
|
import eu.etaxonomy.cdm.model.reference.IJournal;
|
85
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
86
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
87
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
88
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
89
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
90
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
91
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
92
|
import eu.etaxonomy.cdm.strategy.parser.NameTypeParser;
|
93
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
94
|
import eu.etaxonomy.cdm.strategy.parser.SpecimenTypeParser;
|
95
|
import eu.etaxonomy.cdm.strategy.parser.SpecimenTypeParser.TypeInfo;
|
96
|
|
97
|
|
98
|
/**
|
99
|
* @author a.mueller
|
100
|
*
|
101
|
*/
|
102
|
@Component
|
103
|
public class MarkupDocumentImport extends MarkupImportBase implements ICdmIO<MarkupImportState> {
|
104
|
private static final Logger logger = Logger.getLogger(MarkupDocumentImport.class);
|
105
|
|
106
|
|
107
|
|
108
|
private static final boolean CREATE_NEW = true;
|
109
|
private static final boolean IS_IMAGE_GALLERY = true;
|
110
|
private static final boolean NO_IMAGE_GALLERY = false;
|
111
|
|
112
|
private static final String ACCEPTED = "accepted";
|
113
|
private static final String ACCEPTED_NAME = "acceptedName";
|
114
|
private static final String ADDENDA = "addenda";
|
115
|
private static final String ALTERNATEPUBTITLE = "alternatepubtitle";
|
116
|
private static final String ALTERNATIVE_COLLECTION_TYPE_STATUS = "alternativeCollectionTypeStatus";
|
117
|
private static final String ALTERNATIVE_COLLECTOR = "alternativeCollector";
|
118
|
private static final String ALTERNATIVE_FIELD_NUM = "alternativeFieldNum";
|
119
|
private static final String ALTITUDE = "altitude";
|
120
|
private static final String ANNOTATION = "annotation";
|
121
|
private static final String AUTHOR = "author";
|
122
|
private static final String BIOGRAPHIES = "biographies";
|
123
|
private static final String BOLD = "bold";
|
124
|
private static final String BR = "br";
|
125
|
private static final String CHAR = "char";
|
126
|
private static final String CITATION = "citation";
|
127
|
private static final String COLLECTION = "collection";
|
128
|
private static final String COLLECTION_TYPE_STATUS = "collectionTypeStatus";
|
129
|
private static final String COLLECTOR = "collector";
|
130
|
private static final String COORDINATES = "coordinates";
|
131
|
private static final String DATES = "dates";
|
132
|
private static final String DEFAULT_MEDIA_URL = "defaultMediaUrl";
|
133
|
private static final String DESTROYED = "destroyed";
|
134
|
private static final String DETAILS = "details";
|
135
|
private static final String DISTRIBUTION_LIST = "distributionList";
|
136
|
private static final String EDITION = "edition";
|
137
|
private static final String EDITORS = "editors";
|
138
|
private static final String FEATURE = "feature";
|
139
|
private static final String FIGURE = "figure";
|
140
|
private static final String FIGURE_LEGEND = "figureLegend";
|
141
|
private static final String FIGURE_PART = "figurePart";
|
142
|
private static final String FIGURE_REF = "figureRef";
|
143
|
private static final String FIGURE_TITLE = "figureTitle";
|
144
|
private static final String FOOTNOTE = "footnote";
|
145
|
private static final String FOOTNOTE_REF = "footnoteRef";
|
146
|
private static final String FOOTNOTE_STRING = "footnoteString";
|
147
|
private static final String FIELD_NUM = "fieldNum";
|
148
|
private static final String FULL_NAME = "fullName";
|
149
|
private static final String FULL_TYPE = "fullType";
|
150
|
private static final String HEADING = "heading";
|
151
|
private static final String HABITAT_LIST = "habitatList";
|
152
|
private static final String HOMONYM = "homonym";
|
153
|
private static final String HOMOTYPES = "homotypes";
|
154
|
private static final String ID = "id";
|
155
|
private static final String INFRANK = "infrank";
|
156
|
private static final String INFRAUT = "infraut";
|
157
|
private static final String INFRPARAUT = "infrparaut";
|
158
|
private static final String ISSUE = "issue";
|
159
|
private static final String ITALICS = "italics";
|
160
|
private static final String KEY = "key";
|
161
|
private static final String LOCALITY = "locality";
|
162
|
private static final String LOST = "lost";
|
163
|
private static final String META_DATA = "metaData";
|
164
|
private static final String NAME = "name";
|
165
|
private static final String NAME_TYPE = "nameType";
|
166
|
private static final String NOM = "nom";
|
167
|
private static final String NOMENCLATURE = "nomenclature";
|
168
|
private static final String NOT_FOUND = "notFound";
|
169
|
private static final String NOT_SEEN = "notSeen";
|
170
|
private static final String NOTES = "notes";
|
171
|
private static final String NUM = "num";
|
172
|
private static final String ORIGINAL_DETERMINATION = "originalDetermination";
|
173
|
private static final String PARAUT = "paraut";
|
174
|
private static final String PUBFULLNAME = "pubfullname";
|
175
|
private static final String PUBLICATION = "publication";
|
176
|
private static final String PUBNAME = "pubname";
|
177
|
private static final String PUBTITLE = "pubtitle";
|
178
|
private static final String PUBTYPE = "pubtype";
|
179
|
private static final String RANK = "rank";
|
180
|
private static final String REF = "ref";
|
181
|
private static final String REF_PART = "refPart";
|
182
|
private static final String REFERENCES = "references";
|
183
|
private static final String TAXON = "taxon";
|
184
|
private static final String TAXONTITLE = "taxontitle";
|
185
|
private static final String TEXT_SECTION = "textSection";
|
186
|
private static final String TYPE = "type";
|
187
|
private static final String TYPE_STATUS = "typeStatus";
|
188
|
private static final String TREATMENT = "treatment";
|
189
|
private static final String SPECIMEN_TYPE = "specimenType";
|
190
|
private static final String STATUS = "status";
|
191
|
private static final String STRING = "string";
|
192
|
private static final String SUB_HEADING = "subHeading";
|
193
|
private static final String SUB_COLLECTION = "subCollection";
|
194
|
private static final String SYNONYM = "synonym";
|
195
|
private static final String UNKNOWN = "unknown";
|
196
|
private static final String URL = "url";
|
197
|
private static final String USAGE = "usage";
|
198
|
private static final String VOLUME = "volume";
|
199
|
private static final String WRITER = "writer";
|
200
|
private static final String YEAR = "year";
|
201
|
|
202
|
|
203
|
private NonViralNameParserImpl parser = new NonViralNameParserImpl();
|
204
|
|
205
|
//TODO make part of state, but state is renewed when invoking the import a second time
|
206
|
private UnmatchedLeads unmatchedLeads;
|
207
|
|
208
|
|
209
|
//TODO remove preliminary
|
210
|
@Autowired
|
211
|
private AuthenticationManager authenticationManager;
|
212
|
private Authentication authentication;
|
213
|
private PermissionEvaluator permissionEvaluator;
|
214
|
|
215
|
public MarkupDocumentImport(){
|
216
|
super();
|
217
|
System.out.println("TODO remove preliminary authentication");
|
218
|
// UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", "0000");
|
219
|
// authentication = authenticationManager.authenticate(token);
|
220
|
// SecurityContext context = SecurityContextHolder.getContext();
|
221
|
// context.setAuthentication(authentication);
|
222
|
// permissionEvaluator = new CdmPermissionEvaluator();
|
223
|
}
|
224
|
|
225
|
|
226
|
@Override
|
227
|
public boolean doCheck(MarkupImportState state){
|
228
|
state.setCheck(true);
|
229
|
doInvoke(state);
|
230
|
state.setCheck(false);
|
231
|
return state.isSuccess();
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public void doInvoke(MarkupImportState state){
|
236
|
fireProgressEvent("Start import markup document", "Before start of document");
|
237
|
fireWarningEvent("Test a warning", "At start", 17);
|
238
|
|
239
|
Queue<CdmBase> outputStream = new LinkedList<CdmBase>();
|
240
|
|
241
|
//FIXME reset state
|
242
|
doAllTheOldOtherStuff(state);
|
243
|
|
244
|
//START
|
245
|
try {
|
246
|
//StAX
|
247
|
XMLEventReader reader = getStaxReader(state);
|
248
|
state.setReader(reader);
|
249
|
//start document
|
250
|
if (! validateStartOfDocument(reader)){
|
251
|
state.setUnsuccessfull();
|
252
|
return;
|
253
|
}
|
254
|
|
255
|
//publication
|
256
|
String elName = PUBLICATION;
|
257
|
boolean hasPublication = false;
|
258
|
while (reader.hasNext()) {
|
259
|
XMLEvent nextEvent = reader.nextEvent();
|
260
|
if (isStartingElement(nextEvent, elName)){
|
261
|
handlePublication(state, reader, nextEvent, elName);
|
262
|
hasPublication = true;
|
263
|
}else if (nextEvent.isEndDocument()){
|
264
|
if (!hasPublication){
|
265
|
String message = "No publication root element found";
|
266
|
fireWarningEvent(message, nextEvent, 8);
|
267
|
}
|
268
|
//done
|
269
|
}else{
|
270
|
fireSchemaConflictEventExpectedStartTag(elName, reader);
|
271
|
}
|
272
|
}
|
273
|
|
274
|
// //SAX
|
275
|
// ImportHandlerBase handler= new PublicationHandler(this);
|
276
|
// parseSAX(state, handler);
|
277
|
|
278
|
} catch (FactoryConfigurationError e1) {
|
279
|
fireWarningEvent("Some error occurred while setting up xml factory. Data can't be imported", "Start", 16);
|
280
|
state.setUnsuccessfull();
|
281
|
} catch (XMLStreamException e1) {
|
282
|
fireWarningEvent("An XMLStreamException occurred while parsing. Data can't be imported", "Start", 16);
|
283
|
state.setUnsuccessfull();
|
284
|
// } catch (ParserConfigurationException e) {
|
285
|
// fireWarningEvent("A ParserConfigurationException occurred while parsing. Data can't be imported", "Start", 16);
|
286
|
// } catch (SAXException e) {
|
287
|
// fireWarningEvent("A SAXException occurred while parsing. Data can't be imported", "Start", 16);
|
288
|
// } catch (IOException e) {
|
289
|
// fireWarningEvent("An IO exception occurred while parsing. Data can't be imported", "Start", 16);
|
290
|
|
291
|
}
|
292
|
|
293
|
|
294
|
|
295
|
return;
|
296
|
|
297
|
}
|
298
|
|
299
|
|
300
|
|
301
|
|
302
|
private void handlePublication(MarkupImportState state, XMLEventReader reader, XMLEvent currentEvent, String elName) throws XMLStreamException {
|
303
|
|
304
|
//attributes
|
305
|
StartElement element = currentEvent.asStartElement();
|
306
|
Map<String, Attribute> attributes = getAttributes(element);
|
307
|
handleUnexpectedAttributes(element.getLocation(), attributes, "noNamespaceSchemaLocation");
|
308
|
|
309
|
while (reader.hasNext()){
|
310
|
XMLEvent event = readNoWhitespace(reader);
|
311
|
//TODO cardinality of alternative
|
312
|
if (event.isEndElement()){
|
313
|
if (isEndingElement(event, elName)){
|
314
|
return;
|
315
|
}else{
|
316
|
if(isEndingElement(event, BIOGRAPHIES)){
|
317
|
//NOT YET IMPLEMENTED
|
318
|
popUnimplemented(event.asEndElement());
|
319
|
}else if(isEndingElement(event, REFERENCES)){
|
320
|
//NOT YET IMPLEMENTED
|
321
|
popUnimplemented(event.asEndElement());
|
322
|
}else if(isEndingElement(event, TEXT_SECTION)){
|
323
|
//NOT YET IMPLEMENTED
|
324
|
popUnimplemented(event.asEndElement());
|
325
|
}else if(isEndingElement(event, ADDENDA)){
|
326
|
//NOT YET IMPLEMENTED
|
327
|
popUnimplemented(event.asEndElement());
|
328
|
}else{
|
329
|
handleUnexpectedElement(event);
|
330
|
}
|
331
|
}
|
332
|
}else if (event.isStartElement()){
|
333
|
if(isStartingElement(event, META_DATA)){
|
334
|
handleMetaData(state, reader, event);
|
335
|
}else if(isStartingElement(event, TREATMENT)){
|
336
|
handleTreatment(state, reader, event);
|
337
|
}else if(isStartingElement(event, BIOGRAPHIES)){
|
338
|
handleNotYetImplementedElement(event);
|
339
|
}else if(isStartingElement(event, REFERENCES)){
|
340
|
handleNotYetImplementedElement(event);
|
341
|
}else if(isStartingElement(event, TEXT_SECTION)){
|
342
|
handleNotYetImplementedElement(event);
|
343
|
}else if(isStartingElement(event, ADDENDA)){
|
344
|
handleNotYetImplementedElement(event);
|
345
|
}else{
|
346
|
handleUnexpectedStartElement(event);
|
347
|
}
|
348
|
}else{
|
349
|
handleUnexpectedElement(event);
|
350
|
}
|
351
|
}
|
352
|
throw new IllegalStateException("Publication has no ending element");
|
353
|
}
|
354
|
|
355
|
private void handleMetaData(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
356
|
checkNoAttributes(parentEvent);
|
357
|
|
358
|
while (reader.hasNext()){
|
359
|
XMLEvent next = readNoWhitespace(reader);
|
360
|
if (isMyEndingElement(next, parentEvent)){
|
361
|
return;
|
362
|
}else if(isStartingElement(next, DEFAULT_MEDIA_URL)){
|
363
|
String baseUrl = getCData(state, reader, next);
|
364
|
try {
|
365
|
new URL(baseUrl);
|
366
|
state.setBaseMediaUrl(baseUrl);
|
367
|
} catch (MalformedURLException e) {
|
368
|
String message = "defaultMediaUrl '%s' is not a valid URL";
|
369
|
message = String.format(message, baseUrl);
|
370
|
fireWarningEvent(message, next, 8);
|
371
|
}
|
372
|
}else{
|
373
|
handleUnexpectedElement(next);
|
374
|
}
|
375
|
}
|
376
|
throw new IllegalStateException("MetaData has no ending element");
|
377
|
|
378
|
}
|
379
|
|
380
|
|
381
|
private void handleTreatment(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
382
|
checkNoAttributes(parentEvent);
|
383
|
Taxon lastTaxon = null;
|
384
|
while (reader.hasNext()){
|
385
|
XMLEvent next = readNoWhitespace(reader);
|
386
|
if (isStartingElement(next, TAXON)){
|
387
|
Taxon thisTaxon = handleTaxon(state, reader, next.asStartElement());
|
388
|
doTaxonRelation(state, thisTaxon, lastTaxon, parentEvent.getLocation());
|
389
|
lastTaxon = thisTaxon;
|
390
|
//TODO for imports spanning multiple documents ?? Still needed?
|
391
|
state.getConfig().setLastTaxonUuid(lastTaxon.getUuid());
|
392
|
}else if(isMyEndingElement(next, parentEvent)){
|
393
|
return;
|
394
|
}else{
|
395
|
fireSchemaConflictEventExpectedStartTag(TAXON, reader);
|
396
|
state.setUnsuccessfull();
|
397
|
}
|
398
|
}
|
399
|
return;
|
400
|
}
|
401
|
|
402
|
/**
|
403
|
* @param taxon
|
404
|
* @param lastTaxon
|
405
|
*/
|
406
|
private void doTaxonRelation(MarkupImportState state, Taxon taxon, Taxon lastTaxon, Location dataLocation) {
|
407
|
|
408
|
Classification tree = makeTree(state);
|
409
|
if (lastTaxon == null){
|
410
|
tree.addChildTaxon(taxon, null, null, null);
|
411
|
return;
|
412
|
}
|
413
|
Rank thisRank = taxon.getName().getRank();
|
414
|
Rank lastRank = lastTaxon.getName().getRank();
|
415
|
if (lastTaxon.getTaxonNodes().size() > 0){
|
416
|
TaxonNode lastNode = lastTaxon.getTaxonNodes().iterator().next();
|
417
|
if (thisRank.isLower(lastRank ) ){
|
418
|
lastNode.addChildTaxon(taxon, null, null, null);
|
419
|
fillMissingEpithetsForTaxa(lastTaxon, taxon);
|
420
|
}else if (thisRank.equals(lastRank)){
|
421
|
TaxonNode parent = lastNode.getParent();
|
422
|
if (parent != null){
|
423
|
parent.addChildTaxon(taxon, null, null, null);
|
424
|
fillMissingEpithetsForTaxa(parent.getTaxon(), taxon);
|
425
|
}else{
|
426
|
tree.addChildTaxon(taxon, null, null, null);
|
427
|
}
|
428
|
}else if (thisRank.isHigher(lastRank)){
|
429
|
doTaxonRelation(state, taxon, lastNode.getParent().getTaxon(), dataLocation);
|
430
|
// TaxonNode parentNode = handleTaxonRelation(state, taxon, lastNode.getParent().getTaxon());
|
431
|
// parentNode.addChildTaxon(taxon, null, null, null);
|
432
|
}
|
433
|
}else{
|
434
|
|
435
|
String message = "Last taxon has no node";
|
436
|
fireWarningEvent(message, makeLocationStr(dataLocation), 6);
|
437
|
}
|
438
|
}
|
439
|
|
440
|
|
441
|
/**
|
442
|
* @param state
|
443
|
* @return
|
444
|
*/
|
445
|
private Classification makeTree(MarkupImportState state) {
|
446
|
Classification result = state.getTree(null);
|
447
|
if (result == null){
|
448
|
UUID uuid = state.getConfig().getClassificationUuid();
|
449
|
if (uuid == null){
|
450
|
logger.warn("No classification uuid is defined");
|
451
|
result = createNewClassification(state);
|
452
|
}else{
|
453
|
result = getClassificationService().find(uuid);
|
454
|
if (result == null){
|
455
|
result = createNewClassification(state);
|
456
|
result.setUuid(uuid);
|
457
|
}
|
458
|
}
|
459
|
state.putTree(null, result);
|
460
|
}
|
461
|
save(result, state);
|
462
|
return result;
|
463
|
}
|
464
|
|
465
|
|
466
|
private Classification createNewClassification(MarkupImportState state) {
|
467
|
Classification result;
|
468
|
result = Classification.NewInstance(state.getConfig().getClassificationTitle());
|
469
|
state.putTree(null, result);
|
470
|
return result;
|
471
|
}
|
472
|
|
473
|
|
474
|
private Taxon handleTaxon(MarkupImportState state, XMLEventReader reader, StartElement parentEvent) throws XMLStreamException {
|
475
|
//TODO progress monitoring
|
476
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
477
|
Taxon taxon = createTaxonAndName(state, attributes);
|
478
|
state.setCurrentTaxon(taxon);
|
479
|
|
480
|
boolean hasTitle = false;
|
481
|
boolean hasNomenclature = false;
|
482
|
String taxonTitle = null;
|
483
|
Extension writerExtension = null;
|
484
|
|
485
|
|
486
|
while (reader.hasNext()){
|
487
|
XMLEvent next = readNoWhitespace(reader);
|
488
|
if (next.isEndElement()){
|
489
|
if (isMyEndingElement(next, parentEvent)){
|
490
|
|
491
|
checkMandatoryElement(hasTitle, parentEvent, TAXONTITLE);
|
492
|
checkMandatoryElement(hasNomenclature, parentEvent, NOMENCLATURE);
|
493
|
handleUnexpectedAttributes(parentEvent.getLocation(), attributes);
|
494
|
|
495
|
state.setCurrentTaxon(null);
|
496
|
save(taxon, state);
|
497
|
return taxon;
|
498
|
}else{
|
499
|
if(isEndingElement(next, HEADING)){
|
500
|
//NOT YET IMPLEMENTED
|
501
|
popUnimplemented(next.asEndElement());
|
502
|
}else if(isEndingElement(next, TEXT_SECTION)){
|
503
|
//NOT YET IMPLEMENTED
|
504
|
popUnimplemented(next.asEndElement());
|
505
|
}else if(isEndingElement(next, KEY)){
|
506
|
//NOT YET IMPLEMENTED
|
507
|
popUnimplemented(next.asEndElement());
|
508
|
}else if(isEndingElement(next, NOTES)){
|
509
|
//NOT YET IMPLEMENTED
|
510
|
popUnimplemented(next.asEndElement());
|
511
|
}else if(isEndingElement(next, REFERENCES)){
|
512
|
//NOT YET IMPLEMENTED
|
513
|
popUnimplemented(next.asEndElement());
|
514
|
}else{
|
515
|
handleUnexpectedEndElement(next.asEndElement());
|
516
|
}
|
517
|
}
|
518
|
}else if (next.isStartElement()){
|
519
|
if(isStartingElement(next, HEADING)){
|
520
|
handleNotYetImplementedElement(next);
|
521
|
}else if(isStartingElement(next, TAXONTITLE)){
|
522
|
taxonTitle = handleTaxonTitle(state, reader, next);
|
523
|
hasTitle = true;
|
524
|
}else if(isStartingElement(next, WRITER)){
|
525
|
List<FootnoteDataHolder> footNotes = new ArrayList<FootnoteDataHolder>();
|
526
|
writerExtension = handleWriter(state, reader, next, footNotes);
|
527
|
taxon.addExtension(writerExtension);
|
528
|
//TODO what if taxonTitle comes later
|
529
|
if (StringUtils.isNotBlank(taxonTitle) && writerExtension != null){
|
530
|
String writer = writerExtension.getValue();
|
531
|
Reference sec = ReferenceFactory.newBookSection();
|
532
|
sec.setTitle(taxonTitle);
|
533
|
TeamOrPersonBase author = createAuthor(writer);
|
534
|
sec.setAuthorTeam(author);
|
535
|
sec.setInReference(state.getConfig().getSourceReference());
|
536
|
taxon.setSec(sec);
|
537
|
registerFootnotes(state, sec, footNotes);
|
538
|
}else{
|
539
|
String message = "No taxontitle exists for writer";
|
540
|
fireWarningEvent(message, next, 6);
|
541
|
}
|
542
|
|
543
|
}else if(isStartingElement(next, TEXT_SECTION)){
|
544
|
handleNotYetImplementedElement(next);
|
545
|
}else if(isStartingElement(next, KEY)){
|
546
|
handleNotYetImplementedElement(next);
|
547
|
}else if(isStartingElement(next, NOMENCLATURE)){
|
548
|
handleNomenclature(state, reader, next);
|
549
|
hasNomenclature = true;
|
550
|
}else if(isStartingElement(next, FEATURE)){
|
551
|
handleFeature(state, reader, next);
|
552
|
}else if(isStartingElement(next, NOTES)){
|
553
|
handleNotYetImplementedElement(next);
|
554
|
}else if(isStartingElement(next, REFERENCES)){
|
555
|
handleNotYetImplementedElement(next);
|
556
|
}else if(isStartingElement(next, FIGURE)){
|
557
|
handleFigure(state, reader, next);
|
558
|
}else if(isStartingElement(next, FOOTNOTE)){
|
559
|
FootnoteDataHolder footnote = handleFootnote(state, reader, next);
|
560
|
if (footnote.isRef()){
|
561
|
String message = "Ref footnote not implemented here";
|
562
|
fireWarningEvent(message, next, 4);
|
563
|
}else{
|
564
|
registerGivenFootnote(state, footnote);
|
565
|
}
|
566
|
}else{
|
567
|
handleUnexpectedStartElement(next);
|
568
|
}
|
569
|
}else{
|
570
|
handleUnexpectedElement(next);
|
571
|
}
|
572
|
}
|
573
|
//TODO handle missing end element
|
574
|
throw new IllegalStateException("Taxon has no closing tag");
|
575
|
}
|
576
|
|
577
|
|
578
|
/**
|
579
|
* @param state
|
580
|
* @param attributes
|
581
|
*/
|
582
|
private Taxon createTaxonAndName(MarkupImportState state, Map<String, Attribute> attributes) {
|
583
|
NonViralName name;
|
584
|
Rank rank = Rank.SPECIES(); //default
|
585
|
boolean isCultivar = checkAndRemoveAttributeValue(attributes, CLASS, "cultivated");
|
586
|
if (isCultivar){
|
587
|
name = CultivarPlantName.NewInstance(rank);
|
588
|
}else{
|
589
|
name = createNameByCode(state, rank);
|
590
|
}
|
591
|
Taxon taxon = Taxon.NewInstance(name, state.getConfig().getSourceReference());
|
592
|
if (checkAndRemoveAttributeValue(attributes, CLASS, "dubious")){
|
593
|
taxon.setDoubtful(true);
|
594
|
}else if (checkAndRemoveAttributeValue(attributes, CLASS, "excluded")){
|
595
|
taxon.setExcluded(true);
|
596
|
}
|
597
|
//TODO insufficient, new, expected
|
598
|
handleNotYetImplementedAttribute(attributes, CLASS);
|
599
|
//From old version
|
600
|
// MarkerType markerType = getMarkerType(state, attrValue);
|
601
|
// if (markerType == null){
|
602
|
// logger.warn("Class attribute value for taxon not yet supported: " + attrValue);
|
603
|
// }else{
|
604
|
// taxon.addMarker(Marker.NewInstance(markerType, true));
|
605
|
// }
|
606
|
|
607
|
// save(name, state);
|
608
|
// save(taxon, state);
|
609
|
return taxon;
|
610
|
}
|
611
|
|
612
|
|
613
|
/**
|
614
|
* @param state
|
615
|
* @param rank
|
616
|
* @return
|
617
|
*/
|
618
|
private NonViralName createNameByCode(MarkupImportState state, Rank rank) {
|
619
|
NonViralName name;
|
620
|
NomenclaturalCode nc = makeNomenclaturalCode(state);
|
621
|
name = (NonViralName)nc.getNewTaxonNameInstance(rank);
|
622
|
return name;
|
623
|
}
|
624
|
|
625
|
|
626
|
/**
|
627
|
* @param state
|
628
|
* @return
|
629
|
*/
|
630
|
private NomenclaturalCode makeNomenclaturalCode(MarkupImportState state) {
|
631
|
NomenclaturalCode nc = state.getConfig().getNomenclaturalCode();
|
632
|
if (nc == null){
|
633
|
nc = NomenclaturalCode.ICBN; //default;
|
634
|
}
|
635
|
return nc;
|
636
|
}
|
637
|
|
638
|
private String handleTaxonTitle(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
639
|
String text = "";
|
640
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
641
|
String rankAttr = getAndRemoveAttributeValue(attributes, RANK);
|
642
|
Rank rank = makeRank(state, rankAttr, false);
|
643
|
//TODO
|
644
|
// String numAttr = getAndRemoveAttributeValue(attributes, NUM);
|
645
|
handleNotYetImplementedAttribute(attributes, NUM);
|
646
|
checkNoAttributes(attributes, parentEvent);
|
647
|
|
648
|
//TODO handle attributes
|
649
|
while (reader.hasNext()){
|
650
|
XMLEvent next = readNoWhitespace(reader);
|
651
|
if (next.isEndElement()){
|
652
|
if (isMyEndingElement(next, parentEvent)){
|
653
|
Taxon taxon = state.getCurrentTaxon();
|
654
|
String titleText = null;
|
655
|
if (checkMandatoryText(text, parentEvent)){
|
656
|
titleText = normalize(text);
|
657
|
UUID uuidTitle = MarkupTransformer.uuidTaxonTitle;
|
658
|
ExtensionType titleExtension = this.getExtensionType(state, uuidTitle, "Taxon Title ", "taxon title", "title");
|
659
|
taxon.addExtension(titleText, titleExtension);
|
660
|
}
|
661
|
taxon.getName().setRank(rank);
|
662
|
//TODO check title exists
|
663
|
return titleText;
|
664
|
}else{
|
665
|
if(isEndingElement(next, FOOTNOTE)){
|
666
|
//NOT YET IMPLEMENTED
|
667
|
popUnimplemented(next.asEndElement());
|
668
|
}else{
|
669
|
handleUnexpectedEndElement(next.asEndElement());
|
670
|
state.setUnsuccessfull();
|
671
|
}
|
672
|
}
|
673
|
}else if (next.isStartElement()){
|
674
|
if(isStartingElement(next, FOOTNOTE)){
|
675
|
handleNotYetImplementedElement(next);
|
676
|
}else{
|
677
|
handleUnexpectedStartElement(next);
|
678
|
state.setUnsuccessfull();
|
679
|
}
|
680
|
}else if (next.isCharacters()){
|
681
|
text +=next.asCharacters().getData();
|
682
|
|
683
|
}else{
|
684
|
handleUnexpectedElement(next);
|
685
|
state.setUnsuccessfull();
|
686
|
}
|
687
|
}
|
688
|
return null;
|
689
|
|
690
|
|
691
|
}
|
692
|
|
693
|
private Extension handleWriter(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, List<FootnoteDataHolder> footNotes) throws XMLStreamException {
|
694
|
String text = "";
|
695
|
checkNoAttributes(parentEvent);
|
696
|
|
697
|
//TODO handle attributes
|
698
|
while (reader.hasNext()){
|
699
|
XMLEvent next = readNoWhitespace(reader);
|
700
|
if (next.isEndElement()){
|
701
|
if (isMyEndingElement(next, parentEvent)){
|
702
|
if (checkMandatoryText(text, parentEvent)){
|
703
|
UUID uuidWriter = MarkupTransformer.uuidWriter;
|
704
|
ExtensionType titleExtensionType = this.getExtensionType(state, uuidWriter, "Writer", "writer", "writer");
|
705
|
Extension extension = Extension.NewInstance();
|
706
|
extension.setType(titleExtensionType);
|
707
|
extension.setValue(normalize(text));
|
708
|
return extension;
|
709
|
}else{
|
710
|
return null;
|
711
|
}
|
712
|
}else{
|
713
|
if(isEndingElement(next, FOOTNOTE)){
|
714
|
//NOT YET IMPLEMENTED
|
715
|
popUnimplemented(next.asEndElement());
|
716
|
}else{
|
717
|
handleUnexpectedEndElement(next.asEndElement());
|
718
|
state.setUnsuccessfull();
|
719
|
}
|
720
|
}
|
721
|
}else if (next.isStartElement()){
|
722
|
if(isStartingElement(next, FOOTNOTE_REF)){
|
723
|
FootnoteDataHolder footNote = handleFootnoteRef(state, reader, next);
|
724
|
if (footNote.isRef()){
|
725
|
footNotes.add(footNote);
|
726
|
}else{
|
727
|
logger.warn ("Non ref footnotes not yet impelemnted");
|
728
|
}
|
729
|
}else{
|
730
|
handleUnexpectedStartElement(next);
|
731
|
state.setUnsuccessfull();
|
732
|
}
|
733
|
}else if (next.isCharacters()){
|
734
|
text +=next.asCharacters().getData();
|
735
|
|
736
|
}else{
|
737
|
handleUnexpectedElement(next);
|
738
|
state.setUnsuccessfull();
|
739
|
}
|
740
|
}
|
741
|
return null;
|
742
|
}
|
743
|
|
744
|
private void registerFootnotes(MarkupImportState state, AnnotatableEntity entity, List<FootnoteDataHolder> footnotes) {
|
745
|
for (FootnoteDataHolder footNote : footnotes){
|
746
|
registerFootnoteDemand(state, entity, footNote);
|
747
|
}
|
748
|
}
|
749
|
|
750
|
private void registerGivenFootnote(MarkupImportState state, FootnoteDataHolder footnote) {
|
751
|
state.registerFootnote(footnote);
|
752
|
Set<AnnotatableEntity> demands = state.getFootnoteDemands(footnote.id);
|
753
|
if (demands != null){
|
754
|
for (AnnotatableEntity entity : demands){
|
755
|
attachFootnote(state, entity, footnote);
|
756
|
}
|
757
|
}
|
758
|
}
|
759
|
|
760
|
|
761
|
private void registerGivenFigure(MarkupImportState state, String id, Media figure) {
|
762
|
state.registerFigure(id, figure);
|
763
|
Set<AnnotatableEntity> demands = state.getFigureDemands(id);
|
764
|
if (demands != null){
|
765
|
for (AnnotatableEntity entity : demands){
|
766
|
attachFigure(state, entity, figure);
|
767
|
}
|
768
|
}
|
769
|
}
|
770
|
|
771
|
private void registerFootnoteDemand(MarkupImportState state, AnnotatableEntity entity, FootnoteDataHolder footnote) {
|
772
|
FootnoteDataHolder existingFootnote = state.getFootnote(footnote.ref);
|
773
|
if (existingFootnote != null ){
|
774
|
attachFootnote(state, entity, existingFootnote);
|
775
|
}else{
|
776
|
Set<AnnotatableEntity> demands = state.getFootnoteDemands(footnote.ref);
|
777
|
if (demands == null){
|
778
|
demands = new HashSet<AnnotatableEntity>();
|
779
|
state.putFootnoteDemands(footnote.ref, demands);
|
780
|
}
|
781
|
demands.add(entity);
|
782
|
}
|
783
|
}
|
784
|
|
785
|
private void registerFigureDemand(MarkupImportState state, AnnotatableEntity entity, String figureRef) {
|
786
|
Media existingFigure = state.getFigure(figureRef);
|
787
|
if (existingFigure != null ){
|
788
|
attachFigure(state, entity, existingFigure);
|
789
|
}else{
|
790
|
Set<AnnotatableEntity> demands = state.getFigureDemands(figureRef);
|
791
|
if (demands == null){
|
792
|
demands = new HashSet<AnnotatableEntity>();
|
793
|
state.putFigureDemands(figureRef, demands);
|
794
|
}
|
795
|
demands.add(entity);
|
796
|
}
|
797
|
}
|
798
|
|
799
|
|
800
|
private void attachFootnote(MarkupImportState state, AnnotatableEntity entity, FootnoteDataHolder footnote) {
|
801
|
AnnotationType annotationType = this.getAnnotationType(state, MarkupTransformer.uuidFootnote, "Footnote", "An e-flora footnote", "fn", null);
|
802
|
Annotation annotation = Annotation.NewInstance(footnote.string, annotationType, Language.DEFAULT());
|
803
|
// TODO transient objects
|
804
|
entity.addAnnotation(annotation);
|
805
|
save(entity,state);
|
806
|
}
|
807
|
|
808
|
|
809
|
private void attachFigure(MarkupImportState state, AnnotatableEntity entity, Media figure) {
|
810
|
//IdentifiableEntity<?> toSave;
|
811
|
if (entity.isInstanceOf(TextData.class)){
|
812
|
TextData deb = CdmBase.deproxy(entity, TextData.class);
|
813
|
deb.addMedia(figure);
|
814
|
//toSave = ((TaxonDescription)deb.getInDescription()).getTaxon();
|
815
|
}else if (entity.isInstanceOf(IdentifiableMediaEntity.class)){
|
816
|
IdentifiableMediaEntity<?> ime = CdmBase.deproxy(entity, IdentifiableMediaEntity.class);
|
817
|
ime.addMedia(figure);
|
818
|
//toSave = ime;
|
819
|
}else{
|
820
|
String message = "Unsupported entity to attach media: %s";
|
821
|
message = String.format(message, entity.getClass().getName());
|
822
|
//toSave = null;
|
823
|
}
|
824
|
save(entity,state);
|
825
|
}
|
826
|
|
827
|
|
828
|
private void handleFigure(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
829
|
// FigureDataHolder result = new FigureDataHolder();
|
830
|
|
831
|
|
832
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
833
|
String id = getAndRemoveAttributeValue(attributes, ID);
|
834
|
String type = getAndRemoveAttributeValue(attributes, TYPE);
|
835
|
checkNoAttributes(attributes, parentEvent);
|
836
|
|
837
|
String urlString = null;
|
838
|
String legendString = null;
|
839
|
String titleString = null;
|
840
|
String numString = null;
|
841
|
String text = null;
|
842
|
while (reader.hasNext()){
|
843
|
XMLEvent next = readNoWhitespace(reader);
|
844
|
if(isMyEndingElement(next, parentEvent)){
|
845
|
Media media = null;
|
846
|
try {
|
847
|
URL url = new URL(urlString);
|
848
|
if ("lineart".equals(type)){
|
849
|
media = Figure.NewInstance(url.toURI(), null, null, null);
|
850
|
}else if (type == null || "photo".equals(type) || "signature".equals(type) || "others".equals(type) ){
|
851
|
media = Media.NewInstance(url.toURI(), null, null, null);
|
852
|
}else{
|
853
|
String message = "Unknown figure type '%s'";
|
854
|
message = String.format(message, type);
|
855
|
//TODO location
|
856
|
fireWarningEvent(message, "-", 2);
|
857
|
media = Media.NewInstance(url.toURI(), null, null, null);
|
858
|
}
|
859
|
//title
|
860
|
if (StringUtils.isNotBlank(titleString)){
|
861
|
media.putTitle(Language.DEFAULT(), titleString);
|
862
|
}
|
863
|
//legend
|
864
|
if (StringUtils.isNotBlank(legendString)){
|
865
|
media.addDescription(legendString, Language.DEFAULT());
|
866
|
}
|
867
|
if (StringUtils.isNotBlank(numString)){
|
868
|
//TODO use concrete source (e.g. DAPHNIPHYLLACEAE in FM vol.13)
|
869
|
Reference citation = state.getConfig().getSourceReference();
|
870
|
media.addSource(numString, "num", citation, null);
|
871
|
//TODO name used in source if available
|
872
|
}
|
873
|
//TODO which citation
|
874
|
if (StringUtils.isNotBlank(id)){
|
875
|
media.addSource(id, null, state.getConfig().getSourceReference(), null);
|
876
|
}else{
|
877
|
String message = "Figure id should never be empty or null";
|
878
|
fireWarningEvent(message, next, 6);
|
879
|
}
|
880
|
//text
|
881
|
//do nothing
|
882
|
|
883
|
} catch (MalformedURLException e) {
|
884
|
String message = "Media uri has incorrect syntax: %s";
|
885
|
message = String.format(message, urlString);
|
886
|
fireWarningEvent(message, next, 4);
|
887
|
} catch (URISyntaxException e) {
|
888
|
String message = "Media uri has incorrect syntax: %s";
|
889
|
message = String.format(message, urlString);
|
890
|
fireWarningEvent(message, next, 4);
|
891
|
}
|
892
|
|
893
|
registerGivenFigure(state, id, media);
|
894
|
return;
|
895
|
}else if (isStartingElement(next, FIGURE_LEGEND)){
|
896
|
//TODO same as figurestring ?
|
897
|
legendString = handleFootnoteString(state, reader, next);
|
898
|
}else if (isStartingElement(next, FIGURE_TITLE)){
|
899
|
titleString = getCData(state, reader, next);
|
900
|
}else if (isStartingElement(next, URL)){
|
901
|
String localUrl = getCData(state, reader, next);
|
902
|
urlString = CdmUtils.Nz(state.getBaseMediaUrl()) + localUrl;
|
903
|
}else if (isStartingElement(next, NUM)){
|
904
|
numString = getCData(state, reader, next);
|
905
|
}else if (next.isCharacters()){
|
906
|
text +=next.asCharacters().getData();
|
907
|
}else {
|
908
|
fireUnexpectedEvent(next, 0);
|
909
|
}
|
910
|
}
|
911
|
throw new IllegalStateException("<figure> has no end tag");
|
912
|
}
|
913
|
|
914
|
private FigureDataHolder handleFigureRef(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
915
|
FigureDataHolder result = new FigureDataHolder();
|
916
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
917
|
result.ref = getAndRemoveAttributeValue(attributes, REF);
|
918
|
checkNoAttributes(attributes, parentEvent);
|
919
|
|
920
|
//text is not handled, needed only for debugging purposes
|
921
|
String text = "";
|
922
|
while (reader.hasNext()){
|
923
|
XMLEvent next = readNoWhitespace(reader);
|
924
|
if(isMyEndingElement(next, parentEvent)){
|
925
|
return result;
|
926
|
}else if (isStartingElement(next, NUM)){
|
927
|
String num = getCData(state, reader, next);
|
928
|
result.num = num; //num is not handled during import
|
929
|
}else if (isStartingElement(next, FIGURE_PART)){
|
930
|
result.figurePart = getCData(state, reader, next);
|
931
|
}else if (next.isCharacters()){
|
932
|
text +=next.asCharacters().getData();
|
933
|
}else{
|
934
|
fireUnexpectedEvent(next, 0);
|
935
|
}
|
936
|
}
|
937
|
throw new IllegalStateException("<figureRef> has no end tag");
|
938
|
}
|
939
|
|
940
|
|
941
|
private FootnoteDataHolder handleFootnote(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
942
|
FootnoteDataHolder result = new FootnoteDataHolder();
|
943
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
944
|
result.id = getAndRemoveAttributeValue(attributes, ID);
|
945
|
// result.ref = getAndRemoveAttributeValue(attributes, REF);
|
946
|
checkNoAttributes(attributes, parentEvent);
|
947
|
|
948
|
while (reader.hasNext()){
|
949
|
XMLEvent next = readNoWhitespace(reader);
|
950
|
if (isStartingElement(next, FOOTNOTE_STRING)){
|
951
|
String string = handleFootnoteString(state, reader, next);
|
952
|
result.string = string;
|
953
|
}else if(isMyEndingElement(next, parentEvent)){
|
954
|
return result;
|
955
|
}else{
|
956
|
fireUnexpectedEvent(next, 0);
|
957
|
}
|
958
|
}
|
959
|
return result;
|
960
|
}
|
961
|
|
962
|
private FootnoteDataHolder handleFootnoteRef(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
963
|
FootnoteDataHolder result = new FootnoteDataHolder();
|
964
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
965
|
result.ref = getAndRemoveAttributeValue(attributes, REF);
|
966
|
checkNoAttributes(attributes, parentEvent);
|
967
|
|
968
|
//text is not handled, needed only for debugging purposes
|
969
|
String text = "";
|
970
|
while (reader.hasNext()){
|
971
|
XMLEvent next = readNoWhitespace(reader);
|
972
|
// if (isStartingElement(next, FOOTNOTE_STRING)){
|
973
|
// String string = handleFootnoteString(state, reader, next);
|
974
|
// result.string = string;
|
975
|
// }else
|
976
|
if(isMyEndingElement(next, parentEvent)){
|
977
|
return result;
|
978
|
}else if (next.isCharacters()){
|
979
|
text +=next.asCharacters().getData();
|
980
|
|
981
|
}else{
|
982
|
fireUnexpectedEvent(next, 0);
|
983
|
}
|
984
|
}
|
985
|
return result;
|
986
|
}
|
987
|
|
988
|
|
989
|
private void handleNomenclature(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
990
|
checkNoAttributes(parentEvent);
|
991
|
|
992
|
while (reader.hasNext()){
|
993
|
XMLEvent next = readNoWhitespace(reader);
|
994
|
if (isStartingElement(next, HOMOTYPES)){
|
995
|
handleHomotypes(state, reader, next.asStartElement());
|
996
|
}else if(isMyEndingElement(next, parentEvent)){
|
997
|
return;
|
998
|
}else{
|
999
|
fireSchemaConflictEventExpectedStartTag(HOMOTYPES, reader);
|
1000
|
state.setUnsuccessfull();
|
1001
|
}
|
1002
|
}
|
1003
|
return;
|
1004
|
}
|
1005
|
|
1006
|
private String handleFootnoteString(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
1007
|
boolean isTextMode = true;
|
1008
|
String text = "";
|
1009
|
while (reader.hasNext()){
|
1010
|
XMLEvent next = readNoWhitespace(reader);
|
1011
|
if(isMyEndingElement(next, parentEvent)){
|
1012
|
return text;
|
1013
|
}else if (next.isEndElement()){
|
1014
|
if (isEndingElement(next, FULL_NAME)){
|
1015
|
popUnimplemented(next.asEndElement());
|
1016
|
}else if (isEndingElement(next, COLLECTION)){
|
1017
|
popUnimplemented(next.asEndElement());
|
1018
|
}else if (isEndingElement(next, REFERENCES)){
|
1019
|
popUnimplemented(next.asEndElement());
|
1020
|
}else if (isEndingElement(next, BR)){
|
1021
|
isTextMode = true;
|
1022
|
}else if (isHtml(next)){
|
1023
|
text += "</" + getHtmlTag(next) + ">";
|
1024
|
}else {
|
1025
|
handleUnexpectedEndElement(next.asEndElement());
|
1026
|
}
|
1027
|
}else if (next.isStartElement()){
|
1028
|
if (isStartingElement(next, FULL_NAME)){
|
1029
|
handleNotYetImplementedElement(next);
|
1030
|
}else if (isStartingElement(next, COLLECTION)){
|
1031
|
handleNotYetImplementedElement(next);
|
1032
|
}else if (isStartingElement(next, REFERENCES)){
|
1033
|
handleNotYetImplementedElement(next);
|
1034
|
}else if (isStartingElement(next, BR)){
|
1035
|
text += "<br/>";
|
1036
|
isTextMode = false;
|
1037
|
}else if (isHtml(next)){
|
1038
|
text += "<" + getHtmlTag(next) + ">";
|
1039
|
}else {
|
1040
|
handleUnexpectedStartElement(next.asStartElement());
|
1041
|
}
|
1042
|
} else if (next.isCharacters()){
|
1043
|
if (!isTextMode){
|
1044
|
String message = "footnoteString is not in text mode";
|
1045
|
fireWarningEvent(message, next, 6);
|
1046
|
}else{
|
1047
|
text += next.asCharacters().getData();
|
1048
|
}
|
1049
|
}else {
|
1050
|
handleUnexpectedEndElement(next.asEndElement());
|
1051
|
}
|
1052
|
}
|
1053
|
throw new IllegalStateException("<footnoteString> has no closing tag");
|
1054
|
|
1055
|
}
|
1056
|
|
1057
|
|
1058
|
private void handleHomotypes(MarkupImportState state, XMLEventReader reader, StartElement parentEvent) throws XMLStreamException {
|
1059
|
checkNoAttributes(parentEvent);
|
1060
|
|
1061
|
HomotypicalGroup homotypicalGroup = null;
|
1062
|
|
1063
|
boolean hasNom = false;
|
1064
|
while (reader.hasNext()){
|
1065
|
XMLEvent next = readNoWhitespace(reader);
|
1066
|
if (next.isEndElement()){
|
1067
|
if (isMyEndingElement(next, parentEvent)){
|
1068
|
checkMandatoryElement(hasNom, parentEvent, NOM);
|
1069
|
return;
|
1070
|
}else{
|
1071
|
if(isEndingElement(next, NAME_TYPE)){
|
1072
|
state.setNameType(false);
|
1073
|
}else if(isEndingElement(next, NOTES)){
|
1074
|
//NOT YET IMPLEMENTED
|
1075
|
popUnimplemented(next.asEndElement());
|
1076
|
}else{
|
1077
|
handleUnexpectedEndElement(next.asEndElement());
|
1078
|
}
|
1079
|
}
|
1080
|
}else if (next.isStartElement()){
|
1081
|
if(isStartingElement(next, NOM)){
|
1082
|
NonViralName<?> name = handleNom(state, reader, next, homotypicalGroup);
|
1083
|
homotypicalGroup = name.getHomotypicalGroup();
|
1084
|
hasNom = true;
|
1085
|
}else if(isStartingElement(next, NAME_TYPE)){
|
1086
|
state.setNameType(true);
|
1087
|
handleNameType(state, reader, next);
|
1088
|
}else if(isStartingElement(next, SPECIMEN_TYPE)){
|
1089
|
handleSpecimenType(state, reader, next, homotypicalGroup);
|
1090
|
}else if(isStartingElement(next, NOTES)){
|
1091
|
handleNotYetImplementedElement(next);
|
1092
|
}else{
|
1093
|
handleUnexpectedStartElement(next);
|
1094
|
}
|
1095
|
}else{
|
1096
|
handleUnexpectedElement(next);
|
1097
|
}
|
1098
|
}
|
1099
|
//TODO handle missing end element
|
1100
|
throw new IllegalStateException("Homotypes has no closing tag");
|
1101
|
|
1102
|
}
|
1103
|
|
1104
|
|
1105
|
private void handleNameType(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
1106
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
1107
|
String typeStatus = getAndRemoveAttributeValue(attributes, TYPE_STATUS);
|
1108
|
checkNoAttributes(attributes, parentEvent);
|
1109
|
|
1110
|
NameTypeDesignationStatus status;
|
1111
|
try {
|
1112
|
status = NameTypeParser.parseNameTypeStatus(typeStatus);
|
1113
|
} catch (UnknownCdmTypeException e) {
|
1114
|
String message = "Type status could not be recognized: %s";
|
1115
|
message = String.format(message, typeStatus);
|
1116
|
fireWarningEvent(message, parentEvent, 4);
|
1117
|
}
|
1118
|
|
1119
|
boolean hasNom = false;
|
1120
|
while (reader.hasNext()){
|
1121
|
XMLEvent next = readNoWhitespace(reader);
|
1122
|
if (next.isEndElement()){
|
1123
|
if (isMyEndingElement(next, parentEvent)){
|
1124
|
checkMandatoryElement(hasNom, parentEvent.asStartElement(), NOM);
|
1125
|
return;
|
1126
|
}else{
|
1127
|
if(isEndingElement(next, ACCEPTED_NAME)){
|
1128
|
//NOT YET IMPLEMENTED
|
1129
|
popUnimplemented(next.asEndElement());
|
1130
|
}else{
|
1131
|
handleUnexpectedEndElement(next.asEndElement());
|
1132
|
}
|
1133
|
}
|
1134
|
}else if (next.isStartElement()){
|
1135
|
if(isStartingElement(next, NOM)){
|
1136
|
NonViralName name = handleNom(state, reader, next, null);
|
1137
|
hasNom = true;
|
1138
|
}else if(isStartingElement(next, ACCEPTED_NAME)){
|
1139
|
handleNotYetImplementedElement(next);
|
1140
|
}else{
|
1141
|
handleUnexpectedStartElement(next);
|
1142
|
}
|
1143
|
}else{
|
1144
|
handleUnexpectedElement(next);
|
1145
|
}
|
1146
|
}
|
1147
|
//TODO handle missing end element
|
1148
|
throw new IllegalStateException("Homotypes has no closing tag");
|
1149
|
|
1150
|
|
1151
|
}
|
1152
|
|
1153
|
|
1154
|
private void handleSpecimenType(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, HomotypicalGroup homotypicalGroup) throws XMLStreamException {
|
1155
|
//attributes
|
1156
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
1157
|
String typeStatus = getAndRemoveAttributeValue(attributes, TYPE_STATUS);
|
1158
|
String notSeen = getAndRemoveAttributeValue(attributes, NOT_SEEN);
|
1159
|
String unknown = getAndRemoveAttributeValue(attributes, UNKNOWN);
|
1160
|
String notFound = getAndRemoveAttributeValue(attributes, NOT_FOUND);
|
1161
|
String destroyed = getAndRemoveAttributeValue(attributes, DESTROYED);
|
1162
|
String lost = getAndRemoveAttributeValue(attributes, LOST);
|
1163
|
checkNoAttributes(attributes, parentEvent);
|
1164
|
if (StringUtils.isNotEmpty(typeStatus)){
|
1165
|
//TODO
|
1166
|
//currently not needed
|
1167
|
} else if (StringUtils.isNotEmpty(notSeen)){
|
1168
|
handleNotYetImplementedAttribute(attributes, NOT_SEEN);
|
1169
|
}else if (StringUtils.isNotEmpty(unknown)){
|
1170
|
handleNotYetImplementedAttribute(attributes, UNKNOWN);
|
1171
|
}else if (StringUtils.isNotEmpty(notFound)){
|
1172
|
handleNotYetImplementedAttribute(attributes, NOT_FOUND);
|
1173
|
}else if (StringUtils.isNotEmpty(destroyed)){
|
1174
|
handleNotYetImplementedAttribute(attributes, DESTROYED);
|
1175
|
}else if (StringUtils.isNotEmpty(lost)){
|
1176
|
handleNotYetImplementedAttribute(attributes, LOST);
|
1177
|
}
|
1178
|
|
1179
|
NonViralName firstName = null;
|
1180
|
Set<TaxonNameBase> names = homotypicalGroup.getTypifiedNames();
|
1181
|
if (names.isEmpty()){
|
1182
|
String message = "There is no name in a homotypical group. Can't create the specimen type";
|
1183
|
fireWarningEvent(message, parentEvent, 8);
|
1184
|
}else{
|
1185
|
firstName = CdmBase.deproxy(names.iterator().next(), NonViralName.class);
|
1186
|
}
|
1187
|
|
1188
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
|
1189
|
String text = "";
|
1190
|
//elements
|
1191
|
while (reader.hasNext()){
|
1192
|
XMLEvent next = readNoWhitespace(reader);
|
1193
|
if (next.isEndElement()){
|
1194
|
if (isMyEndingElement(next, parentEvent)){
|
1195
|
makeSpecimenType(state, facade, text, firstName, parentEvent);
|
1196
|
return;
|
1197
|
}else{
|
1198
|
if(isEndingElement(next, FULL_TYPE)){
|
1199
|
//NOT YET IMPLEMENTED
|
1200
|
popUnimplemented(next.asEndElement());
|
1201
|
}else if(isEndingElement(next,TYPE_STATUS)){
|
1202
|
//NOT YET IMPLEMENTED
|
1203
|
popUnimplemented(next.asEndElement());
|
1204
|
}else if(isEndingElement(next, ORIGINAL_DETERMINATION)){
|
1205
|
//NOT YET IMPLEMENTED
|
1206
|
popUnimplemented(next.asEndElement());
|
1207
|
}else if(isEndingElement(next, SPECIMEN_TYPE)){
|
1208
|
//NOT YET IMPLEMENTED
|
1209
|
popUnimplemented(next.asEndElement());
|
1210
|
}else if(isEndingElement(next, CITATION)){
|
1211
|
//NOT YET IMPLEMENTED
|
1212
|
popUnimplemented(next.asEndElement());
|
1213
|
}else if(isEndingElement(next, NOTES)){
|
1214
|
//NOT YET IMPLEMENTED
|
1215
|
popUnimplemented(next.asEndElement());
|
1216
|
}else if(isEndingElement(next, ANNOTATION)){
|
1217
|
//NOT YET IMPLEMENTED
|
1218
|
popUnimplemented(next.asEndElement());
|
1219
|
}else{
|
1220
|
handleUnexpectedEndElement(next.asEndElement());
|
1221
|
}
|
1222
|
}
|
1223
|
}else if (next.isStartElement()){
|
1224
|
if(isStartingElement(next, FULL_TYPE)){
|
1225
|
handleNotYetImplementedElement(next);
|
1226
|
// homotypicalGroup = handleNom(state, reader, next, taxon, homotypicalGroup);
|
1227
|
}else if(isStartingElement(next, TYPE_STATUS)){
|
1228
|
handleNotYetImplementedElement(next);
|
1229
|
}else if(isStartingElement(next, COLLECTION)){
|
1230
|
handleCollection(state, reader, next, homotypicalGroup, facade);
|
1231
|
}else if(isStartingElement(next, ORIGINAL_DETERMINATION)){
|
1232
|
handleNotYetImplementedElement(next);
|
1233
|
}else if(isStartingElement(next, SPECIMEN_TYPE)){
|
1234
|
handleNotYetImplementedElement(next);
|
1235
|
}else if(isStartingElement(next, NOTES)){
|
1236
|
handleNotYetImplementedElement(next);
|
1237
|
}else if(isStartingElement(next, ANNOTATION)){
|
1238
|
handleNotYetImplementedElement(next);
|
1239
|
}else{
|
1240
|
handleUnexpectedStartElement(next);
|
1241
|
}
|
1242
|
}else if (next.isCharacters()){
|
1243
|
text += next.asCharacters().getData();
|
1244
|
}else{
|
1245
|
handleUnexpectedElement(next);
|
1246
|
}
|
1247
|
}
|
1248
|
//TODO handle missing end element
|
1249
|
throw new IllegalStateException("Specimen type has no closing tag"); // TODO Auto-generated method stub
|
1250
|
|
1251
|
}
|
1252
|
|
1253
|
|
1254
|
private void makeSpecimenType(MarkupImportState state, DerivedUnitFacade facade, String text, NonViralName name, XMLEvent parentEvent) {
|
1255
|
text = text.trim();
|
1256
|
//remove brackets
|
1257
|
if (text.matches("^\\(.*\\)\\.?$")){
|
1258
|
text = text.replaceAll("\\.", "");
|
1259
|
text = text.substring(1, text.length() - 1);
|
1260
|
}
|
1261
|
String[] split = text.split("[;,]");
|
1262
|
for (String str : split){
|
1263
|
str = str.trim();
|
1264
|
boolean addToAllNamesInGroup = true;
|
1265
|
TypeInfo typeInfo = makeSpecimenTypeTypeInfo(str, parentEvent);
|
1266
|
SpecimenTypeDesignationStatus typeStatus = typeInfo.status;
|
1267
|
Collection collection = createCollection(typeInfo.collectionString);
|
1268
|
|
1269
|
//TODO improve cache strategy handling
|
1270
|
DerivedUnitBase typeSpecimen = facade.addDuplicate(collection, null, null, null, null);
|
1271
|
typeSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
|
1272
|
name.addSpecimenTypeDesignation((Specimen)typeSpecimen, typeStatus, null, null, null, false, addToAllNamesInGroup);
|
1273
|
}
|
1274
|
|
1275
|
|
1276
|
|
1277
|
}
|
1278
|
|
1279
|
private Collection createCollection(String code) {
|
1280
|
//TODO deduplicate
|
1281
|
//TODO code <-> name
|
1282
|
Collection result = Collection.NewInstance();
|
1283
|
result.setCode(code);
|
1284
|
return result;
|
1285
|
}
|
1286
|
|
1287
|
private TypeInfo makeSpecimenTypeTypeInfo(String originalString, XMLEvent event) {
|
1288
|
TypeInfo result = new TypeInfo();
|
1289
|
String[] split = originalString.split("\\s+");
|
1290
|
for (String str : split){
|
1291
|
if (str.matches(SpecimenTypeParser.typeTypePattern)){
|
1292
|
SpecimenTypeDesignationStatus status;
|
1293
|
try {
|
1294
|
status = SpecimenTypeParser.parseSpecimenTypeStatus(str);
|
1295
|
} catch (UnknownCdmTypeException e) {
|
1296
|
String message = "Specimen type status '%s' not recognized by parser";
|
1297
|
message = String.format(message, str);
|
1298
|
fireWarningEvent(message, event, 4);
|
1299
|
status = null;
|
1300
|
}
|
1301
|
result.status = status;
|
1302
|
}else if(str.matches( SpecimenTypeParser.collectionPattern)){
|
1303
|
result.collectionString = str;
|
1304
|
}else{
|
1305
|
String message = "Type part '%s' could not be recognized";
|
1306
|
message = String.format(message, str);
|
1307
|
fireWarningEvent(message, event, 2);
|
1308
|
}
|
1309
|
}
|
1310
|
|
1311
|
return result;
|
1312
|
}
|
1313
|
|
1314
|
|
1315
|
private void handleCollection(MarkupImportState state,XMLEventReader reader, XMLEvent parentEvent, HomotypicalGroup homotypicalGroup, DerivedUnitFacade facade) throws XMLStreamException {
|
1316
|
checkNoAttributes(parentEvent);
|
1317
|
boolean hasCollector = false;
|
1318
|
boolean hasFieldNum = false;
|
1319
|
|
1320
|
//elements
|
1321
|
while (reader.hasNext()){
|
1322
|
XMLEvent next = readNoWhitespace(reader);
|
1323
|
if (next.isEndElement()){
|
1324
|
if (isMyEndingElement(next, parentEvent)){
|
1325
|
checkMandatoryElement(hasCollector, parentEvent.asStartElement(), COLLECTOR);
|
1326
|
checkMandatoryElement(hasFieldNum, parentEvent.asStartElement(), FIELD_NUM);
|
1327
|
return;
|
1328
|
}else{
|
1329
|
if(isEndingElement(next,ALTERNATIVE_COLLECTOR)){
|
1330
|
//NOT YET IMPLEMENTED
|
1331
|
popUnimplemented(next.asEndElement());
|
1332
|
}else if(isEndingElement(next, ALTERNATIVE_FIELD_NUM)){
|
1333
|
//NOT YET IMPLEMENTED
|
1334
|
popUnimplemented(next.asEndElement());
|
1335
|
}else if(isEndingElement(next, COLLECTION_TYPE_STATUS)){
|
1336
|
//NOT YET IMPLEMENTED
|
1337
|
popUnimplemented(next.asEndElement());
|
1338
|
}else if(isEndingElement(next, ALTERNATIVE_COLLECTION_TYPE_STATUS)){
|
1339
|
//NOT YET IMPLEMENTED
|
1340
|
popUnimplemented(next.asEndElement());
|
1341
|
}else if(isEndingElement(next, SUB_COLLECTION)){
|
1342
|
//NOT YET IMPLEMENTED
|
1343
|
popUnimplemented(next.asEndElement());
|
1344
|
}else if(isEndingElement(next, DATES)){
|
1345
|
//NOT YET IMPLEMENTED
|
1346
|
popUnimplemented(next.asEndElement());
|
1347
|
}else if(isEndingElement(next, NOTES)){
|
1348
|
//NOT YET IMPLEMENTED
|
1349
|
popUnimplemented(next.asEndElement());
|
1350
|
}else{
|
1351
|
handleUnexpectedEndElement(next.asEndElement());
|
1352
|
}
|
1353
|
}
|
1354
|
}else if (next.isStartElement()){
|
1355
|
if(isStartingElement(next, COLLECTOR)){
|
1356
|
hasCollector = true;
|
1357
|
String collectorStr = getCData(state, reader, next);
|
1358
|
AgentBase<?> collector = createCollector(collectorStr);
|
1359
|
facade.setCollector(collector);
|
1360
|
}else if(isStartingElement(next, ALTERNATIVE_COLLECTOR)){
|
1361
|
handleNotYetImplementedElement(next);
|
1362
|
}else if(isStartingElement(next, FIELD_NUM)){
|
1363
|
hasFieldNum = true;
|
1364
|
String fieldNumStr = getCData(state, reader, next);
|
1365
|
facade.setFieldNumber(fieldNumStr);
|
1366
|
}else if(isStartingElement(next, ALTERNATIVE_FIELD_NUM)){
|
1367
|
handleNotYetImplementedElement(next);
|
1368
|
}else if(isStartingElement(next, COLLECTION_TYPE_STATUS)){
|
1369
|
handleNotYetImplementedElement(next);
|
1370
|
}else if(isStartingElement(next, ALTERNATIVE_COLLECTION_TYPE_STATUS)){
|
1371
|
handleNotYetImplementedElement(next);
|
1372
|
}else if(isStartingElement(next, SUB_COLLECTION)){
|
1373
|
handleNotYetImplementedElement(next);
|
1374
|
}else if(isStartingElement(next, LOCALITY)){
|
1375
|
handleLocality(state, reader, next, facade);
|
1376
|
}else if(isStartingElement(next, DATES)){
|
1377
|
handleNotYetImplementedElement(next);
|
1378
|
}else if(isStartingElement(next, NOTES)){
|
1379
|
handleNotYetImplementedElement(next);
|
1380
|
}else{
|
1381
|
handleUnexpectedStartElement(next);
|
1382
|
}
|
1383
|
}else{
|
1384
|
handleUnexpectedElement(next);
|
1385
|
}
|
1386
|
}
|
1387
|
//TODO handle missing end element
|
1388
|
throw new IllegalStateException("Collection has no closing tag");
|
1389
|
|
1390
|
}
|
1391
|
|
1392
|
|
1393
|
private void handleLocality(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, DerivedUnitFacade facade) throws XMLStreamException {
|
1394
|
String classValue = getClassOnlyAttribute(parentEvent);
|
1395
|
boolean isLocality = false;
|
1396
|
NamedAreaLevel areaLevel = null;
|
1397
|
if ("locality".equalsIgnoreCase(classValue)){
|
1398
|
isLocality = true;
|
1399
|
}else{
|
1400
|
try {
|
1401
|
areaLevel = state.getTransformer().getNamedAreaLevelByKey(classValue);
|
1402
|
} catch (UndefinedTransformerMethodException e) {
|
1403
|
//do nothing
|
1404
|
}
|
1405
|
if (areaLevel == null){
|
1406
|
String message = "Named area level '%s' not yet implemented.";
|
1407
|
message = String.format(message, classValue);
|
1408
|
fireWarningEvent(message, parentEvent, 6);
|
1409
|
}
|
1410
|
}
|
1411
|
|
1412
|
String text = "";
|
1413
|
//elements
|
1414
|
while (reader.hasNext()){
|
1415
|
XMLEvent next = readNoWhitespace(reader);
|
1416
|
if (next.isEndElement()){
|
1417
|
if (StringUtils.isNotBlank(text)){
|
1418
|
if (isMyEndingElement(next, parentEvent)){
|
1419
|
if (isLocality){
|
1420
|
facade.setLocality(text);
|
1421
|
}else{
|
1422
|
NamedArea area = createArea(text, areaLevel, state);
|
1423
|
facade.addCollectingArea(area);
|
1424
|
}
|
1425
|
}
|
1426
|
//TODO
|
1427
|
return;
|
1428
|
}else{
|
1429
|
if(isEndingElement(next, ALTITUDE)){
|
1430
|
//NOT YET IMPLEMENTED
|
1431
|
popUnimplemented(next.asEndElement());
|
1432
|
}else if(isEndingElement(next,COORDINATES)){
|
1433
|
//NOT YET IMPLEMENTED
|
1434
|
popUnimplemented(next.asEndElement());
|
1435
|
}else if(isEndingElement(next, ANNOTATION)){
|
1436
|
//NOT YET IMPLEMENTED
|
1437
|
popUnimplemented(next.asEndElement());
|
1438
|
}else{
|
1439
|
handleUnexpectedEndElement(next.asEndElement());
|
1440
|
}
|
1441
|
}
|
1442
|
}else if (next.isStartElement()){
|
1443
|
if(isStartingElement(next, ALTITUDE)){
|
1444
|
handleNotYetImplementedElement(next);
|
1445
|
// homotypicalGroup = handleNom(state, reader, next, taxon, homotypicalGroup);
|
1446
|
}else if(isStartingElement(next, COORDINATES)){
|
1447
|
handleNotYetImplementedElement(next);
|
1448
|
}else if(isStartingElement(next, ANNOTATION)){
|
1449
|
handleNotYetImplementedElement(next);
|
1450
|
}else{
|
1451
|
handleUnexpectedStartElement(next);
|
1452
|
}
|
1453
|
}else if (next.isCharacters()){
|
1454
|
text += next.asCharacters().getData();
|
1455
|
}else{
|
1456
|
handleUnexpectedElement(next);
|
1457
|
}
|
1458
|
}
|
1459
|
//TODO handle missing end element
|
1460
|
throw new IllegalStateException("Specimen type has no closing tag"); // TODO Auto-generated method stub
|
1461
|
|
1462
|
}
|
1463
|
|
1464
|
|
1465
|
private NamedArea createArea(String text, NamedAreaLevel areaLevel, MarkupImportState state) {
|
1466
|
NamedArea area = NamedArea.NewInstance(text, text, null);
|
1467
|
area.setLevel(areaLevel);
|
1468
|
save(area, state);
|
1469
|
return area;
|
1470
|
}
|
1471
|
|
1472
|
|
1473
|
private AgentBase<?> createCollector(String collectorStr) {
|
1474
|
return createAuthor(collectorStr);
|
1475
|
}
|
1476
|
|
1477
|
|
1478
|
private String getCData(MarkupImportState state, XMLEventReader reader, XMLEvent next) throws XMLStreamException {
|
1479
|
checkNoAttributes(next);
|
1480
|
|
1481
|
String text = "";
|
1482
|
while (reader.hasNext()){
|
1483
|
XMLEvent myNext = readNoWhitespace(reader);
|
1484
|
if(isMyEndingElement(myNext, next)){
|
1485
|
return text;
|
1486
|
}else if (myNext.isCharacters()){
|
1487
|
text += myNext.asCharacters().getData();
|
1488
|
}else {
|
1489
|
handleUnexpectedElement(myNext);
|
1490
|
}
|
1491
|
}
|
1492
|
throw new IllegalStateException("Event has no closing tag");
|
1493
|
|
1494
|
}
|
1495
|
|
1496
|
|
1497
|
/**
|
1498
|
* Creates the name defined by a nom tag. Adds it to the given homotypical group (if not null).
|
1499
|
* @param state
|
1500
|
* @param reader
|
1501
|
* @param parentEvent
|
1502
|
* @param homotypicalGroup
|
1503
|
* @return
|
1504
|
* @throws XMLStreamException
|
1505
|
*/
|
1506
|
private NonViralName<?> handleNom(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent,
|
1507
|
HomotypicalGroup homotypicalGroup) throws XMLStreamException {
|
1508
|
boolean isSynonym = false;
|
1509
|
boolean isNameType = state.isNameType();
|
1510
|
//attributes
|
1511
|
String classValue = getClassOnlyAttribute(parentEvent);
|
1512
|
NonViralName<?> name;
|
1513
|
if (! isNameType && ACCEPTED.equalsIgnoreCase(classValue)){
|
1514
|
isSynonym = false;
|
1515
|
name = createName(state, homotypicalGroup, isSynonym);
|
1516
|
}else if (! isNameType && SYNONYM.equalsIgnoreCase(classValue)){
|
1517
|
isSynonym = true;
|
1518
|
name = createName(state, homotypicalGroup, isSynonym);
|
1519
|
}else if (isNameType && NAME_TYPE.equalsIgnoreCase(classValue)){
|
1520
|
//TODO do we need to define the rank here?
|
1521
|
name = createNameByCode(state, null);
|
1522
|
}else{
|
1523
|
fireUnexpectedAttributeValue(parentEvent, CLASS, classValue);
|
1524
|
name = createNameByCode(state, null);
|
1525
|
}
|
1526
|
|
1527
|
Map<String, String> nameMap = new HashMap<String, String>();
|
1528
|
|
1529
|
while (reader.hasNext()){
|
1530
|
XMLEvent next = readNoWhitespace(reader);
|
1531
|
if (next.isEndElement()){
|
1532
|
if (isMyEndingElement(next, parentEvent)){
|
1533
|
//fill the name with all data gathered
|
1534
|
fillName(state, nameMap, name, next);
|
1535
|
return name;
|
1536
|
}else{
|
1537
|
if(isEndingElement(next, FULL_NAME)){
|
1538
|
//NOT YET IMPLEMENTED
|
1539
|
popUnimplemented(next.asEndElement());
|
1540
|
}else if(isEndingElement(next, NUM)){
|
1541
|
//NOT YET IMPLEMENTED
|
1542
|
popUnimplemented(next.asEndElement());
|
1543
|
}else if(isEndingElement(next, HOMONYM)){
|
1544
|
//NOT YET IMPLEMENTED
|
1545
|
popUnimplemented(next.asEndElement());
|
1546
|
}else if(isEndingElement(next, NOTES)){
|
1547
|
//NOT YET IMPLEMENTED
|
1548
|
popUnimplemented(next.asEndElement());
|
1549
|
}else if(isEndingElement(next, ANNOTATION)){
|
1550
|
//NOT YET IMPLEMENTED
|
1551
|
popUnimplemented(next.asEndElement());
|
1552
|
}else{
|
1553
|
handleUnexpectedEndElement(next.asEndElement());
|
1554
|
}
|
1555
|
}
|
1556
|
}else if (next.isStartElement()){
|
1557
|
if(isStartingElement(next, FULL_NAME)){
|
1558
|
handleNotYetImplementedElement(next);
|
1559
|
// homotypicalGroup = handleNom(state, reader, next, taxon, homotypicalGroup);
|
1560
|
}else if(isStartingElement(next, NUM)){
|
1561
|
handleNotYetImplementedElement(next);
|
1562
|
}else if(isStartingElement(next, NAME)){
|
1563
|
handleName(state, reader, next, nameMap);
|
1564
|
}else if(isStartingElement(next, CITATION)){
|
1565
|
handleCitation(state, reader, next, name);
|
1566
|
}else if(isStartingElement(next, HOMONYM)){
|
1567
|
handleNotYetImplementedElement(next);
|
1568
|
}else if(isStartingElement(next, NOTES)){
|
1569
|
handleNotYetImplementedElement(next);
|
1570
|
}else if(isStartingElement(next, ANNOTATION)){
|
1571
|
handleNotYetImplementedElement(next);
|
1572
|
}else{
|
1573
|
handleUnexpectedStartElement(next);
|
1574
|
}
|
1575
|
}else{
|
1576
|
handleUnexpectedElement(next);
|
1577
|
}
|
1578
|
}
|
1579
|
//TODO handle missing end element
|
1580
|
throw new IllegalStateException("Nom has no closing tag");
|
1581
|
|
1582
|
}
|
1583
|
|
1584
|
private void fillName(MarkupImportState state, Map<String, String> nameMap, NonViralName name, XMLEvent event) {
|
1585
|
|
1586
|
//Ranks: family, subfamily, tribus, genus, subgenus, section, subsection, species, subspecies, variety, subvariety, forma
|
1587
|
//infrank, paraut, author, infrparaut, infraut, status, notes
|
1588
|
|
1589
|
|
1590
|
String infrank = getAndRemoveMapKey(nameMap, INFRANK);
|
1591
|
String authorStr = getAndRemoveMapKey(nameMap, AUTHOR);
|
1592
|
String paraut = getAndRemoveMapKey(nameMap, PARAUT);
|
1593
|
|
1594
|
String infrParAut = getAndRemoveMapKey(nameMap, INFRPARAUT);
|
1595
|
String infrAut = getAndRemoveMapKey(nameMap, INFRAUT);
|
1596
|
|
1597
|
String statusStr = getAndRemoveMapKey(nameMap, STATUS);
|
1598
|
String notes = getAndRemoveMapKey(nameMap, NOTES);
|
1599
|
|
1600
|
makeRankDecision(state, nameMap, name, event, infrank);
|
1601
|
|
1602
|
//test consistency of rank and authors
|
1603
|
testRankAuthorConsistency(name, event, authorStr, paraut, infrParAut, infrAut);
|
1604
|
|
1605
|
//authors
|
1606
|
makeNomenclaturalAuthors(name, event, authorStr, paraut, infrParAut, infrAut);
|
1607
|
|
1608
|
//status
|
1609
|
//TODO handle pro parte, pro syn. etc.
|
1610
|
if (StringUtils.isNotBlank(statusStr)){
|
1611
|
String proPartePattern = "(pro parte|p.p.)";
|
1612
|
if (statusStr.matches(proPartePattern)){
|
1613
|
state.setProParte(true);
|
1614
|
}
|
1615
|
try {
|
1616
|
//TODO handle trim earlier
|
1617
|
statusStr = statusStr.trim();
|
1618
|
NomenclaturalStatusType nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusStr);
|
1619
|
name.addStatus(NomenclaturalStatus.NewInstance(nomStatusType));
|
1620
|
} catch (UnknownCdmTypeException e) {
|
1621
|
String message = "Status '%s' could not be recognized";
|
1622
|
message = String.format(message, statusStr);
|
1623
|
fireWarningEvent(message, event, 4);
|
1624
|
}
|
1625
|
}
|
1626
|
|
1627
|
//notes
|
1628
|
if (StringUtils.isNotBlank(notes)){
|
1629
|
handleNotYetImplementedAttributeValue(event, CLASS, NOTES);
|
1630
|
}
|
1631
|
|
1632
|
return;
|
1633
|
}
|
1634
|
|
1635
|
|
1636
|
/**
|
1637
|
* @param state
|
1638
|
* @param nameMap
|
1639
|
* @param name
|
1640
|
* @param event
|
1641
|
* @param infrank
|
1642
|
*/
|
1643
|
private void makeRankDecision(MarkupImportState state, Map<String, String> nameMap,
|
1644
|
NonViralName name, XMLEvent event, String infrank) {
|
1645
|
//TODO ranks
|
1646
|
for (String key : nameMap.keySet()){
|
1647
|
Rank rank = makeRank(state, key, false);
|
1648
|
if (rank == null){
|
1649
|
handleNotYetImplementedAttributeValue(event, CLASS, key);
|
1650
|
}else{
|
1651
|
if (name.getRank() == null || rank.isLower(name.getRank())){
|
1652
|
name.setRank(rank);
|
1653
|
}
|
1654
|
String value = nameMap.get(key);
|
1655
|
if (rank.isSupraGeneric() || rank.isGenus()){
|
1656
|
name.setGenusOrUninomial(value);
|
1657
|
}else if (rank.isInfraGeneric()){
|
1658
|
name.setInfraGenericEpithet(value);
|
1659
|
}else if (rank.isSpecies()){
|
1660
|
name.setSpecificEpithet(value);
|
1661
|
}else if (rank.isInfraSpecific()){
|
1662
|
name.setInfraSpecificEpithet(value);
|
1663
|
}else{
|
1664
|
String message = "Invalid rank '%s'. Can't decide which epithet to fill with '%s'";
|
1665
|
message = String.format(message, rank.getTitleCache(), value);
|
1666
|
fireWarningEvent(message, event, 4);
|
1667
|
}
|
1668
|
}
|
1669
|
|
1670
|
}
|
1671
|
if (StringUtils.isNotBlank(infrank)){
|
1672
|
Rank rank = makeRank(state, infrank, true);
|
1673
|
|
1674
|
if (rank == null){
|
1675
|
String message = "Infrank '%s' rank not recognized";
|
1676
|
message = String.format(message, infrank);
|
1677
|
fireWarningEvent(message, event, 4);
|
1678
|
}else{
|
1679
|
if (name.getRank() == null || rank.isLower(name.getRank())){
|
1680
|
name.setRank(rank);
|
1681
|
}else{
|
1682
|
String message = "InfRank '%s' is not lower than existing rank ";
|
1683
|
message = String.format(message, infrank);
|
1684
|
fireWarningEvent(message, event, 2);
|
1685
|
}
|
1686
|
}
|
1687
|
}
|
1688
|
}
|
1689
|
|
1690
|
|
1691
|
/**
|
1692
|
* @param name
|
1693
|
* @param event
|
1694
|
* @param authorStr
|
1695
|
* @param paraut
|
1696
|
* @param infrParAut
|
1697
|
* @param infrAut
|
1698
|
*/
|
1699
|
private void makeNomenclaturalAuthors(NonViralName name, XMLEvent event,
|
1700
|
String authorStr, String paraut, String infrParAut, String infrAut) {
|
1701
|
if (name.getRank().isInfraSpecific()){
|
1702
|
if(StringUtils.isNotBlank(infrAut)){
|
1703
|
INomenclaturalAuthor[] authorAndEx = authorAndEx(infrAut, event);
|
1704
|
name.setCombinationAuthorTeam(authorAndEx[0]);
|
1705
|
name.setExCombinationAuthorTeam(authorAndEx[1]);
|
1706
|
}
|
1707
|
if(StringUtils.isNotBlank(infrParAut)){
|
1708
|
INomenclaturalAuthor[] authorAndEx = authorAndEx(infrParAut, event);
|
1709
|
name.setBasionymAuthorTeam(authorAndEx[0]);
|
1710
|
name.setExBasionymAuthorTeam(authorAndEx[1]);
|
1711
|
}
|
1712
|
}else{
|
1713
|
if(StringUtils.isNotBlank(authorStr)){
|
1714
|
INomenclaturalAuthor[] authorAndEx = authorAndEx(authorStr, event);
|
1715
|
name.setCombinationAuthorTeam(authorAndEx[0]);
|
1716
|
name.setExCombinationAuthorTeam(authorAndEx[1]);
|
1717
|
}
|
1718
|
if(StringUtils.isNotBlank(paraut)){
|
1719
|
INomenclaturalAuthor[] authorAndEx = authorAndEx(paraut, event);
|
1720
|
name.setBasionymAuthorTeam(authorAndEx[0]);
|
1721
|
name.setExBasionymAuthorTeam(authorAndEx[1]);
|
1722
|
}
|
1723
|
}
|
1724
|
}
|
1725
|
|
1726
|
|
1727
|
private TeamOrPersonBase[] authorAndEx(String authorAndEx, XMLEvent xmlEvent) {
|
1728
|
authorAndEx = authorAndEx.trim();
|
1729
|
TeamOrPersonBase[] result = new TeamOrPersonBase[2];
|
1730
|
|
1731
|
String[] split = authorAndEx.split("\\sex\\s");
|
1732
|
if (split.length > 2){
|
1733
|
String message = "There is more then 1 ' ex ' in author string. Can't separate author and ex-author";
|
1734
|
fireWarningEvent(message, xmlEvent, 4);
|
1735
|
result[0] = createAuthor(authorAndEx);
|
1736
|
}else if (split.length ==2){
|
1737
|
result[0] = createAuthor(split[1]);
|
1738
|
result[1] = createAuthor(split[0]);
|
1739
|
}else{
|
1740
|
result[0] = createAuthor(split[0]);
|
1741
|
}
|
1742
|
return result;
|
1743
|
}
|
1744
|
|
1745
|
|
1746
|
/**
|
1747
|
* @param name
|
1748
|
* @param event
|
1749
|
* @param authorStr
|
1750
|
* @param paraut
|
1751
|
* @param infrParAut
|
1752
|
* @param infrAut
|
1753
|
*/
|
1754
|
private void testRankAuthorConsistency(NonViralName name, XMLEvent event,
|
1755
|
String authorStr, String paraut, String infrParAut, String infrAut) {
|
1756
|
if (name.getRank().isInfraSpecific()){
|
1757
|
if (StringUtils.isBlank(infrParAut) && StringUtils.isNotBlank(infrAut) &&
|
1758
|
(StringUtils.isNotBlank(paraut) || StringUtils.isNotBlank(authorStr))){
|
1759
|
String message = "Rank is infraspecicific but has only specific or higher author(s)";
|
1760
|
fireWarningEvent(message, event, 4);
|
1761
|
}
|
1762
|
}else{
|
1763
|
//is not infraspecific
|
1764
|
if (StringUtils.isNotBlank(infrParAut) || StringUtils.isNotBlank(infrAut)){
|
1765
|
String message = "Rank is not infraspecicific but name has infra author(s)";
|
1766
|
fireWarningEvent(message, event, 4);
|
1767
|
}
|
1768
|
}
|
1769
|
}
|
1770
|
|
1771
|
|
1772
|
/**
|
1773
|
* Returns the (empty) name with the correct homotypical group depending on the taxon status.
|
1774
|
* Throws NPE if no currentTaxon is set in state.
|
1775
|
* @param state
|
1776
|
* @param homotypicalGroup
|
1777
|
* @param isSynonym
|
1778
|
* @return
|
1779
|
*/
|
1780
|
private NonViralName<?> createName(MarkupImportState state,HomotypicalGroup homotypicalGroup, boolean isSynonym) {
|
1781
|
NonViralName<?> name;
|
1782
|
Taxon taxon = state.getCurrentTaxon();
|
1783
|
if (isSynonym){
|
1784
|
Rank defaultRank = Rank.SPECIES(); //can be any
|
1785
|
name = createNameByCode(state, defaultRank);
|
1786
|
name.setHomotypicalGroup(homotypicalGroup);
|
1787
|
SynonymRelationshipType synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
|
1788
|
if (taxon.getHomotypicGroup().equals(homotypicalGroup)){
|
1789
|
synonymType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
|
1790
|
}
|
1791
|
taxon.addSynonymName(name, synonymType);
|
1792
|
}else{
|
1793
|
name = CdmBase.deproxy(taxon.getName(), NonViralName.class);
|
1794
|
}
|
1795
|
return name;
|
1796
|
}
|
1797
|
|
1798
|
private void handleName(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Map<String, String> nameMap) throws XMLStreamException {
|
1799
|
String classValue = getClassOnlyAttribute(parentEvent);
|
1800
|
|
1801
|
String text = "";
|
1802
|
while (reader.hasNext()){
|
1803
|
XMLEvent next = readNoWhitespace(reader);
|
1804
|
if(isMyEndingElement(next, parentEvent)){
|
1805
|
nameMap.put(classValue, text);
|
1806
|
return;
|
1807
|
}else if (next.isStartElement()){
|
1808
|
if (isStartingElement(next, ANNOTATION)){
|
1809
|
handleNotYetImplementedElement(next);
|
1810
|
}else {
|
1811
|
handleUnexpectedStartElement(next.asStartElement());
|
1812
|
}
|
1813
|
} else if (next.isCharacters()){
|
1814
|
text += next.asCharacters().getData();
|
1815
|
}else {
|
1816
|
handleUnexpectedEndElement(next.asEndElement());
|
1817
|
}
|
1818
|
}
|
1819
|
throw new IllegalStateException("name has no closing tag");
|
1820
|
|
1821
|
}
|
1822
|
|
1823
|
|
1824
|
/**
|
1825
|
* @param state
|
1826
|
* @param classValue
|
1827
|
* @param byAbbrev
|
1828
|
* @return
|
1829
|
*/
|
1830
|
private Rank makeRank(MarkupImportState state, String value, boolean byAbbrev) {
|
1831
|
Rank rank = null;
|
1832
|
if (StringUtils.isBlank(value)){
|
1833
|
return null;
|
1834
|
}
|
1835
|
try {
|
1836
|
boolean useUnknown = true;
|
1837
|
NomenclaturalCode nc = makeNomenclaturalCode(state);
|
1838
|
if (byAbbrev){
|
1839
|
rank = Rank.getRankByAbbreviation(value, nc, useUnknown);
|
1840
|
}else{
|
1841
|
rank = Rank.getRankByEnglishName(value, nc, useUnknown);
|
1842
|
}
|
1843
|
if (rank.equals(Rank.UNKNOWN_RANK())){
|
1844
|
rank = null;
|
1845
|
}
|
1846
|
} catch (UnknownCdmTypeException e) {
|
1847
|
//doNothing
|
1848
|
}
|
1849
|
return rank;
|
1850
|
}
|
1851
|
|
1852
|
|
1853
|
// public void handleNameNotRank(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, String classValue, NonViralName name) throws XMLStreamException {
|
1854
|
// if (ACCEPTED.equalsIgnoreCase(classValue)){
|
1855
|
// }else if (SYNONYM.equalsIgnoreCase(classValue)){
|
1856
|
// }else{
|
1857
|
// //TODO Not yet implemented
|
1858
|
// handleNotYetImplementedAttributeValue(parentEvent, CLASS, classValue);
|
1859
|
// }
|
1860
|
// }
|
1861
|
|
1862
|
|
1863
|
private void handleCitation(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, NonViralName name) throws XMLStreamException {
|
1864
|
String classValue = getClassOnlyAttribute(parentEvent);
|
1865
|
|
1866
|
state.setCitation(true);
|
1867
|
boolean hasRefPart = false;
|
1868
|
Reference reference = ReferenceFactory.newGeneric();
|
1869
|
Map<String, String> refMap = new HashMap<String, String>();
|
1870
|
while (reader.hasNext()){
|
1871
|
XMLEvent next = readNoWhitespace(reader);
|
1872
|
if (isMyEndingElement(next, parentEvent)){
|
1873
|
checkMandatoryElement(hasRefPart, parentEvent.asStartElement(), REF_PART);
|
1874
|
reference = createReference(state, refMap, next);
|
1875
|
String microReference = refMap.get(DETAILS);
|
1876
|
doCitation(state, name, classValue, reference, microReference, parentEvent);
|
1877
|
state.setCitation(false);
|
1878
|
return;
|
1879
|
}else if(isStartingElement(next, REF_PART)){
|
1880
|
handleRefPart(state, reader, next, refMap);
|
1881
|
hasRefPart = true;
|
1882
|
}else{
|
1883
|
handleUnexpectedElement(next);
|
1884
|
}
|
1885
|
}
|
1886
|
throw new IllegalStateException("Citation has no closing tag");
|
1887
|
|
1888
|
|
1889
|
}
|
1890
|
|
1891
|
private void handleRefPart(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Map<String, String> refMap) throws XMLStreamException {
|
1892
|
String classValue = getClassOnlyAttribute(parentEvent);
|
1893
|
|
1894
|
String text = "";
|
1895
|
while (reader.hasNext()){
|
1896
|
XMLEvent next = readNoWhitespace(reader);
|
1897
|
if(isMyEndingElement(next, parentEvent)){
|
1898
|
refMap.put(classValue, text);
|
1899
|
return;
|
1900
|
}else if (next.isStartElement()){
|
1901
|
if (isStartingElement(next, ANNOTATION)){
|
1902
|
handleNotYetImplementedElement(next);
|
1903
|
}else if (isStartingElement(next, ITALICS)){
|
1904
|
handleNotYetImplementedElement(next);
|
1905
|
}else if (isStartingElement(next, BOLD)){
|
1906
|
handleNotYetImplementedElement(next);
|
1907
|
}else {
|
1908
|
handleUnexpectedStartElement(next.asStartElement());
|
1909
|
}
|
1910
|
} else if (next.isCharacters()){
|
1911
|
text += next.asCharacters().getData();
|
1912
|
}else {
|
1913
|
handleUnexpectedEndElement(next.asEndElement());
|
1914
|
}
|
1915
|
}
|
1916
|
throw new IllegalStateException("RefPart has no closing tag");
|
1917
|
|
1918
|
}
|
1919
|
|
1920
|
|
1921
|
private Reference createReference(MarkupImportState state, Map<String, String> refMap, XMLEvent parentEvent) {
|
1922
|
//TODO
|
1923
|
Reference reference;
|
1924
|
|
1925
|
String type = getAndRemoveMapKey(refMap, PUBTYPE);
|
1926
|
String authorStr = getAndRemoveMapKey(refMap, AUTHOR);
|
1927
|
String titleStr = getAndRemoveMapKey(refMap, PUBTITLE);
|
1928
|
String titleCache = getAndRemoveMapKey(refMap, PUBFULLNAME);
|
1929
|
String volume = getAndRemoveMapKey(refMap, VOLUME);
|
1930
|
String edition = getAndRemoveMapKey(refMap, EDITION);
|
1931
|
String editors = getAndRemoveMapKey(refMap, EDITORS);
|
1932
|
String year = getAndRemoveMapKey(refMap, YEAR);
|
1933
|
String pubName = getAndRemoveMapKey(refMap, PUBNAME);
|
1934
|
|
1935
|
|
1936
|
if (state.isCitation()){
|
1937
|
if (volume != null || "journal".equalsIgnoreCase(type)){
|
1938
|
IArticle article = ReferenceFactory.newArticle();
|
1939
|
if (pubName != null){
|
1940
|
IJournal journal = ReferenceFactory.newJournal();
|
1941
|
journal.setTitle(pubName);
|
1942
|
article.setInJournal(journal);
|
1943
|
}
|
1944
|
reference = (Reference)article;
|
1945
|
|
1946
|
}else{
|
1947
|
Reference bookOrPartOf = ReferenceFactory.newGeneric();
|
1948
|
reference = bookOrPartOf;
|
1949
|
}
|
1950
|
//TODO use existing author from name or before
|
1951
|
TeamOrPersonBase author = createAuthor(authorStr);
|
1952
|
reference.setAuthorTeam(author);
|
1953
|
|
1954
|
|
1955
|
}else{
|
1956
|
reference = ReferenceFactory.newGeneric();
|
1957
|
//TODO type
|
1958
|
TeamOrPersonBase author = createAuthor(authorStr);
|
1959
|
reference.setAuthorTeam(author);
|
1960
|
|
1961
|
reference.setTitle(titleStr);
|
1962
|
if (StringUtils.isNotBlank(titleCache)){
|
1963
|
reference.setTitleCache(titleCache, true);
|
1964
|
}
|
1965
|
reference.setEdition(edition);
|
1966
|
reference.setEditor(editors);
|
1967
|
|
1968
|
if (pubName != null){
|
1969
|
Reference inReference = ReferenceFactory.newGeneric();
|
1970
|
inReference.setTitle(pubName);
|
1971
|
reference.setInReference(inReference);
|
1972
|
}
|
1973
|
}
|
1974
|
reference.setVolume(volume);
|
1975
|
reference.setDatePublished(TimePeriod.parseString(year));
|
1976
|
|
1977
|
|
1978
|
//TODO
|
1979
|
// ALTERNATEPUBTITLE
|
1980
|
// ISSUE
|
1981
|
// NOTES
|
1982
|
for (String key : refMap.keySet()){
|
1983
|
if (! DETAILS.equalsIgnoreCase(key)){
|
1984
|
this.fireUnexpectedAttributeValue(parentEvent, CLASS, key);
|
1985
|
}
|
1986
|
}
|
1987
|
|
1988
|
|
1989
|
|
1990
|
return reference;
|
1991
|
}
|
1992
|
|
1993
|
private TeamOrPersonBase createAuthor(String authorTitle) {
|
1994
|
//TODO atomize and also use by name creation
|
1995
|
TeamOrPersonBase result = Team.NewTitledInstance(authorTitle, authorTitle);
|
1996
|
return result;
|
1997
|
}
|
1998
|
|
1999
|
|
2000
|
private String getAndRemoveMapKey(Map<String, String> map, String key){
|
2001
|
String result = map.get(key);
|
2002
|
map.remove(key);
|
2003
|
if (result != null){
|
2004
|
result = normalize(result);
|
2005
|
}
|
2006
|
return StringUtils.stripToNull(result);
|
2007
|
}
|
2008
|
|
2009
|
private void doCitation(MarkupImportState state, NonViralName name, String classValue, Reference reference, String microCitation, XMLEvent parentEvent) {
|
2010
|
if (PUBLICATION.equalsIgnoreCase(classValue)){
|
2011
|
name.setNomenclaturalReference(reference);
|
2012
|
name.setNomenclaturalMicroReference(microCitation);
|
2013
|
}else if (USAGE.equalsIgnoreCase(classValue)){
|
2014
|
Taxon taxon = state.getCurrentTaxon();
|
2015
|
TaxonDescription td = this.getTaxonDescription(taxon, state.getConfig().getSourceReference(), false, true);
|
2016
|
TextData citation = TextData.NewInstance(Feature.CITATION());
|
2017
|
//TODO name used in source
|
2018
|
citation.addSource(null, null, reference, microCitation);
|
2019
|
td.addElement(citation);
|
2020
|
}else if (TYPE.equalsIgnoreCase(classValue)){
|
2021
|
handleNotYetImplementedAttributeValue(parentEvent, CLASS, classValue);
|
2022
|
}else{
|
2023
|
//TODO Not yet implemented
|
2024
|
handleNotYetImplementedAttributeValue(parentEvent, CLASS, classValue);
|
2025
|
}
|
2026
|
}
|
2027
|
|
2028
|
|
2029
|
private void handleFeature(MarkupImportState state, XMLEventReader reader,XMLEvent parentEvent) throws XMLStreamException {
|
2030
|
String classValue = getClassOnlyAttribute(parentEvent);
|
2031
|
Feature feature = makeFeature(classValue, state, parentEvent);
|
2032
|
Taxon taxon = state.getCurrentTaxon();
|
2033
|
TaxonDescription taxonDescription = getTaxonDescription(taxon, state.getConfig().getSourceReference(), NO_IMAGE_GALLERY, CREATE_NEW);
|
2034
|
// TextData figureHolderTextData = null; //for use with one TextData for all figure only
|
2035
|
|
2036
|
boolean isDescription = feature.equals(Feature.DESCRIPTION());
|
2037
|
DescriptionElementBase lastDescriptionElement = null;
|
2038
|
while (reader.hasNext()){
|
2039
|
XMLEvent next = readNoWhitespace(reader);
|
2040
|
if (next.isEndElement()){
|
2041
|
if (isMyEndingElement(next, parentEvent)){
|
2042
|
return;
|
2043
|
}else{
|
2044
|
if(isEndingElement(next, WRITER)){
|
2045
|
//NOT YET IMPLEMENTED
|
2046
|
popUnimplemented(next.asEndElement());
|
2047
|
}else if(isEndingElement(next, DISTRIBUTION_LIST)){
|
2048
|
//NOT YET IMPLEMENTED
|
2049
|
popUnimplemented(next.asEndElement());
|
2050
|
}else if(isEndingElement(next, HABITAT_LIST)){
|
2051
|
//NOT YET IMPLEMENTED
|
2052
|
popUnimplemented(next.asEndElement());
|
2053
|
}else if(isEndingElement(next, REFERENCES)){
|
2054
|
//NOT YET IMPLEMENTED
|
2055
|
popUnimplemented(next.asEndElement());
|
2056
|
}else{
|
2057
|
handleUnexpectedEndElement(next.asEndElement());
|
2058
|
}
|
2059
|
}
|
2060
|
}else if (next.isStartElement()){
|
2061
|
if(isStartingElement(next, HEADING)){
|
2062
|
String heading = handleHeading(state, reader, next);
|
2063
|
if (StringUtils.isNotBlank(heading)){
|
2064
|
if (! heading.equalsIgnoreCase(classValue)){
|
2065
|
String message = "Feature heading '%s' differs from feature class '%s'";
|
2066
|
message = String.format(message, heading, classValue);
|
2067
|
fireWarningEvent(message, next, 1);
|
2068
|
}else{
|
2069
|
//do nothing
|
2070
|
}
|
2071
|
}
|
2072
|
}else if(isStartingElement(next, WRITER)){
|
2073
|
handleNotYetImplementedElement(next);
|
2074
|
}else if(isStartingElement(next, DISTRIBUTION_LIST)){
|
2075
|
handleNotYetImplementedElement(next);
|
2076
|
}else if(isStartingElement(next, HABITAT_LIST)){
|
2077
|
handleNotYetImplementedElement(next);
|
2078
|
}else if(isStartingElement(next, CHAR)){
|
2079
|
TextData textData = handleChar (state,reader, next);
|
2080
|
taxonDescription.addElement(textData);
|
2081
|
}else if(isStartingElement(next, STRING)){
|
2082
|
String string = handleString(state, reader, next);
|
2083
|
TextData textData = TextData.NewInstance(feature);
|
2084
|
textData.putText(Language.DEFAULT(), string);
|
2085
|
taxonDescription.addElement(textData);
|
2086
|
lastDescriptionElement = textData;
|
2087
|
}else if(isStartingElement(next, FIGURE_REF)){
|
2088
|
FigureDataHolder figureHolder = handleFigureRef(state, reader, next);
|
2089
|
Feature figureFeature = getFeature(state, MarkupTransformer.uuidFigures, "Figures", "Figures", "Fig.", null);
|
2090
|
if (isDescription){
|
2091
|
TextData figureHolderTextData = null;
|
2092
|
// if (figureHolderTextData == null){
|
2093
|
figureHolderTextData = TextData.NewInstance(figureFeature);
|
2094
|
if (StringUtils.isNotBlank(figureHolder.num)){
|
2095
|
String annotationText = "<num>" + figureHolder.num.trim() + "</num>";
|
2096
|
Annotation annotation = Annotation.NewInstance(annotationText, AnnotationType.TECHNICAL(), Language.DEFAULT());
|
2097
|
figureHolderTextData.addAnnotation(annotation);
|
2098
|
}
|
2099
|
if (StringUtils.isNotBlank(figureHolder.figurePart)){
|
2100
|
String annotationText = "<figurePart>" + figureHolder.figurePart.trim() + "</figurePart>";
|
2101
|
Annotation annotation = Annotation.NewInstance(annotationText, AnnotationType.EDITORIAL(), Language.DEFAULT());
|
2102
|
figureHolderTextData.addAnnotation(annotation);
|
2103
|
}
|
2104
|
// if (StringUtils.isNotBlank(figureText)){
|
2105
|
// figureHolderTextData.putText(Language.DEFAULT(), figureText);
|
2106
|
// }
|
2107
|
taxonDescription.addElement(figureHolderTextData);
|
2108
|
// }
|
2109
|
registerFigureDemand(state, figureHolderTextData, figureHolder.ref);
|
2110
|
}else{
|
2111
|
if (lastDescriptionElement == null){
|
2112
|
String message = "No description element created yet that can be referred by figure. Create new TextData instead";
|
2113
|
fireWarningEvent(message, next, 4);
|
2114
|
lastDescriptionElement = TextData.NewInstance(figureFeature);
|
2115
|
taxonDescription.addElement(lastDescriptionElement);
|
2116
|
}
|
2117
|
registerFigureDemand(state, lastDescriptionElement, figureHolder.ref);
|
2118
|
}
|
2119
|
}else if(isStartingElement(next, REFERENCES)){
|
2120
|
handleNotYetImplementedElement(next);
|
2121
|
}else{
|
2122
|
handleUnexpectedStartElement(next);
|
2123
|
}
|
2124
|
}else{
|
2125
|
handleUnexpectedElement(next);
|
2126
|
}
|
2127
|
}
|
2128
|
//TODO handle missing end element
|
2129
|
throw new IllegalStateException("Feature has no closing tag");
|
2130
|
}
|
2131
|
|
2132
|
|
2133
|
|
2134
|
|
2135
|
private String handleHeading(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
2136
|
checkNoAttributes(parentEvent);
|
2137
|
|
2138
|
String text = "";
|
2139
|
while (reader.hasNext()){
|
2140
|
XMLEvent next = readNoWhitespace(reader);
|
2141
|
if(isMyEndingElement(next, parentEvent)){
|
2142
|
return text;
|
2143
|
}else if (next.isStartElement()){
|
2144
|
if (isStartingElement(next, FOOTNOTE)){
|
2145
|
handleNotYetImplementedElement(next);
|
2146
|
}else {
|
2147
|
handleUnexpectedStartElement(next.asStartElement());
|
2148
|
}
|
2149
|
} else if (next.isCharacters()){
|
2150
|
text += next.asCharacters().getData();
|
2151
|
}else {
|
2152
|
handleUnexpectedEndElement(next.asEndElement());
|
2153
|
}
|
2154
|
}
|
2155
|
throw new IllegalStateException("<String> has no closing tag");
|
2156
|
|
2157
|
}
|
2158
|
|
2159
|
|
2160
|
private String handleString(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
2161
|
// String classValue = getClassOnlyAttribute(parentEvent);
|
2162
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
2163
|
checkNoAttributes(attributes, parentEvent);
|
2164
|
|
2165
|
boolean isTextMode = true;
|
2166
|
String text = "";
|
2167
|
while (reader.hasNext()){
|
2168
|
XMLEvent next = readNoWhitespace(reader);
|
2169
|
if(isMyEndingElement(next, parentEvent)){
|
2170
|
return text;
|
2171
|
}else if (next.isEndElement()){
|
2172
|
if (isEndingElement(next, SUB_HEADING)){
|
2173
|
popUnimplemented(next.asEndElement());
|
2174
|
}else if (isEndingElement(next, ANNOTATION)){
|
2175
|
popUnimplemented(next.asEndElement());
|
2176
|
}else if (isEndingElement(next, BR)){
|
2177
|
isTextMode = true;
|
2178
|
}else if (isHtml(next)){
|
2179
|
text += "</" + getHtmlTag(next) + ">";
|
2180
|
}else {
|
2181
|
handleUnexpectedEndElement(next.asEndElement());
|
2182
|
}
|
2183
|
}else if (next.isStartElement()){
|
2184
|
if (isStartingElement(next, SUB_HEADING)){
|
2185
|
handleNotYetImplementedElement(next);
|
2186
|
}else if (isStartingElement(next, ANNOTATION)){
|
2187
|
handleNotYetImplementedElement(next);
|
2188
|
}else if (isStartingElement(next, BR)){
|
2189
|
text += "<br/>";
|
2190
|
isTextMode = false;
|
2191
|
}else if (isHtml(next)){
|
2192
|
text += "<" + getHtmlTag(next) + ">";
|
2193
|
}else {
|
2194
|
handleUnexpectedStartElement(next.asStartElement());
|
2195
|
}
|
2196
|
} else if (next.isCharacters()){
|
2197
|
if (!isTextMode){
|
2198
|
String message = "String is not in text mode";
|
2199
|
fireWarningEvent(message, next, 6);
|
2200
|
}else{
|
2201
|
text += next.asCharacters().getData();
|
2202
|
}
|
2203
|
}else {
|
2204
|
handleUnexpectedEndElement(next.asEndElement());
|
2205
|
}
|
2206
|
}
|
2207
|
throw new IllegalStateException("<String> has no closing tag");
|
2208
|
|
2209
|
}
|
2210
|
|
2211
|
private String getHtmlTag(XMLEvent event) {
|
2212
|
if (event.isStartElement() ){
|
2213
|
String tag = event.asStartElement().getName().getLocalPart();
|
2214
|
return tag;
|
2215
|
}else if (event.isEndElement() ){
|
2216
|
String tag = event.asEndElement().getName().getLocalPart();
|
2217
|
return tag;
|
2218
|
}else{
|
2219
|
String message = "Only start or end elements are allowed as Html tags";
|
2220
|
throw new IllegalStateException(message);
|
2221
|
}
|
2222
|
}
|
2223
|
|
2224
|
protected static final List<String> htmlList = Arrays.asList(
|
2225
|
"sub", "sup", "ol", "ul", "i", "b", "table", "br");
|
2226
|
|
2227
|
|
2228
|
private boolean isHtml(XMLEvent event) {
|
2229
|
if (event.isStartElement() ){
|
2230
|
String tag = event.asStartElement().getName().getLocalPart();
|
2231
|
return htmlList.contains(tag);
|
2232
|
}else if (event.isEndElement() ){
|
2233
|
String tag = event.asEndElement().getName().getLocalPart();
|
2234
|
return htmlList.contains(tag);
|
2235
|
}else{
|
2236
|
return false;
|
2237
|
}
|
2238
|
|
2239
|
}
|
2240
|
|
2241
|
private TextData handleChar(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
|
2242
|
String classValue = getClassOnlyAttribute(parentEvent);
|
2243
|
Feature feature = makeFeature(classValue, state, parentEvent);
|
2244
|
|
2245
|
String text = "";
|
2246
|
while (reader.hasNext()){
|
2247
|
XMLEvent next = readNoWhitespace(reader);
|
2248
|
if(isMyEndingElement(next, parentEvent)){
|
2249
|
TextData textData = TextData.NewInstance(feature);
|
2250
|
textData.putText(Language.DEFAULT(), text);
|
2251
|
return textData;
|
2252
|
}else if (next.isStartElement()){
|
2253
|
if (isStartingElement(next, ANNOTATION)){
|
2254
|
handleNotYetImplementedElement(next);
|
2255
|
}else if (isStartingElement(next, ITALICS)){
|
2256
|
handleNotYetImplementedElement(next);
|
2257
|
}else if (isStartingElement(next, BOLD)){
|
2258
|
handleNotYetImplementedElement(next);
|
2259
|
}else {
|
2260
|
handleUnexpectedStartElement(next.asStartElement());
|
2261
|
}
|
2262
|
} else if (next.isCharacters()){
|
2263
|
text += next.asCharacters().getData();
|
2264
|
}else {
|
2265
|
handleUnexpectedEndElement(next.asEndElement());
|
2266
|
}
|
2267
|
}
|
2268
|
throw new IllegalStateException("RefPart has no closing tag");
|
2269
|
}
|
2270
|
|
2271
|
|
2272
|
/**
|
2273
|
* @param classValue
|
2274
|
* @param state
|
2275
|
* @param parentEvent
|
2276
|
* @return
|
2277
|
* @throws UndefinedTransformerMethodException
|
2278
|
*/
|
2279
|
private Feature makeFeature(String classValue, MarkupImportState state, XMLEvent parentEvent) {
|
2280
|
UUID uuid;
|
2281
|
try {
|
2282
|
Feature feature = state.getTransformer().getFeatureByKey(classValue);
|
2283
|
if (feature != null){
|
2284
|
return feature;
|
2285
|
}
|
2286
|
uuid = state.getTransformer().getFeatureUuid(classValue);
|
2287
|
if (uuid == null){
|
2288
|
logger.info("Uuid is null for " + classValue);
|
2289
|
}
|
2290
|
String featureText = StringUtils.capitalize(classValue);
|
2291
|
|
2292
|
//TODO eFlora vocabulary
|
2293
|
TermVocabulary<Feature> voc = null;
|
2294
|
feature = getFeature(state, uuid, featureText, featureText, classValue, voc);
|
2295
|
if (feature == null){
|
2296
|
throw new NullPointerException(classValue + " not recognized as a feature");
|
2297
|
}
|
2298
|
return feature;
|
2299
|
} catch (Exception e) {
|
2300
|
String message = "Could not create feature for %s: %s" ;
|
2301
|
message = String.format(message, classValue, e.getMessage());
|
2302
|
fireWarningEvent(message, parentEvent, 4);
|
2303
|
return Feature.UNKNOWN();
|
2304
|
}
|
2305
|
}
|
2306
|
|
2307
|
|
2308
|
|
2309
|
/**
|
2310
|
* This comes from the old version, needs to be checked on need
|
2311
|
* @param state
|
2312
|
*/
|
2313
|
private void doAllTheOldOtherStuff(MarkupImportState state) {
|
2314
|
state.putTree(null, null);
|
2315
|
if (unmatchedLeads == null){
|
2316
|
unmatchedLeads = UnmatchedLeads.NewInstance();
|
2317
|
}
|
2318
|
state.setUnmatchedLeads(unmatchedLeads);
|
2319
|
|
2320
|
// TransactionStatus tx = startTransaction();
|
2321
|
unmatchedLeads.saveToSession(getPolytomousKeyNodeService());
|
2322
|
|
2323
|
|
2324
|
//TODO generally do not store the reference object in the config
|
2325
|
Reference sourceReference = state.getConfig().getSourceReference();
|
2326
|
getReferenceService().saveOrUpdate(sourceReference);
|
2327
|
}
|
2328
|
|
2329
|
|
2330
|
/* (non-Javadoc)
|
2331
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
2332
|
*/
|
2333
|
protected boolean isIgnore(MarkupImportState state){
|
2334
|
return ! state.getConfig().isDoTaxa();
|
2335
|
}
|
2336
|
|
2337
|
|
2338
|
|
2339
|
|
2340
|
}
|