source: trunk/gli/src/org/greenstone/gatherer/cdm/Plugin.java@ 12635

Last change on this file since 12635 was 12635, checked in by mdewsnip, 18 years ago

Made some of the new plugins/classifiers code static in preparation for possibly moving it into new classes.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 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.util.StaticStrings;
33import org.w3c.dom.*;
34
35
36/** 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. */
37public class Plugin
38 extends ArgumentContainer
39{
40 private String default_block_expression = "";
41 private String default_process_expression = "";
42 private boolean does_explode_metadata_databases = false;
43
44
45 /** 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)
46 */
47 public Plugin() {
48 }
49
50
51 public Plugin(Element element, Plugin base_plugin) {
52 super(element, base_plugin);
53 }
54
55
56 /** Method to compare two plugins for ordering.
57 * We override the base method cos we compare by plugin name rather than toString()
58 */
59 public int compareTo(Object object) {
60 if(object instanceof Plugin) {
61 return name.compareTo(((Plugin)object).getName());
62 }
63 return -1;
64 }
65
66
67 /** The assigned plugin constructor.
68 * @param element the DOM Element this plugin is based upon
69 */
70 public DOMProxyListEntry create(Element element) {
71 String plugin_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
72 // Determine the base plugin from the plugin name
73 Plugin base_plugin = PluginManager.getPlugin(plugin_name, true);
74 Plugin plugin = new Plugin(element, base_plugin);
75 if (base_plugin == null) {
76 plugin.setAssigned(false);
77 }
78 base_plugin = null;
79 plugin_name = null;
80 return plugin;
81 }
82
83
84 /** Checks whether the plugin this instance is based on processes metadata databases that can be exploded. */
85 public boolean doesExplodeMetadataDatabases()
86 {
87 Plugin base_plugin = CollectionDesignManager.plugin_manager.getPlugin(getName(), false);
88 if (base_plugin == null) {
89 return false;
90 }
91 return base_plugin.does_explode_metadata_databases;
92 }
93
94
95 /** Checks whether this plugin instance will process the specified file (given its block_exp). */
96 public boolean doesBlockFile(File file)
97 {
98 // Check the filename against the plugin's block_exp value
99 ArrayList arguments = getArguments();
100 for (int i = 0; i < arguments.size(); i++) {
101 Argument argument = (Argument) arguments.get(i);
102 if (argument.getName().equals("block_exp")) {
103 // Try the assigned value first, for when the user has manually set the value
104 String regular_expression = argument.getValue();
105 if (regular_expression == null || regular_expression.equals("")) {
106 // Not set, so use the default value
107 regular_expression = argument.getDefaultValue();
108 if (regular_expression.equals("")) {
109 continue;
110 }
111 }
112
113 if (doesFileMatchRegularExpression(file, regular_expression)) {
114 return true;
115 }
116 }
117 }
118
119 // Try the plugin's default block expression
120 if (!default_block_expression.equals("") && doesFileMatchRegularExpression(file, default_block_expression)) {
121 return true;
122 }
123
124 // This plugin will (probably) not deal with the specified file
125 return false;
126 }
127
128
129 /** Checks whether this plugin instance will process the specified file (given its process_exp). */
130 public boolean doesProcessFile(File file)
131 {
132 // Check the filename against the plugin's process_exp value
133 ArrayList arguments = getArguments();
134 for (int i = 0; i < arguments.size(); i++) {
135 Argument argument = (Argument) arguments.get(i);
136 if (argument.getName().equals("process_exp")) {
137 // Try the assigned value first, for when the user has manually set the value
138 String regular_expression = argument.getValue();
139 if (regular_expression == null || regular_expression.equals("")) {
140 // Not set, so use the default value
141 regular_expression = argument.getDefaultValue();
142 if (regular_expression.equals("")) {
143 continue;
144 }
145 }
146
147 if (doesFileMatchRegularExpression(file, regular_expression)) {
148 return true;
149 }
150 }
151 }
152
153 // Try the plugin's default process expression
154 if (!default_process_expression.equals("") && doesFileMatchRegularExpression(file, default_process_expression)) {
155 return true;
156 }
157
158 // This plugin will (probably) not deal with the specified file
159 return false;
160 }
161
162
163 private boolean doesFileMatchRegularExpression(File file, String regular_expression)
164 {
165 // The $ at the end doesn't seem to work in Java, so need to add ".*" at the start
166 if (regular_expression.startsWith("(?i)")) {
167 // Don't mess up case-insensitive matching though
168 regular_expression = "(?i)" + ".*" + regular_expression.substring("(?i)".length());
169 }
170 else {
171 regular_expression = ".*" + regular_expression;
172 }
173
174 // If the filename matches the regular expression, this plugin will deal with the file in some way
175 if (file.getName().matches(regular_expression)) {
176 return true;
177 }
178
179 return false;
180 }
181
182
183 public boolean isSeparator() {
184 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
185 }
186
187
188 public void setDefaultBlockExpression(String default_block_expression)
189 {
190 this.default_block_expression = default_block_expression;
191 }
192
193
194 public void setDefaultProcessExpression(String default_process_expression)
195 {
196 this.default_process_expression = default_process_expression;
197 }
198
199
200 public void setDoesExplodeMetadataDatabases(boolean does_explode_metadata_databases)
201 {
202 this.does_explode_metadata_databases = does_explode_metadata_databases;
203 }
204}
Note: See TracBrowser for help on using the repository browser.