Beispiel #1
0
        public void Load(Stream strm)
        {
            lock (_defines) {
                Nodes.Clear();
                if (Attributes != null)
                {
                    Attributes.Clear();
                }
                _cache.Clear();

                using (XmlTextReader reader = new XmlTextReader(strm)) {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement() && reader.Name == "config")
                        {
                            Load(reader.ReadSubtree());
                        }
                    }
                }

                object[] invokeParams = new object[4];
                foreach (KeyValuePair <string, IDefineValue> entry in _defines)
                {
                    XmlConfigNode node = Lookup(entry.Key, true);
                    Type          tmp  = typeof(XmlConfigNode <>);
                    tmp             = tmp.MakeGenericType(entry.Value.GenericsType);
                    invokeParams[0] = node;
                    invokeParams[1] = entry.Value.ConstructorParams[0];
                    invokeParams[2] = entry.Value.ConstructorParams[1];
                    invokeParams[3] = entry.Value.ConstructorParams[2];
                    XmlConfigNode newNode = (XmlConfigNode)tmp.GetConstructors()[0].Invoke(invokeParams);
                    _cache[entry.Key] = newNode;
                }
            }
        }
Beispiel #2
0
        public bool TryParse(XmlConfigNode node, out T[] value)
        {
            if (node.NodeValue == null)
            {
                value = null;
                return(false);
            }

            List <T> list = new List <T> ();
            T        tmp;

            for (int i = 0; i < node.Nodes.Count; i++)
            {
                XmlConfigNode item = node.Nodes[i];
                if (item.Name != _itemElementName)
                {
                    continue;
                }
                if (_elementParser.TryParse(item, out tmp))
                {
                    list.Add(tmp);
                }
            }
            value = list.ToArray();
            return(true);
        }
Beispiel #3
0
 public bool TryParse(XmlConfigNode node, out int value)
 {
     if (node.NodeValue == null || !int.TryParse(node.NodeValue, out value))
     {
         value = 0;
         return(false);
     }
     return(true);
 }
Beispiel #4
0
 public bool TryParse(XmlConfigNode node, out TimeSpan value)
 {
     if (node.NodeValue == null)
     {
         value = TimeSpan.MinValue;
         return(false);
     }
     return(TimeSpan.TryParse(node.NodeValue, out value));
 }
Beispiel #5
0
 public bool TryParse(XmlConfigNode node, out bool value)
 {
     value = true;
     if (node.NodeValue == null || !bool.TryParse(node.NodeValue, out value))
     {
         return(false);
     }
     return(true);
 }
Beispiel #6
0
 public bool TryParse(XmlConfigNode node, out DateTime value)
 {
     value = DateTime.MinValue;
     if (node.NodeValue == null)
     {
         return(false);
     }
     return(DateTime.TryParse(node.NodeValue, out value));
 }
Beispiel #7
0
        public void Define <T> (string id, IParser <T> parser, IValidator <T> validator, T defaultValue)
        {
            XmlConfigNode     node    = Lookup(id, true);
            XmlConfigNode <T> newNode = new XmlConfigNode <T> (node, parser, validator, defaultValue);

            lock (_defines) {
                _cache[id] = newNode;
                _defines.Add(id, new DefineValue <T> (parser, validator, defaultValue));
            }
        }
Beispiel #8
0
        public bool TryParse(XmlConfigNode node, out string value)
        {
            if (node.NodeValue == null)
            {
                value = string.Empty;
                return(false);
            }

            value = node.NodeValue;
            return(true);
        }
Beispiel #9
0
        public T GetValue <T> (string id)
        {
            XmlConfigNode node;

            if (_cache.TryGetValue(id, out node))
            {
                XmlConfigNode <T> node2 = (XmlConfigNode <T>)node;
                return(node2.Value);
            }
            throw new KeyNotFoundException();
        }
Beispiel #10
0
        public void SetValue <T> (string id, T value, bool raiseException)
        {
            XmlConfigNode node;

            if (_cache.TryGetValue(id, out node))
            {
                XmlConfigNode <T> node2 = (XmlConfigNode <T>)node;
                node2.SetValue(value, raiseException);
                return;
            }
            throw new KeyNotFoundException();
        }
