2 // FilterKonqHistory.cs
4 // Copyright (C) 2005 Debajyoti Bera <dbera.web@gmail.com>
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.
29 using System
.Collections
;
36 using HtmlAgilityPack
;
38 namespace Beagle
.Filters
{
40 public class FilterKonqHistory
: Beagle
.Filters
.FilterHtml
{
42 // use a static buffer to prevent constant alloc and de-alloc
43 private static byte[] buf
= null;
45 public FilterKonqHistory ()
47 RegisterSupportedTypes ();
50 override protected void DoOpen (FileInfo info
)
53 buf
= new byte [1024];
55 StreamReader reader
= new StreamReader (Stream
, Encoding
.GetEncoding (28591));
57 // read the charset hint from indexable
58 string charset
= null;
59 foreach (Property property
in IndexableProperties
) {
60 if (property
.Key
!= (StringFu
.UnindexedNamespace
+ "charset"))
62 charset
= (string) property
.Value
;
63 //Console.WriteLine ("charset hint accepted: " + charset);
68 // now create a memorystream where htmlfilter will begin his show
69 Stream
.Seek (0, SeekOrigin
.Begin
);
70 // count past 8 lines ... Streams suck!
71 int c
= 0; // stores the number of newlines read
73 while (c
< 8 && (b
= Stream
.ReadByte ()) != -1) {
77 // copy the rest of the file to a memory stream
78 MemoryStream mem_stream
= new MemoryStream ();
79 while ((b
= Stream
.Read (buf
, 0, 1024)) != 0)
80 mem_stream
.Write (buf
, 0, b
);
81 mem_stream
.Seek (0, SeekOrigin
.Begin
);
84 HtmlDocument doc
= new HtmlDocument ();
85 doc
.ReportNode
+= HandleNodeEvent
;
86 doc
.StreamMode
= true;
87 // we already determined encoding
88 doc
.OptionReadEncoding
= false;
89 Encoding enc
= Encoding
.UTF8
;
90 if (charset
!= null && charset
!= String
.Empty
)
91 enc
= Encoding
.GetEncoding (charset
);
95 doc
.Load (mem_stream
);
97 doc
.Load (mem_stream
, enc
);
98 } catch (NotSupportedException
) {
99 doc
.Load (mem_stream
, Encoding
.ASCII
);
100 } catch (Exception e
) {
101 Console
.WriteLine (e
.Message
);
102 Console
.WriteLine (e
.StackTrace
);
108 override protected void RegisterSupportedTypes ()
110 AddSupportedFlavor (FilterFlavor
.NewFromMimeType (KonqHistoryUtil
.KonqCacheMimeType
));