3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.common
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.IOException
;
15 import java
.io
.InputStream
;
17 import java
.util
.NoSuchElementException
;
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
;
29 import org
.apache
.log4j
.Logger
;
30 import org
.xml
.sax
.SAXException
;
32 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoEvent
;
33 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoObserver
;
36 * Base class for XML imports
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);
47 protected void fireSchemaConflictEventExpectedStartTag(String elName
, XMLEventReader reader
) throws XMLStreamException
{
48 String type
= "ElementStart";
49 XMLEvent next
= reader
.nextEvent();
50 fireSchemaConflictEvent(type
, elName
, next
);
57 * @throws XMLStreamException
59 protected boolean validateStartOfDocument(XMLEventReader reader
) throws XMLStreamException
{
60 XMLEvent next
= reader
.nextEvent();
61 if (next
.isStartDocument()){
64 fireWarningEvent("Missing start of document", next
.getLocation().toString(), 16);
75 * @throws XMLStreamException
77 protected boolean isStartingElement(XMLEventReader reader
, String elName
) throws XMLStreamException
{
81 } catch (NoSuchElementException e
) {
84 return isStartingElement(next
, elName
);
87 protected boolean isStartingElement(XMLEvent event
, String elName
) throws XMLStreamException
{
88 boolean result
= false;
89 boolean isStart
= event
.isStartElement();
91 QName name
= event
.asStartElement().getName();
92 boolean equals
= name
.getLocalPart().equals(elName
);
99 protected boolean isEndingElement(XMLEventReader reader
, String elName
) throws XMLStreamException
{
102 next
= reader
.peek();
103 } catch (NoSuchElementException e
) {
106 return isEndingElement(next
, elName
);
109 protected boolean isEndingElement(XMLEvent event
, String elName
) throws XMLStreamException
{
110 boolean result
= false;
111 boolean isEnd
= event
.isEndElement();
113 QName name
= event
.asEndElement().getName();
114 result
= name
.getLocalPart().equals(elName
);
121 * Returns an input stream for the given source.
125 protected InputStream
getInputStream(CONFIG config
) {
127 URI uri
= config
.getSource();
128 File file
= new File(uri
);
129 InputStream is
= new FileInputStream(file
);
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);
146 private void fireSchemaConflictEvent(String expectedType
, String expectedName
, XMLEvent next
) {
147 String message
= "Schema conflict: expected %s '%s' but was %s ";
149 if (next
.isStartElement()){
150 eventString
= next
.asStartElement().getName().getLocalPart();
151 }else if(next
.isEndElement()){
152 eventString
= next
.asEndElement().getName().getLocalPart();
154 eventString
= next
.toString();
156 message
= String
.format(message
, expectedType
, expectedName
, eventString
);
157 String location
= "l." + next
.getLocation().getLineNumber() + "/c." + next
.getLocation().getColumnNumber();
158 fireWarningEvent(message
, location
, 16);
163 * Returns the StAX-Reader (XMLEventReader) for the source.
166 * @throws FactoryConfigurationError
167 * @throws XMLStreamException
169 protected XMLEventReader
getStaxReader(STATE state
) throws FactoryConfigurationError
, XMLStreamException
{
170 String fileName
= state
.getConfig().getSource().toString();
171 InputStream is
= null;
172 is
= getInputStream(state
.getConfig()); //throws exception and looks like it is not needed.
173 XMLInputFactory staxFactory
= XMLInputFactory
.newInstance();
174 XMLEventReader reader
= staxFactory
.createXMLEventReader(fileName
, is
);
180 * Parses the source file with the given handler
183 * @throws ParserConfigurationException
184 * @throws SAXException
185 * @throws IOException
187 protected void parseSAX(STATE state
, ImportHandlerBase handler
)
188 throws ParserConfigurationException
, SAXException
, IOException
{
189 handler
.addObserver(this);
190 InputStream is
= getInputStream(state
.getConfig());
191 SAXParserFactory saxFactory
= SAXParserFactory
.newInstance();
192 SAXParser saxParser
= saxFactory
.newSAXParser();
193 saxParser
.parse(is
, handler
);
198 * @see eu.etaxonomy.cdm.io.common.events.IIoObserver#handleEvent(eu.etaxonomy.cdm.io.common.events.IIoEvent)
201 public void handleEvent(IIoEvent event
) {