Beispiel #11
0
		public bool TryParse (XmlConfigNode node, out T value)
		{
			if (node.NodeValue == null) {
				value = default (T);
				return false;
			}
			try {
				value = (T)Enum.Parse (_type, node.NodeValue);
				return true;
			} catch {
				value = default (T);
				return false;
			}
		}
Beispiel #12
0
 public bool TryParse(XmlConfigNode node, out Guid value)
 {
     value = Guid.Empty;
     if (node.NodeValue == null)
     {
         return(false);
     }
     try {
         value = new Guid(node.NodeValue);
         return(true);
     } catch {
         return(false);
     }
 }
Beispiel #13
0
 public XmlConfigNode(XmlConfigNode parent, string name)
 {
     _parent = parent;
     _name = name;
     if (parent == null) {
         _fullpath = string.Empty;
         _doc = (this as XmlConfig);
     } else {
         _doc = parent._doc;
         parent._nodes.Add (this);
         if (_parent._fullpath.Length > 0)
             _fullpath = _parent._fullpath + PathSeparatorChar + name;
         else
             _fullpath = name;
     }
 }
Beispiel #14
0
        public XmlConfigNode Lookup(string path, bool createIfNotFound)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }
            if (path.Length == 0)
            {
                throw new ArgumentException();
            }
            if (path[0] == PathSeparatorChar)
            {
                return(_doc.Lookup(path.Substring(1), createIfNotFound));
            }

            string[]      names = path.Split(PathSeparatorChar);
            XmlConfigNode node  = this;

            for (int i = 0; i < names.Length; i++)
            {
                bool found = false;
                for (int q = 0; q < node.Nodes.Count; q++)
                {
                    if (node.Nodes[q].Name == names[i])
                    {
                        node  = node.Nodes[q];
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    if (createIfNotFound)
                    {
                        node = new XmlConfigNode(node, names[i]);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            return(node);
        }
Beispiel #15
0
        public bool TryParse(XmlConfigNode node, out Font value)
        {
            string       family, strSize, strStyle, strUnit;
            float        size;
            FontStyle    style;
            GraphicsUnit unit;

            value = null;
            if (node.Attributes == null ||
                !node.Attributes.TryGetValue("fontname", out family) ||
                !node.Attributes.TryGetValue("size", out strSize))
            {
                return(false);
            }

            if (!node.Attributes.TryGetValue("style", out strStyle))
            {
                strStyle = FontStyle.Regular.ToString();
            }
            if (!node.Attributes.TryGetValue("unit", out strUnit))
            {
                strUnit = GraphicsUnit.Point.ToString();
            }
            if (!float.TryParse(strSize, out size))
            {
                return(false);
            }
            try {
                style = (FontStyle)Enum.Parse(typeof(FontStyle), strStyle);
            } catch {
                return(false);
            }
            try {
                unit = (GraphicsUnit)Enum.Parse(typeof(GraphicsUnit), strUnit);
            } catch {
                return(false);
            }
            try {
                value = new Font(family, size, style, unit);
                return(true);
            } catch {
                return(false);
            }
        }
Beispiel #16
0
        public bool TryParse(XmlConfigNode node, out Point value)
        {
            value = Point.Empty;
            string sx, sy;

            if (node.Attributes == null ||
                !node.Attributes.TryGetValue("x", out sx) ||
                !node.Attributes.TryGetValue("y", out sy))
            {
                return(false);
            }
            int x, y;

            if (int.TryParse(sx, out x) && int.TryParse(sy, out y))
            {
                value = new Point(x, y);
                return(true);
            }
            return(false);
        }
        public XmlConfigNode(XmlConfigNode node, IParser <T> parser, IValidator <T> validator, T defaultValue) : base(node.ParentNode, node.Name)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("parser");
            }
            _parser       = parser;
            _validator    = validator;
            _defaultValue = defaultValue;

            if (!parser.TryParse(node, out _value) || (validator != null && !validator.Validate(_value)))
            {
                _value = defaultValue;
            }
            ClearRawData();
            if (node.ParentNode != null)
            {
                node.ParentNode.Nodes.Remove(node);
            }
        }
Beispiel #18
0
        public bool TryParse(XmlConfigNode node, out Size value)
        {
            value = Size.Empty;
            string sw, sh;

            if (node.Attributes == null ||
                !node.Attributes.TryGetValue("width", out sw) ||
                !node.Attributes.TryGetValue("height", out sh))
            {
                return(false);
            }
            int width, height;

            if (int.TryParse(sw, out width) && int.TryParse(sh, out height))
            {
                value = new Size(width, height);
                return(true);
            }
            return(false);
        }
Beispiel #19
0
 public bool TryParse(XmlConfigNode node, out byte[] value)
 {
     if (node.NodeValue == null)
     {
         value = null;
         return(false);
     }
     if (node.NodeValue.Length == 0)
     {
         value = new byte[0];
         return(true);
     }
     try {
         value = Convert.FromBase64String(node.NodeValue);
         return(true);
     } catch {
         value = null;
         return(false);
     }
 }
Beispiel #20
0
        protected void Load(XmlReader reader)
        {
            reader.Read();
            if (reader.HasAttributes)
            {
                _atts = new Dictionary <string, string> ();
                while (reader.MoveToNextAttribute())
                {
                    _atts[reader.Name] = reader.Value;
                }
            }

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Text:
                    if (_nodeValue == null)
                    {
                        _nodeValue = reader.Value;
                    }
                    else
                    {
                        _nodeValue += reader.Value;
                    }
                    break;

                case XmlNodeType.Element:
                    XmlConfigNode node = new XmlConfigNode(this, reader.Name);
                    node.Load(reader.ReadSubtree());
                    break;
                }
            }
            if (_nodeValue == null)
            {
                _nodeValue = string.Empty;
            }
        }
