Changeset 14741 in josm


Ignore:
Timestamp:
2019-01-27T18:58:32+01:00 (5 years ago)
Author:
simon04
Message:

see #16497 - revert r14725/r14724/r14723/r14718/r14630

Support for relative paths in .joz session file needs more testing.

Location:
trunk/src/org/openstreetmap/josm/io/session
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/session/GenericSessionExporter.java

    r14718 r14741  
    1313import java.io.IOException;
    1414import java.io.OutputStream;
     15import java.net.MalformedURLException;
    1516
    1617import javax.swing.AbstractAction;
     
    191192            addDataFile(support.getOutputStreamZip(zipPath));
    192193        } else {
    193             File f = layer.getAssociatedFile();
    194             if (f != null) {
    195                 final String fileString = support.relativize(f.toPath());
    196                 file.appendChild(support.createTextNode(fileString));
     194            try {
     195                File f = layer.getAssociatedFile();
     196                if (f != null) {
     197                    file.appendChild(support.createTextNode(f.toURI().toURL().toString()));
     198                }
     199            } catch (MalformedURLException e) {
     200                throw new IOException(e);
    197201            }
    198202        }
  • trunk/src/org/openstreetmap/josm/io/session/GeoImageSessionExporter.java

    r14718 r14741  
    6969                break;
    7070            }
    71             final String fileString = support.relativize(entry.getFile().toPath());
    72             addAttr("file", fileString, imgElem, support);
     71            addAttr("file", entry.getFile().getPath(), imgElem, support);
     72            // FIXME: relative filenames as option
    7373            // FIXME: include images as option (?)
    7474
  • trunk/src/org/openstreetmap/josm/io/session/SessionReader.java

    r14724 r14741  
    1313import java.net.URI;
    1414import java.net.URISyntaxException;
    15 import java.nio.file.FileSystem;
    16 import java.nio.file.FileSystems;
     15import java.nio.charset.StandardCharsets;
    1716import java.nio.file.Files;
    18 import java.nio.file.Path;
    1917import java.util.ArrayList;
    2018import java.util.Collection;
    2119import java.util.Collections;
     20import java.util.Enumeration;
    2221import java.util.HashMap;
    2322import java.util.List;
     
    2524import java.util.Map.Entry;
    2625import java.util.TreeMap;
    27 import java.util.stream.Stream;
     26import java.util.zip.ZipEntry;
     27import java.util.zip.ZipException;
     28import java.util.zip.ZipFile;
    2829
    2930import javax.swing.JOptionPane;
     
    150151    private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>();
    151152
    152     private Path sessionFile;
     153    private URI sessionFileURI;
    153154    private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
    154     private FileSystem zipFile;
     155    private ZipFile zipFile;
    155156    private List<Layer> layers = new ArrayList<>();
    156157    private int active = -1;
     
    307308                }
    308309            } else if (inZipPath != null) {
    309                 return Files.newInputStream(zipFile.getPath(inZipPath));
     310                ZipEntry entry = zipFile.getEntry(inZipPath);
     311                if (entry != null) {
     312                    return zipFile.getInputStream(entry);
     313                }
    310314            }
    311315            throw new IOException(tr("Unable to locate file  ''{0}''.", uriStr));
     
    341345                                // relative to session file - "../" step out of the archive
    342346                                String relPath = uri.getPath().substring(3);
    343                                 return sessionFile.resolveSibling(relPath).toFile();
     347                                return new File(sessionFileURI.resolve(relPath));
    344348                            } else {
    345349                                // file inside zip archive
     
    348352                            }
    349353                        } else
    350                             return sessionFile.resolveSibling(uriStr).toFile();
     354                            return new File(sessionFileURI.resolve(uri));
    351355                    }
    352356                } else
     
    700704    public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException {
    701705        try (InputStream josIS = createInputStream(sessionFile, zip)) {
    702             loadSession(josIS, sessionFile.toPath(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);
     706            loadSession(josIS, sessionFile.toURI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);
    703707        }
    704708    }
     
    707711        if (zip) {
    708712            try {
    709                 // read zip using ZipFileSystem/ZipFileSystemProvider
    710                 zipFile = FileSystems.newFileSystem(new URI("jar:" + sessionFile.toURI()), Collections.emptyMap());
    711                 try (Stream<Path> content = Files.list(zipFile.getPath("/"))) {
    712                     final Path jos = content
    713                             .filter(path -> Utils.hasExtension(path.getFileName().toString(), "jos"))
    714                             .findFirst()
    715                             .orElseThrow(() -> new IllegalDataException(tr("expected .jos file inside .joz archive")));
    716                     return Files.newInputStream(jos);
    717                 }
    718             } catch (URISyntaxException e) {
    719                 throw new IOException(e);
     713                zipFile = new ZipFile(sessionFile, StandardCharsets.UTF_8);
     714                return getZipInputStream(zipFile);
     715            } catch (ZipException ex) {
     716                throw new IOException(ex);
    720717            }
    721718        } else {
     
    724721    }
    725722
    726     private void loadSession(InputStream josIS, Path sessionFile, boolean zip, ProgressMonitor progressMonitor)
     723    private static InputStream getZipInputStream(ZipFile zipFile) throws IOException, IllegalDataException {
     724        ZipEntry josEntry = null;
     725        Enumeration<? extends ZipEntry> entries = zipFile.entries();
     726        while (entries.hasMoreElements()) {
     727            ZipEntry entry = entries.nextElement();
     728            if (Utils.hasExtension(entry.getName(), "jos")) {
     729                josEntry = entry;
     730                break;
     731            }
     732        }
     733        if (josEntry == null) {
     734            error(tr("expected .jos file inside .joz archive"));
     735        }
     736        return zipFile.getInputStream(josEntry);
     737    }
     738
     739    private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor)
    727740            throws IOException, IllegalDataException {
    728741
    729         this.sessionFile = sessionFile;
     742        this.sessionFileURI = sessionFileURI;
    730743        this.zip = zip;
    731744
  • trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java

    r14725 r14741  
    99import java.nio.charset.StandardCharsets;
    1010import java.nio.file.Files;
    11 import java.nio.file.Path;
    1211import java.util.ArrayList;
    1312import java.util.Collection;
     
    1615import java.util.Map;
    1716import java.util.Set;
    18 import java.util.stream.Collectors;
    1917import java.util.zip.ZipEntry;
    2018import java.util.zip.ZipOutputStream;
     
    4644import org.openstreetmap.josm.tools.Logging;
    4745import org.openstreetmap.josm.tools.MultiMap;
    48 import org.openstreetmap.josm.tools.StreamUtils;
    4946import org.openstreetmap.josm.tools.Utils;
    5047import org.openstreetmap.josm.tools.XmlUtils;
     
    6764    private final boolean zip;
    6865
    69     private Path output;
    7066    private ZipOutputStream zipOut;
    7167
     
    199195        public boolean isZip() {
    200196            return zip;
    201         }
    202 
    203         /**
    204          * Returns the path of the output file.
    205          *
    206          * @return the path of the output file
    207          */
    208         public Path getOutput() {
    209             return output;
    210         }
    211 
    212         /**
    213          * Returns a relative path w.r.t. the {@linkplain #getOutput output} directory
    214          * @param path the path to relativize
    215          * @return the relative path
    216          * @see Path#relativize(Path)
    217          */
    218         String relativize(Path path) {
    219             final Path output = getOutput();
    220             if (output != null && path.startsWith(output.getParent())) {
    221                 path = output.getParent().relativize(path);
    222             }
    223             // path.toString() returns backslashes on Windows, see #17228
    224             return (isZip() ? "../" : "") + StreamUtils.toStream(path)
    225                     .map(Object::toString)
    226                     .collect(Collectors.joining("/"));
    227197        }
    228198    }
     
    359329     */
    360330    public void write(File f) throws IOException {
    361         output = f.toPath();
    362         try (OutputStream out = Files.newOutputStream(output)) {
     331        try (OutputStream out = Files.newOutputStream(f.toPath())) {
    363332            write(out);
    364333        }
Note: See TracChangeset for help on using the changeset viewer.