Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / ExtractContent.cs
blobcd7d4d6896e825af9daaa43f04389d8043f4c9b0
1 //
2 // ExtractContent.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // 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 FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Net;
33 using Beagle;
34 using Beagle.Util;
35 using Beagle.Daemon;
37 class ExtractContentTool {
39 static bool tokenize = false;
40 static bool show_children = false;
42 // FIXME: We don't display structural breaks
43 static void DisplayContent (string line)
45 if (tokenize) {
47 string [] parts = line.Split (' ');
48 for (int i = 0; i < parts.Length; ++i) {
49 string part = parts [i].Trim ();
50 if (part != "")
51 Console.WriteLine ("{0}", part);
54 } else {
55 Console.WriteLine (line);
59 static bool first_indexable = true;
61 static void Display (Indexable indexable)
63 if (!first_indexable) {
64 Console.WriteLine ();
65 Console.WriteLine ("-----------------------------------------");
66 Console.WriteLine ();
68 first_indexable = false;
70 Console.WriteLine ("Filename: " + indexable.Uri);
72 if (indexable.MimeType != null)
73 Console.WriteLine ("MimeType: " + indexable.MimeType);
75 Filter filter;
77 if (! FilterFactory.FilterIndexable (indexable, out filter))
78 Console.WriteLine ("No filter!");
80 if (filter != null)
81 Console.WriteLine ("Filter: {0}", filter);
83 Console.WriteLine ();
85 if (filter != null && filter.ChildIndexables != null && filter.ChildIndexables.Count > 0) {
86 Console.WriteLine ("Child indexables:");
88 foreach (Indexable i in filter.ChildIndexables)
89 Console.WriteLine (" {0}", i.Uri);
91 Console.WriteLine ();
94 bool first;
96 first = true;
97 foreach (Beagle.Property prop in indexable.Properties) {
98 if (first) {
99 Console.WriteLine ("Properties:");
100 first = false;
102 Console.WriteLine (" {0} = {1}", prop.Key, prop.Value);
104 if (! first)
105 Console.WriteLine ();
107 if (indexable.NoContent)
108 return;
110 TextReader reader;
112 reader = indexable.GetTextReader ();
113 if (reader != null) {
114 string line;
115 first = true;
116 while ((line = reader.ReadLine ()) != null) {
117 if (first) {
118 Console.WriteLine ("Content:");
119 first = false;
121 DisplayContent (line);
124 if (first)
125 Console.WriteLine ("(no content)");
126 else
127 Console.WriteLine ();
130 reader = indexable.GetHotTextReader ();
131 if (reader != null) {
132 string line;
133 first = true;
134 while ((line = reader.ReadLine ()) != null) {
135 if (first) {
136 Console.WriteLine ("HotContent:");
137 first = false;
139 DisplayContent (line);
142 if (first)
143 Console.WriteLine ("(no hot content)");
144 else
145 Console.WriteLine ();
148 if (show_children && filter != null && filter.ChildIndexables != null) {
149 foreach (Indexable i in filter.ChildIndexables) {
150 i.StoreStream ();
151 i.DeleteContent = true;
152 Display (i);
158 static void PrintUsage ()
160 Console.WriteLine ("beagle-extract-content: Extracts filtered data from a file.");
161 Console.WriteLine ("Copyright (C) 2004-2005 Novell, Inc.");
162 Console.WriteLine ();
163 Console.WriteLine ("Usage: beagle-extract-content [OPTIONS] file [file ...]");
164 Console.WriteLine ();
165 Console.WriteLine ("Options:");
166 Console.WriteLine (" --tokenize\t\tTokenize the text before printing");
167 Console.WriteLine (" --show-children\tShow filtering information for items created by filters");
168 Console.WriteLine (" --help\t\tShow this message");
169 Console.WriteLine ();
172 static int Main (string[] args)
174 Logger.DefaultEcho = true;
175 Logger.DefaultLevel = LogLevel.Debug;
177 if (Array.IndexOf (args, "--help") != -1) {
178 PrintUsage ();
179 return 0;
182 if (Array.IndexOf (args, "--tokenize") != -1)
183 tokenize = true;
185 if (Array.IndexOf (args, "--show-children") != -1)
186 show_children = true;
188 foreach (string arg in args) {
190 // option, skip it
191 if (arg.Substring (0, 2) == "--")
192 continue;
194 Uri uri = UriFu.PathToFileUri (arg);
195 Indexable indexable = new Indexable (uri);
197 try {
198 Display (indexable);
199 } catch (Exception e) {
200 Console.WriteLine ("Unable to filter {0}: {1}", uri, e.Message);
201 return -1;
205 return 0;