1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.Writer;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.expeditee.gui.Frame;
|
---|
11 | import org.expeditee.gui.FrameIO;
|
---|
12 | import org.expeditee.gui.MessageBay;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 | import org.expeditee.settings.UserSettings;
|
---|
15 |
|
---|
16 | public abstract class AbstractHTMLWriter extends DefaultTreeWriter {
|
---|
17 |
|
---|
18 | private static final String INDENT = " ";
|
---|
19 |
|
---|
20 | private static final String FILE_FOLDER_SUFFIX = "_files";
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
24 | _format = ".html";
|
---|
25 | super.initialise(start, writer);
|
---|
26 |
|
---|
27 | // Clear the filesFolder
|
---|
28 | String filesFolderName = FrameIO.EXPORTS_DIR + getFilesFolder();
|
---|
29 | if (filesFolderName.length() > 0) {
|
---|
30 | File filesFolder = new File(filesFolderName);
|
---|
31 | for (File f : filesFolder.listFiles()) {
|
---|
32 | f.delete();
|
---|
33 | }
|
---|
34 | filesFolder.delete();
|
---|
35 | _filesFolderName = null;
|
---|
36 | }
|
---|
37 |
|
---|
38 | _writer.write("<html>" + ItemWriter.NEW_LINE);
|
---|
39 | _writer.write("<head>" + ItemWriter.NEW_LINE);
|
---|
40 | _writer
|
---|
41 | .write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
|
---|
42 | _writer.write("<title>" + start.getTitle());
|
---|
43 | _writer.write("</title>" + ItemWriter.NEW_LINE);
|
---|
44 |
|
---|
45 | // Write in the style for the headers and body text
|
---|
46 | _writer.write("<style>" + ItemWriter.NEW_LINE);
|
---|
47 | _writer.write("<!--" + ItemWriter.NEW_LINE);
|
---|
48 |
|
---|
49 | // Go through all the styles
|
---|
50 | List<Text> style = UserSettings.Style;
|
---|
51 |
|
---|
52 | writeStyleInfo(_writer, "P", style.get(0));
|
---|
53 | for (int i = 1; i < style.size(); i++) {
|
---|
54 | writeStyleInfo(_writer, "H" + i, style.get(i));
|
---|
55 | }
|
---|
56 | _writer.write("-->" + ItemWriter.NEW_LINE);
|
---|
57 | _writer.write("</style>" + ItemWriter.NEW_LINE);
|
---|
58 |
|
---|
59 | _writer.write("</head>" + ItemWriter.NEW_LINE);
|
---|
60 | _writer.write("<body>" + ItemWriter.NEW_LINE);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void writeStyleInfo(ProxyWriter writer, String styleName, Text style)
|
---|
64 | throws IOException {
|
---|
65 | if (style == null)
|
---|
66 | return;
|
---|
67 |
|
---|
68 | Font font = style.getPaintFont();
|
---|
69 | if (font == null)
|
---|
70 | return;
|
---|
71 |
|
---|
72 | writer.write(styleName + " { font-family: "
|
---|
73 | + Conversion.getCssFontFamily(font.getFamily()));
|
---|
74 |
|
---|
75 | // writer.write("; font-size: " + Math.round(style.getSize()) + "px");
|
---|
76 |
|
---|
77 | if (font.isBold()) {
|
---|
78 | writer.write("; font-weight: bold");
|
---|
79 | } else {
|
---|
80 | writer.write("; font-weight: normal");
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (font.isItalic()) {
|
---|
84 | writer.write("; font-style: italic");
|
---|
85 | } else {
|
---|
86 | writer.write("; font-style: normal");
|
---|
87 | }
|
---|
88 | Color c = style.getBackgroundColor();
|
---|
89 | if (c != null) {
|
---|
90 | writer.write("; background-color: " + Conversion.getCssColor(c));
|
---|
91 | }
|
---|
92 | c = style.getColor();
|
---|
93 | if (c != null) {
|
---|
94 | writer.write("; color: " + Conversion.getCssColor(c));
|
---|
95 | }
|
---|
96 |
|
---|
97 | c = style.getBorderColor();
|
---|
98 | if (c != null) {
|
---|
99 | writer.write("; outline-color: " + Conversion.getCssColor(c));
|
---|
100 | writer.write("; outline-style: solid");
|
---|
101 | }
|
---|
102 |
|
---|
103 | writer.write("}" + ItemWriter.NEW_LINE);
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | private String _filesFolderName = null;
|
---|
108 |
|
---|
109 | protected String getFilesFolder() {
|
---|
110 | if (_filesFolderName == null) {
|
---|
111 | _filesFolderName = _filename.substring(0, _filename
|
---|
112 | .lastIndexOf('.'))
|
---|
113 | + FILE_FOLDER_SUFFIX;
|
---|
114 | File dir = new File(_filesFolderName);
|
---|
115 | _filesFolderName = dir.getName();
|
---|
116 | if (!dir.exists()) {
|
---|
117 | try {
|
---|
118 | dir.mkdirs();
|
---|
119 | } catch (Exception e) {
|
---|
120 | MessageBay.errorMessage("Could not create folder: "
|
---|
121 | + dir.getName());
|
---|
122 | _filesFolderName = null;
|
---|
123 | return "";
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | return _filesFolderName + File.separator;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | protected String finaliseTree() throws IOException {
|
---|
133 | _writer.write("</body>" + ItemWriter.NEW_LINE);
|
---|
134 | return super.finaliseTree();
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected void indent() throws IOException {
|
---|
138 | for (int i = 0; i < getIndent(); i++)
|
---|
139 | _writer.write(INDENT);
|
---|
140 | }
|
---|
141 | }
|
---|