/// <summary> /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>. /// </summary> /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param> /// <returns> /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false. /// </returns> public override bool Equals(Object obj) { if (!(obj is ParameterFragment)) { return(false); } ParameterFragment other = (ParameterFragment)obj; return(other.Value.Equals(this.Value)); }
/// <summary> Parses a string to find placeholders of format ${placeholder}. /// <para> /// Example: "My ${thing} is ${color}" /// </para> /// <para> /// The example above parses into 4 fragements: a text fragment of value "My ", /// a parameter fragment "thing", a text fragement " is " and a parameter /// fragment "color". /// </para> /// </summary> /// <param name="parseString">is the string to parse /// </param> /// <returns> list of fragements that can be either text fragments or placeholder fragments /// </returns> /// <throws> PlaceholderParseException if the string cannot be parsed to indicate syntax errors </throws> public static IList <Fragment> ParsePlaceholder(String parseString) { List <Fragment> result = new List <Fragment>(); int currOutputIndex = 0; int currSearchIndex = 0; while (true) { if (currSearchIndex == parseString.Length) { break; } int startIndex = parseString.IndexOf("${", currSearchIndex); if (startIndex == -1) { // no more parameters, add any remainder of string if (currOutputIndex < parseString.Length) { String endString = parseString.Substring(currOutputIndex); TextFragment textFragment = new TextFragment(endString); result.Add(textFragment); } break; } // add text so far if (startIndex > 0) { String textSoFar = parseString.Substring(currOutputIndex, startIndex - currOutputIndex); if (textSoFar.Length != 0) { result.Add(new TextFragment(textSoFar)); } } // check if the parameter is escaped if ((startIndex > 0) && (parseString[startIndex - 1] == '$')) { currOutputIndex = startIndex + 1; currSearchIndex = startIndex + 1; continue; } int endIndex = parseString.IndexOf("}", startIndex); if (endIndex == -1) { throw new PlaceholderParseException("Syntax error in property or variable: '" + parseString.Substring(startIndex) + "'"); } // add placeholder String between = parseString.Substring(startIndex + 2, endIndex - startIndex - 2); ParameterFragment parameterFragment = new ParameterFragment(between); result.Add(parameterFragment); currOutputIndex = endIndex + 1; currSearchIndex = endIndex; } // Combine adjacent text fragements var fragments = new LinkedList <Fragment>(); fragments.AddLast(result[0]); for (int i = 1; i < result.Count; i++) { Fragment fragment = result[i]; if (!(result[i] is TextFragment)) { fragments.AddLast(fragment); continue; } if (!(fragments.Last.Value is TextFragment)) { fragments.AddLast(fragment); continue; } TextFragment textFragment = (TextFragment)fragments.Last.Value; fragments.RemoveLast(); fragments.AddLast(new TextFragment(textFragment.Value + fragment.Value)); } return(new List <Fragment>(fragments)); }