1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Stack;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.Frame;
|
---|
8 | import org.expeditee.gui.FrameGraphics;
|
---|
9 | import org.expeditee.gui.FrameIO;
|
---|
10 | import org.expeditee.items.Item;
|
---|
11 |
|
---|
12 | public abstract class DefaultTreeWriter extends DefaultFrameWriter implements
|
---|
13 | TreeWriter {
|
---|
14 |
|
---|
15 | // the list of frames currently being processed
|
---|
16 | private Stack<FrameCounter> _frames = new Stack<FrameCounter>();
|
---|
17 | private int _frameCount = 0;
|
---|
18 |
|
---|
19 | public int getFrameCount() {
|
---|
20 | return _frameCount;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public String writeTree(Frame toWrite) throws IOException {
|
---|
24 | try {
|
---|
25 | initialise(toWrite);
|
---|
26 |
|
---|
27 | outputTree(toWrite);
|
---|
28 |
|
---|
29 | _running = false;
|
---|
30 | return finaliseTree();
|
---|
31 | } catch (IOException ioe) {
|
---|
32 | _running = false;
|
---|
33 | throw ioe;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * This method is used to output any tags before following the Item's link
|
---|
39 | * when using tree writers.
|
---|
40 | *
|
---|
41 | * @param linker
|
---|
42 | * The linked Item that is about to be followed.
|
---|
43 | * @throws IOException
|
---|
44 | */
|
---|
45 | protected void writeStartLink(Item linker) throws IOException {
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * This method is called after the Frame the Item links to has been
|
---|
51 | * processed. This allows end tags to be written if the format requires it.
|
---|
52 | *
|
---|
53 | * @param linker
|
---|
54 | * The Item whose link was just followed.
|
---|
55 | * @throws IOException
|
---|
56 | */
|
---|
57 | protected void writeEndLink(Item linker) throws IOException {
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void resumeFrame(Frame resuming) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected void outputTree(Frame toWrite) throws IOException {
|
---|
65 | if (toWrite == null)
|
---|
66 | return;
|
---|
67 |
|
---|
68 | _frames.push(new FrameCounter(toWrite.getFrameName(), -1));
|
---|
69 |
|
---|
70 | // process the entire tree of frames in depth-first order
|
---|
71 | while (_frames.size() > 0) {
|
---|
72 | FrameCounter cur = _frames.pop();
|
---|
73 |
|
---|
74 | Frame next = FrameIO.LoadFrame(cur.frame);
|
---|
75 | if (next == null) {
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // the items on the frame currently being processed.
|
---|
80 | List<Item> items = next.getItems();
|
---|
81 |
|
---|
82 | // write any information relating to the end of the link
|
---|
83 | if (cur.index > 0) {
|
---|
84 | this.writeEndLink(items.get(cur.index));
|
---|
85 | this.resumeFrame(next);
|
---|
86 | } else {
|
---|
87 | FrameGraphics.OverwriteMessage("Writing: "
|
---|
88 | + next.getFrameName());
|
---|
89 | _frameCount ++;
|
---|
90 | writeStartFrame(next);
|
---|
91 | }
|
---|
92 |
|
---|
93 | boolean complete = true;
|
---|
94 |
|
---|
95 | // resume from the next item in the list
|
---|
96 | for (int i = cur.index + 1; i < items.size(); i++) {
|
---|
97 | if (_stop)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | Item item = items.get(i);
|
---|
101 |
|
---|
102 | // ignore annotation and framenames
|
---|
103 | if (item.getID() >= 0) {
|
---|
104 | if (item.getLink() != null && !item.isAnnotation()) {
|
---|
105 | cur.index = i;
|
---|
106 | _frames.push(cur);
|
---|
107 |
|
---|
108 | // write any information relating to the start of the
|
---|
109 | // link
|
---|
110 | this.writeStartLink(item);
|
---|
111 |
|
---|
112 | Frame linked = FrameIO.LoadFrame(item.getAbsoluteLink());
|
---|
113 |
|
---|
114 | // if the linked frame was found, then display it next
|
---|
115 | if (linked != null) {
|
---|
116 | FrameCounter fc = new FrameCounter(linked
|
---|
117 | .getFrameName(), -1);
|
---|
118 | if (!_frames.contains(fc)) {
|
---|
119 | // remember what frame we are on before
|
---|
120 | // processing it
|
---|
121 | _frames.push(fc);
|
---|
122 |
|
---|
123 | complete = false;
|
---|
124 | // process the loaded frame immediately
|
---|
125 | // (depth-first)
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } else if (item != next.getTitle())
|
---|
130 | this.writeItem(item);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (complete)
|
---|
135 | writeEndFrame(next);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected String finaliseTree() throws IOException {
|
---|
140 | return "Tree" + finalise();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Inner class used to keep track of what frames have been seen, as well as
|
---|
145 | * what Item in the Frame the processing was up to. Only Frame names are
|
---|
146 | * stored to keep memory usage down.
|
---|
147 | */
|
---|
148 | private class FrameCounter {
|
---|
149 | public int index;
|
---|
150 |
|
---|
151 | public String frame;
|
---|
152 |
|
---|
153 | public FrameCounter(String f, int i) {
|
---|
154 | frame = f.toLowerCase();
|
---|
155 | index = i;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public boolean equals(Object o) {
|
---|
160 | if (o instanceof FrameCounter)
|
---|
161 | return (((FrameCounter) o).frame.equals(frame));// && fc.index
|
---|
162 | // == index);
|
---|
163 |
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|