/**
  * Loads from videos.csv. Replaces everything in memory with information saved in file.
  *
  * @throws FileNotFoundException
  */
 public static void loadDB() throws FileNotFoundException {
   videos = new ArrayList<Video>();
   CSVReader input = new CSVReader();
   while (input.hasVideo()) {
     addVideo(input.nextVideo());
   }
 }
Example #2
0
  /**
   * Tests CSVWriter, also tests a little CSVReader, as it reads back and validates the data, it has
   * written
   *
   * @throws ParseException when creating fake athlete something goes wrong with date, then throws
   *     it.
   */
  @Test
  public void writeDataToCSVTest() throws ParseException {
    ArrayList<Athlete> fakeAthletes = createFakeAthleteList();

    DataToCSVWriter csvWriter = new DataToCSVWriter();

    String testFile = "csvTest.csv";

    csvWriter.writeDataToCSV(fakeAthletes, testFile);

    assertTrue(new File(testFile).exists());

    CSVReader csvReader = new CSVReader();
    ArrayList<String> data = csvReader.readInData(testFile);
    String[] line1 = null;
    String[] line2 = null;

    for (String s : data) {
      if (line1 == null) line1 = s.split(",");
      else line2 = s.split(",");
    }

    assert line1 != null && line2 != null;
    assertTrue(line1[2].contains("Taavi"));
    assertTrue(line2[2].contains("James"));
  }
Example #3
0
  @Test
  public void testDelimiter() {
    CSVReader csvReader = new CSVReader();
    Assert.assertEquals(csvReader.getDelimiter(), ',');

    csvReader.setDelimiter('.');
    Assert.assertEquals(csvReader.getDelimiter(), '.');
  }
