source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/Plugin.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 8.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.io.*;
30import java.util.*;
31import org.greenstone.gatherer.DebugStream;
32import org.greenstone.gatherer.greenstone.Plugins;
33import org.greenstone.gatherer.util.StaticStrings;
34import org.w3c.dom.*;
35
36
37/** This class is responsible for storing information from a parsed pluginfo call in such a way that it allows easy access to parsed details for the purposes of user design and specification of plugins. */
38public class Plugin
39 extends ArgumentContainer
40{
41 private String default_block_expression = "";
42 private String default_process_expression = "";
43 private boolean does_explode_metadata_databases = false;
44 private boolean does_replace_srcdocs_with_html = false; // to work with replace_srcdoc_with_html.pl
45 /** Plugins are loaded as needed. This variable indicates 1. whether a plugin has been loaded already; 2. whether a
46 * plugin has been successfully loaded. When both are true, has_loaded_options will be true. */
47 private boolean has_loaded_options = false;
48
49 /** Constructor used in DOMProxyListModel initializations, and Library Level. Used for Base plugins (those in the list of available plugins, not ones that are in the DOMProxyList)
50 */
51 public Plugin() {
52 }
53
54
55 public Plugin(Element element, Plugin base_plugin) {
56 super(element, base_plugin);
57 }
58
59
60 /** Method to compare two plugins for ordering.
61 * We override the base method cos we compare by plugin name rather than toString()
62 */
63 public int compareTo(Object object) {
64 if(object instanceof Plugin) {
65 return name.compareTo(((Plugin)object).getName());
66 }
67 return -1;
68 }
69
70
71 /** The assigned plugin constructor.
72 * @param element the DOM Element this plugin is based upon
73 */
74 public DOMProxyListEntry create(Element element) {
75 String plugin_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
76 // Determine the base plugin from the plugin name
77 Plugin base_plugin = Plugins.getPlugin(plugin_name, true);
78 Plugin plugin = new Plugin(element, base_plugin);
79 if (base_plugin == null) {
80 plugin.setAssigned(false);
81 }
82 base_plugin = null;
83 plugin_name = null;
84 return plugin;
85 }
86
87
88 public boolean hasLoadedOptions()
89 {
90 return has_loaded_options;
91 }
92
93
94 /** Checks whether the plugin this instance is based on processes metadata databases that can be exploded. */
95 public boolean doesExplodeMetadataDatabases()
96 {
97 Plugin base_plugin = Plugins.getPlugin(getName(), false);
98 if (base_plugin == null) {
99 return false;
100 }
101 return base_plugin.does_explode_metadata_databases;
102 }
103
104 /** Checks whether the plugin this instance is based on processes source documents that can be replaced with their Greenstone generated html variants.
105 * This method works with replace_srcdoc_with_html.pl */
106 public boolean doesReplaceSrcDocsWithHtml()
107 {
108 //return does_replace_srcdocs_with_html;
109 Plugin base_plugin = Plugins.getPlugin(getName(), false);
110 if (base_plugin == null) {
111 return false;
112 }
113 return base_plugin.does_replace_srcdocs_with_html;
114 }
115
116 /** Checks whether this plugin instance will process the specified file (given its block_exp). */
117 public boolean doesBlockFile(File file)
118 {
119 // Check the filename against the plugin's block_exp value
120 ArrayList arguments = getArguments();
121 for (int i = 0; i < arguments.size(); i++) {
122 Argument argument = (Argument) arguments.get(i);
123 if (argument.getName().equals("block_exp")) {
124 // Try the assigned value first, for when the user has manually set the value
125 String regular_expression = argument.getValue();
126 if (regular_expression == null || regular_expression.equals("")) {
127 // Not set, so use the default value
128 regular_expression = argument.getDefaultValue();
129 if (regular_expression.equals("")) {
130 continue;
131 }
132 }
133
134 if (doesFileMatchRegularExpression(file, regular_expression)) {
135 return true;
136 }
137 }
138 }
139
140 // Try the plugin's default block expression
141 if (!default_block_expression.equals("") && doesFileMatchRegularExpression(file, default_block_expression)) {
142 return true;
143 }
144
145 // This plugin will (probably) not deal with the specified file
146 return false;
147 }
148
149
150 /** Checks whether this plugin instance will process the specified file (given its process_exp). */
151 public boolean doesProcessFile(File file)
152 {
153 // Check the filename against the plugin's process_exp value
154 ArrayList arguments = getArguments();
155 for (int i = 0; i < arguments.size(); i++) {
156 Argument argument = (Argument) arguments.get(i);
157 if (argument.getName().equals("process_exp")) {
158 // Try the assigned value first, for when the user has manually set the value
159 String regular_expression = argument.getValue();
160 if (regular_expression == null || regular_expression.equals("")) {
161 // Not set, so use the default value
162 regular_expression = argument.getDefaultValue();
163 if (regular_expression.equals("")) {
164 continue;
165 }
166 }
167
168 if (doesFileMatchRegularExpression(file, regular_expression)) {
169 return true;
170 }
171 }
172 }
173
174 // Try the plugin's default process expression
175 if (!default_process_expression.equals("") && doesFileMatchRegularExpression(file, default_process_expression)) {
176 return true;
177 }
178
179 // This plugin will (probably) not deal with the specified file
180 return false;
181 }
182
183
184 private boolean doesFileMatchRegularExpression(File file, String regular_expression)
185 {
186 // The $ at the end doesn't seem to work in Java, so need to add ".*" at the start
187 if (regular_expression.startsWith("(?i)")) {
188 // Don't mess up case-insensitive matching though
189 regular_expression = "(?i)" + ".*" + regular_expression.substring("(?i)".length());
190 }
191 else {
192 regular_expression = ".*" + regular_expression;
193 }
194
195 // If the filename matches the regular expression, this plugin will deal with the file in some way
196 if (file.getName().matches(regular_expression)) {
197 return true;
198 }
199
200 return false;
201 }
202
203
204 public boolean isSeparator() {
205 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
206 }
207
208
209 public void setDefaultBlockExpression(String default_block_expression)
210 {
211 this.default_block_expression = default_block_expression;
212 }
213
214
215 public void setDefaultProcessExpression(String default_process_expression)
216 {
217 this.default_process_expression = default_process_expression;
218 }
219
220
221 public void setDoesExplodeMetadataDatabases(boolean does_explode_metadata_databases)
222 {
223 this.does_explode_metadata_databases = does_explode_metadata_databases;
224 }
225
226 // To work with replace_srcdoc_with_html.pl
227 public void setDoesReplaceSrcDocsWithHtml(boolean does_replace_srcdocs_with_html)
228 {
229 this.does_replace_srcdocs_with_html = does_replace_srcdocs_with_html;
230 }
231
232 public void setHasLoadedOptions(boolean hasLoadedOptions)
233 {
234 this.has_loaded_options = hasLoadedOptions;
235 }
236}
Note: See TracBrowser for help on using the repository browser.