-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTsvReader.java
270 lines (233 loc) · 9.81 KB
/
TsvReader.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2015 Pradeeban Kathiravelu and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
import util.DirectoryUtil;
import util.Formatter;
import util.ProfileConstants;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Read the tsv input files
* bibtex converted with http://ref.lexique.org/
*/
public class TsvReader {
private static String[][] studentArray;
private static String[][] profArray;
public static void main(String[] args) {
try {
studentArray = TsvReader.read("students.tsv");
profArray = TsvReader.read("profs.tsv");
log(ProfileConstants.isDebugEnabled);
DirectoryUtil.createDirectories();
writeStudents();
writeProfessors();
writeAboutUs();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Write About Us page
*
* @throws IOException
*/
public static void writeAboutUs() throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
Path profInitPath = Paths.get("meta-about-us-professor.html");
Charset charset = StandardCharsets.UTF_8;
Path outPath = Paths.get("profiles/" + "about-us.html");
String content = "<h2>Professors</h2>\n";
for (int i = 1; i < profArray.length; i++) {
if (profArray[i].length > 1) {
String profTemp = new String(Files.readAllBytes(profInitPath), charset);
for (int j = 1; j < profArray[i].length; j++) {
profTemp = profTemp.replaceAll("PLACEHOLDER-" + j, profArray[i][j]);
}
profTemp = profTemp.replaceAll("PLACEHOLDER-0",Formatter.replaceSpace(profArray[i][1]));
content+= profTemp + "\n\n";
}
}
content += "\n<h2>Students</h2>\n";
Path studentInitPath = Paths.get("meta-about-us-student.html");
for (int i = 1; i < studentArray.length; i++) {
String studentTemp = new String(Files.readAllBytes(studentInitPath), charset);
if (studentArray[i].length > 1) {
for (int j = 1; j < studentArray[i].length; j++) {
String rep = "\n\n";
String value = studentArray[i][j];
studentTemp = refactorImages(studentTemp, j, value);
if (j == 5) {
value = dateFormat.format(new Date(value));
}
if (!value.equals("") && value!=null) {
if (j==3) {
studentTemp = studentTemp.replaceAll("Static_Text_S3", ProfileConstants.Static_Text_S3);
}
if (j==9) {
studentTemp = studentTemp.replaceAll("Static_Text_S9", ProfileConstants.Static_Text_S9);
}
studentTemp = studentTemp.replaceAll("PLACEHOLDER-" + j, value);
} else {
studentTemp = studentTemp.replaceAll("Static_Text_S" + j, "");
studentTemp = studentTemp.replaceAll("PLACEHOLDER-" + j, "");
}
studentTemp = studentTemp.replaceAll("PAGE-BREAK", rep);
}
if (studentArray[i].length <= 9) {
for (int j = studentArray[i].length; j <= 9; j++) {
studentTemp = studentTemp.replaceAll("PLACEHOLDER-" + j, "");
studentTemp = studentTemp.replaceAll("Static_Text_S" + j, "");
}
}
studentTemp = studentTemp.replaceAll("PLACEHOLDER-0",Formatter.replaceSpace(studentArray[i][1]));
content+= studentTemp + "\n\n";
}
}
Files.write(outPath, content.getBytes(charset));
}
private static String refactorImages(String studentTemp, int j, String value) {
if (j==4) {
if (!value.equals("") && value!=null) {
studentTemp = studentTemp.replaceAll("PLACEHOLDER-4", "<img src=\"PLACEHOLDER-4\" alt=\"\" height=\"200\" />");
} else {
studentTemp = studentTemp.replaceAll("PLACEHOLDER-4","");
}
}
return studentTemp;
}
private static String refactorImagesAlignRight(String studentTemp, int j, String value) {
if (j==4) {
if (!value.equals("") && value!=null) {
studentTemp = studentTemp.replaceAll("PLACEHOLDER-4", "<img src=\"PLACEHOLDER-4\" alt=\"\" height=\"200\" align=\"right\" />\n");
} else {
studentTemp = studentTemp.replaceAll("PLACEHOLDER-4","");
}
}
return studentTemp;
}
/**
* Write Student HTML files
*
* @throws IOException
*/
public static void writeStudents() throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
Path path = Formatter.chooseMetaFile(ProfileConstants.withHeaders, "meta-student");
Charset charset = StandardCharsets.UTF_8;
for (int i = 1; i < studentArray.length; i++) {
String content = new String(Files.readAllBytes(path), charset);
if (studentArray[i].length > 1) {
// ignore the timestamp
Path newPath = Paths.get("profiles/students/"+studentArray[i][1] + ".html");
for (int j = 1; j < studentArray[i].length; j++) {
String rep;
if (ProfileConstants.withHeaders) {
rep = "<br /><br />";
} else {
rep = "\n\n";
}
String value = studentArray[i][j];
content = refactorImagesAlignRight(content, j, value);
if (j == 5) {
value = dateFormat.format(new Date(value));
}
if (!value.equals("") && value!=null) {
if (j==3) {
content = content.replaceAll("Static_Text_S3", ProfileConstants.Static_Text_S3);
}
if (j==9) {
content = content.replaceAll("Static_Text_S9", ProfileConstants.Static_Text_S9);
}
content = content.replaceAll("PLACEHOLDER-" + j, value);
} else {
content = content.replaceAll("Static_Text_S" + j, "");
content = content.replaceAll("PLACEHOLDER-" + j, "");
}
content = content.replaceAll("PAGE-BREAK", rep);
}
if (studentArray[i].length <= 9) {
for (int j = studentArray[i].length; j <= 9; j++) {
content = content.replaceAll("PLACEHOLDER-" + j, "");
content = content.replaceAll("Static_Text_S" + j, "");
}
}
Files.write(newPath, content.getBytes(charset));
}
}
}
/**
* Write Professor HTML files.
*
* @throws IOException
*/
public static void writeProfessors() throws IOException {
Path path = Formatter.chooseMetaFile(ProfileConstants.withHeaders, "meta-professor");
Charset charset = StandardCharsets.UTF_8;
for (int i = 1; i < profArray.length; i++) {
String content = new String(Files.readAllBytes(path), charset);
if (profArray[i].length > 1) {
// ignore the timestamp
Path newPath = Paths.get("profiles/profs/" + profArray[i][1] + ".html");
for (int j = 1; j < profArray[i].length; j++) {
content = content.replaceAll("PLACEHOLDER-" + j, profArray[i][j]);
}
Files.write(newPath, content.getBytes(charset));
}
}
}
/**
* Log to the console.
*/
public static void log(boolean isDebugEnabled) {
if (isDebugEnabled) {
System.out.println("Students: ");
query(studentArray);
System.out.println("***********");
System.out.println("Professors: ");
query(profArray);
}
}
/**
* Read the tsv file
*
* @param fileName the name of the file
* @return a 2D array
* @throws IOException if the file does not exist.
*/
public static String[][] read(String fileName) throws IOException {
String[][] resultArray;
List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
resultArray = new String[lines.size()][];
for (int i = 0; i < lines.size(); i++) {
resultArray[i] = lines.get(i).split("\t");
}
return resultArray;
}
/**
* Just to check everything is in place by logging the array to the console.
*
* @param strArray the 2D array
*/
public static void query(String[][] strArray) {
// ignore the header
for (int i = 1; i < strArray.length; i++) {
// ignore the timestamp
for (int j = 1; j < strArray[i].length; j++) {
System.out.println("[" + i + "][" + j + "]" + strArray[i][j]);
}
}
}
}