Ignore:
Timestamp:
2013-09-22T16:59:52+02:00 (11 years ago)
Author:
Don-vip
Message:

see #9032, see #4029 - Allow to load session from standard I/O mechanisms (local and remote files) + javadoc

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

Legend:

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

    r5684 r6245  
    8585    }
    8686
    87     private File sessionFile;
    88     private boolean zip; /* true, if session file is a .joz file; false if it is a .jos file */
     87    private URI sessionFileURI;
     88    private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
    8989    private ZipFile zipFile;
    9090    private List<Layer> layers = new ArrayList<Layer>();
     
    208208                                // relative to session file - "../" step out of the archive
    209209                                String relPath = uri.getPath().substring(3);
    210                                 return new File(sessionFile.toURI().resolve(relPath));
     210                                return new File(sessionFileURI.resolve(relPath));
    211211                            } else {
    212212                                // file inside zip archive
     
    215215                            }
    216216                        } else
    217                             return new File(sessionFile.toURI().resolve(uri));
     217                            return new File(sessionFileURI.resolve(uri));
    218218                    }
    219219                } else
     
    225225
    226226        /**
    227          * Returns true if we are reading from a .joz file.
     227         * Determines if we are reading from a .joz file.
     228         * @return {@code true} if we are reading from a .joz file, {@code false} otherwise
    228229         */
    229230        public boolean isZip() {
     
    278279    }
    279280
    280     private void error(String msg) throws IllegalDataException {
     281    private static void error(String msg) throws IllegalDataException {
    281282        throw new IllegalDataException(msg);
    282283    }
     
    530531            progressMonitor = NullProgressMonitor.INSTANCE;
    531532        }
    532         this.sessionFile = sessionFile;
    533         this.zip = zip;
    534533
    535534        InputStream josIS = null;
     
    538537            try {
    539538                zipFile = new ZipFile(sessionFile);
    540                 ZipEntry josEntry = null;
    541                 Enumeration<? extends ZipEntry> entries = zipFile.entries();
    542                 while (entries.hasMoreElements()) {
    543                     ZipEntry entry = entries.nextElement();
    544                     if (entry.getName().toLowerCase().endsWith(".jos")) {
    545                         josEntry = entry;
    546                         break;
    547                     }
    548                 }
    549                 if (josEntry == null) {
    550                     error(tr("expected .jos file inside .joz archive"));
    551                 }
    552                 josIS = zipFile.getInputStream(josEntry);
     539                josIS = getZipInputStream(zipFile);
    553540            } catch (ZipException ze) {
    554541                throw new IOException(ze);
     
    562549        }
    563550
     551        loadSession(josIS, sessionFile.toURI(), zip, progressMonitor);
     552    }
     553
     554    private static InputStream getZipInputStream(ZipFile zipFile) throws ZipException, IOException, IllegalDataException {
     555        ZipEntry josEntry = null;
     556        Enumeration<? extends ZipEntry> entries = zipFile.entries();
     557        while (entries.hasMoreElements()) {
     558            ZipEntry entry = entries.nextElement();
     559            if (entry.getName().toLowerCase().endsWith(".jos")) {
     560                josEntry = entry;
     561                break;
     562            }
     563        }
     564        if (josEntry == null) {
     565            error(tr("expected .jos file inside .joz archive"));
     566        }
     567        return zipFile.getInputStream(josEntry);
     568    }
     569
     570    private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
     571       
     572        this.sessionFileURI = sessionFileURI;
     573        this.zip = zip;
     574       
    564575        try {
    565576            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
     
    581592        return (Element) els.item(0);
    582593    }
    583 
    584594}
Note: See TracChangeset for help on using the changeset viewer.