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
["ScoreRaw"] = hit
.ScoreRaw
.ToString ();
91 values
["ScoreMultiplier"] = hit
.ScoreMultiplier
.ToString ();
92 values
["Timestamp"] = BU
.StringFu
.DateTimeToString (hit
.Timestamp
);
93 values
["Path"] = hit
.Path
;
94 values
["FileName"] = hit
.FileName
;
95 values
["DirectoryName"] = hit
.DirectoryName
;
96 if (hit
.FileInfo
!= null)
97 values
["FolderName"] = hit
.FileInfo
.Directory
.Name
;
98 else if (hit
.DirectoryInfo
!= null)
99 values
["FolderName"] = hit
.DirectoryInfo
.Parent
.Name
;
100 AddProperties (hit
.Properties
);
103 public IDictionary Values
{
104 get { return values; }
107 public string this [string key
] {
108 get { return (string) values[key]; }
111 if (values
.Contains (key
))
115 values
[key
] = value as string;
120 private string GetValue (string key
)
126 pos
= key
.IndexOf ('%');
129 formatter
= key
.Substring (0, pos
);
130 subkey
= key
.Substring (pos
+ 1);
138 if (values
.Contains (subkey
))
139 return (string)values
[subkey
];
143 return Images
.GetHtmlSource (subkey
, "image/png"); // FIXME: other mime types
147 pos
= subkey
.IndexOf ('|');
149 size
= int.Parse (subkey
.Substring (0, pos
));
150 stockid
= subkey
.Substring (pos
+ 1);
156 return Images
.GetHtmlSourceForStock (stockid
,
160 return BU
.StringFu
.DateTimeToPrettyString (BU
.StringFu
.StringToDateTime ((string)values
[subkey
]));
162 return (string)values
[subkey
];
165 return Mono
.Posix
.Catalog
.GetString (subkey
); // I18N
167 if (values
.Contains (key
))
168 return BU
.StringFu
.EscapeStringForHtml ((string)values
[subkey
], false);
174 private string Build ()
176 StringWriter writer
= new StringWriter ();
177 foreach (string line
in lines
) {
178 string new_line
= "";
179 if (line
.Length
< 1) {
180 writer
.WriteLine ("");
184 string [] fragments
= line
.Split ('@');
185 for (int i
= 0; i
< fragments
.Length
; i
++) {
187 string value = GetValue (fragments
[i
]);
192 new_line
= new_line
+ value;
194 new_line
= new_line
+ fragments
[i
];
198 writer
.Write (new_line
+ "\n");
201 return writer
.ToString ();
204 public override string ToString ()
207 string_html
= Build ();