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);
72 * Load motors from files iterated over by a FileIterator. Any errors during
73 * loading are logged, but otherwise ignored.
75 * The iterator is closed at the end of the operation.
77 * @param iterator the FileIterator that iterates of the files to load.
78 * @return a list of all motors loaded.
80 public static List<Motor> load(FileIterator iterator) {
81 GeneralMotorLoader loader = new GeneralMotorLoader();
82 List<Motor> list = new ArrayList<Motor>();
84 while (iterator.hasNext()) {
85 final Pair<String, InputStream> input = iterator.next();
86 log.debug("Loading motors from file " + input.getU());
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());
92 for (Motor m : motors) {
93 list.add((ThrustCurveMotor) m);
95 } catch (IOException e) {
96 log.warn("IOException when loading motor file " + input.getU(), e);
100 } catch (IOException e) {
101 log.error("IOException when closing InputStream", e);