Esempio n. 1
0
        public static string XmlToJson(string inputStr, TemplatorConfig config)
        {
            var xml   = XElement.Parse(inputStr);
            var input = ParseXmlDict(xml.Elements(), config);

            return(InputToJson(input));
        }
Esempio n. 2
0
 public TemplatorParser(TemplatorConfig config)
 {
     Config = config;
     config.PrepareKeywords();
     config.Logger = config.Logger ?? new TemplatorLogger();
     Context       = new TemplatorParsingContext();
 }
Esempio n. 3
0
        public static TextHolder GetHolder(IDictionary <string, object> input, string key, TemplatorConfig config, bool creatIfNoFound = true)
        {
            TextHolder holder  = null;
            var        holders = input.GetOrDefault(config.KeyHolders) as IDictionary <string, TextHolder>;

            if (holders != null && holders.ContainsKey(key))
            {
                holder = holders[key];
            }
            return(holder ?? (creatIfNoFound ? new TextHolder(key) : null));
        }
Esempio n. 4
0
 public static IDictionary <string, object>[] GetChildCollection(IDictionary <string, object> input, string key, TemplatorConfig config)
 {
     if (input != null && input.ContainsKey(key))
     {
         var retArray = input[key] as object[];
         if (retArray != null)
         {
             var ret = retArray.Where(r => r is IDictionary <string, object>).Cast <IDictionary <string, object> >().ToArray();
             for (var i = 0; i < ret.Length; i++)
             {
                 ret[i].AddOrOverwrite(config.ReservedKeywordParent, input);
                 ret[i].AddOrOverwrite(config.ReservedKeywordIndex, i + 1);
                 ret[i].AddOrOverwrite(config.ReservedKeyword0Index, i);
             }
             return(ret);
         }
     }
     return(null);
 }
Esempio n. 5
0
        public static IDictionary <string, object> ParseXmlDict(IEnumerable <XElement> elements, TemplatorConfig config)
        {
            IDictionary <string, object> ret = new Dictionary <string, object>();

            foreach (var e in elements)
            {
                if (e.Name == config.XmlFieldElementName)
                {
                    var nameNode = e.Element(config.XmlNameNodeName);
                    if (nameNode != null)
                    {
                        var collection = e.Elements(config.XmlCollectionNodeName).ToArray();
                        if (collection.Length > 0)
                        {
                            ret.Add(nameNode.Value, collection.Select(item => ParseXmlDict(item.Elements(config.XmlFieldElementName), config)).ToArray());
                        }
                        else
                        {
                            var valueNode = e.Element(config.XmlValueNodeName);
                            if (valueNode != null)
                            {
                                ret.Add(nameNode.Value, valueNode.Value);
                            }
                        }
                    }
                }
            }
            return(ret);
        }
Esempio n. 6
0
 public static IDictionary <string, object> MergeInput(IList <IDictionary <string, object> > inputs, string fieldName, TemplatorConfig config)
 {
     if (inputs != null && inputs.Count > 0)
     {
         var ret  = inputs[0];
         var list = GetChildCollection(ret, fieldName, config).EmptyIfNull().ToList();
         for (var i = 1; i < inputs.Count; i++)
         {
             var subList = GetChildCollection(inputs[i], fieldName, config);
             if (subList != null)
             {
                 list.AddRange(subList);
             }
         }
         ret[fieldName] = list.ToArray();
         return(ret);
     }
     return(null);
 }
Esempio n. 7
0
        public static IDictionary <string, object> MergeInput(IEnumerable <string> inputs, string fieldName, TemplatorConfig config)
        {
            var ret = inputs?.Select(ParseJsonDict).ToList();

            if (ret?.Count > 0)
            {
                return(MergeInput(ret, fieldName, config));
            }
            return(null);
        }