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