QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / Tiles / Template.cs
blobe4aa6fea81eb200efea2c3a2511545fad62e1870
1 //
2 // Template.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.
27 using System.IO;
28 using System;
29 using System.Collections;
30 using System.Reflection;
31 using BU = Beagle.Util;
33 namespace Beagle.Tile {
35 public class Template {
37 private ArrayList lines = new ArrayList ();
39 private bool dirty = true;
40 private string string_html;
42 private Hashtable values = new Hashtable ();
44 private void Setup (Stream stream)
46 StreamReader sr = new StreamReader (stream);
47 string line;
48 while ((line = sr.ReadLine ()) != null)
49 lines.Add (line);
50 dirty = true;
53 public Template (string template_resource)
55 // We look for the resource in the assembly that contains
56 // the type's definition.
57 Assembly assembly = Assembly.GetAssembly (this.GetType ());
58 Stream stream = assembly.GetManifestResourceStream (template_resource);
60 if (stream == null)
61 throw new Exception (String.Format ("No such resource: {0}", template_resource));
62 Setup (stream);
65 public Template (Stream stream)
67 Setup (stream);
70 public void AddProperties (ICollection properties)
72 foreach (Property prop in properties)
73 this.values [prop.Key] = prop.Value;
74 dirty = true;
77 public void AddHit (Hit hit)
79 string uri = hit.Uri.ToString ();
81 values["Uri"] = uri;
83 if (uri.Length > 40)
84 uri = uri.Substring (0, 40) + "...";
85 values["DisplayUri"] = uri;
87 values["MimeType"] = hit.MimeType;
88 values["Source"] = hit.Source;
89 values["Score"] = hit.Score.ToString ();
90 values["Timestamp"] = BU.StringFu.DateTimeToString (hit.Timestamp);
91 values["Path"] = hit.Path;
92 values["FileName"] = hit.FileName;
93 values["DirectoryName"] = hit.DirectoryName;
94 if (hit.FileInfo != null)
95 values["FolderName"] = hit.FileInfo.Directory.Name;
96 else if (hit.DirectoryInfo != null)
97 values["FolderName"] = hit.DirectoryInfo.Parent.Name;
98 AddProperties (hit.Properties);
101 public IDictionary Values {
102 get { return values; }
105 public string this [string key] {
106 get { return (string) values[key]; }
107 set {
108 if (value == null) {
109 if (values.Contains (key))
110 values.Remove (key);
111 return;
113 values[key] = value as string;
114 dirty = true;
118 private string GetValue (string key)
120 int pos;
121 string formatter;
122 string subkey;
124 pos = key.IndexOf ('%');
126 if (pos >= 0) {
127 formatter = key.Substring (0, pos);
128 subkey = key.Substring (pos + 1);
129 } else {
130 formatter = null;
131 subkey = key;
134 switch (formatter) {
135 case "" :
136 if (values.Contains (subkey))
137 return (string)values[subkey];
138 else
139 return null;
140 case "image" :
141 return Images.GetHtmlSource (subkey, "image/png"); // FIXME: other mime types
142 case "stock" :
143 string stockid;
144 int size;
145 pos = subkey.IndexOf ('|');
146 if (pos >= 0) {
147 size = int.Parse (subkey.Substring (0, pos));
148 stockid = subkey.Substring (pos + 1);
149 } else {
150 size = 48;
151 stockid = subkey;
154 return Images.GetHtmlSourceForStock (stockid,
155 size);
156 case "date" :
157 try {
158 return BU.StringFu.DateTimeToPrettyString (BU.StringFu.StringToDateTime ((string) values [subkey]).ToLocalTime ());
159 } catch {
160 return (string) values [subkey];
162 case "text" :
163 return Mono.Unix.Catalog.GetString (subkey); // I18N
164 default :
165 if (values.Contains (key))
166 return BU.StringFu.EscapeStringForHtml ((string)values[subkey], false);
167 else
168 return null;
172 private string Build ()
174 StringWriter writer = new StringWriter ();
175 foreach (string line in lines) {
176 string new_line = "";
177 if (line.Length < 1) {
178 writer.WriteLine ("");
179 continue;
182 string [] fragments = line.Split ('@');
183 for (int i = 0; i < fragments.Length; i++) {
184 if (i % 2 == 1) {
185 string value = GetValue (fragments[i]);
186 if (value == null) {
187 new_line = "";
188 break;
190 new_line = new_line + value;
191 } else {
192 new_line = new_line + fragments[i];
195 if (new_line != "")
196 writer.Write (new_line + "\n");
199 return writer.ToString ();
202 public override string ToString ()
204 if (dirty) {
205 string_html = Build ();
206 dirty = false;
209 return string_html;