1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.benerator.csv;
28
29 import com.rapiddweller.benerator.dataset.DatasetUtil;
30 import com.rapiddweller.common.ConfigurationError;
31 import com.rapiddweller.common.Converter;
32 import com.rapiddweller.format.DataContainer;
33 import com.rapiddweller.format.csv.CSVLineIterator;
34 import com.rapiddweller.script.WeightedSample;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39
40
41
42
43
44
45
46
47 public class CSVGeneratorUtil {
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static <T> List<WeightedSample<T>> parseDatasetFiles(
62 String datasetName, char separator, String nesting, String filenamePattern,
63 String encoding, Converter<String, T> converter) {
64 String[] dataFilenames;
65 if (nesting == null || datasetName == null) {
66 dataFilenames = new String[] {filenamePattern};
67 } else {
68 dataFilenames = DatasetUtil.getDataFiles(filenamePattern, datasetName, nesting);
69 }
70 List<WeightedSample<T>> samples = new ArrayList<>();
71 for (String dataFilename : dataFilenames) {
72 parseFile(dataFilename, separator, encoding, converter, samples);
73 }
74 return samples;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static <T> List<WeightedSample<T>> parseFile(String filename, char separator, String encoding,
88 Converter<String, T> converter) {
89 return parseFile(filename, separator, encoding, converter, new ArrayList<>());
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103 public static <T> List<WeightedSample<T>> parseFile(String filename, char separator, String encoding,
104 Converter<String, T> converter, List<WeightedSample<T>> samples) {
105 try {
106 CSVLineIterator iterator = new CSVLineIterator(filename, separator, encoding);
107 DataContainer<String[]> container = new DataContainer<>();
108 while ((container = iterator.next(container)) != null) {
109 String[] tokens = container.getData();
110 if (tokens.length == 0) {
111 continue;
112 }
113 double weight = (tokens.length < 2 || tokens[1] == null || tokens[1].trim().length() == 0 ? 1. : Double.parseDouble(tokens[1].trim()));
114 T value = converter.convert(tokens[0]);
115 WeightedSample<T> sample = new WeightedSample<>(value, weight);
116 samples.add(sample);
117 }
118 return samples;
119 } catch (IOException e) {
120 throw new ConfigurationError(e);
121 }
122 }
123
124 }