1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
//
// FilterExternal.cs
//
// Copyright (C) 2006 Novell, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// The purpose of this filter is to allow users to easily extract text
// content from structured files using an external program. See
// external-filters.txt for more information.
using System;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using Beagle.Daemon;
using Beagle.Util;
namespace Beagle.Filters {
public class ExternalFilterInfo {
[XmlElement ("mimetype")]
public string [] MimeTypes;
[XmlElement ("extension")]
public string [] Extensions;
[XmlElement ("command")]
public string Command;
[XmlElement ("arguments")]
public string Arguments;
}
[XmlRoot ("external-filters")]
public class ExternalFilterInfoContainer {
[XmlElement ("filter")]
public ExternalFilterInfo [] Items;
}
public class FilterExternal : Beagle.Daemon.Filter {
private ExternalFilterInfo [] filters = null;
public FilterExternal ()
{
string path = Path.Combine (Path.Combine (ExternalStringsHack.SysConfDir, "beagle"), "external-filters.xml");
try {
FileStream fs = File.Open (path, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer serializer = new XmlSerializer (typeof (ExternalFilterInfoContainer));
ExternalFilterInfoContainer container = (ExternalFilterInfoContainer) serializer.Deserialize (fs);
fs.Close ();
this.filters = container.Items;
} catch (FileNotFoundException) {
// Probably not an error if the file isn't there.
} catch (DirectoryNotFoundException) {
// The directory isn't there either, not an error.
} catch (InvalidOperationException ex) {
// Something wrong with the XML
Logger.Log.Error (ex, "Unable to parse {0}", path);
} catch (XmlException ex) {
// Something else wrong with the XML
Logger.Log.Error (ex, "Unable to parse {0}", path);
}
if (this.filters == null)
return;
foreach (ExternalFilterInfo efi in this.filters) {
if (efi.MimeTypes != null) {
foreach (string s in efi.MimeTypes) {
FilterFlavor flavor = FilterFlavor.NewFromMimeType (s);
// External filters have higher priority than default ones
// This allows users to override default filters by something they want
flavor.Priority = 1;
AddSupportedFlavor (flavor);
}
}
if (efi.Extensions != null) {
foreach (string s in efi.Extensions) {
FilterFlavor flavor = FilterFlavor.NewFromExtension (s);
flavor.Priority = 1;
AddSupportedFlavor (flavor);
}
}
}
}
private ExternalFilterInfo GetFilterInfo (string extension, string mime_type)
{
if (this.filters == null || this.filters.Length == 0)
return null;
if (extension != null) {
foreach (ExternalFilterInfo efi in this.filters) {
if (efi.Extensions == null)
continue;
if (ArrayFu.IndexOfString (efi.Extensions, extension) != -1)
return efi;
}
}
if (mime_type != null) {
foreach (ExternalFilterInfo efi in this.filters) {
if (efi.MimeTypes == null)
continue;
if (ArrayFu.IndexOfString (efi.MimeTypes, mime_type) != -1)
return efi;
}
}
return null;
}
protected override void DoPull ()
{
ExternalFilterInfo efi = GetFilterInfo (this.Extension, this.MimeType);
if (efi == null) {
Logger.Log.Warn ("Unable to find a match for extension {0}, mime type {1} when one should have matched", this.Extension, this.MimeType);
Error ();
}
// FIXME: Need to deal with quotation marks in the XML file, probably.
string[] tmp_argv = efi.Arguments.Split (' ');
string[] argv = new string [tmp_argv.Length + 1];
argv [0] = efi.Command;
int j = 1;
for (int i = 0; i < tmp_argv.Length; i++) {
if (tmp_argv [i] == String.Empty)
continue;
if (tmp_argv [i] == "%s")
argv [j] = FileInfo.FullName;
else
argv [j] = tmp_argv [i];
j++;
}
SafeProcess pc = new SafeProcess ();
pc.Arguments = argv;
pc.RedirectStandardOutput = true;
try {
pc.Start ();
} catch (SafeProcessException e) {
Log.Warn (e.Message);
Error ();
return;
}
StreamReader pout = new StreamReader (pc.StandardOutput);
string str;
while ((str = pout.ReadLine ()) != null) {
AppendText (str);
AppendStructuralBreak ();
}
pout.Close ();
pc.Close ();
Finished ();
}
}
}
|