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