cvsimport
[beagle.git] / Filters / FilterChm.cs
blob8ae131e603e775fd22ac87e22eca1539983ab6c9
1 //
2 // FilterChm.cs : Trivial implementation of a CHM filter.
3 //
4 // Copyright (C) 2005,2006 Miguel Cabrera <mfcabrera@gmail.com>
5 //
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
26 using System;
27 using System.Collections;
28 using System.IO;
29 using System.Text;
30 using HtmlAgilityPack;
32 using Beagle.Util;
33 using Beagle.Daemon;
35 namespace Beagle.Filters {
37 public class FilterChm : FilterHtml {
39 ChmFile chmFile;
41 public FilterChm () : base()
44 RegisterSupportedTypes ();
45 SnippetMode = true;
49 override protected void DoOpen (FileInfo info)
52 chmFile = new ChmFile ();
55 try {
56 chmFile.Load(info.FullName);
57 } catch (Exception e) {
58 Logger.Log.Warn (e, "Could not load {0}:", info.Name);
59 Finished ();
60 return;
63 chmFile.ParseContents (FilterFileContents);
66 public void FilterFileContents(TextReader text) {
68 HtmlDocument doc = new HtmlDocument ();
69 doc.StreamMode = true;
70 doc.ReportNode += HandleNodeEvent;
72 try {
73 doc.Load (text);
76 catch (Exception e) {
77 Logger.Log.Warn (e, "Error parsing file contents");
78 //Console.WriteLine (e.Message);
79 //Console.WriteLine (e.StackTrace);
85 override protected void DoPullProperties()
87 AddProperty (Beagle.Property.New ("dc:title", chmFile.Title));
90 override protected void DoPull()
92 Finished ();
95 override protected void DoClose()
97 chmFile.Dispose ();
100 override protected void RegisterSupportedTypes()
102 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-chm"));