NoiseFilter: Dont drop last word of apparent hostnames. Too many non-hostnames can...
[beagle.git] / Filters / FilterOle.cs
blob17c8ef399c9be6f0de8030b276be9735705f2dc0
1 //
2 // FilterOle.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Text;
32 using Gsf;
34 using Beagle.Daemon;
35 using Beagle.Util;
37 namespace Beagle.Filters {
39 public abstract class FilterOle : Beagle.Daemon.Filter {
41 public FilterOle ()
45 protected virtual void ExtractMetaData (Gsf.Input sum_stream,
46 Gsf.Input doc_stream)
50 protected virtual void OpenStorage (FileInfo info) {}
52 protected Infile file;
53 protected DocMetaData sumMeta = null;
54 protected DocMetaData docSumMeta = null;
55 protected string FileName;
57 override protected void DoOpen (FileInfo info)
59 try {
60 Gsf.Global.Init ();
62 Input input = new InputStdio (info.FullName);
64 if (input != null) {
65 Input uncompressed_input = input.Uncompress();
66 input.Dispose ();
67 file = new InfileMSOle (uncompressed_input);
68 uncompressed_input.Dispose ();
71 if (input == null || file == null) {
72 Logger.Log.Error ("Unable to open [{0}] ",info.FullName);
73 Console.WriteLine ("input/file is null");
74 Error ();
75 return;
78 OpenStorage (info);
79 } catch (Exception e) {
80 Logger.Log.Error ("Unable to open "+info.FullName);
81 Console.WriteLine ("{0}", e.Message);
82 Error ();
83 return;
87 void PullMetaData (Gsf.Input sum_stream, Gsf.Input doc_stream)
90 DocProp prop = null;
91 string str = null;
93 sumMeta = new DocMetaData ();
94 if (sum_stream != null)
95 Msole.MetadataRead (sum_stream, sumMeta);
96 else
97 Logger.Log.Warn ("SummaryInformationStream not found in {0}", FileName);
99 docSumMeta = new DocMetaData ();
100 if (doc_stream != null)
101 Msole.MetadataRead (doc_stream, docSumMeta);
102 else
103 Logger.Log.Warn ("DocumentSummaryInformationStream not found in {0}", FileName);
105 if (sumMeta != null) {
106 prop = sumMeta.Lookup ("dc:title");
107 if (prop != null)
108 str = prop.Val as string;
109 if (str != null && str.Length > 0)
110 AddProperty (Beagle.Property.New ("dc:title", str));
112 str = null;
113 prop = sumMeta.Lookup ("dc:subject");
114 if (prop != null)
115 str = prop.Val as string;
116 if (str != null && str.Length > 0)
117 AddProperty (Beagle.Property.New ("dc:subject", str));
119 str = null;
120 prop = sumMeta.Lookup ("dc:description");
121 if (prop != null)
122 str = prop.Val as string;
123 if (str != null && str.Length > 0)
124 AddProperty (Beagle.Property.New ("dc:description", str));
126 str = null;
127 prop = sumMeta.Lookup ("gsf:keywords");
128 if (prop != null)
129 str = prop.Val as string;
130 if (str != null && str.Length > 0)
131 AddProperty (Beagle.Property.New ("fixme:keywords", str));
133 str = null;
134 prop = sumMeta.Lookup ("gsf:creator");
135 if (prop != null)
136 str = prop.Val as string;
137 if (str != null && str.Length > 0)
138 AddProperty (Beagle.Property.New ("fixme:author", str));
140 str = null;
141 prop = sumMeta.Lookup ("gsf:last-saved-by");
142 if (prop != null)
143 str = prop.Val as string;
144 if (str != null && str.Length > 0)
145 AddProperty (Beagle.Property.New ("fixme:last-saved-by", str));
147 str = null;
148 prop = sumMeta.Lookup ("gsf:generator");
149 if (prop != null)
150 str = prop.Val as string;
151 if (str != null && str.Length > 0)
152 AddProperty (Beagle.Property.New ("fixme:generator", str));
154 str = null;
155 prop = sumMeta.Lookup ("gsf:template");
156 if (prop != null)
157 str = prop.Val as string;
158 if (str != null && str.Length > 0)
159 AddProperty (Beagle.Property.New ("fixme:template", str));
162 if (docSumMeta != null) {
163 str = null;
164 prop = docSumMeta.Lookup ("gsf:company");
165 if (prop != null)
166 str = prop.Val as string;
167 if (str != null && str.Length > 0)
168 AddProperty (Beagle.Property.New ("fixme:company", str));
170 str = null;
171 prop = docSumMeta.Lookup ("gsf:category");
172 if (prop != null)
173 str = prop.Val as string;
174 if (str != null && str.Length > 0)
175 AddProperty (Beagle.Property.New ("fixme:category", str));
179 ExtractMetaData (sum_stream, doc_stream);
181 if (sumMeta != null)
182 sumMeta.Dispose ();
184 if (docSumMeta != null)
185 docSumMeta.Dispose ();
188 override protected void DoPullProperties ()
190 Input sum_stream = null;
191 Input doc_stream = null;
192 string str = null;
193 int childCount = 0;
194 int found = 0;
196 if (file == null) {
197 Finished ();
198 return;
201 try {
202 sum_stream = file.ChildByName ("\u0005SummaryInformation");
203 doc_stream = file.ChildByName ("\u0005DocumentSummaryInformation");
205 PullMetaData (sum_stream, doc_stream);
206 } catch (Exception e) {
207 Logger.Log.Error (e, "Exception occurred duing DoPullProperties.");
208 Error ();
209 } finally {
210 if (sum_stream != null)
211 sum_stream.Dispose ();
212 if (doc_stream != null)
213 doc_stream.Dispose ();
217 override protected void DoClose ()
219 if (file != null)
220 file.Dispose ();
222 Log.Debug ("File should be closed now or very shortly.");
224 // FIXME: Uncomment this when Shutdown() is available in gsf#
225 // Gsf.Global.Shutdown ();
228 // FIXME: These are utility functions and can be useful
229 // outside this filter as well.
230 public static uint GetInt32 (byte [] data, int offset) {
231 return (uint)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
233 public static ushort GetInt16 (byte [] data, int offset) {
234 return (ushort)(data[offset] + (data[offset + 1] << 8));