Esempio n. 1
0
        static string GetValue(string propertyName, string[,] customTags)
        {
            // most properties start with res: in lowercase,
            // so we can save 2 string allocations here, in addition to all the jumps
            // All other prefixed properties {prefix:Key} shoulg get handled in the switch below.
            if (propertyName.StartsWith("res:", StringComparison.OrdinalIgnoreCase))
            {
                try {
                    return(Parse(ResourceService.GetString(propertyName.Substring(4)), customTags));
                } catch (ResourceNotFoundException) {
                    return(null);
                }
            }
            if (propertyName.StartsWith("DATE:", StringComparison.OrdinalIgnoreCase))
            {
                try {
                    return(DateTime.Now.ToString(propertyName.Split(':')[1]));
                } catch (Exception ex) {
                    return(ex.Message);
                }
            }
            if (propertyName.Equals("DATE", StringComparison.OrdinalIgnoreCase))
            {
                return(DateTime.Today.ToShortDateString());
            }
            if (propertyName.Equals("TIME", StringComparison.OrdinalIgnoreCase))
            {
                return(DateTime.Now.ToShortTimeString());
            }
            if (propertyName.Equals("ProductName", StringComparison.OrdinalIgnoreCase))
            {
                return(MessageService.ProductName);
            }
            if (propertyName.Equals("GUID", StringComparison.OrdinalIgnoreCase))
            {
                return(Guid.NewGuid().ToString().ToUpperInvariant());
            }

            if (customTags != null)
            {
                for (int j = 0; j < customTags.GetLength(0); ++j)
                {
                    if (propertyName.Equals(customTags[j, 0], StringComparison.OrdinalIgnoreCase))
                    {
                        return(customTags[j, 1]);
                    }
                }
            }

            if (properties.ContainsKey(propertyName))
            {
                return(properties[propertyName]);
            }

            if (stringTagProviders.ContainsKey(propertyName))
            {
                return(stringTagProviders[propertyName].Convert(propertyName));
            }

            int k = propertyName.IndexOf(':');

            if (k <= 0)
            {
                return(null);
            }
            string prefix = propertyName.Substring(0, k);

            propertyName = propertyName.Substring(k + 1);
            switch (prefix.ToUpperInvariant())
            {
            case "SDKTOOLPATH":
                return(FileUtility.GetSdkPath(propertyName));

            case "ADDINPATH":
                foreach (AddIn addIn in AddInTree.AddIns)
                {
                    if (addIn.Manifest.Identities.ContainsKey(propertyName))
                    {
                        return(System.IO.Path.GetDirectoryName(addIn.FileName));
                    }
                }
                return(null);

            case "ENV":
                return(Environment.GetEnvironmentVariable(propertyName));

            case "RES":
                try {
                    return(Parse(ResourceService.GetString(propertyName), customTags));
                } catch (ResourceNotFoundException) {
                    return(null);
                }

            case "PROPERTY":
                return(GetProperty(propertyName));

            default:
                if (propertyObjects.ContainsKey(prefix))
                {
                    return(Get(propertyObjects[prefix], propertyName));
                }
                else
                {
                    return(null);
                }
            }
        }
        /// <summary>
        /// Evaluates a property using the StringParser. Equivalent to StringParser.Parse("${" + propertyName + "}");
        /// </summary>
        public static string GetValue(string propertyName, params StringTagPair[] customTags)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException("propertyName");
            }

            if (customTags != null)
            {
                foreach (StringTagPair pair in customTags)
                {
                    if (propertyName.Equals(pair.Tag, StringComparison.OrdinalIgnoreCase))
                    {
                        return(pair.Value);
                    }
                }
            }

            int k = propertyName.IndexOf(':');

            if (k <= 0)
            {
                // it's property without prefix

                if (propertyName.Equals("DATE", StringComparison.OrdinalIgnoreCase))
                {
                    return(DateTime.Today.ToShortDateString());
                }
                if (propertyName.Equals("TIME", StringComparison.OrdinalIgnoreCase))
                {
                    return(DateTime.Now.ToShortTimeString());
                }
                if (propertyName.Equals("ProductName", StringComparison.OrdinalIgnoreCase))
                {
                    return(MessageService.ProductName);
                }
                if (propertyName.Equals("GUID", StringComparison.OrdinalIgnoreCase))
                {
                    return(Guid.NewGuid().ToString().ToUpperInvariant());
                }
                if (propertyName.Equals("USER", StringComparison.OrdinalIgnoreCase))
                {
                    return(Environment.UserName);
                }
                if (propertyName.Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    return(RevisionClass.FullVersion);
                }
                if (propertyName.Equals("CONFIGDIRECTORY", StringComparison.OrdinalIgnoreCase))
                {
                    return(PropertyService.ConfigDirectory);
                }

                foreach (IStringTagProvider provider in stringTagProviders)
                {
                    string result = provider.ProvideString(propertyName, customTags);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                return(null);
            }
            else
            {
                // it's a prefixed property


                // res: properties are quite common, so optimize by testing for them first
                // before allocaing the prefix/propertyName strings
                // All other prefixed properties {prefix:Key} shoulg get handled in the switch below.
                if (propertyName.StartsWith("res:", StringComparison.OrdinalIgnoreCase))
                {
                    try {
                        return(Parse(ResourceService.GetString(propertyName.Substring(4)), customTags));
                    } catch (ResourceNotFoundException) {
                        return(null);
                    }
                }

                string prefix = propertyName.Substring(0, k);
                propertyName = propertyName.Substring(k + 1);
                switch (prefix.ToUpperInvariant())
                {
                case "SDKTOOLPATH":
                    return(FileUtility.GetSdkPath(propertyName));

                case "ADDINPATH":
                    foreach (AddIn addIn in AddInTree.AddIns)
                    {
                        if (addIn.Manifest.Identities.ContainsKey(propertyName))
                        {
                            return(System.IO.Path.GetDirectoryName(addIn.FileName));
                        }
                    }
                    return(null);

                case "DATE":
                    try {
                        return(DateTime.Now.ToString(propertyName, CultureInfo.CurrentCulture));
                    } catch (Exception ex) {
                        return(ex.Message);
                    }

                case "ENV":
                    return(Environment.GetEnvironmentVariable(propertyName));

                case "PROPERTY":
                    return(GetProperty(propertyName));

                default:
                    IStringTagProvider provider;
                    if (prefixedStringTagProviders.TryGetValue(prefix, out provider))
                    {
                        return(provider.ProvideString(propertyName, customTags));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }