Minor changes:
[castle.git] / Components / Binder / Castle.Components.Binder / Node.cs
blob38ce90a2cf9b3f300832514a074d9ccc9ba8ae6c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Components.Binder
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
21 public enum NodeType
23 Unspecified,
24 Composite,
25 Indexed,
26 Leaf
29 public abstract class Node
31 private readonly String name;
32 private readonly NodeType nodeType;
33 private Node parent;
35 protected Node(String name, NodeType nodeType)
37 this.name = name;
38 this.nodeType = nodeType;
41 public String Name
43 get { return name;}
46 public NodeType NodeType
48 get { return nodeType; }
51 public String FullName
53 get
55 if (Parent != null)
57 return string.Format("{0}.{1}", Parent.FullName, Name);
60 return Name;
64 public Node Parent
66 get { return parent; }
67 set { parent = value; }
71 public class CompositeNode : Node
73 private readonly HybridDictionary name2Node = new HybridDictionary(true);
74 private readonly ArrayList nodeList = new ArrayList();
76 public CompositeNode(String name) : base(name, NodeType.Composite)
80 protected CompositeNode(string name, NodeType nodeType) : base(name, nodeType)
84 public void AddChildNode(Node node)
86 if (node == null) throw new ArgumentNullException("node");
88 name2Node[node.Name] = node;
89 nodeList.Add(node);
90 node.Parent = this;
93 public Node GetChildNode(String name)
95 int index = name.IndexOf(".");
97 if (!name2Node.Contains(name) && index != -1)
99 string firstNodeName = name.Substring(0, index);
100 string remainingName = name.Substring(index + 1);
102 Node innerNode = (Node)name2Node[firstNodeName];
104 if (innerNode != null && innerNode.NodeType == NodeType.Composite)
106 return (innerNode as CompositeNode).GetChildNode(remainingName);
109 throw new BindingException("node is not Composite but still need to recurse to {0}", remainingName);
112 return (Node) name2Node[name];
115 public Node[] ChildNodes
117 get { return (Node[]) nodeList.ToArray(typeof(Node)); }
120 public int ChildrenCount
122 get { return nodeList.Count; }
126 public class IndexedNode : CompositeNode
128 public IndexedNode(string name) : base(name, NodeType.Indexed)
133 public class LeafNode : Node
135 private readonly Type type;
136 private readonly object value;
138 public LeafNode(Type type, string name, object value) : base(name, NodeType.Leaf)
140 if (type == null) throw new ArgumentNullException("type", "A type must be specified");
141 if (value == null) throw new ArgumentNullException("value", "A value must be specified");
143 this.type = type;
144 this.value = value;
147 public Type ValueType
149 get { return type; }
152 public bool IsArray
154 get { return type.IsArray; }
157 public object Value
159 get { return value; }