Dont throw EncodingFoundException unless asked to. Should remove the occassional...
[beagle.git] / Filters / FilterOle.cs
blob0af7805f7426efd7c59be2fd5ca613bbd7fa3e2f
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 ();
61 Input input = new InputStdio (info.FullName);
62 if (input != null) {
63 input = input.Uncompress();
64 file = new InfileMSOle (input);
66 if (input == null || file == null) {
67 Logger.Log.Error ("Unable to open [{0}] ",info.FullName);
68 Console.WriteLine ("input/file is null");
69 Error ();
70 return;
73 OpenStorage (info);
74 } catch (Exception e) {
75 Logger.Log.Error ("Unable to open "+info.FullName);
76 Console.WriteLine ("{0}", e.Message);
77 Error ();
78 return;
82 void PullMetaData (Gsf.Input sum_stream, Gsf.Input doc_stream)
85 DocProp prop = null;
86 string str = null;
88 sumMeta = new DocMetaData ();
89 if (sum_stream != null)
90 Msole.MetadataRead (sum_stream, sumMeta);
91 else
92 Logger.Log.Warn ("SummaryInformationStream not found in {0}", FileName);
94 docSumMeta = new DocMetaData ();
95 if (doc_stream != null)
96 Msole.MetadataRead (doc_stream, docSumMeta);
97 else
98 Logger.Log.Warn ("DocumentSummaryInformationStream not found in {0}", FileName);
100 if (sumMeta != null) {
101 prop = sumMeta.Lookup ("dc:title");
102 if (prop != null)
103 str = prop.Val as string;
104 if (str != null && str.Length > 0)
105 AddProperty (Beagle.Property.New ("dc:title", str));
107 str = null;
108 prop = sumMeta.Lookup ("dc:subject");
109 if (prop != null)
110 str = prop.Val as string;
111 if (str != null && str.Length > 0)
112 AddProperty (Beagle.Property.New ("dc:subject", str));
114 str = null;
115 prop = sumMeta.Lookup ("dc:description");
116 if (prop != null)
117 str = prop.Val as string;
118 if (str != null && str.Length > 0)
119 AddProperty (Beagle.Property.New ("dc:description", str));
121 str = null;
122 prop = sumMeta.Lookup ("gsf:keywords");
123 if (prop != null)
124 str = prop.Val as string;
125 if (str != null && str.Length > 0)
126 AddProperty (Beagle.Property.New ("fixme:keywords", str));
128 str = null;
129 prop = sumMeta.Lookup ("gsf:creator");
130 if (prop != null)
131 str = prop.Val as string;
132 if (str != null && str.Length > 0)
133 AddProperty (Beagle.Property.New ("fixme:author", str));
135 str = null;
136 prop = sumMeta.Lookup ("gsf:last-saved-by");
137 if (prop != null)
138 str = prop.Val as string;
139 if (str != null && str.Length > 0)
140 AddProperty (Beagle.Property.New ("fixme:last-saved-by", str));
142 str = null;
143 prop = sumMeta.Lookup ("gsf:generator");
144 if (prop != null)
145 str = prop.Val as string;
146 if (str != null && str.Length > 0)
147 AddProperty (Beagle.Property.New ("fixme:generator", str));
149 str = null;
150 prop = sumMeta.Lookup ("gsf:template");
151 if (prop != null)
152 str = prop.Val as string;
153 if (str != null && str.Length > 0)
154 AddProperty (Beagle.Property.New ("fixme:template", str));
157 if (docSumMeta != null) {
158 str = null;
159 prop = docSumMeta.Lookup ("gsf:company");
160 if (prop != null)
161 str = prop.Val as string;
162 if (str != null && str.Length > 0)
163 AddProperty (Beagle.Property.New ("fixme:company", str));
165 str = null;
166 prop = docSumMeta.Lookup ("gsf:category");
167 if (prop != null)
168 str = prop.Val as string;
169 if (str != null && str.Length > 0)
170 AddProperty (Beagle.Property.New ("fixme:category", str));
174 ExtractMetaData (sum_stream, doc_stream);
177 override protected void DoPullProperties ()
179 Input sum_stream = null;
180 Input doc_stream = null;
181 string str = null;
182 int childCount = 0;
183 int found = 0;
185 if (file == null) {
186 Finished ();
187 return;
190 try {
191 sum_stream = file.ChildByName ("\u0005SummaryInformation");
192 doc_stream = file.ChildByName ("\u0005DocumentSummaryInformation");
194 PullMetaData (sum_stream, doc_stream);
195 } catch (Exception e) {
196 Logger.Log.Error ("Exception occurred duing DoPullProperties.");
197 Logger.Log.Error (e);
198 Error ();
203 FIXME: Uncomment this when Shutdown() is
204 available in gsf#.
205 override protected void DoClose ()
207 Gsf.Global.Shutdown ();
211 // FIXME: These are utility functions and can be useful
212 // outside this filter as well.
213 public static int GetInt32 (byte [] data, int offset) {
214 return data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24);
216 public static int GetInt16 (byte [] data, int offset) {
217 return data[offset] + (data[offset + 1] << 8);