Advertisement
wreed12345

CMBL File Reader

Nov 4th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package io.github.wreed12345;
  2.  
  3. import java.util.List;
  4. import java.io.BufferedReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. /**
  11.  * Contains all usable data from a CMBL file
  12.  *
  13.  * @author william
  14.  */
  15. public class CMBLData {
  16.  
  17.     private List<Double> time;
  18.     private List<Double> xPosition;
  19.     private List<Double> yPosition;
  20.     private final int amountOfValues;
  21.    
  22.     /**
  23.      * Creates a new data object for a CMBL File
  24.      *
  25.      * @param filePath
  26.      *            the path to the CMBL File
  27.      */
  28.     public CMBLData(String filePath) {
  29.         time = new ArrayList<Double>();
  30.         xPosition = new ArrayList<Double>();
  31.         yPosition = new ArrayList<Double>();
  32.         parseData(filePath);
  33.         if((time.size() != xPosition.size()) || (xPosition.size() != yPosition.size())) {
  34.             throw new RuntimeException("Values for Time, X/Y Position are not of the same size!");
  35.         }
  36.         amountOfValues = time.size();
  37.  
  38.     }
  39.  
  40.     /**
  41.      * ENUM to keep track of what the parser is looking for
  42.      */
  43.     private enum CurrentIteration {
  44.         TIME, XPOSITION, YPOSITION;
  45.     }
  46.  
  47.     /*
  48.      * Order of numbers: 1. Time values 2. X values in meters 3. Y values in
  49.      * meters (not sure if units are always meters, sometimes may be mm for some
  50.      * reason)
  51.      */
  52.  
  53.     /**
  54.      * Parses all usable data from the file
  55.      *
  56.      * @param filePath
  57.      *            path to the file
  58.      */
  59.     private void parseData(String filePath) {
  60.         try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
  61.             String line;
  62.             CurrentIteration curit = CurrentIteration.TIME;
  63.             PRELIM: while ((line = br.readLine()) != null) {
  64.                 if (line.contains("<ColumnCells>")) {
  65.                     boolean getNumbers = true;
  66.                     while (getNumbers) {
  67.                         line = br.readLine();
  68.                         if (line.contains("<")) {
  69.                             getNumbers = false;
  70.                             switch (curit) {
  71.                             case TIME:
  72.                                 curit = CurrentIteration.XPOSITION;
  73.                                 break;
  74.                             case XPOSITION:
  75.                                 curit = CurrentIteration.YPOSITION;
  76.                                 break;
  77.                             case YPOSITION:
  78.                                 break PRELIM;
  79.                             default:
  80.                                 throw new RuntimeException("More than three sets of numbers were found");
  81.                             }
  82.                             break;
  83.                         }
  84.                         switch (curit) {
  85.                         case TIME:
  86.                             time.add(Double.valueOf(line));
  87.                             break;
  88.                         case XPOSITION:
  89.                             xPosition.add(Double.valueOf(line) / 1000);
  90.                             // TODO: check if this is always in mm. In the above
  91.                             // case it is so it is being converted to m
  92.                             break;
  93.                         case YPOSITION:
  94.                             // TODO: same as above
  95.                             yPosition.add(Double.valueOf(line) / 1000);
  96.                             break;
  97.                         default:
  98.                             throw new RuntimeException("More than three sets of numbers were found");
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.             br.close();
  104.         } catch (FileNotFoundException e) {
  105.             System.err.println("CMBL file was not found");
  106.             e.printStackTrace();
  107.         } catch (IOException e) {
  108.             e.printStackTrace();
  109.         }
  110.  
  111.     }
  112.  
  113.     /**
  114.      * @return all time values extracted from the CMBL file
  115.      */
  116.     public List<Double> getTimeVales() {
  117.         return time;
  118.     }
  119.  
  120.     /**
  121.      * @return all X Position values extracted from the CMBL file
  122.      */
  123.     public List<Double> getXPositionValues() {
  124.         return xPosition;
  125.     }
  126.  
  127.     /**
  128.      * @return all Y Position values extracted from the CMBL file
  129.      */
  130.     public List<Double> getYPositionValues() {
  131.         return yPosition;
  132.     }
  133.    
  134.     /**
  135.      * @return the amount of values contained in this CMBL file
  136.      * (EX: 20 values meaning 20 values for time, 20 for x pos, 20 for y pos... etc
  137.      */
  138.     public int amountOfValues() {
  139.         return amountOfValues;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement