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 |
|
---|
19 | package org.expeditee.agents;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.FileNotFoundException;
|
---|
23 | import java.io.FileReader;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.util.Collection;
|
---|
26 | import java.util.HashMap;
|
---|
27 | import java.util.HashSet;
|
---|
28 | import java.util.LinkedList;
|
---|
29 | import java.util.Map;
|
---|
30 |
|
---|
31 | import org.expeditee.gui.Frame;
|
---|
32 | import org.expeditee.gui.FrameIO;
|
---|
33 | import org.expeditee.io.Conversion;
|
---|
34 | import org.expeditee.settings.folders.FolderSettings;
|
---|
35 |
|
---|
36 | public 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 | }
|
---|