Add default search capability
If the search key cannot be found, or the search is only one word,
SearchHandler.Transform() will fall back to a default search plugin if
one is defined. Config was changed to look for DefaultSearchHandler
directives, and a Config.DefaultSearchHandler field was added.
public static string[] Arguments;
public static List<string> Plugins;
public static List<SearchHandlerPair> SearchHandlers;
+ public static string DefaultSearchHandler;
private static HashSet<string> CommandLineOptions;
private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
}
SearchHandlers.Add(new SearchHandlerPair(args[0], args[1]));
break;
+ case "defaultsearchhandler":
+ DefaultSearchHandler = m.Groups[2].Value;
+ break;
default:
Console.WriteLine("Unknown Command in {0}: {1}", filename, line);
break;
public class SearchHandler {
WebThing wt;
Dictionary<string,ISearchPlugin> Handlers;
+ ISearchPlugin DefaultSearchHandler;
public SearchHandler(WebThing wt) {
Handlers = new Dictionary<string,ISearchPlugin>();
foreach (SearchHandlerPair p in Config.SearchHandlers) {
AddHandler(p.Shortcut, p.Plugin);
}
+ if (Config.DefaultSearchHandler != null) {
+ SetDefaultHandler(Config.DefaultSearchHandler);
+ }
+ }
+
+ private bool IsValidSearchHandler(WebThingPlugin p) {
+ Type ptype = p.GetType();
+ if (ptype.GetInterface("ISearchPlugin") != null)
+ return true;
+ return false;
}
public void AddHandler(string key, string plugin) {
}
WebThingPlugin p = wt.Plugins.Plugins[plugin];
- Type ptype = p.GetType();
- if (ptype.GetInterface("ISearchPlugin") != null) {
+ if (IsValidSearchHandler(p)) {
Handlers[key] = (ISearchPlugin) p;
Console.WriteLine("Added handler {0} for key {1}", plugin, key);
} else {
}
}
+ public void SetDefaultHandler(string plugin) {
+ if (wt.Plugins.Plugins.ContainsKey(plugin)) {
+ WebThingPlugin p = wt.Plugins.Plugins[plugin];
+ if (IsValidSearchHandler(p)) {
+ DefaultSearchHandler = (ISearchPlugin) p;
+ } else {
+ Console.WriteLine("Could not set default search handler: {0} does not implement ISearchPlugin", plugin);
+ }
+ } else {
+ Console.WriteLine("Could not set default search handler: {0} is not loaded", plugin);
+ }
+ }
+
// Call a search handler based on the leading word
public string Transform(string search) {
Regex get_handler_re = new Regex(@"^([\w-]+)\s+");
if (m.Success) {
string key = m.Groups[1].Value;
string query = get_handler_re.Replace(search, "");
- if (!Handlers.ContainsKey(key)) {
- Console.WriteLine("Could not search with {0}: No search handler defined", key);
- return null;
+ if (Handlers.ContainsKey(key)) {
+ return Handlers[key].SearchTransform(query);
+ } else {
+ if (DefaultSearchHandler != null) {
+ return DefaultSearchHandler.SearchTransform(search);
+ } else {
+ Console.WriteLine("Could not search with {0} and no default search handler defined", key);
+ return null;
+ }
}
- return Handlers[key].SearchTransform(query);
} else {
- return null;
+ if (DefaultSearchHandler != null) {
+ return DefaultSearchHandler.SearchTransform(search);
+ } else {
+ Console.WriteLine("Could not get search key and no default search handler defined");
+ return null;
+ }
}
}
}