4 // Copyright (C) 2004 Novell, Inc.
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
;
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
);
48 while ((line
= sr
.ReadLine ()) != null)
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
);
61 throw new Exception (String
.Format ("No such resource: {0}", template_resource
));
65 public Template (Stream stream
)
70 public void AddProperties (ICollection properties
)
72 foreach (Property prop
in properties
)
73 this.values
[prop
.Key
] = prop
.Value
;
77 public void AddHit (Hit hit
)
79 string uri
= hit
.Uri
.ToString ();
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]; }
109 if (values
.Contains (key
))
113 values
[key
] = value as string;
118 private string GetValue (string key
)
124 pos
= key
.IndexOf ('%');
127 formatter
= key
.Substring (0, pos
);
128 subkey
= key
.Substring (pos
+ 1);
136 if (values
.Contains (subkey
))
137 return (string)values
[subkey
];
141 return Images
.GetHtmlSource (subkey
, "image/png"); // FIXME: other mime types
145 pos
= subkey
.IndexOf ('|');
147 size
= int.Parse (subkey
.Substring (0, pos
));
148 stockid
= subkey
.Substring (pos
+ 1);
154 return Images
.GetHtmlSourceForStock (stockid
,
158 return BU
.StringFu
.DateTimeToPrettyString (BU
.StringFu
.StringToDateTime ((string) values
[subkey
]).ToLocalTime ());
160 return (string) values
[subkey
];
163 return Mono
.Unix
.Catalog
.GetString (subkey
); // I18N
165 if (values
.Contains (key
))
166 return BU
.StringFu
.EscapeStringForHtml ((string)values
[subkey
], false);
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 ("");
182 string [] fragments
= line
.Split ('@');
183 for (int i
= 0; i
< fragments
.Length
; i
++) {
185 string value = GetValue (fragments
[i
]);
190 new_line
= new_line
+ value;
192 new_line
= new_line
+ fragments
[i
];
196 writer
.Write (new_line
+ "\n");
199 return writer
.ToString ();
202 public override string ToString ()
205 string_html
= Build ();