[919] | 1 | /**
|
---|
| 2 | * DefaultTreeWriter.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;
|
---|
| 22 | import java.util.List;
|
---|
| 23 | import java.util.Stack;
|
---|
| 24 |
|
---|
| 25 | import org.expeditee.gui.Frame;
|
---|
| 26 | import org.expeditee.gui.FrameIO;
|
---|
[121] | 27 | import org.expeditee.gui.MessageBay;
|
---|
[914] | 28 | import org.expeditee.io.flowlayout.XGroupItem;
|
---|
[4] | 29 | import org.expeditee.items.Item;
|
---|
[80] | 30 | import org.expeditee.items.Text;
|
---|
[4] | 31 |
|
---|
| 32 | public abstract class DefaultTreeWriter extends DefaultFrameWriter implements
|
---|
| 33 | TreeWriter {
|
---|
| 34 |
|
---|
[290] | 35 | private int _indent = 0;
|
---|
[376] | 36 |
|
---|
[4] | 37 | // the list of frames currently being processed
|
---|
| 38 | private Stack<FrameCounter> _frames = new Stack<FrameCounter>();
|
---|
[80] | 39 |
|
---|
[4] | 40 | private int _frameCount = 0;
|
---|
[80] | 41 |
|
---|
[46] | 42 | public int getFrameCount() {
|
---|
| 43 | return _frameCount;
|
---|
| 44 | }
|
---|
[4] | 45 |
|
---|
| 46 | public String writeTree(Frame toWrite) throws IOException {
|
---|
| 47 | try {
|
---|
| 48 | initialise(toWrite);
|
---|
| 49 | outputTree(toWrite);
|
---|
| 50 |
|
---|
| 51 | } catch (IOException ioe) {
|
---|
| 52 | _running = false;
|
---|
| 53 | throw ioe;
|
---|
[80] | 54 | } catch (Exception e) {
|
---|
| 55 | e.printStackTrace();
|
---|
[4] | 56 | }
|
---|
[80] | 57 | _running = false;
|
---|
| 58 | return finaliseTree();
|
---|
[4] | 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * This method is used to output any tags before following the Item's link
|
---|
| 63 | * when using tree writers.
|
---|
| 64 | *
|
---|
| 65 | * @param linker
|
---|
| 66 | * The linked Item that is about to be followed.
|
---|
| 67 | * @throws IOException
|
---|
| 68 | */
|
---|
| 69 | protected void writeStartLink(Item linker) throws IOException {
|
---|
[289] | 70 | _indent++;
|
---|
[4] | 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * This method is called after the Frame the Item links to has been
|
---|
| 75 | * processed. This allows end tags to be written if the format requires it.
|
---|
| 76 | *
|
---|
| 77 | * @param linker
|
---|
| 78 | * The Item whose link was just followed.
|
---|
| 79 | * @throws IOException
|
---|
| 80 | */
|
---|
| 81 | protected void writeEndLink(Item linker) throws IOException {
|
---|
[289] | 82 | _indent--;
|
---|
[4] | 83 | }
|
---|
| 84 |
|
---|
| 85 | protected void resumeFrame(Frame resuming) {
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[460] | 88 | protected List<Item> getSortedItems(Frame frame)
|
---|
| 89 | {
|
---|
| 90 | List<Item> items = frame.getItems();
|
---|
| 91 | return items;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[4] | 94 | protected void outputTree(Frame toWrite) throws IOException {
|
---|
[914] | 95 | if (toWrite == null) {
|
---|
[4] | 96 | return;
|
---|
[914] | 97 | }
|
---|
| 98 |
|
---|
| 99 | XGroupItem.doImplicitBoxing = false;
|
---|
| 100 |
|
---|
[80] | 101 | _frames.push(new FrameCounter(toWrite.getName(), -1));
|
---|
[4] | 102 |
|
---|
| 103 | // process the entire tree of frames in depth-first order
|
---|
| 104 | while (_frames.size() > 0) {
|
---|
| 105 | FrameCounter cur = _frames.pop();
|
---|
| 106 |
|
---|
[80] | 107 | if (_stop)
|
---|
| 108 | return;
|
---|
| 109 |
|
---|
[4] | 110 | Frame next = FrameIO.LoadFrame(cur.frame);
|
---|
[376] | 111 |
|
---|
[4] | 112 | if (next == null) {
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[376] | 116 | Text title = next.getTitleItem();
|
---|
| 117 |
|
---|
[4] | 118 | // the items on the frame currently being processed.
|
---|
[460] | 119 | List<Item> items = getSortedItems(next);
|
---|
[4] | 120 |
|
---|
| 121 | // write any information relating to the end of the link
|
---|
| 122 | if (cur.index > 0) {
|
---|
| 123 | this.writeEndLink(items.get(cur.index));
|
---|
| 124 | this.resumeFrame(next);
|
---|
| 125 | } else {
|
---|
[121] | 126 | MessageBay.overwriteMessage("Writing: " + next.getName());
|
---|
[80] | 127 | _frameCount++;
|
---|
[4] | 128 | writeStartFrame(next);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | boolean complete = true;
|
---|
| 132 |
|
---|
| 133 | // resume from the next item in the list
|
---|
| 134 | for (int i = cur.index + 1; i < items.size(); i++) {
|
---|
| 135 | if (_stop)
|
---|
| 136 | return;
|
---|
| 137 |
|
---|
| 138 | Item item = items.get(i);
|
---|
| 139 |
|
---|
| 140 | // ignore annotation and framenames
|
---|
| 141 | if (item.getID() >= 0) {
|
---|
[376] | 142 | // Only follow the links of non annotation text items
|
---|
| 143 | boolean followLink = item instanceof Text
|
---|
| 144 | && item.getLink() != null
|
---|
| 145 | && (!item.isAnnotation() /*|| item.getText()
|
---|
| 146 | .toLowerCase().equals("@next")*/);
|
---|
| 147 |
|
---|
| 148 | if (followLink) {
|
---|
[4] | 149 | cur.index = i;
|
---|
| 150 | _frames.push(cur);
|
---|
| 151 |
|
---|
| 152 | // write any information relating to the start of the
|
---|
| 153 | // link
|
---|
| 154 | this.writeStartLink(item);
|
---|
| 155 |
|
---|
[80] | 156 | Frame linked = FrameIO
|
---|
| 157 | .LoadFrame(item.getAbsoluteLink());
|
---|
[4] | 158 |
|
---|
| 159 | // if the linked frame was found, then display it next
|
---|
| 160 | if (linked != null) {
|
---|
[80] | 161 | FrameCounter fc = new FrameCounter(
|
---|
| 162 | linked.getName(), -1);
|
---|
[4] | 163 | if (!_frames.contains(fc)) {
|
---|
| 164 | // remember what frame we are on before
|
---|
| 165 | // processing it
|
---|
| 166 | _frames.push(fc);
|
---|
| 167 |
|
---|
| 168 | complete = false;
|
---|
| 169 | // process the loaded frame immediately
|
---|
| 170 | // (depth-first)
|
---|
| 171 | break;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
[460] | 174 | // Don't write out the title here because it is written
|
---|
[80] | 175 | // out earlier
|
---|
[376] | 176 | } else if (item != title)
|
---|
[4] | 177 | this.writeItem(item);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (complete)
|
---|
| 182 | writeEndFrame(next);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | protected String finaliseTree() throws IOException {
|
---|
[46] | 187 | return "Tree" + finalise();
|
---|
[4] | 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * Inner class used to keep track of what frames have been seen, as well as
|
---|
| 192 | * what Item in the Frame the processing was up to. Only Frame names are
|
---|
| 193 | * stored to keep memory usage down.
|
---|
| 194 | */
|
---|
| 195 | private class FrameCounter {
|
---|
| 196 | public int index;
|
---|
| 197 |
|
---|
| 198 | public String frame;
|
---|
| 199 |
|
---|
| 200 | public FrameCounter(String f, int i) {
|
---|
| 201 | frame = f.toLowerCase();
|
---|
| 202 | index = i;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | @Override
|
---|
| 206 | public boolean equals(Object o) {
|
---|
| 207 | if (o instanceof FrameCounter)
|
---|
| 208 | return (((FrameCounter) o).frame.equals(frame));// && fc.index
|
---|
[80] | 209 | // == index);
|
---|
[4] | 210 |
|
---|
| 211 | return false;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
[376] | 214 |
|
---|
[290] | 215 | protected int getIndent() {
|
---|
| 216 | return _indent;
|
---|
| 217 | }
|
---|
[4] | 218 | }
|
---|