377ade46b150458eddca4b863405f82e892c41dc
[debian/openrocket] / 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.main.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         
71         /**
72          * Load motors from files iterated over by a FileIterator.  Any errors during
73          * loading are logged, but otherwise ignored.
74          * <p>
75          * The iterator is closed at the end of the operation.
76          * 
77          * @param iterator      the FileIterator that iterates of the files to load.
78          * @return                      a list of all motors loaded.
79          */
80         public static List<Motor> load(FileIterator iterator) {
81                 GeneralMotorLoader loader = new GeneralMotorLoader();
82                 List<Motor> list = new ArrayList<Motor>();
83                 
84                 while (iterator.hasNext()) {
85                         final Pair<String, InputStream> input = iterator.next();
86                         log.debug("Loading motors from file " + input.getU());
87                         try {
88                                 List<Motor> motors = loader.load(input.getV(), input.getU());
89                                 if (motors.size() == 0) {
90                                         log.warn("No motors found in file " + input.getU());
91                                 }
92                                 for (Motor m : motors) {
93                                         list.add((ThrustCurveMotor) m);
94                                 }
95                         } catch (IOException e) {
96                                 log.warn("IOException when loading motor file " + input.getU(), e);
97                         } finally {
98                                 try {
99                                         input.getV().close();
100                                 } catch (IOException e) {
101                                         log.error("IOException when closing InputStream", e);
102                                 }
103                         }
104                 }
105                 iterator.close();
106                 
107                 return list;
108         }
109         
110 }