| using System; |
| using System.Collections.Generic; |
| using System.Text; |
| using System.Globalization; |
| using Telerik.Cms.Engine; |
| using Telerik.Blogs; |
| using Telerik.Framework.Search; |
| using System.Reflection; |
| |
| namespace RecipeIndexer |
| { |
| public class RecipeIndexerInfo: IIndexerInfo |
| { |
| // Fields |
| protected IContent content; |
| private CultureInfo culture; |
| protected string[] metaFields; |
| private string mimeType; |
| private string path; |
| |
| // Methods |
| public RecipeIndexerInfo(string path, IContent content, string[] metaFields) : this(path, content, metaFields, null) |
| { |
| } |
| |
| public RecipeIndexerInfo(string path, IContent content, string[] metaFields, CultureInfo culture) |
| { |
| this.path = path; |
| this.content = content; |
| if (metaFields == null) |
| { |
| this.metaFields = new string[0]; |
| } |
| else |
| { |
| this.metaFields = metaFields; |
| } |
| this.culture = culture; |
| } |
| |
| public virtual byte[] GetData() |
| { |
| if (this.culture != null) |
| { |
| if (this.culture.Name.Length == 0) |
| { |
| PropertyInfo property = this.content.GetType().GetProperty("LangID"); |
| if (property != null) |
| { |
| property.SetValue(this.content, 0x7f, null); |
| } |
| } |
| else |
| { |
| this.content.Language = this.culture.Name; |
| } |
| } |
| string content = (string) this.content.Content; |
| if (this.metaFields.Length > 0) |
| { |
| content = content + this.GetMetaData(); |
| } |
| return this.Encoding.GetBytes(content); |
| } |
| |
| protected virtual string GetMetaData() |
| { |
| StringBuilder builder = new StringBuilder(); |
| foreach (string str in this.metaFields) |
| { |
| builder.AppendLine(); |
| builder.Append("<"); |
| builder.Append(str); |
| builder.Append(">"); |
| |
| switch (str) |
| { |
| case "Ingredients": |
| builder.Append(DecodeIngredients((string) this.content.GetMetaData(str))); |
| break; |
| case "Vegetarian": |
| if (DecodeStringBool((string) this.content.GetMetaData(str))) |
| builder.Append(str.ToLower()); |
| break; |
| case "LowFat": |
| if (DecodeStringBool((string)this.content.GetMetaData(str))) |
| builder.Append("lowfat, low fat, lo fat, lofat"); |
| break; |
| default: |
| builder.Append(this.content.GetMetaData(str)); |
| break; |
| } |
| |
| builder.Append("</"); |
| builder.Append(str); |
| builder.Append(">"); |
| } |
| return builder.ToString(); |
| } |
| |
| private bool DecodeStringBool(string mc) |
| { |
| if (String.IsNullOrEmpty(mc)) |
| return false; |
| else |
| { |
| if (mc.Equals("1")) |
| return true; |
| else |
| return false; |
| } |
| } |
| |
| private string DecodeIngredients(string ci) |
| { |
| if(String.IsNullOrEmpty(ci)) |
| return ""; |
| else |
| { |
| char[] splitter = { ';' }; |
| StringBuilder builder = new StringBuilder(); |
| string[] s = ci.Split(splitter); |
| for(int i = 0; i < (s.GetUpperBound(0) -1); i++) |
| builder.Append(System.Web.HttpContext.Current.Server.UrlDecode(s[i]) + "<br />"); |
| |
| return builder.ToString(); |
| } |
| } |
| |
| // Properties |
| public virtual Encoding Encoding |
| { |
| get |
| { |
| return Encoding.Unicode; |
| } |
| } |
| |
| public virtual string MimeType |
| { |
| get |
| { |
| return this.content.MimeType; |
| } |
| } |
| |
| public virtual string Path |
| { |
| get |
| { |
| return this.path; |
| } |
| } |
| } |
| } |
| |