create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / motor / MotorLoaderHelper.java
1 package net.sf.openrocket.file.motor;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
11
12 import net.sf.openrocket.file.iterator.DirectoryIterator;
13 import net.sf.openrocket.file.iterator.FileIterator;
14 import net.sf.openrocket.gui.util.SimpleFileFilter;
15 import net.sf.openrocket.logging.LogHelper;
16 import net.sf.openrocket.motor.Motor;
17 import net.sf.openrocket.motor.ThrustCurveMotor;
18 import net.sf.openrocket.startup.Application;
19 import net.sf.openrocket.util.Pair;
20
21 public final class MotorLoaderHelper {
22
23         private static final LogHelper log = Application.getLogger();
24
25         private MotorLoaderHelper() {
26                 // Prevent construction
27         }
28
29         /**
30          * Load a file or directory of thrust curves.  Directories are loaded
31          * recursively.  Any errors during loading are logged, but otherwise ignored.
32          * 
33          * @param target        the file or directory to load.
34          * @return                      a list of all motors in the file/directory.
35          */
36         public static List<Motor> load(File target) {
37                 GeneralMotorLoader loader = new GeneralMotorLoader();
38
39                 if (target.isDirectory()) {
40
41                         try {
42                                 return load(new DirectoryIterator(target, new SimpleFileFilter("", loader.getSupportedExtensions()), true));
43                         } catch (IOException e) {
44                                 log.warn("Could not read directory " + target, e);
45                                 return Collections.emptyList();
46                         }
47
48                 } else {
49
50                         InputStream is = null;
51                         try {
52                                 is = new FileInputStream(target);
53                                 return loader.load(new BufferedInputStream(is), target.getName());
54                         } catch (IOException e) {
55                                 log.warn("Could not load file " + target, e);
56                                 return Collections.emptyList();
57                         } finally {
58                                 if (is != null) {
59                                         try {
60                                                 is.close();
61                                         } catch (IOException e) {
62                                                 log.error("Could not close file " + target, e);
63                                         }
64                                 }
65                         }
66
67                 }
68         }
69
70         public static List<Motor> load( InputStream is, String fileName ) {
71                 GeneralMotorLoader loader = new GeneralMotorLoader();
72                 try {
73                         List<Motor> motors = loader.load(is, fileName);
74                         if (motors.size() == 0) {
75                                 log.warn("No motors found in file " + fileName);
76                         }
77                         return motors;
78                 } catch (IOException e) {
79                         log.warn("IOException when loading motor file " + fileName, e);
80                 }
81                 return Collections.<Motor>emptyList();
82         }
83
84         /**
85          * Load motors from files iterated over by a FileIterator.  Any errors during
86          * loading are logged, but otherwise ignored.
87          * <p>
88          * The iterator is closed at the end of the operation.
89          * 
90          * @param iterator      the FileIterator that iterates of the files to load.
91          * @return                      a list of all motors loaded.
92          */
93         public static List<Motor> load(FileIterator iterator) {
94                 List<Motor> list = new ArrayList<Motor>();
95
96                 while (iterator.hasNext()) {
97                         final Pair<String, InputStream> input = iterator.next();
98                         log.debug("Loading motors from file " + input.getU());
99                         try {
100                                 List<Motor> motors = load(input.getV(), input.getU());
101                                 for (Motor m : motors) {
102                                         list.add((ThrustCurveMotor) m);
103                                 }
104                         } finally {
105                                 try {
106                                         input.getV().close();
107                                 } catch (IOException e) {
108                                         log.error("IOException when closing InputStream", e);
109                                 }
110                         }
111                 }
112                 iterator.close();
113
114                 return list;
115         }
116
117 }