Beispiel #1
0
        private static Dictionary <string, string> LoadSettings(string filePath)
        {
            if (!File.Exists(filePath))
            {
                //throw new FileNotFoundException("Can't find the AppSettings config file: " + filePath + "!");
                return(new Dictionary <string, string>(0));
            }
            Dictionary <string, string> rst = new Dictionary <string, string>();
            XElement doc = XElement.Load(filePath);

            foreach (var x in doc.Descendants("add"))
            {
                if (x.Attribute("key") == null || x.Attribute("key").Value == null || x.Attribute("key").Value.Trim().Length <= 0)
                {
                    throw new ApplicationException("There are some 'add' node without attribute 'key' in the AppSettings config file: " + filePath + ", please check it!");
                }
                string key = x.Attribute("key").Value.Trim().ToUpper();
                if (rst.ContainsKey(key))
                {
                    throw new ApplicationException("Duplicated value '" + x.Attribute("key").Value.Trim() + "' of attribute 'key' in 'add' node in the AppSettings config file: " + filePath + ", please check it (ex: ignore case)!");
                }
                string value = x.Attribute("value") == null ? null : (x.Attribute("value").Value == null ? null : x.Attribute("value").Value.Trim());
                value = value ?? x.Value;
                rst.Add(key, EnvironmentVariableManager.ReplaceVariable(value));
            }
            return(rst);
        }
Beispiel #2
0
        private static Dictionary <string, string> LoadTextResource(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(new Dictionary <string, string>(0));
                //throw new FileNotFoundException("Can't find the CodeNamePair config file: " + getFullConfigPath + "!");
            }
            Dictionary <string, string> rst = new Dictionary <string, string>();
            XElement doc = XElement.Load(filePath);

            foreach (var x in doc.Descendants("Message"))
            {
                if (x.Attribute("name") == null || x.Attribute("name").Value == null || x.Attribute("name").Value.Trim().Length <= 0)
                {
                    throw new ApplicationException("There are some 'Message' node without attribute 'name' in the TextResource config file: " + filePath + ", please check it!");
                }
                string name = x.Attribute("name").Value.Trim().ToUpper();
                if (rst.ContainsKey(name))
                {
                    throw new ApplicationException("Duplicated value '" + x.Attribute("name").Value.Trim() + "' of attribute 'name' in 'Message' node in the TextResource config file: " + filePath + ", please check it (ex: ignore case)!");
                }
                rst.Add(name, EnvironmentVariableManager.ReplaceVariable(x.Value));
            }
            return(rst);
        }
Beispiel #3
0
        public static string ReplaceVariable(string input)
        {
            if (string.IsNullOrWhiteSpace(input) || input.IndexOf(SPLITTER) < 0)
            {
                return(input);
            }
            MatchCollection mc = s_Regex.Matches(input);

            return(s_Regex.Replace(input, delegate(Match m)
            {
                if (m.Groups.Count > 1 && !string.IsNullOrWhiteSpace(m.Groups[1].Value))
                {
                    string key = m.Groups[1].Value.Trim();
                    if (EnvironmentVariableManager.IsVariableDefined(key))
                    {
                        return EnvironmentVariableManager.GetVariableValue(key);
                    }
                }
                return m.Value;
            }));
        }
Beispiel #4
0
        private static Dictionary <string, List <CodeNamePair> > LoadConfigFile(string getFullConfigPath, string languageCode)
        {
            if (!File.Exists(getFullConfigPath))
            {
                return(new Dictionary <string, List <CodeNamePair> >(0));
                //throw new FileNotFoundException("Can't find the CodeNamePair config file: " + getFullConfigPath + "!");
            }

            Dictionary <string, List <CodeNamePair> > rst = new Dictionary <string, List <CodeNamePair> >();
            XElement doc = XElement.Load(getFullConfigPath);

            foreach (var x in doc.Descendants("codeNamePair"))
            {
                if (x.Attribute("key") == null || x.Attribute("key").Value == null || x.Attribute("key").Value.Trim().Length <= 0)
                {
                    throw new ApplicationException("There are some 'codeNamePair' node without attribute 'key' in the CodeNamePair config file: " + getFullConfigPath + ", please check it!");
                }
                string key = x.Attribute("key").Value.Trim().ToUpper();
                if (rst.ContainsKey(key))
                {
                    throw new ApplicationException("Duplicated value '" + x.Attribute("key").Value.Trim() + "' of attribute 'key' in 'codeNamePair' node in the CodeNamePair config file: " + getFullConfigPath + ", please check it (ex: ignore case)!");
                }
                List <CodeNamePair> v = new List <CodeNamePair>();
                foreach (var listItem in x.Descendants("item"))
                {
                    if (listItem.Attribute("code") != null && listItem.Attribute("code").Value != null && listItem.Attribute("code").Value.Trim().Length > 0)
                    {
                        string itemKey  = listItem.Attribute("code").Value.Trim();
                        string itemText = listItem.Attribute("name") == null ? null : (listItem.Attribute("name").Value == null ? null : listItem.Attribute("name").Value.Trim());
                        v.Add(new CodeNamePair {
                            Code = itemKey, Name = EnvironmentVariableManager.ReplaceVariable(itemText)
                        });
                    }
                }
                rst.Add(key, v);
            }
            return(rst);
        }