1 package net.sf.openrocket.preset.loader;
3 import au.com.bytecode.opencsv.CSVReader;
4 import net.sf.openrocket.gui.print.PrintUnit;
5 import net.sf.openrocket.preset.TypedPropertyMap;
6 import net.sf.openrocket.unit.Unit;
7 import net.sf.openrocket.unit.UnitGroup;
8 import net.sf.openrocket.util.ArrayList;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.util.List;
19 * Primary entry point for parsing component CSV files that are in Rocksim format.
21 public abstract class RocksimComponentFileLoader {
23 private String basePath = "";
27 protected List<RocksimComponentFileColumnParser> fileColumns = new ArrayList<RocksimComponentFileColumnParser>();
32 * @param theBasePathToLoadFrom base path
34 public RocksimComponentFileLoader(File theBasePathToLoadFrom) {
35 dir = theBasePathToLoadFrom;
36 basePath = dir.getAbsolutePath();
42 * @param theBasePathToLoadFrom base path
44 public RocksimComponentFileLoader(String theBasePathToLoadFrom) {
45 dir = new File(basePath);
46 basePath = theBasePathToLoadFrom;
49 protected abstract RocksimComponentFileType getFileType();
56 * Read a comma separated component file and return the parsed contents as a list of string arrays. Not for
57 * production use - just here for smoke testing.
59 * @param type the type of component file to read; uses the default file name
61 * @return a list (guaranteed never to be null) of string arrays. Each element of the list represents a row in the
62 * component data file; the element in the list itself is an array of String, where each item in the array
63 * is a column (cell) in the row. The string array is in sequential order as it appeared in the file.
65 private void load(RocksimComponentFileType type) {
67 throw new IllegalArgumentException(basePath + " does not exist");
69 if (!dir.isDirectory()) {
70 throw new IllegalArgumentException(basePath + " is not directory");
73 throw new IllegalArgumentException(basePath + " is not readable");
76 FileInputStream fis = new FileInputStream(new File(dir, type.getDefaultFileName()));
79 catch (FileNotFoundException ex) {
85 * Read a comma separated component file and return the parsed contents as a list of string arrays.
87 * @param file the file to read and parse
89 * @return a list (guaranteed never to be null) of string arrays. Each element of the list represents a row in the
90 * component data file; the element in the list itself is an array of String, where each item in the array
91 * is a column (cell) in the row. The string array is in sequential order as it appeared in the file.
93 private void load(File file) throws FileNotFoundException {
94 load(new FileInputStream(file));
98 * Read a comma separated component file and return the parsed contents as a list of string arrays.
100 * @param is the stream to read and parse
102 * @return a list (guaranteed never to be null) of string arrays. Each element of the list represents a row in the
103 * component data file; the element in the list itself is an array of String, where each item in the array
104 * is a column (cell) in the row. The string array is in sequential order as it appeared in the file.
106 private void load(InputStream is) {
110 InputStreamReader r = null;
112 r = new InputStreamReader(is);
114 // Create the CSV reader. Use comma separator.
115 CSVReader reader = new CSVReader(r, ',', '\'', '\\');
117 //Read and throw away the header row.
118 parseHeaders(reader.readNext());
120 String[] data = null;
121 while ((data = reader.readNext()) != null) {
122 // detect empty lines and skip:
123 if (data.length == 0) {
126 if (data.length == 1 && "".equals(data[0].trim())) {
131 //Read the rest of the file as data rows.
134 catch (IOException e) {
141 catch (IOException e) {
148 protected void parseHeaders(String[] headers) {
149 for (RocksimComponentFileColumnParser column : fileColumns) {
150 column.configure(headers);
154 protected void parseData(String[] data) {
155 if (data == null || data.length == 0) {
158 TypedPropertyMap props = new TypedPropertyMap();
162 for (RocksimComponentFileColumnParser column : fileColumns) {
163 column.parse(data, props);
168 protected void preProcess(String[] data) {
169 for (int i = 0; i < data.length; i++) {
175 d = stripAll(d, '"');
181 protected abstract void postProcess(TypedPropertyMap props);
184 * Rocksim CSV units are either inches or mm. A value of 0 or "in." indicate inches. A value of 1 or "mm" indicate
187 * @param units the value from the file
189 * @return true if it's inches
191 protected static boolean isInches(String units) {
192 String tmp = units.trim().toLowerCase();
193 return "0".equals(tmp) || tmp.startsWith("in");
197 * Convert inches or millimeters to meters.
199 * @param units a Rocksim CSV string representing the kind of units.
200 * @param value the original value within the CSV file
202 * @return the value in meters
204 protected static double convertLength(String units, double value) {
205 if (isInches(units)) {
206 return PrintUnit.INCHES.toMeters(value);
209 return PrintUnit.MILLIMETERS.toMeters(value);
213 protected static double convertMass(String units, double value) {
214 if ("oz".equals(units)) {
215 Unit u = UnitGroup.UNITS_MASS.getUnit(2);
216 return u.fromUnit(value);
222 * Remove all occurrences of the given character. Note: this is done because some manufacturers embed double quotes
223 * in their descriptions or material names. Those are stripped away because they cause all sorts of matching/lookup
226 * @param target the target string to be operated upon
227 * @param toBeRemoved the character to remove
229 * @return target, minus every occurrence of toBeRemoved
231 protected static String stripAll(String target, Character toBeRemoved) {
232 StringBuilder sb = new StringBuilder();
233 for (int i = 0; i < target.length(); i++) {
234 Character c = target.charAt(i);
235 if (!c.equals(toBeRemoved)) {
239 return sb.toString();
243 * Convert all words in a given string to Camel Case (first letter capitalized). Words are assumed to be separated
244 * by a space. Note: this is done because some manufacturers define their material name in Camel Case but the
245 * component part references the material in lower case. That causes matching/lookup issues that's easiest handled
246 * this way (rather than converting everything to lower case.
248 * @param target the target string to be operated upon
250 * @return target, with the first letter of each word in uppercase
252 protected static String toCamelCase(String target) {
253 StringBuilder sb = new StringBuilder();
254 String[] t = target.split("[ ]");
255 if (t != null && t.length > 0) {
256 for (String aT : t) {
258 s = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
259 sb.append(s).append(" ");
261 return sb.toString().trim();
271 //The oddities I've found thus far in the stock Rocksim data:
272 //1. BTDATA.CSV - Totally Tubular goofed up their part no. and description columns (They messed up TCDATA also)
273 //2. NCDATA.CSV - Estes Balsa nose cones are classified as G10 Fiberglass
274 //3. TRDATA.CSV - Apogee Saturn LEM Transition has no part number; Balsa Machining transitions have blank diameter