remove some javadoc
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / XmlImportBase.java
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 = 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);
175 return reader;
176 }
177
178
179 /**
180 * Parses the source file with the given handler
181 * @param is
182 * @param handler
183 * @throws ParserConfigurationException
184 * @throws SAXException
185 * @throws IOException
186 */
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);
194 }
195
196
197 /* (non-Javadoc)
198 * @see eu.etaxonomy.cdm.io.common.events.IIoObserver#handleEvent(eu.etaxonomy.cdm.io.common.events.IIoEvent)
199 */
200 @Override
201 public void handleEvent(IIoEvent event) {
202 fire(event);
203 }
204
205 }