private string TokenizeValue(XmlAttribute targetAttribute, XmlAttribute transformAttribute, bool fTokenizeParameter, List <Dictionary <string, string> > parameters) { this.tokenizeValueCurrentXmlAttribute = transformAttribute; string xPathToAttribute = this.GetXPathToAttribute(targetAttribute); string input = SubstituteKownValue(transformAttribute.Value, ParentAttributeRegex, "$(", key => this.EscapeDirRegexSpecialCharacter(this.GetAttributeValue(key), true)); if (fTokenizeParameter && (parameters != null)) { int startIndex = 0; StringBuilder builder = new StringBuilder(input.Length); startIndex = 0; List <Match> list = new List <Match>(); while (true) { startIndex = input.IndexOf("{%", startIndex, StringComparison.OrdinalIgnoreCase); if (startIndex > -1) { Match item = DirRegex.Match(input, startIndex); if (!item.Success) { startIndex++; } else { list.Add(item); startIndex = item.Index + item.Length; } } if (startIndex <= -1) { if (list.Count > 0) { builder.Remove(0, builder.Length); startIndex = 0; int num2 = 0; foreach (Match match2 in list) { builder.Append(input.Substring(startIndex, match2.Index - startIndex)); CaptureCollection captures = match2.Groups["attrname"].Captures; if ((captures != null) && (captures.Count > 0)) { GetValueCallback getValueDelegate = null; CaptureCollection captures2 = match2.Groups["attrval"].Captures; Dictionary <string, string> paramDictionary = new Dictionary <string, string>(4, StringComparer.OrdinalIgnoreCase) { [XPathWithIndex] = xPathToAttribute, [TokenNumber] = num2.ToString(CultureInfo.InvariantCulture) }; int num3 = 0; while (true) { if (num3 >= captures.Count) { string tokenFormat = null; if (!paramDictionary.TryGetValue(Token, out tokenFormat)) { tokenFormat = this.storageDictionary.TokenFormat; } if (!string.IsNullOrEmpty(tokenFormat)) { paramDictionary[Token] = tokenFormat; } int count = paramDictionary.Count; string[] array = new string[count]; paramDictionary.Keys.CopyTo(array, 0); int index = 0; while (true) { if (index >= count) { string str9; if (paramDictionary.TryGetValue(Token, out tokenFormat)) { builder.Append(tokenFormat); } if (paramDictionary.TryGetValue(XpathLocator, out str9) && !string.IsNullOrEmpty(str9)) { IList <string> locators = XmlArgumentUtility.SplitArguments(str9); string str10 = this.GetXPathToAttribute(targetAttribute, locators); if (!string.IsNullOrEmpty(str10)) { paramDictionary[XPathWithLocator] = str10; } } parameters.Add(paramDictionary); break; } string str6 = array[index]; string transformValue = paramDictionary[str6]; if (getValueDelegate == null) { getValueDelegate = key => paramDictionary.ContainsKey(key) ? paramDictionary[key] : null; } paramDictionary[str6] = SubstituteKownValue(transformValue, TokenFormatRegex, "#(", getValueDelegate); index++; } break; } string str3 = captures[num3].Value; string str4 = null; if ((captures2 != null) && (num3 < captures2.Count)) { str4 = this.EscapeDirRegexSpecialCharacter(captures2[num3].Value, false); } paramDictionary[str3] = str4; num3++; } } startIndex = match2.Index + match2.Length; num2++; } builder.Append(input.Substring(startIndex)); input = builder.ToString(); } break; } } } return(input); }
private string TokenizeValue(XmlAttribute targetAttribute, XmlAttribute transformAttribute, bool fTokenizeParameter, List <Dictionary <string, string> > parameters) { Debug.Assert(!fTokenizeParameter || parameters != null); tokenizeValueCurrentXmlAttribute = transformAttribute; string transformValue = transformAttribute.Value; string xpath = GetXPathToAttribute(targetAttribute); //subsitute the know value first in the transformAttribute transformValue = SubstituteKownValue(transformValue, ParentAttributeRegex, "$(", delegate(string key) { return(EscapeDirRegexSpecialCharacter(GetAttributeValue(key), true)); }); // then use the directive to parse the value. --- if TokenizeParameterize is enable if (fTokenizeParameter && parameters != null) { int position = 0; System.Text.StringBuilder strbuilder = new StringBuilder(transformValue.Length); position = 0; List <RegularExpressions.Match> matchs = new List <RegularExpressions.Match>(); do { position = transformValue.IndexOf("{%", position, StringComparison.OrdinalIgnoreCase); if (position > -1) { RegularExpressions.Match match = DirRegex.Match(transformValue, position); // Add the successful match to collection if (match.Success) { matchs.Add(match); position = match.Index + match.Length; } else { position++; } } } while (position > -1); if (matchs.Count > 0) { strbuilder.Remove(0, strbuilder.Length); position = 0; int index = 0; foreach (RegularExpressions.Match match in matchs) { strbuilder.Append(transformValue.Substring(position, match.Index - position)); RegularExpressions.CaptureCollection attrnames = match.Groups["attrname"].Captures; if (attrnames != null && attrnames.Count > 0) { RegularExpressions.CaptureCollection attrvalues = match.Groups["attrval"].Captures; Dictionary <string, string> paramDictionary = new Dictionary <string, string>(4, StringComparer.OrdinalIgnoreCase); paramDictionary[XPathWithIndex] = xpath; paramDictionary[TokenNumber] = index.ToString(System.Globalization.CultureInfo.InvariantCulture); // Get the key-value pare of the in the tranform form for (int i = 0; i < attrnames.Count; i++) { string name = attrnames[i].Value; string val = null; if (attrvalues != null && i < attrvalues.Count) { val = EscapeDirRegexSpecialCharacter(attrvalues[i].Value, false); } paramDictionary[name] = val; } //Identify the Token format string strTokenFormat = null; if (!paramDictionary.TryGetValue(Token, out strTokenFormat)) { strTokenFormat = storageDictionary.TokenFormat; } if (!string.IsNullOrEmpty(strTokenFormat)) { paramDictionary[Token] = strTokenFormat; } // Second translation of #() -- replace with the existing Parameters int count = paramDictionary.Count; string[] keys = new string[count]; paramDictionary.Keys.CopyTo(keys, 0); for (int i = 0; i < count; i++) { // if token format contain the #(),we replace with the known value such that it is unique identify // for example, intokenizeTransformXml.cs, default token format is // string.Concat("$(ReplacableToken_#(", SetTokenizedAttributes.ParameterAttribute, ")_#(", SetTokenizedAttributes.TokenNumber, "))"); // which ParameterAttribute will be translate to parameterDictionary["parameter"} and TokenNumber will be translate to parameter // parameterDictionary["TokenNumber"] string keyindex = keys[i]; string val = paramDictionary[keyindex]; string newVal = SubstituteKownValue(val, TokenFormatRegex, "#(", delegate(string key) { return(paramDictionary.ContainsKey(key) ? paramDictionary[key] : null); }); paramDictionary[keyindex] = newVal; } if (paramDictionary.TryGetValue(Token, out strTokenFormat)) { // Replace with token strbuilder.Append(strTokenFormat); } string attributeLocator; if (paramDictionary.TryGetValue(XpathLocator, out attributeLocator) && !string.IsNullOrEmpty(attributeLocator)) { IList <string> locators = XmlArgumentUtility.SplitArguments(attributeLocator); string xpathwithlocator = GetXPathToAttribute(targetAttribute, locators); if (!string.IsNullOrEmpty(xpathwithlocator)) { paramDictionary[XPathWithLocator] = xpathwithlocator; } } parameters.Add(paramDictionary); } position = match.Index + match.Length; index++; } strbuilder.Append(transformValue.Substring(position)); transformValue = strbuilder.ToString(); } } return(transformValue); }