2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.common
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
16 import java
.util
.NoSuchElementException
;
18 import javax
.xml
.namespace
.QName
;
19 import javax
.xml
.parsers
.ParserConfigurationException
;
20 import javax
.xml
.parsers
.SAXParser
;
21 import javax
.xml
.parsers
.SAXParserFactory
;
22 import javax
.xml
.stream
.FactoryConfigurationError
;
23 import javax
.xml
.stream
.XMLEventReader
;
24 import javax
.xml
.stream
.XMLInputFactory
;
25 import javax
.xml
.stream
.XMLStreamException
;
26 import javax
.xml
.stream
.events
.XMLEvent
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.xml
.sax
.SAXException
;
31 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoEvent
;
32 import eu
.etaxonomy
.cdm
.io
.common
.events
.IIoObserver
;
35 * Base class for XML imports
39 public abstract class XmlImportBase
<CONFIG
extends XmlImportConfiguratorBase
<STATE
>, STATE
extends XmlImportState
<CONFIG
, ?
>>
40 extends CdmImportBase
<CONFIG
, STATE
>
41 implements IIoObserver
{
43 private static final long serialVersionUID
= 1505202381104645545L;
44 @SuppressWarnings("unused")
45 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
);
56 * @throws XMLStreamException
58 protected boolean validateStartOfDocument(XMLEventReader reader
) throws XMLStreamException
{
59 XMLEvent next
= reader
.nextEvent();
60 if (next
.isStartDocument()){
63 fireWarningEvent("Missing start of document", next
.getLocation().toString(), 16);
74 * @throws XMLStreamException
76 protected boolean isStartingElement(XMLEventReader reader
, String elName
) throws XMLStreamException
{
80 } catch (NoSuchElementException e
) {
83 return isStartingElement(next
, elName
);
86 protected boolean isStartingElement(XMLEvent event
, String elName
) {
87 boolean result
= false;
88 boolean isStart
= event
.isStartElement();
90 QName name
= event
.asStartElement().getName();
91 boolean equals
= name
.getLocalPart().equals(elName
);
97 protected boolean isEndingElement(XMLEventReader reader
, String elName
) throws XMLStreamException
{
100 next
= reader
.peek();
101 } catch (NoSuchElementException e
) {
104 return isEndingElement(next
, elName
);
107 protected boolean isEndingElement(XMLEvent event
, String elName
) throws XMLStreamException
{
108 boolean result
= false;
109 boolean isEnd
= event
.isEndElement();
111 QName name
= event
.asEndElement().getName();
112 result
= name
.getLocalPart().equals(elName
);
119 * Returns an input stream for the given source.
123 protected InputStream
getInputStream(CONFIG config
) {
125 URI uri
= config
.getSource();
126 File file
= new File(uri
);
127 InputStream is
= new FileInputStream(file
);
129 }catch (Exception e
) {
130 String message
= "Problem reading source file %s. Import can not be executed. Reason: %s.";
131 message
= String
.format(message
, config
.getSource(), e
.getMessage());
132 fireWarningEvent(message
, "Read file", 16);
137 private void fireSchemaConflictEvent(String expectedType
, String expectedName
, XMLEvent next
) {
138 String message
= "Schema conflict: expected %s '%s' but was %s ";
140 if (next
.isStartElement()){
141 eventString
= next
.asStartElement().getName().getLocalPart();
142 }else if(next
.isEndElement()){
143 eventString
= next
.asEndElement().getName().getLocalPart();
145 eventString
= next
.toString();
147 message
= String
.format(message
, expectedType
, expectedName
, eventString
);
148 String location
= "l." + next
.getLocation().getLineNumber() + "/c." + next
.getLocation().getColumnNumber();
149 fireWarningEvent(message
, location
, 16);
154 * Returns the StAX-Reader (XMLEventReader) for the source.
157 * @throws FactoryConfigurationError
158 * @throws XMLStreamException
160 protected XMLEventReader
getStaxReader(STATE state
) throws FactoryConfigurationError
, XMLStreamException
{
161 String fileName
= state
.getConfig().getSource().toString();
162 InputStream is
= null;
163 is
= getInputStream(state
.getConfig()); //throws exception and looks like it is not needed.
164 XMLInputFactory staxFactory
= XMLInputFactory
.newInstance();
165 XMLEventReader reader
= staxFactory
.createXMLEventReader(fileName
, is
);
171 * Parses the source file with the given handler
174 * @throws ParserConfigurationException
175 * @throws SAXException
176 * @throws IOException
178 protected void parseSAX(STATE state
, ImportHandlerBase handler
)
179 throws ParserConfigurationException
, SAXException
, IOException
{
180 handler
.addObserver(this);
181 InputStream is
= getInputStream(state
.getConfig());
182 SAXParserFactory saxFactory
= SAXParserFactory
.newInstance();
183 SAXParser saxParser
= saxFactory
.newSAXParser();
184 saxParser
.parse(is
, handler
);
189 * @see eu.etaxonomy.cdm.io.common.events.IIoObserver#handleEvent(eu.etaxonomy.cdm.io.common.events.IIoEvent)
192 public void handleEvent(IIoEvent event
) {