1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.markup;
|
11
|
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Map;
|
16
|
import java.util.Stack;
|
17
|
|
18
|
import javax.xml.namespace.QName;
|
19
|
import javax.xml.stream.Location;
|
20
|
import javax.xml.stream.XMLEventReader;
|
21
|
import javax.xml.stream.XMLStreamConstants;
|
22
|
import javax.xml.stream.XMLStreamException;
|
23
|
import javax.xml.stream.events.Attribute;
|
24
|
import javax.xml.stream.events.EndElement;
|
25
|
import javax.xml.stream.events.StartElement;
|
26
|
import javax.xml.stream.events.XMLEvent;
|
27
|
|
28
|
import org.apache.commons.lang.StringUtils;
|
29
|
import org.apache.log4j.Logger;
|
30
|
|
31
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
32
|
import eu.etaxonomy.cdm.io.common.XmlImportBase;
|
33
|
import eu.etaxonomy.cdm.io.common.events.IIoEvent;
|
34
|
import eu.etaxonomy.cdm.io.common.events.IoProblemEvent;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
37
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40
|
|
41
|
/**
|
42
|
* @author a.mueller
|
43
|
* @created 04.08.2008
|
44
|
* @version 1.0
|
45
|
*/
|
46
|
public abstract class MarkupImportBase extends XmlImportBase<MarkupImportConfigurator, MarkupImportState> {
|
47
|
@SuppressWarnings("unused")
|
48
|
private static final Logger logger = Logger.getLogger(MarkupImportBase.class);
|
49
|
|
50
|
protected static final String CLASS = "class";
|
51
|
|
52
|
|
53
|
protected abstract void doInvoke(MarkupImportState state);
|
54
|
|
55
|
private Stack<QName> unhandledElements = new Stack<QName>();
|
56
|
private Stack<QName> handledElements = new Stack<QName>();
|
57
|
|
58
|
|
59
|
protected <T extends CdmBase> void save(Collection<T> collection, MarkupImportState state) {
|
60
|
if (state.isCheck() || collection.isEmpty()){
|
61
|
return;
|
62
|
}
|
63
|
T example = collection.iterator().next();
|
64
|
if (example.isInstanceOf(TaxonBase.class)){
|
65
|
Collection<TaxonBase> typedCollection = (Collection<TaxonBase>)collection;
|
66
|
getTaxonService().saveOrUpdate(typedCollection);
|
67
|
}else if (example.isInstanceOf(Classification.class)){
|
68
|
Collection<Classification> typedCollection = (Collection<Classification>)collection;
|
69
|
getClassificationService().saveOrUpdate(typedCollection);
|
70
|
}else if (example.isInstanceOf(PolytomousKey.class)){
|
71
|
Collection<PolytomousKey> typedCollection = (Collection<PolytomousKey>)collection;
|
72
|
getPolytomousKeyService().saveOrUpdate(typedCollection);
|
73
|
}else if (example.isInstanceOf(DefinedTermBase.class)){
|
74
|
Collection<DefinedTermBase> typedCollection = (Collection<DefinedTermBase>)collection;
|
75
|
getTermService().saveOrUpdate(typedCollection);
|
76
|
}
|
77
|
|
78
|
}
|
79
|
|
80
|
protected void save(CdmBase cdmBase, MarkupImportState state) {
|
81
|
if (state.isCheck()){
|
82
|
return;
|
83
|
}
|
84
|
if (cdmBase.isInstanceOf(TaxonBase.class)){
|
85
|
TaxonBase<?> taxonBase = CdmBase.deproxy(cdmBase, TaxonBase.class);
|
86
|
getTaxonService().saveOrUpdate(taxonBase);
|
87
|
}else if (cdmBase.isInstanceOf(Classification.class)){
|
88
|
Classification classification = CdmBase.deproxy(cdmBase, Classification.class);
|
89
|
getClassificationService().saveOrUpdate(classification);
|
90
|
}else if (cdmBase.isInstanceOf(PolytomousKey.class)){
|
91
|
PolytomousKey key = CdmBase.deproxy(cdmBase, PolytomousKey.class);
|
92
|
getPolytomousKeyService().saveOrUpdate(key);
|
93
|
}else if (cdmBase.isInstanceOf(DefinedTermBase.class)){
|
94
|
DefinedTermBase<?> term = CdmBase.deproxy(cdmBase, DefinedTermBase.class);
|
95
|
getTermService().saveOrUpdate(term);
|
96
|
}
|
97
|
//logger.warn("Saved " + cdmBase);
|
98
|
}
|
99
|
|
100
|
//*********************** Attribute methods *************************************/
|
101
|
|
102
|
/**
|
103
|
* Returns a map for all attributes of an start element
|
104
|
* @param event
|
105
|
* @return
|
106
|
*/
|
107
|
protected Map<String, Attribute> getAttributes(XMLEvent event) {
|
108
|
Map<String, Attribute> result = new HashMap<String, Attribute>();
|
109
|
if (!event.isStartElement()){
|
110
|
fireWarningEvent("Event is not an startElement. Can't check attributes", makeLocationStr(event.getLocation()), 1, 1);
|
111
|
return result;
|
112
|
}
|
113
|
StartElement element = event.asStartElement();
|
114
|
Iterator<Attribute> attributes = element.getAttributes();
|
115
|
while (attributes.hasNext()){
|
116
|
Attribute attribute = attributes.next();
|
117
|
//TODO namespaces
|
118
|
result.put(attribute.getName().getLocalPart(), attribute);
|
119
|
}
|
120
|
return result;
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Throws an unexpected attributes event if the event has any attributes.
|
125
|
* @param event
|
126
|
*/
|
127
|
protected void checkNoAttributes(Map<String, Attribute> attributes, XMLEvent event) {
|
128
|
String[] exceptions = new String[]{};
|
129
|
handleUnexpectedAttributes(event.getLocation(), attributes, 1, exceptions);
|
130
|
}
|
131
|
|
132
|
|
133
|
|
134
|
/**
|
135
|
* Throws an unexpected attributes event if the event has any attributes.
|
136
|
* @param event
|
137
|
*/
|
138
|
protected void checkNoAttributes(XMLEvent event) {
|
139
|
String[] exceptions = new String[]{};
|
140
|
checkNoAttributes(event, 1, exceptions);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Throws an unexpected attributes event if the event has any attributes except those mentioned in "exceptions".
|
145
|
* @param event
|
146
|
* @param exceptions
|
147
|
*/
|
148
|
protected void checkNoAttributes(XMLEvent event, int stackDepth, String... exceptions) {
|
149
|
if (! event.isStartElement()){
|
150
|
fireWarningEvent("Event is not an startElement. Can't check attributes", makeLocationStr(event.getLocation()), 1, 1);
|
151
|
return;
|
152
|
}
|
153
|
StartElement startElement = event.asStartElement();
|
154
|
Map<String, Attribute> attributes = getAttributes(startElement);
|
155
|
handleUnexpectedAttributes(startElement.getLocation(), attributes, stackDepth+1, exceptions);
|
156
|
}
|
157
|
|
158
|
|
159
|
/**
|
160
|
* Checks if the given attribute exists and has the given value.
|
161
|
* If yes, true is returned and the attribute is removed from the attributes map.
|
162
|
* Otherwise false is returned.
|
163
|
* @param attributes
|
164
|
* @param attrName
|
165
|
* @param value
|
166
|
* @return <code>true</code> if attribute has given value, <code>false</code> otherwise
|
167
|
*/
|
168
|
protected boolean checkAndRemoveAttributeValue( Map<String, Attribute> attributes, String attrName, String value) {
|
169
|
Attribute attr = attributes.get(attrName);
|
170
|
if (attr == null ||value == null ){
|
171
|
return false;
|
172
|
}else{
|
173
|
if (value.equals(attr.getValue())){
|
174
|
attributes.remove(attrName);
|
175
|
return true;
|
176
|
}else{
|
177
|
return false;
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
|
182
|
|
183
|
/**
|
184
|
* Returns the value of a given attribute name and returns the attribute from the attributes map.
|
185
|
* @param attributes
|
186
|
* @param attrName
|
187
|
* @return
|
188
|
*/
|
189
|
protected String getAndRemoveAttributeValue(Map<String, Attribute> attributes, String attrName) {
|
190
|
return getAndRemoveAttributeValue(null, attributes, attrName, false, 1);
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* Returns the value of a given attribute name and returns the attribute from the attributes map.
|
195
|
* Fires a mandatory field is missing event if the attribute does not exist.
|
196
|
* @param xmlEvent
|
197
|
* @param attributes
|
198
|
* @param attrName
|
199
|
* @return
|
200
|
*/
|
201
|
protected String getAndRemoveRequiredAttributeValue(XMLEvent xmlEvent, Map<String, Attribute> attributes, String attrName) {
|
202
|
return getAndRemoveAttributeValue(xmlEvent, attributes, attrName, true, 1);
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Returns the value of a given attribute name and returns the attribute from the attributes map.
|
207
|
* If required is <code>true</code> and the attribute does not exist a mandatory field is missing event is fired.
|
208
|
* @param xmlEvent
|
209
|
* @param attributes
|
210
|
* @param attrName
|
211
|
* @param isRequired
|
212
|
* @return
|
213
|
*/
|
214
|
private String getAndRemoveAttributeValue(XMLEvent xmlEvent, Map<String, Attribute> attributes, String attrName, boolean isRequired, int stackDepth) {
|
215
|
Attribute attr = attributes.get(attrName);
|
216
|
if (attr == null ){
|
217
|
if (isRequired){
|
218
|
fireMandatoryElementIsMissing(xmlEvent, attrName, 8, stackDepth+1);
|
219
|
}
|
220
|
return null;
|
221
|
}else{
|
222
|
attributes.remove(attrName);
|
223
|
return attr.getValue();
|
224
|
}
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Fires an not yet implemented event if the given attribute exists in attributes.
|
229
|
* @param attributes
|
230
|
* @param attrName
|
231
|
*/
|
232
|
protected void handleNotYetImplementedAttribute(Map<String, Attribute> attributes, String attrName) {
|
233
|
Attribute attr = attributes.get(attrName);
|
234
|
if (attr != null){
|
235
|
attributes.remove(attrName);
|
236
|
QName qName = attr.getName();
|
237
|
fireNotYetImplementedAttribute(attr.getLocation(), qName, 1);
|
238
|
}
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Fires an unhandled attributes event, if attributes exist in attributes map not covered by the exceptions.
|
243
|
* No event is fired if the unhandled elements stack is not empty.
|
244
|
* @param location
|
245
|
* @param attributes
|
246
|
* @param exceptions
|
247
|
*/
|
248
|
protected void handleUnexpectedAttributes(Location location,Map<String, Attribute> attributes, String... exceptions) {
|
249
|
handleUnexpectedAttributes(location, attributes, 1, exceptions);
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* see {@link #handleUnexpectedAttributes(Location, Map, String...)}
|
254
|
*
|
255
|
* @param location
|
256
|
* @param attributes
|
257
|
* @param stackDepth the stack trace depth
|
258
|
* @param exceptions
|
259
|
*/
|
260
|
private void handleUnexpectedAttributes(Location location,Map<String, Attribute> attributes, int stackDepth, String... exceptions) {
|
261
|
if (attributes.size() > 0){
|
262
|
if (this.unhandledElements.size() == 0 ){
|
263
|
boolean hasUnhandledAttributes = false;
|
264
|
for (String key : attributes.keySet()){
|
265
|
boolean isException = false;
|
266
|
for (String exception : exceptions){
|
267
|
if(key.equals(exception)){
|
268
|
isException = true;
|
269
|
}
|
270
|
}
|
271
|
if (!isException){
|
272
|
hasUnhandledAttributes = true;
|
273
|
}
|
274
|
}
|
275
|
if (hasUnhandledAttributes){
|
276
|
fireUnexpectedAttributes(location, attributes, stackDepth+1);
|
277
|
}
|
278
|
}
|
279
|
}
|
280
|
}
|
281
|
|
282
|
|
283
|
private void fireUnexpectedAttributes(Location location, Map<String, Attribute> attributes, int stackDepth) {
|
284
|
String attributesString = "";
|
285
|
for (String key : attributes.keySet()){
|
286
|
Attribute attribute = attributes.get(key);
|
287
|
attributesString = CdmUtils.concat(",", attributesString, attribute.getName().getLocalPart() + ":" + attribute.getValue());
|
288
|
}
|
289
|
String message = "Unexpected attributes: %s";
|
290
|
IoProblemEvent event = makeProblemEvent(location, String.format(message, attributesString), 1 , stackDepth +1 );
|
291
|
fire(event);
|
292
|
}
|
293
|
|
294
|
|
295
|
protected void fireUnexpectedAttributeValue(XMLEvent parentEvent, String attrName, String attrValue) {
|
296
|
String message = "Unexpected attribute value %s='%s'";
|
297
|
message = String.format(message, attrName, attrValue);
|
298
|
IoProblemEvent event = makeProblemEvent(parentEvent.getLocation(), message, 1 , 1 );
|
299
|
fire(event);
|
300
|
}
|
301
|
|
302
|
protected void handleNotYetImplementedAttributeValue(XMLEvent xmlEvent, String attrName, String attrValue) {
|
303
|
String message = "Attribute %s not yet implemented vor value '%s'";
|
304
|
message = String.format(message, attrName, attrValue);
|
305
|
IIoEvent event = makeProblemEvent(xmlEvent.getLocation(), message, 1, 1 );
|
306
|
fire(event);
|
307
|
}
|
308
|
|
309
|
protected void fireNotYetImplementedAttribute(Location location, QName qName, int stackDepth) {
|
310
|
String message = "Attribute not yet implemented: %s";
|
311
|
IIoEvent event = makeProblemEvent(location, String.format(message, qName.getLocalPart()), 1, stackDepth+1 );
|
312
|
fire(event);
|
313
|
}
|
314
|
|
315
|
|
316
|
|
317
|
|
318
|
protected void fireUnexpectedEvent(XMLEvent xmlEvent, int stackDepth) {
|
319
|
Location location = xmlEvent.getLocation();
|
320
|
String message = "Unexpected event: %s";
|
321
|
IIoEvent event = makeProblemEvent(location, String.format(message, xmlEvent.toString()), 2, stackDepth +1);
|
322
|
fire(event);
|
323
|
}
|
324
|
|
325
|
protected void fireUnexpectedStartElement(Location location, StartElement startElement, int stackDepth) {
|
326
|
QName qName = startElement.getName();
|
327
|
String message = "Unexpected start element: %s";
|
328
|
IIoEvent event = makeProblemEvent(location, String.format(message, qName.getLocalPart()), 2, stackDepth +1);
|
329
|
fire(event);
|
330
|
}
|
331
|
|
332
|
|
333
|
protected void fireUnexpectedEndElement(Location location, EndElement endElement, int stackDepth) {
|
334
|
QName qName = endElement.getName();
|
335
|
String message = "Unexpected end element: %s";
|
336
|
IIoEvent event = makeProblemEvent(location, String.format(message, qName.getLocalPart()), 16, stackDepth+1);
|
337
|
fire(event);
|
338
|
}
|
339
|
|
340
|
|
341
|
protected void fireNotYetImplementedElement(Location location, QName qName, int stackDepth) {
|
342
|
String message = "Element not yet implemented: %s";
|
343
|
IIoEvent event = makeProblemEvent(location, String.format(message, qName.getLocalPart()), 1, stackDepth+1 );
|
344
|
fire(event);
|
345
|
}
|
346
|
|
347
|
/**
|
348
|
* Creates a problem event.
|
349
|
* Be aware of the right depths of the stack trace !
|
350
|
* @param location
|
351
|
* @param message
|
352
|
* @param severity
|
353
|
* @return
|
354
|
*/
|
355
|
private IoProblemEvent makeProblemEvent(Location location, String message, int severity, int stackDepth) {
|
356
|
stackDepth++;
|
357
|
StackTraceElement[] stackTrace = new Exception().getStackTrace();
|
358
|
int lineNumber = stackTrace[stackDepth].getLineNumber();
|
359
|
String methodName = stackTrace[stackDepth].getMethodName();
|
360
|
String locationStr = makeLocationStr(location);
|
361
|
IoProblemEvent event = IoProblemEvent.NewInstance(this.getClass(), message,
|
362
|
locationStr, lineNumber, severity, methodName);
|
363
|
return event;
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
* Creates a string from a location
|
368
|
* @param location
|
369
|
* @return
|
370
|
*/
|
371
|
protected String makeLocationStr(Location location) {
|
372
|
String locationStr = location == null ? " - no location - " : "l." + location.getLineNumber() + "/c."+ location.getColumnNumber();
|
373
|
return locationStr;
|
374
|
}
|
375
|
|
376
|
|
377
|
/**
|
378
|
* Fires an unexpected element event if the unhandled elements stack is empty.
|
379
|
* Otherwise adds the element to the stack.
|
380
|
* @param event
|
381
|
*/
|
382
|
protected void handleUnexpectedStartElement(XMLEvent event) {
|
383
|
QName qName = event.asStartElement().getName();
|
384
|
if (! unhandledElements.empty()){
|
385
|
unhandledElements.push(qName);
|
386
|
}else{
|
387
|
fireUnexpectedStartElement(event.getLocation(), event.asStartElement(), 1);
|
388
|
}
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* Fires an unexpected element event if the event is not the last on the stack.
|
393
|
* Otherwise removes last stack element.
|
394
|
* @param event
|
395
|
*/
|
396
|
protected void handleUnexpectedEndElement(EndElement event) {
|
397
|
QName qName = event.asEndElement().getName();
|
398
|
if (!unhandledElements.isEmpty() && unhandledElements.peek().equals(qName)){
|
399
|
unhandledElements.pop();
|
400
|
}else{
|
401
|
fireUnexpectedEndElement(event.getLocation(), event.asEndElement(), 1);
|
402
|
}
|
403
|
}
|
404
|
|
405
|
/**
|
406
|
*
|
407
|
* @param endElement
|
408
|
*/
|
409
|
protected void popUnimplemented(EndElement endElement) {
|
410
|
QName qName = endElement.asEndElement().getName();
|
411
|
if (unhandledElements.peek().equals(qName)){
|
412
|
unhandledElements.pop();
|
413
|
}else{
|
414
|
String message = "End element is not last on stack: %s";
|
415
|
message = String.format(message, qName.getLocalPart());
|
416
|
IIoEvent event = makeProblemEvent(endElement.getLocation(), message, 16, 1);
|
417
|
fire(event);
|
418
|
}
|
419
|
|
420
|
}
|
421
|
|
422
|
|
423
|
/**
|
424
|
* Fires an unexpected element event if the unhandled element stack is empty.
|
425
|
* @param event
|
426
|
*/
|
427
|
protected void handleUnexpectedElement(XMLEvent event) {
|
428
|
if (event.isStartElement()){
|
429
|
handleUnexpectedStartElement(event);
|
430
|
}else if (event.isEndElement()){
|
431
|
handleUnexpectedEndElement(event.asEndElement());
|
432
|
}else if (event.getEventType() == XMLStreamConstants.COMMENT){
|
433
|
//do nothing
|
434
|
}else if (! unhandledElements.empty()){
|
435
|
//do nothing
|
436
|
}else{
|
437
|
fireUnexpectedEvent(event, 1);
|
438
|
}
|
439
|
}
|
440
|
|
441
|
/**
|
442
|
* Fires an not yet implemented event and adds the element name to the unhandled elements stack.
|
443
|
* @param event
|
444
|
*/
|
445
|
protected void handleNotYetImplementedElement(XMLEvent event) {
|
446
|
QName qName = event.asStartElement().getName();
|
447
|
boolean isTopLevel = unhandledElements.size() == 0;
|
448
|
unhandledElements.push(qName);
|
449
|
if (isTopLevel){
|
450
|
fireNotYetImplementedElement(event.getLocation(), qName, 1);
|
451
|
}
|
452
|
}
|
453
|
|
454
|
/**
|
455
|
* Checks if a mandatory text is not empty or null.
|
456
|
* Returns true if text is given.
|
457
|
* Fires an mandatory element is missing event otherwise and returns <code>null</code>.
|
458
|
* @param text
|
459
|
* @param parentEvent
|
460
|
* @return
|
461
|
*/
|
462
|
protected boolean checkMandatoryText(String text, XMLEvent parentEvent) {
|
463
|
if (! StringUtils.isNotBlank(text)){
|
464
|
fireMandatoryElementIsMissing(parentEvent, "CData", 4, 1);
|
465
|
return false;
|
466
|
}
|
467
|
return true;
|
468
|
}
|
469
|
|
470
|
/**
|
471
|
* Fires an mandatory element is missing event if exists is <code>false</code>.
|
472
|
* @param hasMandatory
|
473
|
* @param parentEvent
|
474
|
* @param string
|
475
|
*/
|
476
|
protected void checkMandatoryElement(boolean exists, StartElement parentEvent, String attrName) {
|
477
|
if (! exists){
|
478
|
fireMandatoryElementIsMissing(parentEvent, attrName, 5, 1);
|
479
|
}
|
480
|
}
|
481
|
|
482
|
|
483
|
/**
|
484
|
* Fires an element is missing event.
|
485
|
* @param xmlEvent
|
486
|
* @param string
|
487
|
* @param severity
|
488
|
* @param stackDepth
|
489
|
* @throws IllegalStateException if xmlEvent is not a StartElement and not an Attribute
|
490
|
*/
|
491
|
private void fireMandatoryElementIsMissing(XMLEvent xmlEvent, String missingEventName, int severity, int stackDepth) throws IllegalStateException{
|
492
|
Location location = xmlEvent.getLocation();
|
493
|
String typeName;
|
494
|
QName qName;
|
495
|
if (xmlEvent.isAttribute()){
|
496
|
Attribute attribute = ((Attribute)xmlEvent);
|
497
|
typeName = "attribute";
|
498
|
qName = attribute.getName();
|
499
|
}else if (xmlEvent.isStartElement()){
|
500
|
typeName = "element";
|
501
|
qName = xmlEvent.asStartElement().getName();
|
502
|
}else{
|
503
|
throw new IllegalStateException("mandatory element only allowed for attributes and start tags in " + makeLocationStr(location));
|
504
|
}
|
505
|
String message = "Mandatory %s '%s' is missing in %s";
|
506
|
message = String.format(message, typeName , missingEventName, qName.getLocalPart());
|
507
|
IIoEvent event = makeProblemEvent(location, message, severity, stackDepth +1);
|
508
|
fire(event);
|
509
|
}
|
510
|
|
511
|
|
512
|
|
513
|
|
514
|
/**
|
515
|
* Returns true if the "next" event is the ending tag for the "parent" event.
|
516
|
* @param next end element to test, must not be null
|
517
|
* @param parentEvent start element to test
|
518
|
* @return true if the "next" event is the ending tag for the "parent" event.
|
519
|
* @throws XMLStreamException
|
520
|
*/
|
521
|
protected boolean isMyEndingElement(XMLEvent next, XMLEvent parentEvent) throws XMLStreamException {
|
522
|
if (! parentEvent.isStartElement()){
|
523
|
String message = "Parent event should be start tag";
|
524
|
fireWarningEvent(message, makeLocationStr(next.getLocation()), 6);
|
525
|
return false;
|
526
|
}
|
527
|
return isEndingElement(next, parentEvent.asStartElement().getName().getLocalPart());
|
528
|
}
|
529
|
|
530
|
|
531
|
/**
|
532
|
* Trims the text and removes turns all whitespaces into single empty space.
|
533
|
* @param text
|
534
|
* @return
|
535
|
*/
|
536
|
protected String normalize(String text) {
|
537
|
text = StringUtils.trimToEmpty(text);
|
538
|
text = text.replaceAll("\\s+", " ");
|
539
|
return text;
|
540
|
}
|
541
|
|
542
|
|
543
|
/**
|
544
|
* Read next event. Ignore whitespace events.
|
545
|
* @param reader
|
546
|
* @return
|
547
|
* @throws XMLStreamException
|
548
|
*/
|
549
|
protected XMLEvent readNoWhitespace(XMLEventReader reader) throws XMLStreamException {
|
550
|
XMLEvent event = reader.nextEvent();
|
551
|
while (event.isCharacters() && event.asCharacters().isWhiteSpace()){
|
552
|
event = reader.nextEvent();
|
553
|
}
|
554
|
return event;
|
555
|
}
|
556
|
|
557
|
/**
|
558
|
* Returns the REQUIRED "class" attribute for a given event and checks that it is the only attribute.
|
559
|
* @param parentEvent
|
560
|
* @return
|
561
|
*/
|
562
|
protected String getClassOnlyAttribute(XMLEvent parentEvent) {
|
563
|
return getClassOnlyAttribute(parentEvent, true);
|
564
|
}
|
565
|
|
566
|
|
567
|
/**
|
568
|
* Returns the "class" attribute for a given event and checks that it is the only attribute.
|
569
|
* @param parentEvent
|
570
|
* @return
|
571
|
*/
|
572
|
protected String getClassOnlyAttribute(XMLEvent parentEvent, boolean required) {
|
573
|
return getOnlyAttribute(parentEvent, CLASS, required);
|
574
|
}
|
575
|
|
576
|
/**
|
577
|
* Returns the value for the only attribute for a given event and checks that it is the only attribute.
|
578
|
* @param parentEvent
|
579
|
* @return
|
580
|
*/
|
581
|
protected String getOnlyAttribute(XMLEvent parentEvent, String attrName, boolean required) {
|
582
|
Map<String, Attribute> attributes = getAttributes(parentEvent);
|
583
|
String classValue =getAndRemoveAttributeValue(parentEvent, attributes, attrName, required, 1);
|
584
|
checkNoAttributes(attributes, parentEvent);
|
585
|
return classValue;
|
586
|
}
|
587
|
|
588
|
protected void fireWarningEvent(String message, XMLEvent event, Integer severity) {
|
589
|
fireWarningEvent(message, makeLocationStr(event.getLocation()), severity, 1);
|
590
|
}
|
591
|
|
592
|
//********************************************** OLD *************************************
|
593
|
|
594
|
// protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
|
595
|
// boolean result = true;
|
596
|
// List<Element> list = parentElement.getChildren();
|
597
|
// for (Element element : list){
|
598
|
// if (! excludeList.contains(element.getName())){
|
599
|
// logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
|
600
|
// result = false;
|
601
|
// }
|
602
|
// }
|
603
|
// return result;
|
604
|
// }
|
605
|
//
|
606
|
//
|
607
|
// protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
|
608
|
// T result = null;
|
609
|
// String linkType = element.getAttributeValue("linkType");
|
610
|
// String ref = element.getAttributeValue("ref");
|
611
|
// if(ref == null && linkType == null){
|
612
|
// result = getInstance(clazz);
|
613
|
// if (result != null){
|
614
|
// String title = element.getTextNormalize();
|
615
|
// result.setTitleCache(title, true);
|
616
|
// }
|
617
|
// }else if (linkType == null || linkType.equals("local")){
|
618
|
// //TODO
|
619
|
// result = objectMap.get(ref);
|
620
|
// if (result == null){
|
621
|
// logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
|
622
|
// }
|
623
|
// }else if(linkType.equals("external")){
|
624
|
// logger.warn("External link types not yet implemented");
|
625
|
// }else if(linkType.equals("other")){
|
626
|
// logger.warn("Other link types not yet implemented");
|
627
|
// }else{
|
628
|
// logger.warn("Unknown link type or missing ref");
|
629
|
// }
|
630
|
// if (result == null){
|
631
|
// success.setValue(false);
|
632
|
// }
|
633
|
// return result;
|
634
|
// }
|
635
|
//
|
636
|
//
|
637
|
// protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
638
|
// Reference result = null;
|
639
|
// if (elAccordingTo != null){
|
640
|
// String childName = "AccordingToDetailed";
|
641
|
// boolean obligatory = false;
|
642
|
// Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
643
|
//
|
644
|
// childName = "Simple";
|
645
|
// obligatory = true;
|
646
|
// Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
647
|
//
|
648
|
// if (elAccordingToDetailed != null){
|
649
|
// result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
|
650
|
// }else{
|
651
|
// result = ReferenceFactory.newGeneric();
|
652
|
// String title = elSimple.getTextNormalize();
|
653
|
// result.setTitleCache(title, true);
|
654
|
// }
|
655
|
// }
|
656
|
// return result;
|
657
|
// }
|
658
|
//
|
659
|
//
|
660
|
// private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
661
|
// Reference result = null;
|
662
|
// Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
|
663
|
// if (elAccordingToDetailed != null){
|
664
|
// //AuthorTeam
|
665
|
// String childName = "AuthorTeam";
|
666
|
// boolean obligatory = false;
|
667
|
// Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
668
|
// makeAccordingToAuthorTeam(elAuthorTeam, success);
|
669
|
//
|
670
|
// //PublishedIn
|
671
|
// childName = "PublishedIn";
|
672
|
// obligatory = false;
|
673
|
// Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
674
|
// result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
|
675
|
//
|
676
|
// //MicroReference
|
677
|
// childName = "MicroReference";
|
678
|
// obligatory = false;
|
679
|
// Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
680
|
// String microReference = elMicroReference.getTextNormalize();
|
681
|
// if (CdmUtils.Nz(microReference).equals("")){
|
682
|
// //TODO
|
683
|
// logger.warn("MicroReference not yet implemented for AccordingToDetailed");
|
684
|
// }
|
685
|
// }
|
686
|
// return result;
|
687
|
// }
|
688
|
//
|
689
|
// private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
|
690
|
// Team result = null;
|
691
|
// if (elAuthorTeam != null){
|
692
|
// //TODO
|
693
|
// logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
|
694
|
// }
|
695
|
// return result;
|
696
|
// }
|
697
|
|
698
|
|
699
|
|
700
|
}
|