source: trunk/src/org/expeditee/io/AbstractHTMLWriter.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 4.5 KB
Line 
1/**
2 * AbstractHTMLWriter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.File;
22import java.io.IOException;
23import java.io.Writer;
24import java.util.List;
25
26import org.expeditee.core.Colour;
27import org.expeditee.core.Font;
28import org.expeditee.gui.Frame;
29import org.expeditee.gui.FrameIO;
30import org.expeditee.gui.MessageBay;
31import org.expeditee.items.Text;
32import org.expeditee.settings.UserSettings;
33
34public abstract class AbstractHTMLWriter extends DefaultTreeWriter {
35
36 private static final String INDENT = " ";
37
38 private static final String FILE_FOLDER_SUFFIX = "_files";
39
40 @Override
41 protected void initialise(Frame start, Writer writer) throws IOException {
42 _format = ".html";
43 super.initialise(start, writer);
44
45 // Clear the filesFolder
46 String filesFolderName = FrameIO.EXPORTS_DIR + getFilesFolder();
47 if (filesFolderName.length() > 0) {
48 File filesFolder = new File(filesFolderName);
49 for (File f : filesFolder.listFiles()) {
50 f.delete();
51 }
52 filesFolder.delete();
53 _filesFolderName = null;
54 }
55
56 _writer.write("<html>" + ItemWriter.NEW_LINE);
57 _writer.write("<head>" + ItemWriter.NEW_LINE);
58 _writer
59 .write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
60 _writer.write("<title>" + start.getTitle());
61 _writer.write("</title>" + ItemWriter.NEW_LINE);
62
63 // Write in the style for the headers and body text
64 _writer.write("<style>" + ItemWriter.NEW_LINE);
65 _writer.write("<!--" + ItemWriter.NEW_LINE);
66
67 // Go through all the styles
68 List<Text> style = UserSettings.Style.get();
69
70 writeStyleInfo(_writer, "P", style.get(0));
71 for (int i = 1; i < style.size(); i++) {
72 writeStyleInfo(_writer, "H" + i, style.get(i));
73 }
74 _writer.write("-->" + ItemWriter.NEW_LINE);
75 _writer.write("</style>" + ItemWriter.NEW_LINE);
76
77 _writer.write("</head>" + ItemWriter.NEW_LINE);
78 _writer.write("<body>" + ItemWriter.NEW_LINE);
79 }
80
81 private void writeStyleInfo(ProxyWriter writer, String styleName, Text style)
82 throws IOException {
83 if (style == null)
84 return;
85
86 Font font = style.getPaintFont();
87 if (font == null)
88 return;
89
90 writer.write(styleName + " { font-family: " + Conversion.getCssFontFamily(font.getFamilyName()));
91
92 // writer.write("; font-size: " + Math.round(style.getSize()) + "px");
93
94 if (font.isBold()) {
95 writer.write("; font-weight: bold");
96 } else {
97 writer.write("; font-weight: normal");
98 }
99
100 if (font.isItalic()) {
101 writer.write("; font-style: italic");
102 } else {
103 writer.write("; font-style: normal");
104 }
105 Colour c = style.getBackgroundColor();
106 if (c != null) {
107 writer.write("; background-color: " + Conversion.getCssColor(c));
108 }
109 c = style.getColor();
110 if (c != null) {
111 writer.write("; color: " + Conversion.getCssColor(c));
112 }
113
114 c = style.getBorderColor();
115 if (c != null) {
116 writer.write("; outline-color: " + Conversion.getCssColor(c));
117 writer.write("; outline-style: solid");
118 }
119
120 writer.write("}" + ItemWriter.NEW_LINE);
121
122 }
123
124 private String _filesFolderName = null;
125
126 protected String getFilesFolder() {
127 if (_filesFolderName == null) {
128 _filesFolderName = _filename.substring(0, _filename
129 .lastIndexOf('.'))
130 + FILE_FOLDER_SUFFIX;
131 File dir = new File(_filesFolderName);
132 _filesFolderName = dir.getName();
133 if (!dir.exists()) {
134 try {
135 dir.mkdirs();
136 } catch (Exception e) {
137 MessageBay.errorMessage("Could not create folder: "
138 + dir.getName());
139 _filesFolderName = null;
140 return "";
141 }
142 }
143 }
144
145 return _filesFolderName + File.separator;
146 }
147
148 @Override
149 protected String finaliseTree() throws IOException {
150 _writer.write("</body>" + ItemWriter.NEW_LINE);
151 return super.finaliseTree();
152 }
153
154 protected void indent() throws IOException {
155 for (int i = 0; i < getIndent(); i++)
156 _writer.write(INDENT);
157 }
158}
Note: See TracBrowser for help on using the repository browser.