source: trunk/src/org/expeditee/agents/SearchTreeNoResults.java@ 1444

Last change on this file since 1444 was 1244, checked in by davidb, 5 years ago

After change to have resources-public and resources-private, some changes needed to support running Expeditee for a single user; other main change is to allow FrameDirs to specify relative directory paths, to help with when Expeditee is run on the cloud -- similar work still needs to occurr for ImageDir and AudioDir; some other minor changes also made.

File size: 3.6 KB
Line 
1/**
2 * SearchTreeNoResults.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.Frame;
32import org.expeditee.gui.FrameIO;
33import org.expeditee.io.Conversion;
34import org.expeditee.settings.folders.FolderSettings;
35
36public class SearchTreeNoResults extends SearchAgent {
37 private Map<String, Collection<String>> _results = new HashMap<String, Collection<String>>();
38
39 public SearchTreeNoResults(String searchText) {
40 super(searchText);
41 }
42
43 @Override
44 protected Frame process(Frame frame) {
45 searchTree(frame.getName(), _pattern, _results, new HashSet<String>());
46 return null;
47 }
48
49 public Map<String, Collection<String>> getResult(){
50 return _results;
51 }
52
53 @Override
54 public boolean hasResultFrame() {
55 return false;
56 }
57
58 /**
59 * Returns a list of the frames searched and any matches on those frames.
60 * @param frameName the name of the top not in the tree of frames to search
61 * @param pattern the pattern to search for
62 * @param results a list of frames on which matches were found and the text that matched the pattern
63 * @param visited a list of the frames that were visited in the searchTree
64 */
65 public void searchTree(String frameName, String pattern, Map<String, Collection<String>> results, Collection<String> visited) {
66 //Check if this node has already been visited
67 if(visited.contains(frameName))
68 return;
69
70 visited.add(frameName);
71
72 String fullPath = null;
73 for (String possiblePath : FolderSettings.FrameDirs.getAbsoluteDirs()) {
74 fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
75 if (fullPath != null)
76 break;
77 }
78
79 // If the frame was not located return null
80 if (fullPath == null)
81 return;
82
83 _frameCount++;
84
85 String frameset = Conversion.getFramesetName(frameName);
86
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;
92 while (reader.ready() && ((next = reader.readLine()) != null)) {
93 if (next.startsWith("T")) {
94 String toSearch = next.substring(2);
95 if (toSearch.toLowerCase().contains(pattern))
96 frameResults.add(toSearch);
97 }else if (next.startsWith("F")) {
98 String link = next.substring(2);
99 if(!FrameIO.isValidFrameName(link))
100 link = frameset + link;
101 searchTree(link, pattern, results, visited);
102 }
103 }
104 //Only add the results if a match was found on the frame
105 if(frameResults.size() > 0)
106 results.put(frameName, frameResults);
107 } catch (FileNotFoundException e) {
108 e.printStackTrace();
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.