/* * This work is licensed under the Creative Commons Attribution 2.5 License. * To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, * San Francisco, California, 94105, USA. * * Original developer: David Betz * */ using System; using System.Collections.Generic; using System.Text; namespace FeedBuilder { internal static class TemplateParser { static internal String ParseLink(String template, Int32 id, String title, String description) { StringBuilder s = new StringBuilder(); Int32 previous = 0; Int32 start = -1; Int32 end = -1; for (Int32 i = 0; i < template.Length; i++) { if (template[i] == '{') { start = i; } if (start > -1 && template[i] == '}') { s.Append(template.Substring(previous, start - previous)); end = i; String templateCode = template.Substring(start + 1, end - start - 1); String value = MatchTemplateToken(template.Substring(start + 1, end - start - 1), id, title, description); if (!String.IsNullOrEmpty(value)) { s.Append(value); } previous = end + 1; start = -1; end = -1; } } return s.ToString(); } static private String MatchTemplateToken(String token, Int32 id, String title, String description) { String lowerToken = token.ToLower(); String lowerId = id.ToString().ToLower(); String lowerTitle = title.ToLower(); String lowerDescription = description.ToLower(); if (lowerToken == "id") { return lowerId; } else if (lowerToken == "title") { return title; } else if (lowerToken == "description") { return description; } return String.Empty; } } }