[164] | 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.io.Conversion;
|
---|
[570] | 16 | import org.expeditee.settings.UserSettings;
|
---|
[164] | 17 |
|
---|
| 18 | public class SearchTreeNoResults extends SearchAgent {
|
---|
| 19 | private Map<String, Collection<String>> _results = new HashMap<String, Collection<String>>();
|
---|
| 20 |
|
---|
[291] | 21 | public SearchTreeNoResults(String searchText) {
|
---|
| 22 | super(searchText);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[164] | 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);
|
---|
[176] | 77 | if (toSearch.toLowerCase().contains(pattern))
|
---|
[164] | 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 | }
|
---|