source: trunk/src/org/expeditee/agents/SearchTreeNoResults.java@ 291

Last change on this file since 291 was 291, checked in by ra33, 16 years ago

Fixed up search stuff... to adjust to getting params off the cursor

File size: 2.8 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.Frame;
14import org.expeditee.gui.FrameIO;
15import org.expeditee.gui.UserSettings;
16import org.expeditee.io.Conversion;
17
18public class SearchTreeNoResults extends SearchAgent {
19 private Map<String, Collection<String>> _results = new HashMap<String, Collection<String>>();
20
21 public SearchTreeNoResults(String searchText) {
22 super(searchText);
23 }
24
25 @Override
26 protected Frame process(Frame frame) {
27 searchTree(frame.getName(), _pattern, _results, new HashSet<String>());
28 return null;
29 }
30
31 public Map<String, Collection<String>> getResult(){
32 return _results;
33 }
34
35 @Override
36 public boolean hasResultFrame() {
37 return false;
38 }
39
40 /**
41 * Returns a list of the frames searched and any matches on those frames.
42 * @param frameName the name of the top not in the tree of frames to search
43 * @param pattern the pattern to search for
44 * @param results a list of frames on which matches were found and the text that matched the pattern
45 * @param visited a list of the frames that were visited in the searchTree
46 */
47 public void searchTree(String frameName, String pattern, Map<String, Collection<String>> results, Collection<String> visited) {
48 //Check if this node has already been visited
49 if(visited.contains(frameName))
50 return;
51
52 visited.add(frameName);
53
54 String fullPath = null;
55 for (String possiblePath : UserSettings.FrameDirs) {
56 fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
57 if (fullPath != null)
58 break;
59 }
60
61 // If the frame was not located return null
62 if (fullPath == null)
63 return;
64
65 _frameCount++;
66
67 String frameset = Conversion.getFramesetName(frameName);
68
69 Collection<String> frameResults = new LinkedList<String>();
70 // Open the file and search the text items
71 try {
72 BufferedReader reader = new BufferedReader(new FileReader(fullPath));
73 String next;
74 while (reader.ready() && ((next = reader.readLine()) != null)) {
75 if (next.startsWith("T")) {
76 String toSearch = next.substring(2);
77 if (toSearch.toLowerCase().contains(pattern))
78 frameResults.add(toSearch);
79 }else if (next.startsWith("F")) {
80 String link = next.substring(2);
81 if(!FrameIO.isValidFrameName(link))
82 link = frameset + link;
83 searchTree(link, pattern, results, visited);
84 }
85 }
86 //Only add the results if a match was found on the frame
87 if(frameResults.size() > 0)
88 results.put(frameName, frameResults);
89 } catch (FileNotFoundException e) {
90 e.printStackTrace();
91 } catch (IOException e) {
92 e.printStackTrace();
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.