Ejemplo n.º 1
0
        /// <summary>  Get a string configuration value. System properties will override values specified
        /// in the configuration file.
        /// </summary>
        /// <param name="sName">The name of the variable to retrieve.
        /// </param>
        /// <param name="sDefault">The default value to return if the given variable was not found.
        /// </param>
        /// <returns> The value associated with the given name, or <tt>sDefault</tt> if the
        /// value was not found.
        /// </returns>
        public virtual String GetS(String sName, String sDefault)
        {
            String sValue;

            // System properties override configuration file properties
            sValue = Environment.GetEnvironmentVariable(sName);
            if (sValue != null)
            {
                return(sValue);
            }

            // Return the configuration file property if it exists

            Object o = null;

            try
            {
                o = m_prop[sName];
            }
            catch { }
            if (o != null)
            {
                sValue = (String)o;
            }
            else if (sDefault != null)
            {
                sValue = sDefault;
            }
            while (StringUtility.NotEmpty(sValue) && sValue.IndexOf("${") >= 0)
            {
                int    firstIndex = sValue.IndexOf("${");
                int    lastIndex  = sValue.IndexOf("}");
                String valStart   = sValue.Substring(0, firstIndex);
                if (valStart == null)
                {
                    valStart = string.Empty;
                }
                String valEnd = sValue.Substring(lastIndex + 1);
                if (valEnd == null)
                {
                    valEnd = string.Empty;
                }
                String val = GetS(sValue.Substring(firstIndex + 2, (lastIndex - firstIndex - 2)));
                sValue = valStart + val + valEnd;
            }

            return(sValue);
        }
Ejemplo n.º 2
0
        /// <summary>  Parses a list of string tokens from a variable in the conf file
        /// and returns them in a ArrayList.
        /// </summary>
        /// <param name="sVarName">    The name of the conf file variable whose value
        /// contains the tokens.
        /// </param>
        /// <param name="sDelimeters"> The delimeter characters that will be passed
        /// into the <see cref="StringTokenizer"/>  constructor.
        /// Pass null to use the default whitespace characters
        /// (see the <see cref="StringTokenizer"/> documentation for
        /// the complete list).
        /// </param>
        /// <param name="bLowercase">  Pass true to lowercase all tokens before storing
        /// in the set, false to leave case as-is.
        /// </param>
        /// <returns> A list containing the tokens found, or null if
        /// no tokens were found.
        ///
        /// </returns>
        public virtual ArrayList GetTokens(String sVarName, String sDelimeters, bool bLowercase)
        {
            ArrayList hsTokens = null;
            String    sTokens  = GetS(sVarName);

            if (StringUtility.NotEmpty(sTokens))
            {
                // Build the string tokenizer
                StringTokenizer tokens;
                if (StringUtility.NotEmpty(sDelimeters))
                {
                    tokens = new StringTokenizer(sTokens, sDelimeters);
                }
                else
                {
                    tokens = new StringTokenizer(sTokens);
                }

                // Add the tokens to the set
                String sToken;
                while (tokens.HasMoreTokens())
                {
                    sToken = tokens.NextToken();

                    if (hsTokens == null)
                    {
                        hsTokens = new ArrayList();
                    }

                    if (bLowercase)
                    {
                        sToken = sToken.ToLower();
                    }

                    hsTokens.Add(sToken);
                }
            }

            return(hsTokens);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// resolves a value from a set of objects base on a reference that contains the key to the object in the dictionary
        /// and optionally a dot notation reference to properties of the object.
        /// </summary>
        /// <param name="indexedRefStart">an indexed reference start string such as [ in some.reference[index]</param>
        /// <param name="indexedRefEnd">an indexed reference start string such as ] in some.reference[index]</param>
        /// <param name="objects"></param>
        /// <param name="refName"></param>
        /// <returns></returns>
        public static object GetObjectFromReference(
            string indexedRefStart,
            string indexedRefEnd,
            Dictionary <string, object> objects,
            string refName
            )
        {
            object refValue                  = null;
            int    firstIndexOfDot           = refName.IndexOf(".");
            int    firstIndexOfSquareBracket = refName.IndexOf(indexedRefStart);

            if (StringUtility.Contains(refName, ".") &&
                (firstIndexOfSquareBracket < 0 || firstIndexOfDot < firstIndexOfSquareBracket))
            {
                StringTokenizer tokenizer = new StringTokenizer(refName, ".");
                string          envName   = tokenizer.NextToken();
                object          obj       = null;
                if (StringUtility.NotEmpty(envName))
                {
                    obj = objects[envName];
                    if (obj != null)
                    {
                        while (tokenizer.HasMoreTokens())
                        {
                            if (obj == null)
                            {
                                break;
                            }
                            string token    = tokenizer.NextToken();
                            string propName = token;
                            if (StringUtility.NotEmpty(token))
                            {
                                bool hasIndex = false;
                                if (StringUtility.Contains(token, indexedRefStart))
                                {
                                    hasIndex = true;
                                    propName = token.Substring(0, token.IndexOf(indexedRefStart));
                                }
                                PropertyInfo prop = obj.GetType().GetProperty(propName);
                                if (prop != null && prop.CanRead)
                                {
                                    if (typeof(IDictionary).IsAssignableFrom(prop.PropertyType))
                                    {
                                        IDictionary dictObj = (IDictionary)prop.GetValue(obj, null);
                                        if (hasIndex)
                                        {
                                            obj = dictObj[StringUtility.ExtractFromDelimiters(token, indexedRefStart, indexedRefEnd)];
                                        }
                                        else
                                        {
                                            obj = dictObj;
                                        }
                                    }
                                    else if (typeof(IList).IsAssignableFrom(prop.PropertyType))
                                    {
                                        IList listObj = (IList)prop.GetValue(obj, null);
                                        if (hasIndex)
                                        {
                                            try
                                            {
                                                int indx = Convert.ToInt32(StringUtility.ExtractFromDelimiters(token, indexedRefStart, indexedRefEnd));
                                                obj = listObj[indx];
                                            }
                                            catch
                                            {
                                                obj = listObj;
                                            }
                                        }
                                        else
                                        {
                                            obj = listObj;
                                        }
                                    }
                                    else
                                    {
                                        obj = prop.GetValue(obj, null);
                                    }
                                }
                                else
                                {
                                    obj = null;
                                    break;
                                }
                            }
                        }
                    }
                }
                refValue = obj;
            }
            else
            {
                bool   hasIndex = false;
                string propName = refName;
                if (StringUtility.Contains(refName, indexedRefStart))
                {
                    hasIndex = true;
                    propName = refName.Substring(0, refName.IndexOf(indexedRefStart));
                }
                object obj = objects[propName];
                if (obj == null)
                {
                    refValue = null;
                }
                else
                {
                    if (hasIndex)
                    {
                        if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
                        {
                            IDictionary dictObj = (IDictionary)obj;
                            obj = dictObj[StringUtility.ExtractFromDelimiters(refName, indexedRefStart, indexedRefEnd)];
                        }
                        else if (typeof(IList).IsAssignableFrom(obj.GetType()))
                        {
                            IList listObj = (IList)obj;
                            int   indx    = Convert.ToInt32(StringUtility.ExtractFromDelimiters(refName, indexedRefStart, indexedRefEnd));
                            obj = listObj[indx];
                        }
                    }
                    refValue = obj;
                }
            }
            return(refValue);
        }