Beispiel #21
0
 public XmlConfigNode(XmlConfigNode parent, string name)
 {
     _parent = parent;
     _name   = name;
     if (parent == null)
     {
         _fullpath = string.Empty;
         _doc      = (this as XmlConfig);
     }
     else
     {
         _doc = parent._doc;
         parent._nodes.Add(this);
         if (_parent._fullpath.Length > 0)
         {
             _fullpath = _parent._fullpath + PathSeparatorChar + name;
         }
         else
         {
             _fullpath = name;
         }
     }
 }
Beispiel #22
0
        public bool TryParse(XmlConfigNode node, out Rectangle value)
        {
            value = Rectangle.Empty;
            XmlConfigNode locNode  = node.Lookup("location", false);
            XmlConfigNode sizeNode = node.Lookup("size", false);

            if (locNode == null || sizeNode == null)
            {
                return(false);
            }
            Size  size;
            Point location;

            if (!SizeParser.Instance.TryParse(sizeNode, out size))
            {
                return(false);
            }
            if (!PointParser.Instance.TryParse(locNode, out location))
            {
                return(false);
            }
            value = new Rectangle(location, size);
            return(true);
        }
Beispiel #23
0
        protected void Load(XmlReader reader)
        {
            reader.Read ();
            if (reader.HasAttributes) {
                _atts = new Dictionary<string, string> ();
                while (reader.MoveToNextAttribute ())
                    _atts[reader.Name] = reader.Value;
            }

            while (reader.Read ()) {
                switch (reader.NodeType) {
                    case XmlNodeType.Text:
                        if (_nodeValue == null)
                            _nodeValue = reader.Value;
                        else
                            _nodeValue += reader.Value;
                        break;
                    case XmlNodeType.Element:
                        XmlConfigNode node = new XmlConfigNode (this, reader.Name);
                        node.Load (reader.ReadSubtree ());
                        break;
                }
            }
            if (_nodeValue == null)
                _nodeValue = string.Empty;
        }
Beispiel #24
0
        public XmlConfigNode Lookup(string path, bool createIfNotFound)
        {
            if (path == null) throw new ArgumentNullException ();
            if (path.Length == 0) throw new ArgumentException ();
            if (path[0] == PathSeparatorChar) return _doc.Lookup (path.Substring (1), createIfNotFound);

            string[] names = path.Split (PathSeparatorChar);
            XmlConfigNode node = this;
            for (int i = 0; i < names.Length; i++) {
                bool found = false;
                for (int q = 0; q < node.Nodes.Count; q++) {
                    if (node.Nodes[q].Name == names[i]) {
                        node = node.Nodes[q];
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    if (createIfNotFound)
                        node = new XmlConfigNode (node, names[i]);
                    else
                        return null;
                }
            }
            return node;
        }