Merging from head
[beagle.git] / Filters / FilterOle.cs
blob988d2ccfa42e3ff466aaa312b77770575f6c23b4
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;
92 sumMeta = new DocMetaData ();
93 if (sum_stream != null)
94 Msole.MetadataRead (sum_stream, sumMeta);
95 else
96 Logger.Log.Warn ("SummaryInformationStream not found in {0}", FileName);
98 docSumMeta = new DocMetaData ();
99 if (doc_stream != null)
100 Msole.MetadataRead (doc_stream, docSumMeta);
101 else
102 Logger.Log.Warn ("DocumentSummaryInformationStream not found in {0}", FileName);
104 if (sumMeta != null) {
105 prop = sumMeta.Lookup ("dc:title");
106 if (prop != null)
107 AddProperty (Beagle.Property.New ("dc:title", prop.Val as string));
109 prop = sumMeta.Lookup ("dc:subject");
110 if (prop != null)
111 AddProperty (Beagle.Property.New ("dc:subject", prop.Val as string));
113 prop = sumMeta.Lookup ("dc:description");
114 if (prop != null)
115 AddProperty (Beagle.Property.New ("dc:description", prop.Val as string));
117 prop = sumMeta.Lookup ("gsf:keywords");
118 if (prop != null)
119 AddProperty (Beagle.Property.New ("fixme:keywords", prop.Val as string));
121 prop = sumMeta.Lookup ("gsf:creator");
122 if (prop != null)
123 AddProperty (Beagle.Property.New ("fixme:author", prop.Val as string));
125 prop = sumMeta.Lookup ("gsf:last-saved-by");
126 if (prop != null)
127 AddProperty (Beagle.Property.New ("fixme:last-saved-by", prop.Val as string));
129 prop = sumMeta.Lookup ("gsf:generator");
130 if (prop != null)
131 AddProperty (Beagle.Property.New ("fixme:generator", prop.Val as string));
133 prop = sumMeta.Lookup ("gsf:template");
134 if (prop != null)
135 AddProperty (Beagle.Property.New ("fixme:template", prop.Val as string));
138 if (docSumMeta != null) {
139 prop = docSumMeta.Lookup ("gsf:company");
140 if (prop != null)
141 AddProperty (Beagle.Property.New ("fixme:company", prop.Val as string));
143 prop = docSumMeta.Lookup ("gsf:category");
144 if (prop != null)
145 AddProperty (Beagle.Property.New ("fixme:category", prop.Val as string));
148 ExtractMetaData (sum_stream, doc_stream);
150 if (sumMeta != null)
151 sumMeta.Dispose ();
153 if (docSumMeta != null)
154 docSumMeta.Dispose ();
157 override protected void DoPullProperties ()
159 Input sum_stream = null;
160 Input doc_stream = null;
162 if (file == null) {
163 Finished ();
164 return;
167 try {
168 sum_stream = file.ChildByName ("\u0005SummaryInformation");
169 doc_stream = file.ChildByName ("\u0005DocumentSummaryInformation");
171 PullMetaData (sum_stream, doc_stream);
172 } catch (Exception e) {
173 Logger.Log.Error (e, "Exception occurred duing DoPullProperties.");
174 Error ();
175 } finally {
176 if (sum_stream != null)
177 sum_stream.Dispose ();
178 if (doc_stream != null)
179 doc_stream.Dispose ();
183 override protected void DoClose ()
185 if (file != null)
186 file.Dispose ();
188 Log.Debug ("File should be closed now or very shortly.");
190 // FIXME: Uncomment this when Shutdown() is available in gsf#
191 // Gsf.Global.Shutdown ();
194 // FIXME: These are utility functions and can be useful
195 // outside this filter as well.
196 public static uint GetInt32 (byte [] data, int offset) {
197 return (uint)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
199 public static ushort GetInt16 (byte [] data, int offset) {
200 return (ushort)(data[offset] + (data[offset + 1] << 8));