source: trunk/src/org/expeditee/agents/SearchTree.java@ 376

Last change on this file since 376 was 376, checked in by ra33, 16 years ago
File size: 4.4 KB
Line 
1package org.expeditee.agents;
2
3import java.io.BufferedReader;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.LinkedList;
11import java.util.Map;
12
13import org.expeditee.gui.AttributeValuePair;
14import org.expeditee.gui.Frame;
15import org.expeditee.gui.FrameIO;
16import org.expeditee.gui.UserSettings;
17import org.expeditee.io.Conversion;
18
19public class SearchTree extends SearchAgent {
20 private Map<String, Collection<String>> _searchResults = new HashMap<String, Collection<String>>();
21
22 public SearchTree(String searchText) {
23 super(searchText);
24 }
25
26 @Override
27 protected Frame process(Frame frame) {
28 if (frame == null)
29 frame = FrameIO.LoadFrame(_startName);
30
31 searchTree(frame.getName(), _searchResults, new HashSet<String>());
32 _results.save();
33
34 String resultFrameName = _results.getName();
35 if (_clicked != null)
36 _clicked.setLink(resultFrameName);
37
38 return _results.getFirstFrame();
39 }
40
41 @Override
42 /**
43 * @param frame
44 * @return
45 */
46 protected String getSearchDescription(Frame frame) {
47 return frame.getName();
48 }
49
50 /**
51 * Returns a list of the frames searched and any matches on those frames.
52 *
53 * @param frameName
54 * the name of the top not in the tree of frames to search
55 * @param pattern
56 * the pattern to search for
57 * @param results
58 * a list of frames on which matches were found and the text that
59 * matched the pattern
60 * @param visited
61 * a list of the frames that were visited in the searchTree
62 */
63 public void searchTree(String frameName,
64 Map<String, Collection<String>> results, Collection<String> visited) {
65 // Check if this node has already been visited
66 if (visited.contains(frameName))
67 return;
68
69 visited.add(frameName);
70
71 String fullPath = null;
72 for (String possiblePath : UserSettings.FrameDirs) {
73 fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
74 if (fullPath != null)
75 break;
76 }
77
78 // If the frame was not located return null
79 if (fullPath == null)
80 return;
81
82 _frameCount++;
83 overwriteMessage("Searching " + frameName);
84
85 String frameset = Conversion.getFramesetName(frameName);
86
87 Collection<String> frameResults = new LinkedList<String>();
88 // Open the file and search the text items
89 try {
90 BufferedReader reader = new BufferedReader(new FileReader(fullPath));
91 String next;
92 StringBuffer sb = new StringBuffer();
93 String link = null;
94 boolean ignore = false;
95
96 while (reader.ready() && ((next = reader.readLine()) != null)) {
97
98 if (next.length() == 0) {
99 // Ignore annotations
100 if (ignore) {
101 ignore = false;
102 link = null;
103 continue;
104 }
105
106 // Ignore non text items
107 if (sb.length() == 0) {
108 link = null;
109 continue;
110 }
111
112 String toSearch = sb.substring(0, sb.length() - 1);
113 String resultSurrogate = getResultSurrogate(toSearch);
114 if (resultSurrogate != null) {
115 frameResults.add(resultSurrogate);
116 }
117
118 if (link != null) {
119 searchTree(link, results, visited);
120 }
121
122 // Reinit the item variables
123 link = null;
124 sb = new StringBuffer();
125
126 } else if (ignore) {
127 continue;
128 } else if (next.startsWith("T")) {
129 String text = next.substring(2).trim();
130 // Ignore the rest of annotation items...
131 if (text.length() > 0
132 && text.charAt(0) == AttributeValuePair.ANNOTATION_CHAR) {
133 ignore = true;
134 continue;
135 }
136 sb.append(text).append('\n');
137 } else if (next.startsWith("F")) {
138 link = next.substring(2);
139 // Convert number only links
140 if (Character.isDigit(link.charAt(0)))
141 link = frameset + link;
142 }
143 }
144 // Only add the results if a match was found on the frame
145 if (frameResults.size() > 0) {
146 results.put(frameName, frameResults);
147 }
148 } catch (FileNotFoundException e) {
149 e.printStackTrace();
150 } catch (IOException e) {
151 e.printStackTrace();
152 }
153 int size = frameResults.size();
154 if (size > 0) {
155 addResults(frameName, frameResults);
156
157 // String repeats = size > 1 ? ("(" + size + ")") : "";
158 // _results.addText(frameName + repeats, null, frameName, null,
159 // false);
160 // FrameGraphics.requestRefresh(true);
161 }
162 }
163
164 protected String getResultSurrogate(String toSearch) {
165 if (toSearch.toLowerCase().contains(_pattern))
166 return toSearch;
167 return null;
168 }
169}
Note: See TracBrowser for help on using the repository browser.