[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 |
|
---|
[376] | 13 | import org.expeditee.gui.AttributeValuePair;
|
---|
[164] | 14 | import org.expeditee.gui.Frame;
|
---|
| 15 | import org.expeditee.gui.FrameIO;
|
---|
| 16 | import org.expeditee.io.Conversion;
|
---|
[570] | 17 | import org.expeditee.settings.UserSettings;
|
---|
[164] | 18 |
|
---|
[376] | 19 | public class SearchTree extends SearchAgent {
|
---|
[164] | 20 | private Map<String, Collection<String>> _searchResults = new HashMap<String, Collection<String>>();
|
---|
| 21 |
|
---|
[376] | 22 | public SearchTree(String searchText) {
|
---|
[291] | 23 | super(searchText);
|
---|
| 24 | }
|
---|
[376] | 25 |
|
---|
[164] | 26 | @Override
|
---|
| 27 | protected Frame process(Frame frame) {
|
---|
[376] | 28 | if (frame == null)
|
---|
[176] | 29 | frame = FrameIO.LoadFrame(_startName);
|
---|
[376] | 30 |
|
---|
| 31 | searchTree(frame.getName(), _searchResults, new HashSet<String>());
|
---|
[164] | 32 | _results.save();
|
---|
| 33 |
|
---|
| 34 | String resultFrameName = _results.getName();
|
---|
| 35 | if (_clicked != null)
|
---|
| 36 | _clicked.setLink(resultFrameName);
|
---|
[376] | 37 |
|
---|
[164] | 38 | return _results.getFirstFrame();
|
---|
| 39 | }
|
---|
[376] | 40 |
|
---|
| 41 | @Override
|
---|
[164] | 42 | /**
|
---|
[376] | 43 | * @param frame
|
---|
| 44 | * @return
|
---|
| 45 | */
|
---|
| 46 | protected String getSearchDescription(Frame frame) {
|
---|
| 47 | return frame.getName();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
[164] | 51 | * Returns a list of the frames searched and any matches on those frames.
|
---|
[376] | 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
|
---|
[164] | 62 | */
|
---|
[376] | 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))
|
---|
[164] | 67 | return;
|
---|
[376] | 68 |
|
---|
[164] | 69 | visited.add(frameName);
|
---|
[376] | 70 |
|
---|
[164] | 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;
|
---|
[376] | 81 |
|
---|
[164] | 82 | _frameCount++;
|
---|
| 83 | overwriteMessage("Searching " + frameName);
|
---|
[376] | 84 |
|
---|
[164] | 85 | String frameset = Conversion.getFramesetName(frameName);
|
---|
[376] | 86 |
|
---|
[164] | 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;
|
---|
[376] | 92 | StringBuffer sb = new StringBuffer();
|
---|
| 93 | String link = null;
|
---|
| 94 | boolean ignore = false;
|
---|
| 95 |
|
---|
[164] | 96 | while (reader.ready() && ((next = reader.readLine()) != null)) {
|
---|
[376] | 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)))
|
---|
[164] | 141 | link = frameset + link;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[376] | 144 | // Only add the results if a match was found on the frame
|
---|
| 145 | if (frameResults.size() > 0) {
|
---|
[164] | 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();
|
---|
[376] | 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);
|
---|
[164] | 161 | }
|
---|
| 162 | }
|
---|
[376] | 163 |
|
---|
| 164 | protected String getResultSurrogate(String toSearch) {
|
---|
| 165 | if (toSearch.toLowerCase().contains(_pattern))
|
---|
| 166 | return toSearch;
|
---|
| 167 | return null;
|
---|
| 168 | }
|
---|
[164] | 169 | }
|
---|