/// <summary> /// This method will parse the attribute value to look for some special syntax such as /// [@requestKey] /// [%sessionKey] /// [#pageElement] /// [$recursiveValue] /// </summary> /// <param name="pageElements"></param> /// <param name="attributeValue"></param> /// <returns></returns> /// <remarks> /// You can even apply fallback's separated by comma's like: /// /// [@requestKey],[%sessionKey] /// /// </remarks> public static string parseAttribute(IDictionary pageElements, string attributeValue) { // Check for potential querystring/cookie variables // SD: not sure why we are checking for len 3 here? if (attributeValue.Length > 3 && attributeValue.StartsWith("[")) { var attributeValueSplit = (attributeValue).Split(','); attributeValueSplit = attributeValueSplit.Select(x => x.Trim()).ToArray(); // before proceeding, we don't want to process anything here unless each item starts/ends with a [ ] // this is because the attribute value could actually just be a json array like [1,2,3] which we don't want to parse // // however, the last one can be a literal, must take care of this! // so here, don't check the last one, which can be just anything if (attributeValueSplit.Take(attributeValueSplit.Length - 1).All(x => //must end with [ x.EndsWith("]") && //must start with [ and a special char (x.StartsWith("[@") || x.StartsWith("[%") || x.StartsWith("[#") || x.StartsWith("[$"))) == false) { return(attributeValue); } foreach (var attributeValueItem in attributeValueSplit) { attributeValue = attributeValueItem; var trimmedValue = attributeValue.Trim(); // Check for special variables (always in square-brackets like [name]) if (trimmedValue.StartsWith("[") && trimmedValue.EndsWith("]")) { attributeValue = trimmedValue; // find key name var keyName = attributeValue.Substring(2, attributeValue.Length - 3); var keyType = attributeValue.Substring(1, 1); switch (keyType) { case "@": attributeValue = HttpContext.Current.Request[keyName]; break; case "%": attributeValue = StateHelper.GetSessionValue <string>(keyName); if (String.IsNullOrEmpty(attributeValue)) { attributeValue = StateHelper.GetCookieValue(keyName); } break; case "#": if (pageElements == null) { pageElements = GetPageElements(); } if (pageElements[keyName] != null) { attributeValue = pageElements[keyName].ToString(); } else { attributeValue = ""; } break; case "$": if (pageElements == null) { pageElements = GetPageElements(); } if (pageElements[keyName] != null && pageElements[keyName].ToString() != string.Empty) { attributeValue = pageElements[keyName].ToString(); } else { // reset attribute value in case no value has been found on parents attributeValue = String.Empty; XmlDocument umbracoXML = presentation.UmbracoContext.Current.GetXml(); 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; } string xpath = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema ? "./data [@alias = '{0}']" : "{0}"; XmlNode currentNode = element.SelectSingleNode(string.Format(xpath, 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 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 <string>(keyName); if (String.IsNullOrEmpty(attributeValue)) { attributeValue = StateHelper.GetCookieValue(keyName); } break; case "#": if (pageElements[keyName] != null) { attributeValue = pageElements[keyName].ToString(); } else { attributeValue = ""; } break; case "$": if (pageElements[keyName] != null && pageElements[keyName].ToString() != string.Empty) { attributeValue = pageElements[keyName].ToString(); } else { // reset attribute value in case no value has been found on parents attributeValue = String.Empty; XmlDocument umbracoXML = presentation.UmbracoContext.Current.GetXml(); 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; } string xpath = UmbracoSettings.UseLegacyXmlSchema ? "./data [@alias = '{0}']" : "{0}"; XmlNode currentNode = element.SelectSingleNode(string.Format(xpath, 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); }