0e8e051e6bfed7a4bf1c054def8df3af5e712c41
[debian/openrocket] / src / net / sf / openrocket / file / RocketLoader.java
1 package net.sf.openrocket.file;
2
3 import java.awt.Component;
4 import java.io.BufferedInputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9 import java.io.InputStream;
10
11 import javax.swing.ProgressMonitorInputStream;
12
13 import net.sf.openrocket.aerodynamics.WarningSet;
14 import net.sf.openrocket.document.OpenRocketDocument;
15
16
17 public abstract class RocketLoader {
18         protected final WarningSet warnings = new WarningSet();
19
20         
21         public final OpenRocketDocument load(File source, Component parent) 
22         throws RocketLoadException {
23                 warnings.clear();
24                 
25                 try {
26                         return load(new BufferedInputStream(new ProgressMonitorInputStream(
27                                         parent, "Loading " + source.getName(),
28                                         new FileInputStream(source))));
29                 } catch (FileNotFoundException e) {
30                         throw new RocketLoadException("File not found: " + source);
31                 }
32         }
33
34         /**
35          * Loads a rocket from the specified File object.
36          */
37         public final OpenRocketDocument load(File source) throws RocketLoadException {
38                 warnings.clear();
39
40                 try {
41                         return load(new BufferedInputStream(new FileInputStream(source)));
42                 } catch (FileNotFoundException e) {
43                         throw new RocketLoadException("File not found: " + source);
44                 }
45         }
46
47         /**
48          * Loads a rocket from the specified InputStream.
49          */
50         public final OpenRocketDocument load(InputStream source) throws RocketLoadException {
51                 warnings.clear();
52
53                 try {
54                         return loadFromStream(source);
55                 } catch (RocketLoadException e) {
56                         throw e;
57                 } catch (IOException e) {
58                         throw new RocketLoadException("I/O error: " + e.getMessage());
59                 } catch (Exception e) {
60                         throw new RocketLoadException("An unknown error occurred.  Please report a bug.", e);
61                 } catch (Throwable e) {
62                         throw new RocketLoadException("A serious error occurred and the software may be "
63                                         + "unstable.  Save your designs and restart OpenRocket.", e);
64                 }
65         }
66
67         
68         
69         /**
70          * This method is called by the default implementations of {@link #load(File)} 
71          * and {@link #load(InputStream)} to load the rocket.
72          * 
73          * @throws RocketLoadException  if an error occurs during loading.
74          */
75         protected abstract OpenRocketDocument loadFromStream(InputStream source) throws IOException,
76                         RocketLoadException;
77
78
79
80         public final WarningSet getWarnings() {
81                 return warnings;
82         }
83 }