2010-10-20 22:45:52 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright 2010, Google Inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
* Neither the name of Google Inc. nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.importers;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2010-08-06 07:04:25 +02:00
|
|
|
import java.io.IOException;
|
2010-05-05 01:24:48 +02:00
|
|
|
import java.io.InputStream;
|
2011-09-19 11:06:36 +02:00
|
|
|
import java.io.PushbackInputStream;
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.util.List;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.xml.stream.XMLInputFactory;
|
|
|
|
import javax.xml.stream.XMLStreamConstants;
|
|
|
|
import javax.xml.stream.XMLStreamException;
|
|
|
|
import javax.xml.stream.XMLStreamReader;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2011-08-22 23:47:15 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2010-05-26 21:22:38 +02:00
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.ProjectMetadata;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.importers.tree.ImportColumnGroup;
|
|
|
|
import com.google.refine.importers.tree.TreeImportingParserBase;
|
|
|
|
import com.google.refine.importers.tree.TreeReader;
|
|
|
|
import com.google.refine.importing.ImportingJob;
|
|
|
|
import com.google.refine.importing.ImportingUtilities;
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.model.Project;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.util.JSONUtilities;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
public class XmlImporter extends TreeImportingParserBase {
|
2011-08-22 23:47:15 +02:00
|
|
|
static final Logger logger = LoggerFactory.getLogger(XmlImporter.class);
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
public XmlImporter() {
|
|
|
|
super(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static private class PreviewParsingState {
|
|
|
|
int tokenCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private int PREVIEW_PARSING_LIMIT = 1000;
|
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
@Override
|
2011-08-02 05:34:47 +02:00
|
|
|
public JSONObject createParserUIInitializationData(
|
|
|
|
ImportingJob job, List<JSONObject> fileRecords, String format) {
|
|
|
|
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
|
|
|
|
try {
|
2011-08-18 19:39:18 +02:00
|
|
|
if (fileRecords.size() > 0) {
|
|
|
|
JSONObject firstFileRecord = fileRecords.get(0);
|
|
|
|
File file = ImportingUtilities.getFile(job, firstFileRecord);
|
|
|
|
InputStream is = new FileInputStream(file);
|
2011-09-19 11:06:36 +02:00
|
|
|
|
2011-08-18 19:39:18 +02:00
|
|
|
try {
|
|
|
|
XMLStreamReader parser = createXMLStreamReader(is);
|
|
|
|
PreviewParsingState state = new PreviewParsingState();
|
|
|
|
|
|
|
|
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
|
|
|
|
int tokenType = parser.next();
|
|
|
|
state.tokenCount++;
|
|
|
|
if (tokenType == XMLStreamConstants.START_ELEMENT) {
|
|
|
|
JSONObject rootElement = descendElement(parser, state);
|
|
|
|
if (rootElement != null) {
|
|
|
|
JSONUtilities.safePut(options, "dom", rootElement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ignore everything else
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-22 23:47:15 +02:00
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
logger.warn("Error generating parser UI initialization data for XML file", e);
|
2011-08-18 19:39:18 +02:00
|
|
|
} finally {
|
|
|
|
is.close();
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2011-08-22 23:47:15 +02:00
|
|
|
logger.error("Error generating parser UI initialization data for XML file", e);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2011-08-22 23:47:15 +02:00
|
|
|
final static private JSONObject descendElement(XMLStreamReader parser, PreviewParsingState state) {
|
2011-08-02 05:34:47 +02:00
|
|
|
JSONObject result = new JSONObject();
|
2010-05-05 01:24:48 +02:00
|
|
|
{
|
2011-08-02 05:34:47 +02:00
|
|
|
String name = parser.getLocalName();
|
|
|
|
JSONUtilities.safePut(result, "n", name);
|
|
|
|
|
|
|
|
String prefix = parser.getPrefix();
|
|
|
|
if (prefix != null) {
|
|
|
|
JSONUtilities.safePut(result, "p", prefix);
|
|
|
|
}
|
|
|
|
String nsUri = parser.getNamespaceURI();
|
|
|
|
if (nsUri != null) {
|
|
|
|
JSONUtilities.safePut(result, "uri", nsUri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int namespaceCount = parser.getNamespaceCount();
|
|
|
|
if (namespaceCount > 0) {
|
|
|
|
JSONArray namespaces = new JSONArray();
|
|
|
|
JSONUtilities.safePut(result, "ns", namespaces);
|
|
|
|
|
|
|
|
for (int i = 0; i < namespaceCount; i++) {
|
|
|
|
JSONObject namespace = new JSONObject();
|
|
|
|
JSONUtilities.append(namespaces, namespace);
|
|
|
|
JSONUtilities.safePut(namespace, "p", parser.getNamespacePrefix(i));
|
|
|
|
JSONUtilities.safePut(namespace, "uri", parser.getNamespaceURI(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int attributeCount = parser.getAttributeCount();
|
|
|
|
if (attributeCount > 0) {
|
|
|
|
JSONArray attributes = new JSONArray();
|
|
|
|
JSONUtilities.safePut(result, "a", attributes);
|
|
|
|
|
|
|
|
for (int i = 0; i < attributeCount; i++) {
|
|
|
|
JSONObject attribute = new JSONObject();
|
|
|
|
JSONUtilities.append(attributes, attribute);
|
|
|
|
JSONUtilities.safePut(attribute, "n", parser.getAttributeLocalName(i));
|
|
|
|
JSONUtilities.safePut(attribute, "v", parser.getAttributeValue(i));
|
|
|
|
String prefix = parser.getAttributePrefix(i);
|
|
|
|
if (prefix != null) {
|
|
|
|
JSONUtilities.safePut(attribute, "p", prefix);
|
2010-08-06 07:04:25 +02:00
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONArray children = new JSONArray();
|
2011-08-22 23:47:15 +02:00
|
|
|
try {
|
|
|
|
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
|
|
|
|
int tokenType = parser.next();
|
|
|
|
state.tokenCount++;
|
|
|
|
if (tokenType == XMLStreamConstants.END_ELEMENT) {
|
|
|
|
break;
|
|
|
|
} else if (tokenType == XMLStreamConstants.START_ELEMENT) {
|
|
|
|
JSONObject childElement = descendElement(parser, state);
|
|
|
|
if (childElement != null) {
|
|
|
|
JSONUtilities.append(children, childElement);
|
|
|
|
}
|
|
|
|
} else if (tokenType == XMLStreamConstants.CHARACTERS ||
|
|
|
|
tokenType == XMLStreamConstants.CDATA ||
|
|
|
|
tokenType == XMLStreamConstants.SPACE) {
|
|
|
|
JSONObject childElement = new JSONObject();
|
|
|
|
JSONUtilities.safePut(childElement, "t", parser.getText());
|
2011-08-02 05:34:47 +02:00
|
|
|
JSONUtilities.append(children, childElement);
|
2011-08-22 23:47:15 +02:00
|
|
|
} else {
|
|
|
|
// ignore everything else
|
2010-09-27 19:40:51 +02:00
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2011-08-22 23:47:15 +02:00
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
logger.error("Error generating parser UI initialization data for XML file", e);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
if (children.length() > 0) {
|
|
|
|
JSONUtilities.safePut(result, "c", children);
|
|
|
|
}
|
|
|
|
return result;
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
@Override
|
2011-08-02 05:34:47 +02:00
|
|
|
public void parseOneFile(Project project, ProjectMetadata metadata,
|
|
|
|
ImportingJob job, String fileSource, InputStream inputStream,
|
|
|
|
ImportColumnGroup rootColumnGroup, int limit, JSONObject options,
|
|
|
|
List<Exception> exceptions) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
parseOneFile(project, metadata, job, fileSource,
|
|
|
|
new XmlParser(inputStream), rootColumnGroup, limit, options, exceptions);
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
exceptions.add(e);
|
2011-09-19 11:06:36 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
exceptions.add(e);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static public class XmlParser implements TreeReader {
|
|
|
|
final protected XMLStreamReader parser;
|
|
|
|
|
2011-09-19 11:06:36 +02:00
|
|
|
public XmlParser(InputStream inputStream) throws XMLStreamException, IOException {
|
2011-08-02 05:34:47 +02:00
|
|
|
parser = createXMLStreamReader(inputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Token next() throws ServletException {
|
|
|
|
try {
|
|
|
|
if (!parser.hasNext()) {
|
|
|
|
throw new ServletException("End of XML stream");
|
|
|
|
}
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
throw new ServletException(e);
|
2010-05-26 15:18:48 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
int currentToken = -1;
|
|
|
|
try {
|
|
|
|
currentToken = parser.next();
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
throw new ServletException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapToToken(currentToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Token mapToToken(int token) throws ServletException {
|
|
|
|
switch(token){
|
|
|
|
case XMLStreamConstants.START_ELEMENT: return Token.StartEntity;
|
|
|
|
case XMLStreamConstants.END_ELEMENT: return Token.EndEntity;
|
|
|
|
case XMLStreamConstants.CHARACTERS: return Token.Value;
|
|
|
|
case XMLStreamConstants.START_DOCUMENT: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.END_DOCUMENT: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.SPACE: return Token.Value;
|
|
|
|
case XMLStreamConstants.PROCESSING_INSTRUCTION: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.NOTATION_DECLARATION: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.NAMESPACE: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.ENTITY_REFERENCE: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.DTD: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.COMMENT: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.CDATA: return Token.Ignorable;
|
|
|
|
case XMLStreamConstants.ATTRIBUTE: return Token.Ignorable;
|
|
|
|
default:
|
|
|
|
return Token.Ignorable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Token current() throws ServletException{
|
|
|
|
return this.mapToToken(parser.getEventType());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() throws ServletException{
|
|
|
|
try {
|
|
|
|
return parser.hasNext();
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
throw new ServletException(e);
|
2010-05-26 15:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFieldName() throws ServletException{
|
|
|
|
try{
|
|
|
|
return parser.getLocalName();
|
|
|
|
}catch(IllegalStateException e){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPrefix(){
|
|
|
|
return parser.getPrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFieldValue(){
|
|
|
|
return parser.getText();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getAttributeCount(){
|
|
|
|
return parser.getAttributeCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAttributeValue(int index){
|
|
|
|
return parser.getAttributeValue(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAttributePrefix(int index){
|
|
|
|
return parser.getAttributePrefix(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAttributeLocalName(int index){
|
|
|
|
return parser.getAttributeLocalName(index);
|
|
|
|
}
|
2010-05-26 15:18:48 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
2011-09-19 11:06:36 +02:00
|
|
|
final static private XMLStreamReader createXMLStreamReader(InputStream inputStream) throws XMLStreamException, IOException {
|
2011-08-02 05:34:47 +02:00
|
|
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
|
|
factory.setProperty(XMLInputFactory.IS_COALESCING, true);
|
|
|
|
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
|
|
|
|
|
2011-09-19 11:06:36 +02:00
|
|
|
return factory.createXMLStreamReader(wrapPrefixSpaceRemovingInputStream(inputStream));
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 11:06:36 +02:00
|
|
|
final static private InputStream wrapPrefixSpaceRemovingInputStream(InputStream inputStream) throws IOException {
|
|
|
|
PushbackInputStream pis = new PushbackInputStream(inputStream);
|
|
|
|
int b;
|
|
|
|
while ((b = pis.read()) >= 0) {
|
|
|
|
if (!Character.isWhitespace(b)) {
|
|
|
|
pis.unread(b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pis;
|
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|