1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.common;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStream;
|
16
|
import java.net.URI;
|
17
|
import java.util.NoSuchElementException;
|
18
|
|
19
|
import javax.xml.namespace.QName;
|
20
|
import javax.xml.parsers.ParserConfigurationException;
|
21
|
import javax.xml.parsers.SAXParser;
|
22
|
import javax.xml.parsers.SAXParserFactory;
|
23
|
import javax.xml.stream.FactoryConfigurationError;
|
24
|
import javax.xml.stream.XMLEventReader;
|
25
|
import javax.xml.stream.XMLInputFactory;
|
26
|
import javax.xml.stream.XMLStreamException;
|
27
|
import javax.xml.stream.events.XMLEvent;
|
28
|
|
29
|
import org.apache.log4j.Logger;
|
30
|
import org.xml.sax.SAXException;
|
31
|
|
32
|
import eu.etaxonomy.cdm.io.common.events.IIoEvent;
|
33
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
34
|
|
35
|
/**
|
36
|
* Base class for XML imports
|
37
|
* @author a.mueller
|
38
|
* @date 28.06.2011
|
39
|
*
|
40
|
*/
|
41
|
public abstract class XmlImportBase<CONFIG extends XmlImportConfiguratorBase<STATE>, STATE extends XmlImportState<CONFIG, ?>> extends CdmImportBase<CONFIG, STATE> implements IIoObserver {
|
42
|
@SuppressWarnings("unused")
|
43
|
private static final Logger logger = Logger.getLogger(XmlImportBase.class);
|
44
|
|
45
|
|
46
|
|
47
|
protected void fireSchemaConflictEventExpectedStartTag(String elName, XMLEventReader reader) throws XMLStreamException {
|
48
|
String type = "ElementStart";
|
49
|
XMLEvent next = reader.nextEvent();
|
50
|
fireSchemaConflictEvent(type, elName, next);
|
51
|
}
|
52
|
|
53
|
|
54
|
/**
|
55
|
* @param r
|
56
|
* @return
|
57
|
* @throws XMLStreamException
|
58
|
*/
|
59
|
protected boolean validateStartOfDocument(XMLEventReader reader) throws XMLStreamException {
|
60
|
XMLEvent next = reader.nextEvent();
|
61
|
if (next.isStartDocument()){
|
62
|
return true;
|
63
|
}else {
|
64
|
fireWarningEvent("Missing start of document", next.getLocation().toString(), 16);
|
65
|
return false;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
|
70
|
/**
|
71
|
* TODO namespace
|
72
|
* @param elName
|
73
|
* @param reader
|
74
|
* @return
|
75
|
* @throws XMLStreamException
|
76
|
*/
|
77
|
protected boolean isStartingElement(XMLEventReader reader, String elName) throws XMLStreamException {
|
78
|
XMLEvent next;
|
79
|
try {
|
80
|
next = reader.peek();
|
81
|
} catch (NoSuchElementException e) {
|
82
|
return false;
|
83
|
}
|
84
|
return isStartingElement(next, elName);
|
85
|
}
|
86
|
|
87
|
protected boolean isStartingElement(XMLEvent event, String elName) throws XMLStreamException {
|
88
|
boolean result = false;
|
89
|
boolean isStart = event.isStartElement();
|
90
|
if (isStart){
|
91
|
QName name = event.asStartElement().getName();
|
92
|
boolean equals = name.getLocalPart().equals(elName);
|
93
|
result = equals;
|
94
|
}
|
95
|
return result;
|
96
|
}
|
97
|
|
98
|
|
99
|
protected boolean isEndingElement(XMLEventReader reader, String elName) throws XMLStreamException {
|
100
|
XMLEvent next;
|
101
|
try {
|
102
|
next = reader.peek();
|
103
|
} catch (NoSuchElementException e) {
|
104
|
return false;
|
105
|
}
|
106
|
return isEndingElement(next, elName);
|
107
|
}
|
108
|
|
109
|
protected boolean isEndingElement(XMLEvent event, String elName) throws XMLStreamException {
|
110
|
boolean result = false;
|
111
|
boolean isEnd = event.isEndElement();
|
112
|
if (isEnd){
|
113
|
QName name = event.asEndElement().getName();
|
114
|
result = name.getLocalPart().equals(elName);
|
115
|
}
|
116
|
return result;
|
117
|
}
|
118
|
|
119
|
|
120
|
/**
|
121
|
* Returns an input stream for the given source.
|
122
|
* @param config
|
123
|
* @return
|
124
|
*/
|
125
|
protected InputStream getInputStream(CONFIG config) {
|
126
|
try {
|
127
|
URI uri = config.getSource();
|
128
|
File file = new File(uri);
|
129
|
InputStream is = new FileInputStream(file);
|
130
|
return is;
|
131
|
}catch (Exception e) {
|
132
|
String message = "Problem reading source file %s. Import can not be executed. Reason: %s.";
|
133
|
message = String.format(message, config.getSource(), e.getMessage());
|
134
|
fireWarningEvent(message, "Read file", 16);
|
135
|
return null;
|
136
|
}
|
137
|
}
|
138
|
|
139
|
|
140
|
/**
|
141
|
* @param elName
|
142
|
* @param next
|
143
|
* @param message
|
144
|
* @param type
|
145
|
*/
|
146
|
private void fireSchemaConflictEvent(String expectedType, String expectedName, XMLEvent next) {
|
147
|
String message = "Schema conflict: expected %s '%s' but was %s ";
|
148
|
String eventString;
|
149
|
if (next.isStartElement()){
|
150
|
eventString = next.asStartElement().getName().getLocalPart();
|
151
|
}else if(next.isEndElement()){
|
152
|
eventString = next.asEndElement().getName().getLocalPart();
|
153
|
}else{
|
154
|
eventString = next.toString();
|
155
|
}
|
156
|
message = String.format(message, expectedType, expectedName, eventString);
|
157
|
String location = "l." + next.getLocation().getLineNumber() + "/c." + next.getLocation().getColumnNumber();
|
158
|
fireWarningEvent(message, location, 16);
|
159
|
}
|
160
|
|
161
|
|
162
|
/**
|
163
|
* Returns the StAX-Reader (XMLEventReader) for the source.
|
164
|
* @param state
|
165
|
* @return
|
166
|
* @throws FactoryConfigurationError
|
167
|
* @throws XMLStreamException
|
168
|
*/
|
169
|
protected XMLEventReader getStaxReader(STATE state) throws FactoryConfigurationError, XMLStreamException {
|
170
|
String fileName = state.getConfig().getSource().toString();
|
171
|
InputStream is = getInputStream(state.getConfig());
|
172
|
XMLInputFactory staxFactory = XMLInputFactory.newInstance();
|
173
|
XMLEventReader reader = staxFactory.createXMLEventReader(fileName, is);
|
174
|
return reader;
|
175
|
}
|
176
|
|
177
|
|
178
|
/**
|
179
|
* Parses the source file with the given handler
|
180
|
* @param is
|
181
|
* @param handler
|
182
|
* @throws ParserConfigurationException
|
183
|
* @throws SAXException
|
184
|
* @throws IOException
|
185
|
*/
|
186
|
protected void parseSAX(STATE state, ImportHandlerBase handler)
|
187
|
throws ParserConfigurationException, SAXException, IOException {
|
188
|
handler.addObserver(this);
|
189
|
InputStream is = getInputStream(state.getConfig());
|
190
|
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
|
191
|
SAXParser saxParser = saxFactory.newSAXParser();
|
192
|
saxParser.parse(is, handler);
|
193
|
}
|
194
|
|
195
|
|
196
|
/* (non-Javadoc)
|
197
|
* @see eu.etaxonomy.cdm.io.common.events.IIoObserver#handleEvent(eu.etaxonomy.cdm.io.common.events.IIoEvent)
|
198
|
*/
|
199
|
@Override
|
200
|
public void handleEvent(IIoEvent event) {
|
201
|
fire(event);
|
202
|
}
|
203
|
|
204
|
}
|