Updates
[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                 InputStream stream = null;
24
25                 try {
26                         
27                         stream = new BufferedInputStream(new FileInputStream(source));
28                         return load(stream);
29                         
30                 } catch (FileNotFoundException e) {
31                         throw new RocketLoadException("File not found: " + source);
32                 } finally {
33                         if (stream != null) {
34                                 try {
35                                         stream.close();
36                                 } catch (IOException e) {
37                                         e.printStackTrace();
38                                 }
39                         }
40                 }
41         }
42
43         /**
44          * Loads a rocket from the specified InputStream.
45          */
46         public final OpenRocketDocument load(InputStream source) throws RocketLoadException {
47                 warnings.clear();
48
49                 try {
50                         return loadFromStream(source);
51                 } catch (RocketLoadException e) {
52                         throw e;
53                 } catch (IOException e) {
54                         throw new RocketLoadException("I/O error: " + e.getMessage());
55                 } catch (Exception e) {
56                         throw new RocketLoadException("An unknown error occurred.  Please report a bug.", e);
57                 } catch (Throwable e) {
58                         throw new RocketLoadException("A serious error occurred and the software may be "
59                                         + "unstable.  Save your designs and restart OpenRocket.", e);
60                 }
61         }
62
63         
64         
65         /**
66          * This method is called by the default implementations of {@link #load(File)} 
67          * and {@link #load(InputStream)} to load the rocket.
68          * 
69          * @throws RocketLoadException  if an error occurs during loading.
70          */
71         protected abstract OpenRocketDocument loadFromStream(InputStream source) throws IOException,
72                         RocketLoadException;
73
74
75
76         public final WarningSet getWarnings() {
77                 return warnings;
78         }
79 }