source: trunk/src/org/expeditee/agents/SearchTree.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 5.1 KB
Line 
1/**
2 * SearchTree.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.agents;
20
21import java.io.BufferedReader;
22import java.io.FileNotFoundException;
23import java.io.FileReader;
24import java.io.IOException;
25import java.util.Collection;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.LinkedList;
29import java.util.Map;
30
31import org.expeditee.gui.AttributeValuePair;
32import org.expeditee.gui.Frame;
33import org.expeditee.gui.FrameIO;
34import org.expeditee.io.Conversion;
35import org.expeditee.settings.folders.FolderSettings;
36
37public class SearchTree extends SearchAgent {
38 private Map<String, Collection<String>> _searchResults = new HashMap<String, Collection<String>>();
39
40 public SearchTree(String searchText) {
41 super(searchText);
42 }
43
44 @Override
45 protected Frame process(Frame frame) {
46 if (frame == null)
47 frame = FrameIO.LoadFrame(_startName);
48
49 searchTree(frame.getName(), _searchResults, new HashSet<String>());
50 _results.save();
51
52 String resultFrameName = _results.getName();
53 if (_clicked != null)
54 _clicked.setLink(resultFrameName);
55
56 return _results.getFirstFrame();
57 }
58
59 @Override
60 /**
61 * @param frame
62 * @return
63 */
64 protected String getSearchDescription(Frame frame) {
65 return frame.getName();
66 }
67
68 /**
69 * Returns a list of the frames searched and any matches on those frames.
70 *
71 * @param frameName
72 * the name of the top not in the tree of frames to search
73 * @param pattern
74 * the pattern to search for
75 * @param results
76 * a list of frames on which matches were found and the text that
77 * matched the pattern
78 * @param visited
79 * a list of the frames that were visited in the searchTree
80 */
81 public void searchTree(String frameName,
82 Map<String, Collection<String>> results, Collection<String> visited) {
83 // Check if this node has already been visited
84 if (visited.contains(frameName))
85 return;
86
87 visited.add(frameName);
88
89 String fullPath = null;
90 for (String possiblePath : FolderSettings.FrameDirs.get()) {
91 fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
92 if (fullPath != null)
93 break;
94 }
95
96 // If the frame was not located return null
97 if (fullPath == null)
98 return;
99
100 _frameCount++;
101 overwriteMessage("Searching " + frameName);
102
103 String frameset = Conversion.getFramesetName(frameName);
104
105 Collection<String> frameResults = new LinkedList<String>();
106 // Open the file and search the text items
107 try {
108 BufferedReader reader = new BufferedReader(new FileReader(fullPath));
109 String next;
110 StringBuffer sb = new StringBuffer();
111 String link = null;
112 boolean ignore = false;
113
114 while (reader.ready() && ((next = reader.readLine()) != null)) {
115
116 if (next.length() == 0) {
117 // Ignore annotations
118 if (ignore) {
119 ignore = false;
120 link = null;
121 continue;
122 }
123
124 // Ignore non text items
125 if (sb.length() == 0) {
126 link = null;
127 continue;
128 }
129
130 String toSearch = sb.substring(0, sb.length() - 1);
131 String resultSurrogate = getResultSurrogate(toSearch);
132 if (resultSurrogate != null) {
133 frameResults.add(resultSurrogate);
134 }
135
136 if (link != null) {
137 searchTree(link, results, visited);
138 }
139
140 // Reinit the item variables
141 link = null;
142 sb = new StringBuffer();
143
144 } else if (ignore) {
145 continue;
146 } else if (next.startsWith("T")) {
147 String text = next.substring(2).trim();
148 // Ignore the rest of annotation items...
149 if (text.length() > 0
150 && text.charAt(0) == AttributeValuePair.ANNOTATION_CHAR) {
151 ignore = true;
152 continue;
153 }
154 sb.append(text).append('\n');
155 } else if (next.startsWith("F")) {
156 link = next.substring(2);
157 // Convert number only links
158 if (Character.isDigit(link.charAt(0)))
159 link = frameset + link;
160 }
161 }
162 // Only add the results if a match was found on the frame
163 if (frameResults.size() > 0) {
164 results.put(frameName, frameResults);
165 }
166 } catch (FileNotFoundException e) {
167 e.printStackTrace();
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 int size = frameResults.size();
172 if (size > 0) {
173 addResults(frameName, frameResults);
174
175 // String repeats = size > 1 ? ("(" + size + ")") : "";
176 // _results.addText(frameName + repeats, null, frameName, null,
177 // false);
178 // FrameGraphics.requestRefresh(true);
179 }
180 }
181
182 protected String getResultSurrogate(String toSearch) {
183 if (toSearch.toLowerCase().contains(_pattern))
184 return toSearch;
185 return null;
186 }
187}
Note: See TracBrowser for help on using the repository browser.