Changeset 14741 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2019-01-27T18:58:32+01:00 (6 years ago)
- 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 13 13 import java.io.IOException; 14 14 import java.io.OutputStream; 15 import java.net.MalformedURLException; 15 16 16 17 import javax.swing.AbstractAction; … … 191 192 addDataFile(support.getOutputStreamZip(zipPath)); 192 193 } 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); 197 201 } 198 202 } -
trunk/src/org/openstreetmap/josm/io/session/GeoImageSessionExporter.java
r14718 r14741 69 69 break; 70 70 } 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 73 73 // FIXME: include images as option (?) 74 74 -
trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
r14724 r14741 13 13 import java.net.URI; 14 14 import java.net.URISyntaxException; 15 import java.nio.file.FileSystem; 16 import java.nio.file.FileSystems; 15 import java.nio.charset.StandardCharsets; 17 16 import java.nio.file.Files; 18 import java.nio.file.Path;19 17 import java.util.ArrayList; 20 18 import java.util.Collection; 21 19 import java.util.Collections; 20 import java.util.Enumeration; 22 21 import java.util.HashMap; 23 22 import java.util.List; … … 25 24 import java.util.Map.Entry; 26 25 import java.util.TreeMap; 27 import java.util.stream.Stream; 26 import java.util.zip.ZipEntry; 27 import java.util.zip.ZipException; 28 import java.util.zip.ZipFile; 28 29 29 30 import javax.swing.JOptionPane; … … 150 151 private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>(); 151 152 152 private Path sessionFile;153 private URI sessionFileURI; 153 154 private boolean zip; // true, if session file is a .joz file; false if it is a .jos file 154 private FileSystemzipFile;155 private ZipFile zipFile; 155 156 private List<Layer> layers = new ArrayList<>(); 156 157 private int active = -1; … … 307 308 } 308 309 } 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 } 310 314 } 311 315 throw new IOException(tr("Unable to locate file ''{0}''.", uriStr)); … … 341 345 // relative to session file - "../" step out of the archive 342 346 String relPath = uri.getPath().substring(3); 343 return sessionFile.resolveSibling(relPath).toFile();347 return new File(sessionFileURI.resolve(relPath)); 344 348 } else { 345 349 // file inside zip archive … … 348 352 } 349 353 } else 350 return sessionFile.resolveSibling(uriStr).toFile();354 return new File(sessionFileURI.resolve(uri)); 351 355 } 352 356 } else … … 700 704 public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException { 701 705 try (InputStream josIS = createInputStream(sessionFile, zip)) { 702 loadSession(josIS, sessionFile.to Path(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);706 loadSession(josIS, sessionFile.toURI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE); 703 707 } 704 708 } … … 707 711 if (zip) { 708 712 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); 720 717 } 721 718 } else { … … 724 721 } 725 722 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) 727 740 throws IOException, IllegalDataException { 728 741 729 this.sessionFile = sessionFile;742 this.sessionFileURI = sessionFileURI; 730 743 this.zip = zip; 731 744 -
trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java
r14725 r14741 9 9 import java.nio.charset.StandardCharsets; 10 10 import java.nio.file.Files; 11 import java.nio.file.Path;12 11 import java.util.ArrayList; 13 12 import java.util.Collection; … … 16 15 import java.util.Map; 17 16 import java.util.Set; 18 import java.util.stream.Collectors;19 17 import java.util.zip.ZipEntry; 20 18 import java.util.zip.ZipOutputStream; … … 46 44 import org.openstreetmap.josm.tools.Logging; 47 45 import org.openstreetmap.josm.tools.MultiMap; 48 import org.openstreetmap.josm.tools.StreamUtils;49 46 import org.openstreetmap.josm.tools.Utils; 50 47 import org.openstreetmap.josm.tools.XmlUtils; … … 67 64 private final boolean zip; 68 65 69 private Path output;70 66 private ZipOutputStream zipOut; 71 67 … … 199 195 public boolean isZip() { 200 196 return zip; 201 }202 203 /**204 * Returns the path of the output file.205 *206 * @return the path of the output file207 */208 public Path getOutput() {209 return output;210 }211 212 /**213 * Returns a relative path w.r.t. the {@linkplain #getOutput output} directory214 * @param path the path to relativize215 * @return the relative path216 * @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 #17228224 return (isZip() ? "../" : "") + StreamUtils.toStream(path)225 .map(Object::toString)226 .collect(Collectors.joining("/"));227 197 } 228 198 } … … 359 329 */ 360 330 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())) { 363 332 write(out); 364 333 }
Note:
See TracChangeset
for help on using the changeset viewer.