Beispiel #1
0
        /// <summary>
        /// Returns a <see cref="string"/> that contains the value of the specified variable.
        /// </summary>
        /// <param name="name">The name of the variable to be read.</param>
        /// <param name="defaultValue">The value to be returned if <see cref="IVariableSource.ResolveVariable"/> returns <see lang="null"/> or <see cref="String.Empty"/>.</param>
        /// <returns>
        /// A <see cref="string"/> that contains the value of the specified variable
        /// or <paramref name="defaultValue"/>, if <see cref="IVariableSource.ResolveVariable"/> returns <c>null</c>.
        /// </returns>
        public string GetString(string name, string defaultValue)
        {
            string value = null;

            if (variableSource != null && variableSource.CanResolveVariable(name))
            {
                value = variableSource.ResolveVariable(name);
            }

            if (!StringUtils.HasLength(value))
            {
                return(defaultValue);
            }

            return(value);
        }
            private string ParseAndResolveVariables(string strVal, ISet visitedPlaceholders)
            {
                if (strVal == null)
                {
                    return(null);
                }

                int startIndex = strVal.IndexOf(owner.placeholderPrefix);

                while (startIndex != -1)
                {
                    int endIndex = strVal.IndexOf(
                        owner.placeholderSuffix, startIndex + owner.placeholderPrefix.Length);
                    if (endIndex != -1)
                    {
                        int    pos         = startIndex + owner.placeholderPrefix.Length;
                        string placeholder = strVal.Substring(pos, endIndex - pos);
                        if (visitedPlaceholders.Contains(placeholder))
                        {
                            throw new ObjectDefinitionStoreException(
                                      string.Format(
                                          CultureInfo.InvariantCulture,
                                          "Circular placeholder reference '{0}' detected. ",
                                          placeholder));
                        }
                        visitedPlaceholders.Add(placeholder);

                        if (variableSource.CanResolveVariable(placeholder))
                        {
                            string resolvedValue = variableSource.ResolveVariable(placeholder);
                            resolvedValue = ParseAndResolveVariables(resolvedValue, visitedPlaceholders);

                            if (logger.IsEnabled(LogLevel.Debug))
                            {
                                logger.LogDebug(string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    "Resolving placeholder '{0}' to '{1}'.", placeholder, resolvedValue));
                            }

                            if (resolvedValue == null &&
                                startIndex == 0 &&
                                strVal.Length <= endIndex + owner.placeholderSuffix.Length)
                            {
                                return(null);
                            }
                            strVal     = strVal.Substring(0, startIndex) + resolvedValue + strVal.Substring(endIndex + owner.placeholderSuffix.Length);
                            startIndex = strVal.IndexOf(owner.placeholderPrefix, startIndex + (resolvedValue == null ? 0 : resolvedValue.Length));
                        }
                        else if (owner.ignoreUnresolvablePlaceholders)
                        {
                            // simply return the unprocessed value...
                            return(strVal);
                        }
                        else
                        {
                            throw new ObjectDefinitionStoreException(string.Format(
                                                                         CultureInfo.InvariantCulture,
                                                                         "Could not resolve placeholder '{0}'.", placeholder));
                        }
                        visitedPlaceholders.Remove(placeholder);
                    }
                    else
                    {
                        startIndex = -1;
                    }
                }
                return(strVal);
            }