Changeset 15377 in josm


Ignore:
Timestamp:
2019-09-26T20:05:02+02:00 (5 years ago)
Author:
Don-vip
Message:

see #13679 - refactor session import/export classes for easier plugin extensions

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/build.xml

    r15243 r15377  
    806806            <ignore classname="org.fusesource.*"/>
    807807            <ignore classname="org.gdal.*"/>
     808            <ignore classname="org.geotools.data.h2.*"/>
     809            <ignore classname="org.geotools.gce.imagemosaic.*"/>
    808810            <ignore classname="org.hibernate.*"/>
    809811            <ignore classname="org.hsqldb.*"/>
  • trunk/src/org/openstreetmap/josm/data/osm/Tags.java

    r15376 r15377  
    4242        return values;
    4343    }
     44
     45    @Override
     46    public int hashCode() {
     47        return Objects.hash(key, values);
     48    }
     49
     50    @Override
     51    public boolean equals(Object obj) {
     52        if (this == obj)
     53            return true;
     54        if (obj == null || getClass() != obj.getClass())
     55            return false;
     56        Tags other = (Tags) obj;
     57        return Objects.equals(key, other.key) && Objects.equals(values, other.values);
     58    }
     59
     60    @Override
     61    public String toString() {
     62        return "Tags [key=" + key + ", values=" + values + ']';
     63    }
    4464}
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

    r12800 r15377  
    88import java.nio.charset.StandardCharsets;
    99
     10import org.openstreetmap.josm.data.osm.DataSet;
    1011import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1112import org.openstreetmap.josm.io.OsmWriter;
     
    2829    @Override
    2930    protected void addDataFile(OutputStream out) {
     31        export(layer.data, out);
     32    }
     33
     34    /**
     35     * Exports OSM data to the given output stream.
     36     * @param data data set
     37     * @param out output stream
     38     * @since 15377
     39     */
     40    public static void export(DataSet data, OutputStream out) {
    3041        Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
    31         OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
    32         layer.data.getReadLock().lock();
     42        OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, data.getVersion());
     43        data.getReadLock().lock();
    3344        try {
    34             w.write(layer.data);
     45            w.write(data);
    3546            w.flush();
    3647        } finally {
    37             layer.data.getReadLock().unlock();
     48            data.getReadLock().unlock();
    3849        }
    3950    }
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionImporter.java

    r12671 r15377  
    2929    @Override
    3030    public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
     31        checkMetaVersion(elem);
     32        String fileStr = extractFileName(elem, support);
     33        return importData(new OsmImporter(), support, fileStr, progressMonitor);
     34    }
     35
     36    /**
     37     * Checks that element defines the expected version number.
     38     * @param elem element to check
     39     * @throws IllegalDataException if version is not the expected one
     40     * @since 15377
     41     */
     42    public static void checkMetaVersion(Element elem) throws IllegalDataException {
    3143        String version = elem.getAttribute("version");
    3244        if (!"0.1".equals(version)) {
    3345            throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
    3446        }
     47    }
     48
     49    /**
     50     * Extract file name from element.
     51     * @param elem element to parse
     52     * @param support import/export support
     53     * @return file name, if present
     54     * @throws IllegalDataException if file name missing or empty
     55     * @since 15377
     56     */
     57    public static String extractFileName(Element elem, ImportSupport support) throws IllegalDataException {
    3558        try {
    3659            XPathFactory xPathFactory = XPathFactory.newInstance();
     
    4164                throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
    4265            }
    43 
    44             OsmImporter importer = new OsmImporter();
    45             try (InputStream in = support.getInputStream(fileStr)) {
    46                 OsmImporter.OsmImporterData importData = importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(),
    47                         progressMonitor);
    48 
    49                 support.addPostLayersTask(importData.getPostLayerTask());
    50                 return importData.getLayer();
    51             }
     66            return fileStr;
    5267        } catch (XPathExpressionException e) {
    5368            throw new IllegalDataException(e);
    5469        }
    5570    }
     71
     72    /**
     73     * Import data as a new layer.
     74     * @param osmImporter OSM importer
     75     * @param support import/export support
     76     * @param fileStr file name to import
     77     * @param progressMonitor progress monitor
     78     * @return new layer
     79     * @throws IOException in case of I/O error
     80     * @throws IllegalDataException in case of illegal data
     81     * @since 15377
     82     */
     83    public static Layer importData(OsmImporter osmImporter, ImportSupport support, String fileStr, ProgressMonitor progressMonitor)
     84            throws IOException, IllegalDataException {
     85        try (InputStream in = support.getInputStream(fileStr)) {
     86            OsmImporter.OsmImporterData importData = osmImporter.loadLayer(
     87                    in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
     88
     89            support.addPostLayersTask(importData.getPostLayerTask());
     90            return importData.getLayer();
     91        }
     92    }
    5693}
Note: See TracChangeset for help on using the changeset viewer.