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