예제 #1
0
        private void AddXml(LSObject parent, XmlNode doc)
        {
            var obj = new LSObject();

            if (doc.Attributes != null)
            {
                foreach (XmlAttribute attr in doc.Attributes)
                {
                    obj.Add(attr.Name, attr.Value.Parse());
                }
            }
            foreach (XmlNode node in doc.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    AddXml(obj, node);
                }
            }
            obj.Name = doc.Name;
            if (obj.Count == 0)
            {
                obj.Value = doc.InnerText.Trim().Parse();
            }
            parent.Add(obj);
        }
예제 #2
0
        public void WriteObject(object obj, Stream stream)
        {
            var root = new LSObject();

            root.Value = obj;
            Write(root, stream);
        }
예제 #3
0
        public LSObject ReadXml(XmlDocument doc)
        {
            var obj = new LSObject();

            AddXml(obj, doc.DocumentElement);
            obj = obj[0];
            obj = obj.Clone() as LSObject;
            return(obj);
        }
예제 #4
0
        public LSObject ReadProperties(string properties)
        {
            var obj   = new LSObject();
            var props = properties.Split("\r\n");

            foreach (var p in props)
            {
                var i  = p.IndexOf("//");//过滤注释部分
                var tp = i > 0 ? p.Substring(0, i) : p;
                if (tp.Length > 0)
                {
                    var entry = tp.Split("=");
                    var key   = entry[0];
                    var value = entry[1];
                    obj.Add(key, value.Parse());
                }
            }
            return(obj);
        }
예제 #5
0
 public void Write(LSObject root, Stream stream)
 {
     using (var fw = new StreamWriter(stream))
         fw.Write(root);
 }
예제 #6
0
 public void Write(LSObject root, string file)
 {
     using (var fs = new FileStream(file, FileMode.Create))
         Write(root, fs);
 }
예제 #7
0
        private LSObject ParseObject()
        {
            Consume();
            var obj  = new LSObject();
            var name = new StringBuilder();

            while (true)
            {
                switch (LookAhead())
                {
                case LSToken.Comma:
                    Consume();
                    //分隔符,清除name
                    name.Clear();
                    break;

                case LSToken.RBracket:    //]} 对象终止
                case LSToken.RBrace:
                    Consume();
                    //处理未能识别的表达式
                    foreach (var t in dictionary.Keys.ToArray())
                    {
                        var t2 = obj.Find(dictionary[t]);
                        if (t2 != null)
                        {
                            if (t.ValueType == LSType.Object)
                            {
                                t2.CopyTo(t);
                            }
                            else if (t.ValueType == LSType.String && t2.ValueType != LSType.Object)
                            {
                                var s = t.Value as string;
                                t.Value = s.Replace("${" + dictionary[t] + "}", t2.Value?.ToString());
                            }
                            dictionary.Remove(t);
                        }
                    }
                    return(obj);

                case LSToken.Colon:     //:
                    Consume();
                    if (LookAhead() != LSToken.Expression)
                    {
                        var value = ParseValue();
                        var t     = obj.Add(name.ToString(), value);
                        if (value is string s)
                        {
                            var match = Regex.Match(s, @"\$\{.*\}");
                            var exp   = match.Value;
                            if (exp != string.Empty && (match.Index < 1 || s[match.Index - 1] != '\\'))
                            {
                                dictionary.Add(t, exp.Substring(2, exp.Length - 3));
                            }
                        }
                        name.Clear();
                    }
                    break;

                case LSToken.Dot:
                    Consume();
                    //refrence
                    name.Append(".");
                    break;

                case LSToken.Expression:
                    var key     = ParseExpression();
                    var current = obj.Find(key);
                    var tp      = obj.Add(name.ToString(), current);
                    if (current == null)
                    {
                        dictionary.Add(tp, key);
                    }
                    current = null;
                    name.Clear();
                    break;

                case LSToken.Identifier:
                    name.Append(ParseIdentifier());
                    break;

                case LSToken.Charcter:    // 字符/字符串
                case LSToken.String:
                    var val = ParseValue();
                    if (LookAhead() == LSToken.Colon)       //兼容字符串命名
                    {
                        name.Clear();
                        name.Append(val + "");
                    }
                    else
                    {
                        obj.Add(val);
                    }
                    break;

                case LSToken.LBrace:
                case LSToken.LBracket:
                case LSToken.True:
                case LSToken.False:
                case LSToken.Number:
                case LSToken.Null:
                    obj.Add(ParseValue());
                    break;

                default:
                    throw new Exception($"无法识别的字符!'{source[index]}'");
                }
            }
        }