1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using System
.Collections
.Specialized
;
30 public abstract class Node
32 private readonly String name
;
33 private readonly NodeType nodeType
;
36 protected Node(String name
, NodeType nodeType
)
39 this.nodeType
= nodeType
;
47 public NodeType NodeType
49 get { return nodeType; }
52 public String FullName
58 return string.Format("{0}.{1}", Parent
.FullName
, Name
);
67 get { return parent; }
68 set { parent = value; }
72 public class CompositeNode
: Node
74 private readonly HybridDictionary name2Node
= new HybridDictionary(true);
75 private readonly List
<Node
> nodeList
= new List
<Node
>();
77 public CompositeNode(String name
) : base(name
, NodeType
.Composite
)
81 protected CompositeNode(string name
, NodeType nodeType
) : base(name
, nodeType
)
85 public void AddChildNode(Node node
)
87 if (node
== null) throw new ArgumentNullException("node");
89 name2Node
[node
.Name
] = node
;
94 public Node
GetChildNode(String name
)
96 int index
= name
.IndexOf(".");
98 if (!name2Node
.Contains(name
) && index
!= -1)
100 string firstNodeName
= name
.Substring(0, index
);
101 string remainingName
= name
.Substring(index
+ 1);
103 Node innerNode
= (Node
)name2Node
[firstNodeName
];
105 if (innerNode
!= null && innerNode
.NodeType
== NodeType
.Composite
)
107 return (innerNode
as CompositeNode
).GetChildNode(remainingName
);
110 throw new BindingException("node is not Composite but still need to recurse to {0}", remainingName
);
113 return (Node
) name2Node
[name
];
116 public Node
[] ChildNodes
118 get { return nodeList.ToArray(); }
121 public int ChildrenCount
123 get { return nodeList.Count; }
127 public class IndexedNode
: CompositeNode
129 public IndexedNode(string name
) : base(name
, NodeType
.Indexed
)
134 public class LeafNode
: Node
136 private readonly Type type
;
137 private readonly object value;
139 public LeafNode(Type type
, string name
, object value) : base(name
, NodeType
.Leaf
)
141 if (type
== null) throw new ArgumentNullException("type", "A type must be specified");
142 if (value == null) throw new ArgumentNullException("value", "A value must be specified");
148 public Type ValueType
155 get { return type.IsArray; }
160 get { return value; }