source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java@ 5361

Last change on this file since 5361 was 5361, checked in by Don-vip, 12 years ago

fix #7879 - Allow to open local and remote gzipped/bzipped osmChange files + remote osm.gz files + make some public constants of File filters to share between same importers/exporters

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedInputStream;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.List;
11import java.util.zip.GZIPInputStream;
12
13import javax.swing.JOptionPane;
14
15import org.apache.tools.bzip2.CBZip2InputStream;
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.ExtensionFileFilter;
18import org.openstreetmap.josm.gui.HelpAwareOptionPane;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20
21public abstract class FileImporter implements Comparable<FileImporter> {
22
23 public final ExtensionFileFilter filter;
24
25 public FileImporter(ExtensionFileFilter filter) {
26 this.filter = filter;
27 }
28
29 public boolean acceptFile(File pathname) {
30 return filter.acceptName(pathname.getName());
31 }
32
33 /**
34 * A batch importer is a file importer that prefers to read multiple files at the same time.
35 */
36 public boolean isBatchImporter() {
37 return false;
38 }
39
40 /**
41 * Needs to be implemented if isBatchImporter() returns false.
42 */
43 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
44 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
45 }
46
47 /**
48 * Needs to be implemented if isBatchImporter() returns true.
49 */
50 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
51 throw new IOException(tr("Could not import files."));
52 }
53
54 /**
55 * Wrapper to give meaningful output if things go wrong.
56 * @return true if data import was successful
57 */
58 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
59 try {
60 System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
61 importData(f, progressMonitor);
62 return true;
63 } catch (Exception e) {
64 e.printStackTrace();
65 HelpAwareOptionPane.showMessageDialogInEDT(
66 Main.parent,
67 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
68 tr("Error"),
69 JOptionPane.ERROR_MESSAGE, null
70 );
71 return false;
72 }
73 }
74 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
75 try {
76 System.out.println("Open "+files.size()+" files");
77 importData(files, progressMonitor);
78 return true;
79 } catch (Exception e) {
80 e.printStackTrace();
81 HelpAwareOptionPane.showMessageDialogInEDT(
82 Main.parent,
83 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
84 tr("Error"),
85 JOptionPane.ERROR_MESSAGE, null
86 );
87 return false;
88 }
89 }
90
91 /**
92 * If multiple files (with multiple file formats) are selected,
93 * they are opened in the order of their priorities.
94 * Highest priority comes first.
95 */
96 public double getPriority() {
97 return 0;
98 }
99
100 public int compareTo(FileImporter other) {
101 return (new Double(this.getPriority())).compareTo(other.getPriority());
102 }
103
104 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
105 if (in == null) {
106 return null;
107 }
108 BufferedInputStream bis = new BufferedInputStream(in);
109 int b = bis.read();
110 if (b != 'B')
111 throw new IOException(tr("Invalid bz2 file."));
112 b = bis.read();
113 if (b != 'Z')
114 throw new IOException(tr("Invalid bz2 file."));
115 return new CBZip2InputStream(bis);
116 }
117
118 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
119 if (in == null) {
120 return null;
121 }
122 return new GZIPInputStream(in);
123 }
124}
Note: See TracBrowser for help on using the repository browser.