1 | /**
|
---|
2 | * TXTWriter.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.io.IOException;
|
---|
22 | import java.io.Writer;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.items.Item;
|
---|
27 | import org.expeditee.items.ItemUtils;
|
---|
28 | import org.expeditee.items.Text;
|
---|
29 |
|
---|
30 | public class TXTWriter extends DefaultTreeWriter {
|
---|
31 |
|
---|
32 | private boolean _join = false;
|
---|
33 |
|
---|
34 | private int _indent = 0;
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
38 | _format = ".txt";
|
---|
39 | super.initialise(start, writer);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | protected void writeStartFrame(Frame toParse) throws IOException {
|
---|
44 | if (ItemUtils.ContainsTag(toParse.getItems(), "@join"))
|
---|
45 | _join = !_join;
|
---|
46 |
|
---|
47 | if (ItemUtils.ContainsTag(toParse.getItems(), "@indent"))
|
---|
48 | _indent++;
|
---|
49 |
|
---|
50 | super.writeStartFrame(toParse);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void writeTitle(Text title, List<Item> items) throws IOException {
|
---|
55 | int indent = 0;
|
---|
56 |
|
---|
57 | if (_indent > 0)
|
---|
58 | indent = _indent - 1;
|
---|
59 |
|
---|
60 | for (String s : title.getTextList()) {
|
---|
61 |
|
---|
62 | for (int i = 0; i < indent; i++)
|
---|
63 | _writer.write("\t");
|
---|
64 |
|
---|
65 | _writer.write(s);
|
---|
66 | _writer.write(ItemWriter.NEW_LINE);
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (!_join)
|
---|
70 | _writer.write(ItemWriter.NEW_LINE);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected void writeEndFrame(Frame toParse) throws IOException {
|
---|
75 | if (ItemUtils.ContainsTag(toParse.getItems(), "@indent"))
|
---|
76 | if (_indent > 0)
|
---|
77 | _indent--;
|
---|
78 |
|
---|
79 | if (ItemUtils.ContainsTag(toParse.getItems(), "@join"))
|
---|
80 | _join = !_join;
|
---|
81 |
|
---|
82 | _writer.write(ItemWriter.NEW_LINE);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | protected void resumeFrame(Frame resuming) {
|
---|
87 | _join = ItemUtils.ContainsTag(resuming.getItems(), "@join");
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | protected void writeText(Text text) throws IOException {
|
---|
92 | String s = text.getText();
|
---|
93 |
|
---|
94 | for (int i = 0; i < _indent; i++)
|
---|
95 | _writer.write("\t");
|
---|
96 |
|
---|
97 | _writer.write(s);
|
---|
98 | _writer.write(ItemWriter.NEW_LINE);
|
---|
99 |
|
---|
100 | if (!_join)
|
---|
101 | _writer.write(ItemWriter.NEW_LINE);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | protected void writeAnnotationText(Text toWrite) throws IOException {
|
---|
106 | if (toWrite.startsWith("@BlankLine"))
|
---|
107 | _writer.write(ItemWriter.NEW_LINE);
|
---|
108 | }
|
---|
109 | }
|
---|