1 package net.sf.openrocket.file.motor;
3 import java.io.BufferedInputStream;
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;
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;
21 public final class MotorLoaderHelper {
23 private static final LogHelper log = Application.getLogger();
25 private MotorLoaderHelper() {
26 // Prevent construction
30 * Load a file or directory of thrust curves. Directories are loaded
31 * recursively. Any errors during loading are logged, but otherwise ignored.
33 * @param target the file or directory to load.
34 * @return a list of all motors in the file/directory.
36 public static List<Motor> load(File target) {
37 GeneralMotorLoader loader = new GeneralMotorLoader();
39 if (target.isDirectory()) {
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();
50 InputStream is = null;
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();
61 } catch (IOException e) {
62 log.error("Could not close file " + target, e);
70 public static List<Motor> load( InputStream is, String fileName ) {
71 GeneralMotorLoader loader = new GeneralMotorLoader();
73 List<Motor> motors = loader.load(is, fileName);
74 if (motors.size() == 0) {
75 log.warn("No motors found in file " + fileName);
78 } catch (IOException e) {
79 log.warn("IOException when loading motor file " + fileName, e);
81 return Collections.<Motor>emptyList();
85 * Load motors from files iterated over by a FileIterator. Any errors during
86 * loading are logged, but otherwise ignored.
88 * The iterator is closed at the end of the operation.
90 * @param iterator the FileIterator that iterates of the files to load.
91 * @return a list of all motors loaded.
93 public static List<Motor> load(FileIterator iterator) {
94 List<Motor> list = new ArrayList<Motor>();
96 while (iterator.hasNext()) {
97 final Pair<String, InputStream> input = iterator.next();
98 log.debug("Loading motors from file " + input.getU());
100 List<Motor> motors = load(input.getV(), input.getU());
101 for (Motor m : motors) {
102 list.add((ThrustCurveMotor) m);
106 input.getV().close();
107 } catch (IOException e) {
108 log.error("IOException when closing InputStream", e);