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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.awt.Color;
|
---|
22 | import java.awt.Font;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.Writer;
|
---|
26 | import java.util.List;
|
---|
27 |
|
---|
28 | import org.expeditee.gui.Frame;
|
---|
29 | import org.expeditee.gui.FrameIO;
|
---|
30 | import org.expeditee.gui.MessageBay;
|
---|
31 | import org.expeditee.items.Text;
|
---|
32 | import org.expeditee.settings.UserSettings;
|
---|
33 |
|
---|
34 | public 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: "
|
---|
91 | + Conversion.getCssFontFamily(font.getFamily()));
|
---|
92 |
|
---|
93 | // writer.write("; font-size: " + Math.round(style.getSize()) + "px");
|
---|
94 |
|
---|
95 | if (font.isBold()) {
|
---|
96 | writer.write("; font-weight: bold");
|
---|
97 | } else {
|
---|
98 | writer.write("; font-weight: normal");
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (font.isItalic()) {
|
---|
102 | writer.write("; font-style: italic");
|
---|
103 | } else {
|
---|
104 | writer.write("; font-style: normal");
|
---|
105 | }
|
---|
106 | Color c = style.getBackgroundColor();
|
---|
107 | if (c != null) {
|
---|
108 | writer.write("; background-color: " + Conversion.getCssColor(c));
|
---|
109 | }
|
---|
110 | c = style.getColor();
|
---|
111 | if (c != null) {
|
---|
112 | writer.write("; color: " + Conversion.getCssColor(c));
|
---|
113 | }
|
---|
114 |
|
---|
115 | c = style.getBorderColor();
|
---|
116 | if (c != null) {
|
---|
117 | writer.write("; outline-color: " + Conversion.getCssColor(c));
|
---|
118 | writer.write("; outline-style: solid");
|
---|
119 | }
|
---|
120 |
|
---|
121 | writer.write("}" + ItemWriter.NEW_LINE);
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | private String _filesFolderName = null;
|
---|
126 |
|
---|
127 | protected String getFilesFolder() {
|
---|
128 | if (_filesFolderName == null) {
|
---|
129 | _filesFolderName = _filename.substring(0, _filename
|
---|
130 | .lastIndexOf('.'))
|
---|
131 | + FILE_FOLDER_SUFFIX;
|
---|
132 | File dir = new File(_filesFolderName);
|
---|
133 | _filesFolderName = dir.getName();
|
---|
134 | if (!dir.exists()) {
|
---|
135 | try {
|
---|
136 | dir.mkdirs();
|
---|
137 | } catch (Exception e) {
|
---|
138 | MessageBay.errorMessage("Could not create folder: "
|
---|
139 | + dir.getName());
|
---|
140 | _filesFolderName = null;
|
---|
141 | return "";
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | return _filesFolderName + File.separator;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | protected String finaliseTree() throws IOException {
|
---|
151 | _writer.write("</body>" + ItemWriter.NEW_LINE);
|
---|
152 | return super.finaliseTree();
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected void indent() throws IOException {
|
---|
156 | for (int i = 0; i < getIndent(); i++)
|
---|
157 | _writer.write(INDENT);
|
---|
158 | }
|
---|
159 | }
|
---|