1 | /**
|
---|
2 | * SearchTreeAndReplace.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.util.Collection;
|
---|
22 | import java.util.HashSet;
|
---|
23 |
|
---|
24 | import org.expeditee.gui.DisplayController;
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.gui.FrameIO;
|
---|
27 | import org.expeditee.items.Text;
|
---|
28 |
|
---|
29 | public class SearchTreeAndReplace extends SearchAgent {
|
---|
30 | public SearchTreeAndReplace(String searchText) {
|
---|
31 | super(searchText);
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | protected Frame process(Frame frame) {
|
---|
36 | try {
|
---|
37 | searchTree(new HashSet<String>(), _startName);
|
---|
38 | } catch (Exception e) {
|
---|
39 | e.printStackTrace();
|
---|
40 | }
|
---|
41 | _results.save();
|
---|
42 |
|
---|
43 | String resultFrameName = _results.getName();
|
---|
44 | if (_clicked != null)
|
---|
45 | _clicked.setLink(resultFrameName);
|
---|
46 |
|
---|
47 | return _results.getFirstFrame();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public boolean searchTree(Collection<String> visitedFrames, String frameName)
|
---|
51 | throws Exception {
|
---|
52 | if (_stop) {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 | // Avoid infinate loops
|
---|
56 | if (visitedFrames.contains(frameName))
|
---|
57 | return false;
|
---|
58 | visitedFrames.add(frameName);
|
---|
59 |
|
---|
60 | Frame frameToSearch = FrameIO.LoadFrame(frameName);
|
---|
61 | if (frameToSearch == null)
|
---|
62 | return false;
|
---|
63 |
|
---|
64 | overwriteMessage("Searching " + frameName);
|
---|
65 |
|
---|
66 | for (Text itemToSearch : frameToSearch.getTextItems()) {
|
---|
67 | // Search for the item and add it to the results page if
|
---|
68 | // it is found
|
---|
69 | if (searchItem(itemToSearch, _pattern,
|
---|
70 | _replacementString)) {
|
---|
71 | // Add a linked item to the results frame
|
---|
72 | _results.addText(frameName, null, frameName, null, false);
|
---|
73 | }
|
---|
74 | if (!itemToSearch.isAnnotation()) {
|
---|
75 | String link = itemToSearch.getAbsoluteLink();
|
---|
76 | if (link != null) {
|
---|
77 | searchTree(visitedFrames, link);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | DisplayController.requestRefresh(true);
|
---|
82 | FrameIO.SaveFrame(frameToSearch, false);
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 | }
|
---|