4fc8dada7cbd12e37c1ce6c3946e37336778e4a3
[debian/openrocket] / core / 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(), e);
55                 }
56         }
57         
58         
59
60         /**
61          * This method is called by the default implementations of {@link #load(File)} 
62          * and {@link #load(InputStream)} to load the rocket.
63          * 
64          * @throws RocketLoadException  if an error occurs during loading.
65          */
66         protected abstract OpenRocketDocument loadFromStream(InputStream source) throws IOException,
67                         RocketLoadException;
68         
69         
70
71         public final WarningSet getWarnings() {
72                 return warnings;
73         }
74 }