1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.expeditee.gui.Frame;
|
---|
14 | import org.expeditee.gui.FrameGraphics;
|
---|
15 | import org.expeditee.gui.FrameIO;
|
---|
16 | import org.expeditee.gui.UserSettings;
|
---|
17 | import org.expeditee.io.Conversion;
|
---|
18 |
|
---|
19 | public class SearchTreeFast extends SearchAgent {
|
---|
20 | private Map<String, Collection<String>> _searchResults = new HashMap<String, Collection<String>>();
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | protected Frame process(Frame frame) {
|
---|
24 | if(frame == null)
|
---|
25 | frame = FrameIO.LoadFrame(_startName);
|
---|
26 |
|
---|
27 | searchTree(frame.getName(), _pattern, _searchResults, new HashSet<String>());
|
---|
28 |
|
---|
29 | _results.save();
|
---|
30 |
|
---|
31 | String resultFrameName = _results.getName();
|
---|
32 | if (_clicked != null)
|
---|
33 | _clicked.setLink(resultFrameName);
|
---|
34 |
|
---|
35 | return _results.getFirstFrame();
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Returns a list of the frames searched and any matches on those frames.
|
---|
40 | * @param frameName the name of the top not in the tree of frames to search
|
---|
41 | * @param pattern the pattern to search for
|
---|
42 | * @param results a list of frames on which matches were found and the text that matched the pattern
|
---|
43 | * @param visited a list of the frames that were visited in the searchTree
|
---|
44 | */
|
---|
45 | public void searchTree(String frameName, String pattern, Map<String, Collection<String>> results, Collection<String> visited) {
|
---|
46 | //Check if this node has already been visited
|
---|
47 | if(visited.contains(frameName))
|
---|
48 | return;
|
---|
49 |
|
---|
50 | visited.add(frameName);
|
---|
51 |
|
---|
52 | String fullPath = null;
|
---|
53 | for (String possiblePath : UserSettings.FrameDirs) {
|
---|
54 | fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
|
---|
55 | if (fullPath != null)
|
---|
56 | break;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // If the frame was not located return null
|
---|
60 | if (fullPath == null)
|
---|
61 | return;
|
---|
62 |
|
---|
63 | _frameCount++;
|
---|
64 | overwriteMessage("Searching " + frameName);
|
---|
65 |
|
---|
66 | String frameset = Conversion.getFramesetName(frameName);
|
---|
67 |
|
---|
68 | Collection<String> frameResults = new LinkedList<String>();
|
---|
69 | // Open the file and search the text items
|
---|
70 | try {
|
---|
71 | BufferedReader reader = new BufferedReader(new FileReader(fullPath));
|
---|
72 | String next;
|
---|
73 | while (reader.ready() && ((next = reader.readLine()) != null)) {
|
---|
74 | if (next.startsWith("T")) {
|
---|
75 | String toSearch = next.substring(2);
|
---|
76 | if (toSearch.toLowerCase().contains(pattern))
|
---|
77 | frameResults.add(toSearch);
|
---|
78 | }else if (next.startsWith("F")) {
|
---|
79 | String link = next.substring(2);
|
---|
80 | if(!FrameIO.isValidFrameName(link))
|
---|
81 | link = frameset + link;
|
---|
82 | searchTree(link, pattern, results, visited);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | //Only add the results if a match was found on the frame
|
---|
86 | if(frameResults.size() > 0){
|
---|
87 | results.put(frameName, frameResults);
|
---|
88 | }
|
---|
89 | } catch (FileNotFoundException e) {
|
---|
90 | e.printStackTrace();
|
---|
91 | } catch (IOException e) {
|
---|
92 | e.printStackTrace();
|
---|
93 | }
|
---|
94 | int size = frameResults.size();
|
---|
95 | if(size > 0){
|
---|
96 | String repeats = size > 1? ("("+ size+ ")") : "";
|
---|
97 | _results.addText(frameName + repeats, null, frameName, null, false);
|
---|
98 | FrameGraphics.requestRefresh(true);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|