-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrinter.java
More file actions
182 lines (146 loc) · 6.01 KB
/
Printer.java
File metadata and controls
182 lines (146 loc) · 6.01 KB
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
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;
import java.util.ArrayList;
/**
* Created by krejcir on 1.5.14.
*/
public class Printer {
private DocumentBuilderFactory documentBuilderFactory;
private DocumentBuilder documentBuilder;
public Printer() {
try {
this.documentBuilderFactory = DocumentBuilderFactory.newInstance();
this.documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
}
}
public void print(Schedule schedule, String file) {
Item[][] prepareSchedule = this.prepareSchedule(schedule);
Document document = documentBuilder.newDocument();
Element body = this.buildHtmlPage(document);
body.appendChild(this.buildTable(document, prepareSchedule));
this.write(document, file);
}
public void print(SmartScheduleException e, String file) {
Document document = documentBuilder.newDocument();
Element body = this.buildHtmlPage(document);
Element h1 = document.createElement("h1");
h1.appendChild(document.createTextNode(e.getMessage()));
body.appendChild(h1);
this.write(document, file);
}
private Item[][] prepareSchedule(Schedule schedule) {
ArrayList<Item> items = schedule.getItems();
Item[][] table = new Item[Solver.WEEK_LENGTH][Solver.DAY_LENGTH + 1];
for (Item item : items) {
for (int i = 0; i < item.getLength(); i++) {
table[item.getDay()][item.getStart() + i] = item;
}
}
return table;
}
private void write(Document document, String file) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(file));
transformer.transform(source, result);
System.out.println("File saved!");
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());
} catch (TransformerException e) {
System.out.println(e.getMessage());
}
}
private Element buildHtmlPage(Document document) {
Element html = document.createElement("html");
document.appendChild(html);
Element head = document.createElement("head");
html.appendChild(head);
Element css = document.createElement("link");
css.setAttribute("rel", "stylesheet");
css.setAttribute("type", "text/css");
css.setAttribute("href", "./src/style.css");
head.appendChild(css);
Element body = document.createElement("body");
html.appendChild(body);
return body;
}
private Element buildTable(Document document, Item[][] prepareSchedule) {
Element table = document.createElement("table");
Element tHead = document.createElement("thead");
table.appendChild(tHead);
Element tr = document.createElement("tr");
tHead.appendChild(tr);
Element thn = document.createElement("th");
thn.appendChild(document.createTextNode(""));
tr.appendChild(thn);
for (int i = 0; i < prepareSchedule[0].length; i++) {
Element th = document.createElement("th");
th.appendChild(document.createTextNode(i + ""));
tr.appendChild(th);
}
Element tBody = document.createElement("tbody");
table.appendChild(tBody);
for (int i = 0; i < prepareSchedule.length; i++) {
Element trb = document.createElement("tr");
tBody.appendChild(trb);
Element tday = document.createElement("td");
String dayName = "";
switch (i) {
case Day.MONDAY:
dayName = "Monday";
break;
case Day.TUESDAY:
dayName = "Tuesday";
break;
case Day.WEDNESDAY:
dayName = "Wednesday";
break;
case Day.THURSDAY:
dayName = "Thursday";
break;
case Day.FRIDAY:
dayName = "Friday";
break;
case Day.SATURDAY:
dayName = "Saturday";
break;
case Day.SUNDAY:
dayName = "Sunday";
break;
}
tday.appendChild(document.createTextNode(dayName));
trb.appendChild(tday);
for (int j = 0; j < prepareSchedule[i].length; j++) {
Element td = document.createElement("td");
Item item = prepareSchedule[i][j];
if (prepareSchedule[i][j] != null) {
if (item.getStart() <= j && j <= item.getStart() + item.getLength()) {
if (item instanceof Exercise) {
td.appendChild(document.createTextNode(item.getSubjectName()));
} else if (item instanceof Lecture) {
Element bold = document.createElement("b");
bold.appendChild(document.createTextNode(item.getSubjectName()));
td.appendChild(bold);
}
}
}
trb.appendChild(td);
}
}
return table;
}
}