Example #4
0
  @Test
  public void testOneRowInconsistentQuotes() throws IOException {
    String data = "\"a\",b,\"c\"";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b", "c"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #5
0
  @Test
  public void testOneRowWithLineBreak() throws IOException {
    String data = "a,b,c\n";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b", "c"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #6
0
  @Test
  public void testOneRowWithQuoteCharacters2() throws IOException {
    String data = "\"\"\"quoted\"\"\"";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"\"quoted\""}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #7
0
  @Test
  public void testOneRowQuotesLineBreak() throws IOException {
    String data = "\"a\",\"b\nc\",\"d\"";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b\nc", "d"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #8
0
  @Test
  public void testOneRowEmptyQuoted() throws IOException {
    String data = "aaa,\"\",ccc";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"aaa", "", "ccc"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #9
0
  @Test
  public void testTwoRowsBuffering() throws IOException {
    String data = "aaa,bbb,ccc\nddd,eee,fff";
    CSVReader reader = new CSVReader(new StringReader(data), 15);

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"aaa", "bbb", "ccc"}, row);
    row = reader.next();
    compareRows("second row read incorrectly", new String[] {"ddd", "eee", "fff"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #10
0
  @Test
  public void testTwoRowsRN() throws IOException {
    String data = "a,b,c\r\nd,e,f";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b", "c"}, row);
    row = reader.next();
    compareRows("second row read incorrectly", new String[] {"d", "e", "f"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #11
0
  @Test
  public void testTwoRowsQuotes() throws IOException {
    String data = "\"a\",\"b\",\"c\"\n\"d\",\"e\",\"f\"";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b", "c"}, row);
    row = reader.next();
    compareRows("second row read incorrectly", new String[] {"d", "e", "f"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #12
0
  @Test
  public void testSeparator() throws IOException {
    String data = "a;b;c\nd;e;f";
    CSVReader reader = new CSVReader(new StringReader(data));
    reader.setSeparator(';');

    String[] row = reader.next();
    compareRows("first row read incorrectly", new String[] {"a", "b", "c"}, row);
    row = reader.next();
    compareRows("second row read incorrectly", new String[] {"d", "e", "f"}, row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
  @Test
  public void testAddNewDataAndSortByDefault() throws Exception {
    // given
    Product expectedProductFirst = new Product(1, 4.14, "ritona", "12.10.2015");
    Product expectedProductLast = new Product(5, 3.14, "bizona", "12.10.2017");

    // when
    csvHolder.addNewDataAndSortBy(
        "./src/test/resources/test.csv", products, CSVHolderImpl.CompareType.DEFAULT);

    // then
    assertEquals(
        expectedProductFirst, csvReader.readFromFile("./src/test/resources/test.csv").get(0));
    assertEquals(
        expectedProductLast, csvReader.readFromFile("./src/test/resources/test.csv").get(4));
  }
 @Before
 public void setUp() throws Exception {
   csvHolder = new CSVHolderImpl();
   csvReader = new CSVReaderImpl();
   products = csvReader.readFromFile("./src/test/resources/product.csv");
   file = new File("./src/test/resources/test.csv");
 }
Example #15
0
  public static void saveUpload(Reader csvFile, CSVStrategy strategy, ValueProcessorProvider vpp) {
    try {

      CSVReaderBuilder<Bank> builder = new CSVReaderBuilder<Bank>(csvFile);

      builder.strategy(strategy);
      CSVReader<Bank> csvReader =
          builder.entryParser(new AnnotationEntryParser<Bank>(Bank.class, vpp)).build();

      List<Bank> banks = csvReader.readAll();
      for (Bank bank : banks) {
        XPersistence.getManager().merge(bank);
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Example #16
0
  @Test
  public void testOneRowWithQuoteCharacters() throws IOException {
    String data =
        "TRMMMXN128F42936A5,\"Symphony No. 1 G minor \"\"Sinfonie Serieuse\"\"/Allegro con energia\",SOZVAPQ12A8C13B63C,\"Berwald: Symphonies Nos. 1/2/3/4\",AR2NS5Y1187FB5879D";
    CSVReader reader = new CSVReader(new StringReader(data));

    String[] row = reader.next();
    compareRows(
        "first row read incorrectly",
        new String[] {
          "TRMMMXN128F42936A5",
          "Symphony No. 1 G minor \"Sinfonie Serieuse\"/Allegro con energia",
          "SOZVAPQ12A8C13B63C",
          "Berwald: Symphonies Nos. 1/2/3/4",
          "AR2NS5Y1187FB5879D"
        },
        row);
    compareRows("reading not terminated correctly", null, reader.next());
  }
Example #17
0
  private void CSVinitialize() throws Exception {
    File f = new File("cars.csv");
    CSVReader.readCSV(f, rows, columnNames);

    // Calculate the mean, deviation for each column
    // Calculate the bin counter for each column
    CSVReader.calMean(rows, CSVReader.rowsNum);
    CSVReader.calDeviation(rows, CSVReader.rowsNum);
    CSVReader.countBin(rows, CSVReader.rowsNum);
    mean = CSVReader.mean;
    deviation = CSVReader.deviation;
    binCounter = CSVReader.binCounter;

    StringBuffer buffer = new StringBuffer();

    int len = rows.get(0).size();
    max = new Double[len];
    min = new Double[len];
    for (int i = 0; i < len; i++) {
      max[i] = Double.MIN_VALUE;
      min[i] = Double.MAX_VALUE;
    }

    columnNamestring = new String[columnNames.size()];
    columnNamestringZ = new String[columnNames.size() + 1];
    for (int i = 0; i < columnNames.size(); i++) {
      columnNamestring[i] = columnNames.get(i);
      columnNamestringZ[i] = columnNames.get(i);
    }
    columnNamestringZ[columnNames.size()] = "Unset";

    for (int irow = 0; irow < rows.size(); irow++) {
      ArrayList<Double> row = rows.get(irow);
      buffer = new StringBuffer();
      for (int ivalue = 0; ivalue < row.size(); ivalue++) {
        min[ivalue] = Math.min(min[ivalue], row.get(ivalue));
        max[ivalue] = Math.max(max[ivalue], row.get(ivalue));
      }
    }
  }
  private <T> void printModel(CSVReader<T> csvReader) throws Exception {

    T model;
    CSVReadOutcome<T> outcome;
    int attempts = 0;
    do {

      outcome = csvReader.read();
      model = outcome.getModel();
      if (outcome.getCSVReadingContext().isError())
        logger.warn(outcome.getCSVReadingContext().getError().getMessage());
      else if (model != null) logger.info(String.valueOf(model));

    } while (++attempts < 5);
  }
Example #19
0
  public static void main(String args[]) {
    final int DATA_OR_DEFINITION_SEARCH_COUNT = 2;
    String in = "";
    String out = "";
    ArrayList<String> mesgDefinitionsToOutput = new ArrayList<String>();
    ArrayList<String> dataMessagesToOutput = new ArrayList<String>();
    boolean fitToCsv = false;
    boolean csvToFit = false;
    boolean test = false;
    boolean checkIntegrity = false;
    boolean showInvalidValues = false;
    boolean invalidsToEmpty = false;
    boolean hideUnknownData = false;
    int nextArgumentDefinition = 0;
    int nextArgumentData = 0;
    int numUnknownFields = 0;
    int numUnknownMesgs = 0;

    int arg = 0;

    System.out.printf(
        "FIT CSV Tool - Protocol %d.%d Profile %.2f %s\n",
        Fit.PROTOCOL_VERSION_MAJOR,
        Fit.PROTOCOL_VERSION_MINOR,
        Fit.PROFILE_VERSION / 100.0,
        Fit.PROFILE_TYPE);

    while (arg < args.length) {
      if (args[arg].equals("-b")) {
        if ((args.length - arg) < 3) {
          printUsage();
          return;
        }

        fitToCsv = true;
        in = args[arg + 1];
        out = args[arg + 2];

        arg += 2;
      } else if (args[arg].equals("-c")) {
        if ((args.length - arg) < 3) {
          printUsage();
          return;
        }

        csvToFit = true;
        in = args[arg + 1];
        out = args[arg + 2];

        arg += 2;
      } else if (args[arg].equals("-t")) {
        test = true;
      } else if (args[arg].equals("-d")) {
        Fit.debug = true;
        test = true;
      } else if (args[arg].equals("-i")) {
        checkIntegrity = true;
      } else if (args[arg].equals("--defn")) {
        nextArgumentDefinition = DATA_OR_DEFINITION_SEARCH_COUNT;
      } else if (args[arg].equals("--data")) {
        nextArgumentData = DATA_OR_DEFINITION_SEARCH_COUNT;
      } else if (args[arg].charAt(0) != '-') {

        if (nextArgumentDefinition > 0) {
          mesgDefinitionsToOutput =
              new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(",")));
        } else if (nextArgumentData > 0) {
          dataMessagesToOutput =
              new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(",")));
        } else {
          in = args[arg];
          if (in.endsWith(".fit")) {
            fitToCsv = true;
            out = in.substring(0, in.length() - 4) + ".csv";
          } else if (in.endsWith(".csv")) {
            csvToFit = true;
            out = in.substring(0, in.length() - 4) + ".fit";
          }
        }
      } else if (args[arg].equals("-s")) {
        showInvalidValues = true;
      } else if (args[arg].equals("-se")) {
        showInvalidValues = true;
        invalidsToEmpty = true;
      } else if (args[arg].equals("-u")) {
        hideUnknownData = true;
      }

      if (nextArgumentDefinition > 0) {
        nextArgumentDefinition--;
        if ((nextArgumentDefinition == 0) && (mesgDefinitionsToOutput.isEmpty())) {
          System.out.println(
              "No mesg definitions defined for --defn option.  Use 'none' if no definitions are desired.");
          return;
        }
      }
      if (nextArgumentData > 0) {
        nextArgumentData--;
        if ((nextArgumentData == 0) && (dataMessagesToOutput.isEmpty())) {
          System.out.println(
              "No data messages defined for --data option.  Use 'none' if no data is desired.");
          return;
        }
      }
      arg++;
    }

    if (fitToCsv) {
      if ((out.length() >= 4)
          && (out.substring(out.length() - 4, out.length()).compareTo(".csv") == 0))
        out = out.substring(0, out.length() - 4); // Remove .csv extension.

      if (checkIntegrity) {
        try {
          if (!Decode.checkIntegrity((InputStream) new FileInputStream(in))) {
            if (!Decode.getInvalidDataSize())
              throw new RuntimeException("FIT file integrity failure.");
            else {
              System.out.println("FIT file integrity failure. Invalid file size in header.");
              System.out.println("Trying to continue...");
            }
          }
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }
      }

      if (test) {
        Tests tests = new Tests();
        System.out.println("Running FIT verification tests...");
        if (tests.run(in)) System.out.println("Passed FIT verification.");
        else System.out.println("Failed FIT verification.");
      }

      try {
        Decode decode = new Decode();
        MesgCSVWriter mesgWriter = new MesgCSVWriter(out + ".csv");
        FileInputStream fileInputStream = new FileInputStream(in);
        if (showInvalidValues == true) decode.showInvalidValues();

        MesgFilter mesgFilter = new MesgFilter();
        mesgFilter.setMesgDefinitionsToOutput(mesgDefinitionsToOutput);
        mesgFilter.setDataMessagesToOutput(dataMessagesToOutput);

        MesgDataCSVWriter dataMesgWriter = new MesgDataCSVWriter(out + "_data.csv");
        if (invalidsToEmpty) {
          mesgWriter.showInvalidsAsEmptyCells();
          dataMesgWriter.showInvalidsAsEmptyCells();
        }

        if (hideUnknownData) {
          mesgWriter.hideUnknownData();
          dataMesgWriter.hideUnknownData();
        }

        mesgFilter.addListener((MesgDefinitionListener) mesgWriter);
        mesgFilter.addListener((MesgListener) mesgWriter);
        mesgFilter.addListener((MesgListener) dataMesgWriter);

        decode.addListener((MesgDefinitionListener) mesgFilter);
        decode.addListener((MesgListener) mesgFilter);

        while (fileInputStream.available()
            > 0) { // Try to read a file while more data is available.
          try {
            decode.read((InputStream) fileInputStream);
            decode.nextFile(); // Initialize to read next file (if any).
          } catch (FitRuntimeException e) {
            if (decode.getInvalidDataSize()) continue;
          }
        }

        mesgWriter.close();
        dataMesgWriter.close();

        numUnknownFields = mesgWriter.getNumUnknownFields();
        numUnknownMesgs = mesgWriter.getNumUnknownMesgs();

      } catch (java.io.IOException e) {
        throw new RuntimeException(e);
      }

      if (hideUnknownData)
        System.out.printf(
            "Hid %d unknown field(s) and %d unknown message(s).\n",
            numUnknownFields, numUnknownMesgs);
      System.out.printf("FIT binary file %s decoded to %s*.csv files.\n", in, out);
    } else if (csvToFit) {
      try {
        FileEncoder encoder = new FileEncoder(new File(out));
        if (!CSVReader.read((InputStream) new FileInputStream(in), encoder, encoder))
          throw new RuntimeException("FIT encoding error.");
        encoder.close();

        System.out.printf("%s encoded into FIT binary file %s.\n", in, out);
      } catch (java.io.IOException e) {
        throw new RuntimeException(e);
      }
    } else {
      printUsage();
    }
  }
Example #20
0
 @Test
 public void testEmpty() throws IOException {
   String data = "";
   CSVReader reader = new CSVReader(new StringReader(data));
   compareRows("couldn't read empty file correctly", null, reader.next());
 }