]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/util/CSVParser.java
change build.xml clean ..
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / util / CSVParser.java
1 package at.ac.tuwien.lsdc.util;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import at.ac.tuwien.lsdc.types.Application;
10 import au.com.bytecode.opencsv.CSVReader;
11 import au.com.bytecode.opencsv.CSVWriter;
12
13 public class CSVParser {
14
15         private static int count = 0;
16
17         public static LinkedList<Application> parseFile(File csvFile)
18                         throws IOException {
19                 CSVReader reader = new CSVReader(new FileReader(csvFile), ';',
20                                 CSVWriter.NO_QUOTE_CHARACTER);
21                 List<String[]> file = reader.readAll();
22                 LinkedList<Application> apps = new LinkedList<Application>();
23                 // start at 1 to ignore header
24                 for (int i = 1; i < file.size(); i++) {
25                         String[] line = file.get(i);
26                         if (line.length != 5) {
27                                 reader.close();
28                                 throw new IOException("CSVParser: Not right amount of elements in line " + i + 1);
29                         } else {
30                                 try {
31                                         Application a = parseLine(line);
32                                         apps.add(a);
33                                 } catch (NumberFormatException e) {
34                                         reader.close();
35                                         throw new IOException("Couldn't parse line");
36                                 }
37                         }
38                 }
39                 reader.close();
40                 return apps;
41         }
42
43         private static Application parseLine(String[] appStr)
44                         throws NumberFormatException {
45                 int id = count;
46                 count++;
47                 long timestamp = Long.parseLong(appStr[0]);
48                 int size = Integer.parseInt(appStr[1]);
49                 int ram = Integer.parseInt(appStr[2]);
50                 int cpu = Integer.parseInt(appStr[3]);
51                 int duration = Integer.parseInt(appStr[4]);
52                 Application app = new Application(id, timestamp, size, ram, cpu,
53                                 duration);
54                 return app;
55         }
56
57 }