source: trunk/src/org/expeditee/agents/TreeProcessor.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.8 KB
Line 
1/**
2 * TreeProcessor.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
19package org.expeditee.agents;
20
21import java.util.List;
22import java.util.Stack;
23
24import org.expeditee.gui.Frame;
25import org.expeditee.gui.FrameIO;
26import org.expeditee.items.Item;
27
28/**
29 * This class provides some generic methods that process a tree of Frames.
30 * Subclasses that only use text items can simply implement
31 * processTextItem(Text, int)
32 */
33public abstract class TreeProcessor extends DefaultAgent {
34
35 // the list of frames currently being processed
36 private Stack<FrameCounter> _frames = new Stack<FrameCounter>();
37
38 public TreeProcessor(String delay) {
39 super(delay);
40 }
41
42 public TreeProcessor() {
43 super();
44 }
45
46 /**
47 * Processes the Frame by calling processItem() for each unlinked item
48 * encountered. Linked items call processStartLinkItem(), then load the
49 * linked frame and process it recursively, upon returning
50 * processEndLinkItem() is called.
51 */
52 protected Frame process(Frame toProcess) {
53 if (_stop)
54 return null;
55
56 if (toProcess == null)
57 return null;
58
59 _frames.push(new FrameCounter(toProcess.getName(), -1));
60
61 // process the entire tree of frames in depth-first order
62 while (_frames.size() > 0) {
63 if (_stop)
64 return null;
65
66 FrameCounter cur = _frames.pop();
67
68 Frame next = FrameIO.LoadFrame(cur.frame);
69 if (next == null) {
70 message("There is no " + cur.frame + " Frame to display");
71 return null;
72 }
73
74 if (cur.index < 0) {
75 if (next != null)
76 overwriteMessage("Processing: " + next.getName());
77 processFrame(next);
78 }
79
80 // the items on the frame currently being processed.
81 List<Item> items = next.getItems();
82
83 // resume from the next item in the list
84 for (int i = cur.index + 1; i < items.size(); i++) {
85 Item item = items.get(i);
86
87 // ignore annotation and framenames
88 if (!item.isAnnotation() && item.getID() >= 0) {
89 if (item.getLink() != null) {
90 cur.index = i;
91 _frames.push(cur);
92
93 Frame linked = FrameIO.LoadFrame(item.getAbsoluteLink());
94
95 // if the linked frame was found, then display it next
96 if (linked != null) {
97 FrameCounter fc = new FrameCounter(linked
98 .getName(), -1);
99 if (!_frames.contains(fc)) {
100 // remember what frame we are on before
101 // processing it
102 _frames.push(fc);
103
104 // processFrame(linked);
105
106 // process the loaded frame immediately
107 // (depth-first)
108 break;
109 }
110 }
111 }
112 }
113 }
114 }
115
116 return null;
117 }
118
119 protected abstract void processFrame(Frame toProcess);
120
121 /**
122 * Inner class used to keep track of what frames have been seen, as well as
123 * what Item in the Frame the processing was up to. Only Frame names are
124 * stored to keep memory usage down.
125 */
126 private class FrameCounter {
127 public int index;
128
129 public String frame;
130
131 public FrameCounter(String f, int i) {
132 frame = f.toLowerCase();
133 index = i;
134 }
135
136 @Override
137 public boolean equals(Object o) {
138 if (o instanceof FrameCounter)
139 return (((FrameCounter) o).frame.equals(frame));// && fc.index
140 // == index);
141
142 return false;
143 }
144 }
145
146}
Note: See TracBrowser for help on using the repository browser.