diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderFactory.java index 47a182576..27dcfd68f 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderFactory.java @@ -1,47 +1,27 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; import org.openstreetmap.osmosis.core.pipeline.v0_6.RunnableChangeSourceManager; - /** * The task manager factory for a change downloader. * * @author Brett Henderson */ -public class IntervalDownloaderFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class IntervalDownloaderFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableChangeSourceManager( taskConfig.getId(), new IntervalDownloader( taskConfig.getId(), - workingDirectory + this.getWorkingDirectory(taskConfig) ), taskConfig.getPipeArgs() ); diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderInitializerFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderInitializerFactory.java index c75fabeb7..32679331f 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderInitializerFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/IntervalDownloaderInitializerFactory.java @@ -1,51 +1,34 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; import java.util.Date; import java.util.TimeZone; import org.openstreetmap.osmosis.core.pipeline.common.RunnableTaskManager; import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; - /** * The task manager factory for a change download initializer. * * @author Brett Henderson */ -public class IntervalDownloaderInitializerFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; +public class IntervalDownloaderInitializerFactory extends WorkingTaskManagerFactory { private static final String ARG_INITIAL_DATE = "initialDate"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - - + /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; Date initialDate; - File workingDirectory; - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); initialDate = getDateArgument(taskConfig, ARG_INITIAL_DATE, TimeZone.getTimeZone("UTC")); - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableTaskManager( taskConfig.getId(), new IntervalDownloaderInitializer( - workingDirectory, + this.getWorkingDirectory(taskConfig), initialDate ), taskConfig.getPipeArgs() diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderFactory.java index b98682169..76b077be2 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderFactory.java @@ -1,44 +1,24 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; import org.openstreetmap.osmosis.core.pipeline.v0_6.RunnableChangeSourceManager; - /** * The task manager factory for a replication file downloader. */ -public class ReplicationDownloaderFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationDownloaderFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableChangeSourceManager( taskConfig.getId(), new ReplicationDownloader( - workingDirectory + this.getWorkingDirectory(taskConfig) ), taskConfig.getPipeArgs() ); diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderInitializerFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderInitializerFactory.java index e2aabe55e..57213e4ef 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderInitializerFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationDownloaderInitializerFactory.java @@ -1,44 +1,24 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.RunnableTaskManager; import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; - /** * The task manager factory for a replication file download initializer. */ -public class ReplicationDownloaderInitializerFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationDownloaderInitializerFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableTaskManager( taskConfig.getId(), new ReplicationDownloaderInitializer( - workingDirectory + this.getWorkingDirectory(taskConfig) ), taskConfig.getPipeArgs() ); diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerFactory.java index 149d6b664..d21a04f37 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerFactory.java @@ -1,44 +1,25 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.RunnableTaskManager; import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; /** * The task manager factory for a replication file merger. */ -public class ReplicationFileMergerFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationFileMergerFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableTaskManager( taskConfig.getId(), new ReplicationFileMerger( - workingDirectory + this.getWorkingDirectory(taskConfig) ), taskConfig.getPipeArgs() ); diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerInitializerFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerInitializerFactory.java index 163e62555..a112f2d60 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerInitializerFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationFileMergerInitializerFactory.java @@ -1,44 +1,24 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.RunnableTaskManager; import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; - /** * The task manager factory for a replication file merger initializer. */ -public class ReplicationFileMergerInitializerFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationFileMergerInitializerFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableTaskManager( taskConfig.getId(), new ReplicationFileMergerInitializer( - workingDirectory + this.getWorkingDirectory(taskConfig) ), taskConfig.getPipeArgs() ); diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationLagReaderFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationLagReaderFactory.java index e3fb135b6..7304329be 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationLagReaderFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationLagReaderFactory.java @@ -1,54 +1,35 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.RunnableTaskManager; import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; - /** * The task manager factory for a replication lag reader. * * @author Peter Koerner */ -public class ReplicationLagReaderFactory extends TaskManagerFactory { +public class ReplicationLagReaderFactory extends WorkingTaskManagerFactory { private static final String ARG_HUMAN_READABLE = "humanReadable"; private static final boolean DEFAULT_HUMAN_READABLE = false; - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; boolean humanReadableFlag; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument( - taskConfig, - ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) - ); humanReadableFlag = getBooleanArgument( taskConfig, ARG_HUMAN_READABLE, DEFAULT_HUMAN_READABLE ); - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new RunnableTaskManager( taskConfig.getId(), new ReplicationLagReader( - workingDirectory, + this.getWorkingDirectory(taskConfig), humanReadableFlag ), taskConfig.getPipeArgs() diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationToChangeWriterFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationToChangeWriterFactory.java index 6b7e257db..f012b8dfa 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationToChangeWriterFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationToChangeWriterFactory.java @@ -1,40 +1,23 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; import org.openstreetmap.osmosis.core.pipeline.v0_6.ChangeSinkChangeSourceManager; - /** * The task manager factory for a replication to change writer. */ -public class ReplicationToChangeWriterFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationToChangeWriterFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument(taskConfig, ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY)); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new ChangeSinkChangeSourceManager( taskConfig.getId(), - new ReplicationToChangeWriter(workingDirectory), + new ReplicationToChangeWriter(this.getWorkingDirectory(taskConfig)), taskConfig.getPipeArgs()); } } diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationWriterFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationWriterFactory.java index 65beeace3..57850ca98 100644 --- a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationWriterFactory.java +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/ReplicationWriterFactory.java @@ -1,40 +1,24 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.replication.v0_6; -import java.io.File; - import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; import org.openstreetmap.osmosis.core.pipeline.common.TaskManager; -import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; import org.openstreetmap.osmosis.core.pipeline.v0_6.ChangeSinkManager; /** * The task manager factory for a replication file downloader. */ -public class ReplicationWriterFactory extends TaskManagerFactory { - private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; - private static final String DEFAULT_WORKING_DIRECTORY = "./"; - +public class ReplicationWriterFactory extends WorkingTaskManagerFactory { /** * {@inheritDoc} */ @Override protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) { - String workingDirectoryString; - File workingDirectory; - - // Get the task arguments. - workingDirectoryString = getStringArgument(taskConfig, ARG_WORKING_DIRECTORY, - getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY)); - - // Convert argument strings to strongly typed objects. - workingDirectory = new File(workingDirectoryString); - return new ChangeSinkManager( taskConfig.getId(), - new ReplicationWriter(workingDirectory), + new ReplicationWriter(this.getWorkingDirectory(taskConfig)), taskConfig.getPipeArgs()); } } diff --git a/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/WorkingTaskManagerFactory.java b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/WorkingTaskManagerFactory.java new file mode 100644 index 000000000..80f694c55 --- /dev/null +++ b/osmosis-replication/src/main/java/org/openstreetmap/osmosis/replication/v0_6/WorkingTaskManagerFactory.java @@ -0,0 +1,31 @@ +// This software is released into the Public Domain. See copying.txt for details. +package org.openstreetmap.osmosis.replication.v0_6; + + import java.io.File; + + import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration; +import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory; + + /** + * Base Factory for the replication classes, specifically for the working directory argument. + * + * @author mcuthbert + */ +public abstract class WorkingTaskManagerFactory extends TaskManagerFactory { + private static final String ARG_WORKING_DIRECTORY = "workingDirectory"; + private static final String DEFAULT_WORKING_DIRECTORY = "./"; + + /** + * Gets the current working directory for the task. + * + * @param taskConfig {@link TaskConfiguration} + * @return {@link File} + */ + protected File getWorkingDirectory(final TaskConfiguration taskConfig) { + return new File(this.getStringArgument( + taskConfig, + ARG_WORKING_DIRECTORY, + getDefaultStringArgument(taskConfig, DEFAULT_WORKING_DIRECTORY) + )); + } +} diff --git a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/FastXmlReader.java b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/FastXmlReader.java index 5f6fe15ef..91006b7dd 100644 --- a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/FastXmlReader.java +++ b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/FastXmlReader.java @@ -2,23 +2,22 @@ package org.openstreetmap.osmosis.xml.v0_6; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collections; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; -import org.openstreetmap.osmosis.core.OsmosisRuntimeException; import org.openstreetmap.osmosis.core.task.v0_6.RunnableSource; import org.openstreetmap.osmosis.core.task.v0_6.Sink; -import org.openstreetmap.osmosis.xml.common.CompressionActivator; + import org.openstreetmap.osmosis.xml.common.CompressionMethod; +import org.openstreetmap.osmosis.xml.v0_6.impl.BaseXMLReader; import org.openstreetmap.osmosis.xml.v0_6.impl.FastXmlParser; - +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; /** * An OSM data source reading from an xml file. The entire contents of the file @@ -27,90 +26,58 @@ * @author Jiri Clement * @author Brett Henderson */ -public class FastXmlReader implements RunnableSource { - - private static Logger log = Logger.getLogger(FastXmlReader.class.getName()); - - private Sink sink; - private final File file; - private final boolean enableDateParsing; - private final CompressionMethod compressionMethod; - +public class FastXmlReader extends BaseXMLReader implements RunnableSource { + private Sink sink; - /** - * Creates a new instance. - * - * @param file - * The file to read. - * @param enableDateParsing - * If true, dates will be parsed from xml data, else the current - * date will be used thus saving parsing time. - * @param compressionMethod - * Specifies the compression method to employ. - */ - public FastXmlReader(File file, boolean enableDateParsing, CompressionMethod compressionMethod) { - this.file = file; - this.enableDateParsing = enableDateParsing; - this.compressionMethod = compressionMethod; - } + /** + * Creates a new instance. + * + * @param file + * The file to read. + * @param enableDateParsing + * If true, dates will be parsed from xml data, else the current + * date will be used thus saving parsing time. + * @param compressionMethod + * Specifies the compression method to employ. + */ + public FastXmlReader(File file, boolean enableDateParsing, CompressionMethod compressionMethod) { + super(file, enableDateParsing, compressionMethod); + } + /** + * {@inheritDoc} + */ + public void setSink(Sink sink) { + this.sink = sink; + } - /** - * {@inheritDoc} - */ - public void setSink(Sink sink) { - this.sink = sink; - } - - - - /** - * Reads all data from the file and send it to the sink. - */ - public void run() { - InputStream inputStream = null; - FastXmlParser parser = null; - - try { - sink.initialize(Collections.emptyMap()); - - // make "-" an alias for /dev/stdin - if (file.getName().equals("-")) { - inputStream = System.in; - } else { - inputStream = new FileInputStream(file); - } - - - inputStream = - new CompressionActivator(compressionMethod). - createCompressionInputStream(inputStream); - - XMLInputFactory factory = XMLInputFactory.newInstance(); - factory.setProperty(XMLInputFactory.IS_COALESCING, false); - factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); - factory.setProperty(XMLInputFactory.IS_VALIDATING, false); - XMLStreamReader xpp = factory.createXMLStreamReader(inputStream); - - parser = new FastXmlParser(sink, xpp, enableDateParsing); - - parser.readOsm(); - - sink.complete(); - - } catch (Exception e) { - throw new OsmosisRuntimeException("Unable to read XML file " + file + ".", e); - } finally { - sink.close(); - - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.log(Level.SEVERE, "Unable to close input stream.", e); - } - inputStream = null; - } - } - } + /** + * Reads all data from the file and send it to the sink. + */ + public void run() { + try { + this.sink.initialize(Collections.emptyMap()); + this.handleXML(null); + this.sink.complete(); + } finally { + this.sink.close(); + } + } + + @Override + protected void parseXML(InputStream stream, DefaultHandler handler) + throws SAXException, IOException { + try { + final XMLInputFactory factory = XMLInputFactory.newInstance(); + factory.setProperty(XMLInputFactory.IS_COALESCING, false); + factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); + factory.setProperty(XMLInputFactory.IS_VALIDATING, false); + final XMLStreamReader xpp = factory.createXMLStreamReader(stream); + + final FastXmlParser parser = new FastXmlParser(this.sink, xpp, this.isEnableDateParsing()); + parser.readOsm(); + } catch (final XMLStreamException e) { + throw new SAXException(e); + } + } } diff --git a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlChangeReader.java b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlChangeReader.java index faecb050d..7982816cf 100644 --- a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlChangeReader.java +++ b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlChangeReader.java @@ -1,25 +1,15 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.xml.v0_6; -import org.openstreetmap.osmosis.core.OsmosisRuntimeException; import org.openstreetmap.osmosis.core.task.v0_6.ChangeSink; import org.openstreetmap.osmosis.core.task.v0_6.RunnableChangeSource; -import org.openstreetmap.osmosis.xml.common.CompressionActivator; + import org.openstreetmap.osmosis.xml.common.CompressionMethod; -import org.openstreetmap.osmosis.xml.common.SaxParserFactory; +import org.openstreetmap.osmosis.xml.v0_6.impl.BaseXMLReader; import org.openstreetmap.osmosis.xml.v0_6.impl.OsmChangeHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import javax.xml.parsers.SAXParser; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.Collections; -import java.util.logging.Level; -import java.util.logging.Logger; - /** * A change source reading from an xml file. The entire contents of the file @@ -27,15 +17,8 @@ * * @author Brett Henderson */ -public class XmlChangeReader implements RunnableChangeSource { - - private static Logger log = Logger.getLogger(XmlReader.class.getName()); - +public class XmlChangeReader extends BaseXMLReader implements RunnableChangeSource { private ChangeSink changeSink; - private File file; - private boolean enableDateParsing; - private CompressionMethod compressionMethod; - /** * Creates a new instance. @@ -49,12 +32,9 @@ public class XmlChangeReader implements RunnableChangeSource { * Specifies the compression method to employ. */ public XmlChangeReader(File file, boolean enableDateParsing, CompressionMethod compressionMethod) { - this.file = file; - this.enableDateParsing = enableDateParsing; - this.compressionMethod = compressionMethod; + super(file, enableDateParsing, compressionMethod); } - /** * {@inheritDoc} */ @@ -62,58 +42,16 @@ public void setChangeSink(ChangeSink changeSink) { this.changeSink = changeSink; } - /** * Reads all data from the file and send it to the sink. */ public void run() { - InputStream inputStream = null; - try { - SAXParser parser; - - changeSink.initialize(Collections.emptyMap()); - - // make "-" an alias for /dev/stdin - if (file.getName().equals("-")) { - inputStream = System.in; - } else { - inputStream = new FileInputStream(file); - } - - inputStream = - new CompressionActivator(compressionMethod). - createCompressionInputStream(inputStream); - - parser = SaxParserFactory.createParser(); - - parser.parse(inputStream, new OsmChangeHandler(changeSink, enableDateParsing)); - - changeSink.complete(); - - } catch (SAXParseException e) { - throw new OsmosisRuntimeException( - "Unable to parse xml file " + file - + ". publicId=(" + e.getPublicId() - + "), systemId=(" + e.getSystemId() - + "), lineNumber=" + e.getLineNumber() - + ", columnNumber=" + e.getColumnNumber() + ".", - e); - } catch (SAXException e) { - throw new OsmosisRuntimeException("Unable to parse XML.", e); - } catch (IOException e) { - throw new OsmosisRuntimeException("Unable to read XML file " + file + ".", e); + this.changeSink.initialize(Collections.emptyMap()); + this.handleXML(new OsmChangeHandler(this.changeSink, this.isEnableDateParsing())); + this.changeSink.complete(); } finally { - changeSink.close(); - - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.log(Level.SEVERE, "Unable to close input stream.", e); - } - inputStream = null; - } - } + this.changeSink.close(); + } } } diff --git a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlReader.java b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlReader.java index a333901c2..01be06077 100644 --- a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlReader.java +++ b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/XmlReader.java @@ -1,24 +1,15 @@ // This software is released into the Public Domain. See copying.txt for details. package org.openstreetmap.osmosis.xml.v0_6; -import org.openstreetmap.osmosis.core.OsmosisRuntimeException; import org.openstreetmap.osmosis.core.task.v0_6.RunnableSource; import org.openstreetmap.osmosis.core.task.v0_6.Sink; -import org.openstreetmap.osmosis.xml.common.CompressionActivator; + import org.openstreetmap.osmosis.xml.common.CompressionMethod; -import org.openstreetmap.osmosis.xml.common.SaxParserFactory; +import org.openstreetmap.osmosis.xml.v0_6.impl.BaseXMLReader; import org.openstreetmap.osmosis.xml.v0_6.impl.OsmHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import javax.xml.parsers.SAXParser; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.Collections; -import java.util.logging.Level; -import java.util.logging.Logger; /** @@ -27,15 +18,8 @@ * * @author Brett Henderson */ -public class XmlReader implements RunnableSource { - - private static Logger log = Logger.getLogger(XmlReader.class.getName()); - +public class XmlReader extends BaseXMLReader implements RunnableSource { private Sink sink; - private File file; - private boolean enableDateParsing; - private CompressionMethod compressionMethod; - /** * Creates a new instance. @@ -49,12 +33,9 @@ public class XmlReader implements RunnableSource { * Specifies the compression method to employ. */ public XmlReader(File file, boolean enableDateParsing, CompressionMethod compressionMethod) { - this.file = file; - this.enableDateParsing = enableDateParsing; - this.compressionMethod = compressionMethod; + super(file, enableDateParsing, compressionMethod); } - /** * {@inheritDoc} */ @@ -62,58 +43,16 @@ public void setSink(Sink sink) { this.sink = sink; } - /** * Reads all data from the file and send it to the sink. */ public void run() { - InputStream inputStream = null; - try { - SAXParser parser; - - sink.initialize(Collections.emptyMap()); - - // make "-" an alias for /dev/stdin - if (file.getName().equals("-")) { - inputStream = System.in; - } else { - inputStream = new FileInputStream(file); - } - - inputStream = - new CompressionActivator(compressionMethod). - createCompressionInputStream(inputStream); - - parser = SaxParserFactory.createParser(); - - parser.parse(inputStream, new OsmHandler(sink, enableDateParsing)); - - sink.complete(); - - } catch (SAXParseException e) { - throw new OsmosisRuntimeException( - "Unable to parse xml file " + file - + ". publicId=(" + e.getPublicId() - + "), systemId=(" + e.getSystemId() - + "), lineNumber=" + e.getLineNumber() - + ", columnNumber=" + e.getColumnNumber() + ".", - e); - } catch (SAXException e) { - throw new OsmosisRuntimeException("Unable to parse XML.", e); - } catch (IOException e) { - throw new OsmosisRuntimeException("Unable to read XML file " + file + ".", e); + this.sink.initialize(Collections.emptyMap()); + this.handleXML(new OsmHandler(this.sink, this.isEnableDateParsing())); + this.sink.complete(); } finally { - sink.close(); - - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.log(Level.SEVERE, "Unable to close input stream.", e); - } - inputStream = null; - } + this.sink.close(); } } } diff --git a/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/impl/BaseXMLReader.java b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/impl/BaseXMLReader.java new file mode 100644 index 000000000..45869bc6b --- /dev/null +++ b/osmosis-xml/src/main/java/org/openstreetmap/osmosis/xml/v0_6/impl/BaseXMLReader.java @@ -0,0 +1,140 @@ +// This software is released into the Public Domain. See copying.txt for details. +package org.openstreetmap.osmosis.xml.v0_6.impl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.GZIPInputStream; + +import javax.xml.parsers.SAXParser; + +import org.openstreetmap.osmosis.core.OsmosisRuntimeException; +import org.openstreetmap.osmosis.xml.common.CompressionActivator; +import org.openstreetmap.osmosis.xml.common.CompressionMethod; +import org.openstreetmap.osmosis.xml.common.SaxParserFactory; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.DefaultHandler; + + /** + * Handles common functionality used by XML readers. + * + * @author mcuthbert + */ +public abstract class BaseXMLReader { + private final File file; + private final boolean enableDateParsing; + private final CompressionMethod method; + + /** + * Default Constructor. + * + * @param file File to parse + * @param enableDateParsing whether to enable date parsing or not + * @param method The compression method if any + */ + public BaseXMLReader(final File file, final boolean enableDateParsing, final CompressionMethod method) { + this.file = file; + this.enableDateParsing = enableDateParsing; + this.method = method; + } + + /** + * Returns whether this object enables date parsing or not. + * + * @return true or false + */ + public boolean isEnableDateParsing() { + return this.enableDateParsing; + } + + /** + * Function to parse xml, this default function just uses the SAXParser. + * + * @param stream InputStream for the XML + * @param handler A handler for the XML + * @throws SAXException If there is any exceptions while parsing the XML + * @throws IOException If there is any issues with the input stream + */ + protected void parseXML(final InputStream stream, final DefaultHandler handler) + throws SAXException, IOException { + final SAXParser parser = SaxParserFactory.createParser(); + parser.parse(stream, handler); + } + + /** + * Function to handle the XML for the sub classes. + * + * @param handler A {@link DefaultHandler} + */ + protected void handleXML(final DefaultHandler handler) { + try (InputStream stream = this.getInputStream()) { + try (InputStream compressionStream = new CompressionActivator(this.method) + .createCompressionInputStream(stream)) { + this.parseXML(compressionStream, handler); + } + } catch (final SAXParseException e) { + // if we get a sax parse failure, there is a good chance it may be this one: + // Caused by: org.xml.sax.SAXParseException; + // lineNumber: ?; columnNumber: ?; Invalid byte 2 of 4-byte UTF-8 sequence. + // This can be solved by unzipping the contents to a temporary file and then parsing from there. + this.unzipParse(handler); + } catch (SAXException e) { + throw new OsmosisRuntimeException("Unable to parse XML.", e); + } catch (IOException e) { + throw new OsmosisRuntimeException("Unable to read XML file " + this.file + ".", e); + } + } + + private void unzipParse(final DefaultHandler handler) { + File tempFile; + try { + tempFile = File.createTempFile(this.getTempFilePrefix(), null); + } catch (final IOException e) { + throw new OsmosisRuntimeException("Failed to create temporary file.", e); + } + + try (InputStream fis = this.getInputStream(); + GZIPInputStream gzipStream = new GZIPInputStream(fis); + FileOutputStream fos = new FileOutputStream(tempFile)) { + final byte[] buffer = new byte[1024]; + int length; + while ((length = gzipStream.read(buffer)) > 0) { + fos.write(buffer, 0, length); + } + } catch (IOException e) { + throw new OsmosisRuntimeException("Unable to unzip gz file " + this.file + ".", e); + } + + try (InputStream unzippedStream = new FileInputStream(tempFile)) { + this.parseXML(unzippedStream, handler); + } catch (final SAXParseException e) { + throw new OsmosisRuntimeException( + "Unable to parse xml file " + this.file + + ". publicId=(" + e.getPublicId() + + "), systemId=(" + e.getSystemId() + + "), lineNumber=" + e.getLineNumber() + + ", columnNumber=" + e.getColumnNumber() + ".", + e); + } catch (SAXException e) { + throw new OsmosisRuntimeException("Unable to parse XML.", e); + } catch (IOException e) { + throw new OsmosisRuntimeException("Unable to read XML file " + this.file + ".", e); + } + } + + private InputStream getInputStream() throws FileNotFoundException { + if (this.file.getName().equals("-")) { + return System.in; + } else { + return new FileInputStream(this.file); + } + } + + private String getTempFilePrefix() { + return this.file.getName() + "_" + System.currentTimeMillis(); + } +}