1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.tethys.core.control;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogManager;
21 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogger;
22 import io.github.tonywasher.joceanus.tethys.api.dialog.TethysUIFileSelector;
23 import io.github.tonywasher.joceanus.tethys.core.base.TethysUIDataException;
24 import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
25 import org.jsoup.Jsoup;
26 import org.jsoup.nodes.Document;
27 import org.jsoup.nodes.Document.OutputSettings;
28 import org.jsoup.nodes.Element;
29 import org.jsoup.nodes.Entities.EscapeMode;
30 import org.jsoup.select.Elements;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.nio.charset.StandardCharsets;
36
37
38
39
40 public class TethysUICoreHTMLToFile {
41
42
43
44 private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(TethysUICoreHTMLToFile.class);
45
46
47
48
49 private static final String ELEMENT_A = "a";
50
51
52
53
54 private static final String ATTR_HREF = "href";
55
56
57
58
59 private static final String ELEMENT_DIV = "div";
60
61
62
63
64 private static final String ELEMENT_STYLE = "style";
65
66
67
68
69 private final TethysUICoreFactory<?> theFactory;
70
71
72
73
74 private final TethysUICoreHTMLManager theHTMLManager;
75
76
77
78
79 private TethysUIFileSelector theFileSelector;
80
81
82
83
84
85
86
87 public TethysUICoreHTMLToFile(final TethysUICoreFactory<?> pFactory,
88 final TethysUICoreHTMLManager pHTMLManager) {
89
90 theFactory = pFactory;
91 theHTMLManager = pHTMLManager;
92 }
93
94
95
96
97
98
99 private TethysUIFileSelector initFileSelector() {
100 final TethysUIFileSelector myFileSelector = theFactory.dialogFactory().newFileSelector();
101 myFileSelector.setUseSave(true);
102 myFileSelector.setExtension(".html");
103 return myFileSelector;
104 }
105
106
107
108
109 public void writeToFile() {
110 try {
111
112 if (theFileSelector == null) {
113 theFileSelector = initFileSelector();
114 }
115
116
117 final File myFile = theFileSelector.selectFile();
118 if (myFile != null) {
119 writeToFile(myFile);
120 }
121 } catch (OceanusException e) {
122 LOGGER.error("Failed to write to file", e);
123 }
124 }
125
126
127
128
129
130
131
132 private void writeToFile(final File pFile) throws OceanusException {
133
134 final Document myDoc = createXMLDocument(theHTMLManager.getHTMLString(), theHTMLManager.getProcessedCSS());
135
136
137 writeDocumentToFile(myDoc, pFile);
138 }
139
140
141
142
143
144
145
146
147 private static Document createXMLDocument(final String pXML,
148 final String pStyleSheet) {
149
150 final Document myDoc = Jsoup.parse(pXML);
151
152
153 final OutputSettings mySettings = myDoc.outputSettings();
154 mySettings.charset(StandardCharsets.UTF_8);
155 mySettings.escapeMode(EscapeMode.extended);
156 mySettings.prettyPrint(true);
157
158
159 final Element myElement = myDoc.createElement(ELEMENT_STYLE);
160 myElement.text(pStyleSheet);
161
162
163 final Element myHead = myDoc.head();
164 myHead.appendChild(myElement);
165
166
167 final Elements myLinks = myDoc.getElementsByTag(ELEMENT_A);
168 myLinks.forEach(l -> {
169
170 l.attributes().remove(ATTR_HREF);
171 l.tagName(ELEMENT_DIV);
172 });
173
174
175 return myDoc;
176 }
177
178
179
180
181
182
183
184
185 private static void writeDocumentToFile(final Document pDoc,
186 final File pFile) throws OceanusException {
187
188 try (PrintWriter myWriter = new PrintWriter(pFile, StandardCharsets.UTF_8.name())) {
189
190 final String myHTML = pDoc.outerHtml();
191 myWriter.print(myHTML);
192
193 } catch (IOException e) {
194 throw new TethysUIDataException("Failed to output XML", e);
195 }
196 }
197 }