1 | /**
|
---|
2 | * SearchAgent.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 |
|
---|
23 | import org.expeditee.gui.DisplayController;
|
---|
24 | import org.expeditee.gui.Frame;
|
---|
25 | import org.expeditee.gui.FrameCreator;
|
---|
26 | import org.expeditee.gui.FrameIO;
|
---|
27 | import org.expeditee.io.Conversion;
|
---|
28 | import org.expeditee.items.Item;
|
---|
29 | import org.expeditee.items.Text;
|
---|
30 |
|
---|
31 | public abstract class SearchAgent extends DefaultAgent {
|
---|
32 |
|
---|
33 | private static final String DEFAULT_RESULTS_FRAMESET = "SearchResults";
|
---|
34 |
|
---|
35 | public static final int SURROGATE_LENGTH = 50;
|
---|
36 |
|
---|
37 | protected FrameCreator _results;
|
---|
38 |
|
---|
39 | protected String _pattern;
|
---|
40 |
|
---|
41 | protected String _replacementString;
|
---|
42 |
|
---|
43 | protected String _startName;
|
---|
44 |
|
---|
45 | public SearchAgent(String searchText) {
|
---|
46 | if (searchText != null)
|
---|
47 | _pattern = searchText.toLowerCase();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public boolean initialise(Frame frame, Item item) {
|
---|
52 | String pattern = item.getText();
|
---|
53 | String resultFrameset = null;
|
---|
54 |
|
---|
55 | // TODO use a results frame specified on the profile frame
|
---|
56 | if (item.getLink() == null) {
|
---|
57 | resultFrameset = DEFAULT_RESULTS_FRAMESET;
|
---|
58 | } else {
|
---|
59 | resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
|
---|
60 | false);
|
---|
61 | }
|
---|
62 | return initialise(frame, item, getSearchDescription(frame), resultFrameset,
|
---|
63 | null, pattern);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @param frame
|
---|
68 | * @return
|
---|
69 | */
|
---|
70 | protected String getSearchDescription(Frame frame) {
|
---|
71 | return frame.getFramesetName();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | *
|
---|
76 | * @param frame
|
---|
77 | * @param item
|
---|
78 | * @param startName
|
---|
79 | * @param resultsFrameset
|
---|
80 | * @param replacementString
|
---|
81 | * @param pattern
|
---|
82 | * is ignored if the pattern has already been set earlier.
|
---|
83 | * @return
|
---|
84 | */
|
---|
85 | public boolean initialise(Frame frame, Item item, String startName,
|
---|
86 | String resultsFrameset, String replacementString, String pattern) {
|
---|
87 | // TODO: Put the init params in the constructor!! Dont want to be
|
---|
88 | // setting _pattern in two places!
|
---|
89 |
|
---|
90 | if (_pattern == null)
|
---|
91 | _pattern = pattern.toLowerCase();
|
---|
92 | _replacementString = replacementString;
|
---|
93 | _startName = startName;
|
---|
94 |
|
---|
95 | // Create a frame to put the results on with the search query
|
---|
96 | // and type as the title
|
---|
97 | String title = this.getClass().getSimpleName() + " [" + startName
|
---|
98 | + "]"+getResultsTitleSuffix();
|
---|
99 | _results = new FrameCreator(resultsFrameset, FrameIO.FRAME_PATH, title,
|
---|
100 | false, true);
|
---|
101 | // Set the frame to be displayed after running the agent
|
---|
102 | _end = _results.getFirstFrame();
|
---|
103 |
|
---|
104 | return super.initialise(frame, item);
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected String getResultsTitleSuffix() {
|
---|
108 | return " [" + _pattern + "]";
|
---|
109 | }
|
---|
110 |
|
---|
111 | public String getResultsFrameName() {
|
---|
112 | return _results.getName();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public static boolean searchItem(Text itemToSearch, String pattern,
|
---|
116 | String replacementString) {
|
---|
117 | String searchStr = itemToSearch.getText().toLowerCase();
|
---|
118 | boolean bFound = searchStr.contains(pattern.toLowerCase());
|
---|
119 | // If it is a find and replace... then replace with the replacement
|
---|
120 | // string
|
---|
121 | if (bFound && replacementString != null) {
|
---|
122 | itemToSearch.setText(searchStr.replaceAll(pattern,
|
---|
123 | replacementString));
|
---|
124 | }
|
---|
125 | return bFound;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public static boolean searchFrame(FrameCreator results, String frameName, String pattern, String replacementString)
|
---|
129 | {
|
---|
130 | boolean wasXrayMode = DisplayController.isXRayMode();
|
---|
131 | if (!wasXrayMode) DisplayController.ToggleXRayMode();
|
---|
132 | Frame frameToSearch = FrameIO.LoadFrame(frameName);
|
---|
133 | if (!wasXrayMode) DisplayController.ToggleXRayMode();
|
---|
134 | if (frameToSearch == null)
|
---|
135 | return false;
|
---|
136 | for (Text itemToSearch : frameToSearch.getTextItems()) {
|
---|
137 | // Search for the item and add it to the results page if
|
---|
138 | // it is found
|
---|
139 | if (searchItem(itemToSearch, pattern, replacementString)) {
|
---|
140 | // Add a linked item to the results frame
|
---|
141 | results.addText(frameName, null, frameName, null, false);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | DisplayController.requestRefresh(true);
|
---|
145 | FrameIO.SaveFrame(frameToSearch, false);
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected int addResults(String frameName, Collection<String> found) {
|
---|
150 | return addResults(frameName, frameName, found);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * @param frameNumber
|
---|
155 | * @param frameName
|
---|
156 | * @param found
|
---|
157 | * @return
|
---|
158 | */
|
---|
159 | protected int addResults(String frameNumber, String frameName, Collection<String> found) {
|
---|
160 | int size = found == null ? 0 : found.size();
|
---|
161 | // If the frame exists
|
---|
162 | if (found != null)
|
---|
163 | _frameCount++;
|
---|
164 | if (size > 0) {
|
---|
165 | // String repeats = size > 1? ("("+ size+ ")") : "";
|
---|
166 | for (String s : found) {
|
---|
167 | StringBuffer surrogate = new StringBuffer();
|
---|
168 | surrogate.append("[").append(frameNumber).append("] ");
|
---|
169 | if (s.length() > SearchAgent.SURROGATE_LENGTH)
|
---|
170 | surrogate.append(
|
---|
171 | s.substring(0, SearchAgent.SURROGATE_LENGTH - 3))
|
---|
172 | .append("...");
|
---|
173 | else {
|
---|
174 | surrogate.append(s);
|
---|
175 | }
|
---|
176 |
|
---|
177 | _results.addText(surrogate.toString(), null,
|
---|
178 | frameName, null, false);
|
---|
179 | DisplayController.requestRefresh(true);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | return size;
|
---|
183 | }
|
---|
184 | }
|
---|