create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / AbstractRocketLoader.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 AbstractRocketLoader implements RocketLoader {
15         protected final WarningSet warnings = new WarningSet();
16         
17         
18         /**
19          * Loads a rocket from the specified File object.
20          */
21         @Override
22         public final OpenRocketDocument load(File source, MotorFinder motorFinder) throws RocketLoadException {
23                 warnings.clear();
24                 InputStream stream = null;
25                 
26                 try {
27                         
28                         stream = new BufferedInputStream(new FileInputStream(source));
29                         return load(stream, motorFinder);
30                         
31                 } catch (FileNotFoundException e) {
32                         throw new RocketLoadException("File not found: " + source);
33                 } finally {
34                         if (stream != null) {
35                                 try {
36                                         stream.close();
37                                 } catch (IOException e) {
38                                         e.printStackTrace();
39                                 }
40                         }
41                 }
42         }
43         
44         /**
45          * Loads a rocket from the specified InputStream.
46          */
47         @Override
48         public final OpenRocketDocument load(InputStream source, MotorFinder motorFinder) throws RocketLoadException {
49                 warnings.clear();
50                 
51                 try {
52                         return loadFromStream(source, motorFinder);
53                 } catch (RocketLoadException e) {
54                         throw e;
55                 } catch (IOException e) {
56                         throw new RocketLoadException("I/O error: " + e.getMessage(), e);
57                 }
58         }
59         
60         
61         
62         /**
63          * This method is called by the default implementations of #load(File) 
64          * and load(InputStream) to load the rocket.
65          * 
66          * @throws RocketLoadException  if an error occurs during loading.
67          */
68         protected abstract OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws IOException,
69                         RocketLoadException;
70         
71         
72         
73         @Override
74         public final WarningSet getWarnings() {
75                 return warnings;
76         }
77 }