source: trunk/org/expeditee/io/DefaultTreeWriter.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

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