using System; using System.Collections; using System.Text.RegularExpressions; using System.Web; using umbraco.BusinessLogic; using System.Xml; using System.Web.UI; namespace umbraco { /// /// Summary description for helper. /// public class helper { public static bool IsNumeric(string Number) { int result; return int.TryParse(Number, out result); } public static User GetCurrentUmbracoUser() { return umbraco.BasePages.UmbracoEnsuredPage.CurrentUser; } [Obsolete("Use umbraco.Presentation.UmbracoContext.Current.Request[key]",false)] public static string Request(string text) { string temp = string.Empty; if (HttpContext.Current.Request[text.ToLower()] != null) if (HttpContext.Current.Request[text] != string.Empty) temp = HttpContext.Current.Request[text]; return temp; } public static Hashtable ReturnAttributes(String tag) { Hashtable ht = new Hashtable(); MatchCollection m = Regex.Matches(tag, "(?\\S*)=\"(?[^\"]*)\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); // fix for issue 14862: return lowercase attributes for case insensitive matching foreach (Match attributeSet in m) ht.Add(attributeSet.Groups["attributeName"].Value.ToString().ToLower(), attributeSet.Groups["attributeValue"].Value.ToString()); return ht; } public static String FindAttribute(IDictionary attributes, String key) { return FindAttribute(null, attributes, key); } public static String FindAttribute(IDictionary pageElements, IDictionary attributes, String key) { // fix for issue 14862: lowercase for case insensitive matching key = key.ToLower(); string attributeValue = string.Empty; if (attributes[key] != null) attributeValue = attributes[key].ToString(); attributeValue = parseAttribute(pageElements, attributeValue); return attributeValue; } public static string parseAttribute(IDictionary pageElements, string attributeValue) { // Check for potential querystring/cookie variables if (attributeValue.Length > 3 && attributeValue.Substring(0, 1) == "[") { string[] attributeValueSplit = (attributeValue).Split(','); foreach (string attributeValueItem in attributeValueSplit) { attributeValue = attributeValueItem; // Check for special variables (always in square-brackets like [name]) if (attributeValueItem.Substring(0, 1) == "[" && attributeValueItem.Substring(attributeValueItem.Length - 1, 1) == "]") { // find key name string keyName = attributeValueItem.Substring(2, attributeValueItem.Length - 3); string keyType = attributeValueItem.Substring(1, 1); switch (keyType) { case "@": attributeValue = HttpContext.Current.Request[keyName]; break; case "%": attributeValue = StateHelper.GetSessionValue(keyName); break; case "#": if (pageElements[keyName] != null) attributeValue = pageElements[keyName].ToString(); else attributeValue = ""; break; case "$": if (pageElements[keyName] != null) { attributeValue = pageElements[keyName].ToString(); } else { XmlDocument umbracoXML = content.Instance.XmlContent; String[] splitpath = (String[])pageElements["splitpath"]; for (int i = 0; i < splitpath.Length - 1; i++) { XmlNode element = umbracoXML.GetElementById(splitpath[splitpath.Length - i - 1].ToString()); if (element == null) continue; XmlNode currentNode = element.SelectSingleNode(string.Format("./data [@alias = '{0}']", keyName)); if (currentNode != null && currentNode.FirstChild != null && !string.IsNullOrEmpty(currentNode.FirstChild.Value) && !string.IsNullOrEmpty(currentNode.FirstChild.Value.Trim())) { HttpContext.Current.Trace.Write("parameter.recursive", "Item loaded from " + splitpath[splitpath.Length - i - 1]); attributeValue = currentNode.FirstChild.Value; break; } } } break; } if (attributeValue != null) { attributeValue = attributeValue.Trim(); if (attributeValue != string.Empty) break; } else attributeValue = string.Empty; } } } return attributeValue; } public static string SpaceCamelCasing(string text) { string s = text; if (2 > s.Length) { return s; } var sb = new System.Text.StringBuilder(); var ca = s.ToCharArray(); ca[0] = char.ToUpper(ca[0]); sb.Append(ca[0]); for (int i = 1; i < ca.Length - 1; i++) { char c = ca[i]; if (char.IsUpper(c) && (char.IsLower(ca[i + 1]) || char.IsLower(ca[i - 1]))) { sb.Append(' '); } sb.Append(c); } sb.Append(ca[ca.Length - 1]); return sb.ToString(); /* OLD way string _tempString = text.Substring(0, 1).ToUpper(); for (int i = 1; i < text.Length; i++) { if (text.Substring(i, 1) == " ") break; if (text.Substring(i, 1).ToUpper() == text.Substring(i, 1)) _tempString += " "; _tempString += text.Substring(i, 1); } return _tempString; */ } [Obsolete("Use umbraco.presentation.UmbracContext.Current.GetBaseUrl()")] public static string GetBaseUrl(HttpContext Context) { return Context.Request.Url.GetLeftPart(UriPartial.Authority); } } /// /// Class that adapts an to the interface. /// public class AttributeCollectionAdapter : IDictionary { private AttributeCollection m_Collection; /// /// Initializes a new instance of the class. /// /// The collection. public AttributeCollectionAdapter(AttributeCollection collection) { m_Collection = collection; } #region IDictionary Members /// /// Adds an element with the provided key and value to the object. /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. public void Add(object key, object value) { m_Collection.Add(key.ToString(), value.ToString()); } /// /// Removes all elements from the object. /// /// /// The object is read-only. /// public void Clear() { m_Collection.Clear(); } /// /// Determines whether the object contains an element with the specified key. /// /// The key to locate in the object. /// /// true if the contains an element with the key; otherwise, false. /// public bool Contains(object key) { return m_Collection[key.ToString()] != null; } /// /// Returns an object for the object. /// /// /// An object for the object. /// public IDictionaryEnumerator GetEnumerator() { return new AttributeCollectionAdapterEnumerator(this); } /// /// Gets a value indicating whether the object has a fixed size. /// /// /// true if the object has a fixed size; otherwise, false. /// public bool IsFixedSize { get { return false; } } /// /// Gets a value indicating whether the object is read-only. /// /// /// true if the object is read-only; otherwise, false. /// public bool IsReadOnly { get { return false; } } /// /// Gets an object containing the keys of the object. /// /// /// /// An object containing the keys of the object. /// public ICollection Keys { get { return m_Collection.Keys; } } /// /// Removes the element with the specified key from the object. /// /// The key of the element to remove. public void Remove(object key) { m_Collection.Remove(key.ToString()); } /// /// Gets an object containing the values in the object. /// /// /// /// An object containing the values in the object. /// public ICollection Values { get { throw new NotImplementedException(); } } /// /// Gets or sets the with the specified key. /// /// public object this[object key] { get { return m_Collection[key.ToString()]; } set { m_Collection[key.ToString()] = value.ToString(); } } #endregion #region ICollection Members /// Not implemented. /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// The zero-based index in at which copying begins. public void CopyTo(Array array, int index) { throw new NotImplementedException(); } /// /// Gets the number of elements contained in the . /// /// /// /// The number of elements contained in the . /// public int Count { get { return m_Collection.Count; } } /// /// Gets a value indicating whether access to the is synchronized (thread safe). /// /// /// true if access to the is synchronized (thread safe); otherwise, false. /// public bool IsSynchronized { get { return false; } } /// /// Gets an object that can be used to synchronize access to the . /// /// /// /// An object that can be used to synchronize access to the . /// public object SyncRoot { get { return m_Collection; } } #endregion #region IEnumerable Members /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// IEnumerator IEnumerable.GetEnumerator() { foreach (object key in m_Collection.Keys) yield return m_Collection[(string)key]; } #endregion /// /// for the class. /// private class AttributeCollectionAdapterEnumerator : IDictionaryEnumerator { private AttributeCollectionAdapter m_Adapter; private IEnumerator m_Enumerator; /// /// Initializes a new instance of the class. /// /// The adapter. public AttributeCollectionAdapterEnumerator(AttributeCollectionAdapter adapter) { m_Adapter = adapter; m_Enumerator = ((IEnumerable)adapter).GetEnumerator(); } #region IDictionaryEnumerator Members /// /// Gets both the key and the value of the current dictionary entry. /// /// /// /// A containing both the key and the value of the current dictionary entry. /// /// /// The is positioned before the first entry of the dictionary or after the last entry. /// public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } } /// /// Gets the key of the current dictionary entry. /// /// /// /// The key of the current element of the enumeration. /// /// /// The is positioned before the first entry of the dictionary or after the last entry. /// public object Key { get { return m_Enumerator.Current; } } /// /// Gets the value of the current dictionary entry. /// /// /// /// The value of the current element of the enumeration. /// /// /// The is positioned before the first entry of the dictionary or after the last entry. /// public object Value { get { return m_Adapter[m_Enumerator.Current]; } } #endregion #region IEnumerator Members /// /// Gets the current element in the collection. /// /// /// /// The current element in the collection. /// /// /// The enumerator is positioned before the first element of the collection or after the last element. /// public object Current { get { return m_Enumerator.Current; } } /// /// Advances the enumerator to the next element of the collection. /// /// /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. /// /// /// The collection was modified after the enumerator was created. /// public bool MoveNext() { return m_Enumerator.MoveNext(); } /// /// Sets the enumerator to its initial position, which is before the first element in the collection. /// /// /// The collection was modified after the enumerator was created. /// public void Reset() { m_Enumerator.Reset(); } #endregion } } }