[919] | 1 | /**
|
---|
| 2 | * oldHTMLWriter.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 |
|
---|
[4] | 19 | package org.expeditee.io;
|
---|
| 20 |
|
---|
| 21 | import java.io.IOException;
|
---|
[306] | 22 | import java.io.Writer;
|
---|
[4] | 23 | import java.util.List;
|
---|
| 24 |
|
---|
| 25 | import org.expeditee.gui.Frame;
|
---|
| 26 | import org.expeditee.items.Item;
|
---|
| 27 | import org.expeditee.items.Picture;
|
---|
| 28 | import org.expeditee.items.Text;
|
---|
| 29 |
|
---|
[376] | 30 | public class oldHTMLWriter extends DefaultTreeWriter {
|
---|
[4] | 31 |
|
---|
| 32 | private int _indent = 0;
|
---|
| 33 |
|
---|
| 34 | private static final String INDENT = " ";
|
---|
| 35 |
|
---|
| 36 | @Override
|
---|
[306] | 37 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
[4] | 38 | _format = ".html";
|
---|
[306] | 39 | super.initialise(start, writer);
|
---|
[4] | 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | protected void writeStartLink(Item linker) throws IOException {
|
---|
| 44 | // only output text items
|
---|
| 45 | if (!(linker instanceof Text)) {
|
---|
| 46 | _indent++;
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | Text text = (Text) linker;
|
---|
[80] | 51 | List<String> toWrite = text.getTextList();
|
---|
[4] | 52 |
|
---|
| 53 | String first = toWrite.get(0);
|
---|
| 54 | int ind = first.indexOf(":");
|
---|
| 55 |
|
---|
| 56 | if (ind > 0) {
|
---|
| 57 | String tag = first.substring(0, ind);
|
---|
| 58 | String remain = first.substring(ind + 1);
|
---|
| 59 |
|
---|
| 60 | if (remain.length() > 0) {
|
---|
| 61 | // indenting for comments
|
---|
| 62 | indent();
|
---|
| 63 | _writer.write("<!-- " + remain);
|
---|
| 64 |
|
---|
| 65 | for (int i = 1; i < toWrite.size(); i++)
|
---|
| 66 | _writer.write(ItemWriter.NEW_LINE + toWrite.get(i));
|
---|
| 67 |
|
---|
| 68 | _writer.write(" -->" + ItemWriter.NEW_LINE);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | // indenting for tag
|
---|
| 72 | indent();
|
---|
| 73 | _writer.write("<" + tag + ">" + ItemWriter.NEW_LINE);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | _indent++;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | @Override
|
---|
| 80 | protected void writeEndLink(Item linker) throws IOException {
|
---|
| 81 | _indent--;
|
---|
| 82 |
|
---|
| 83 | // only output text items
|
---|
| 84 | if (!(linker instanceof Text))
|
---|
| 85 | return;
|
---|
| 86 |
|
---|
| 87 | Text text = (Text) linker;
|
---|
[80] | 88 | List<String> toWrite = text.getTextList();
|
---|
[4] | 89 |
|
---|
| 90 | String first = toWrite.get(0);
|
---|
| 91 | int ind = first.indexOf(":");
|
---|
| 92 |
|
---|
| 93 | if (ind > 0) {
|
---|
| 94 | String tag = first.substring(0, ind);
|
---|
| 95 |
|
---|
| 96 | // indenting for tag
|
---|
| 97 | indent();
|
---|
| 98 | _writer.write("</" + tag + ">" + ItemWriter.NEW_LINE);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | @Override
|
---|
| 104 | protected void writeText(Text text) throws IOException {
|
---|
[80] | 105 | for (String s : text.getTextList()) {
|
---|
[4] | 106 |
|
---|
| 107 | indent();
|
---|
| 108 | // check if the first word is proceeded by a :
|
---|
| 109 | int firstColonIndex = s.indexOf(":");
|
---|
| 110 | int firstSpaceIndex = s.indexOf(" ");
|
---|
| 111 | if (firstColonIndex > 0 && firstColonIndex < firstSpaceIndex) {
|
---|
| 112 | _writer.write("<" + s.substring(0, firstColonIndex) + ">");
|
---|
| 113 | if (firstColonIndex < s.length())
|
---|
| 114 | _writer.write(s.substring(firstColonIndex + 1));
|
---|
| 115 | _writer.write("</" + s.substring(0, firstColonIndex) + ">");
|
---|
| 116 | } else
|
---|
| 117 | _writer.write(s);
|
---|
| 118 | _writer.write(ItemWriter.NEW_LINE);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private void indent() throws IOException {
|
---|
| 123 | for (int i = 0; i < _indent; i++)
|
---|
| 124 | _writer.write(INDENT);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Override
|
---|
| 128 | protected void writePicture(Picture pic) throws IOException {
|
---|
[108] | 129 | Text source = pic.getSource();
|
---|
[4] | 130 |
|
---|
| 131 | String line = source.getFirstLine();
|
---|
| 132 | line = line.substring(line.indexOf(":") + 1).trim();
|
---|
| 133 |
|
---|
| 134 | indent();
|
---|
| 135 |
|
---|
| 136 | _writer.write("<img src = '../images/" + line + "'>");
|
---|
| 137 | _writer.write(ItemWriter.NEW_LINE);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|