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
.Specialized
;
29 public abstract class Node
31 private readonly String name
;
32 private readonly NodeType nodeType
;
35 protected Node(String name
, NodeType nodeType
)
38 this.nodeType
= nodeType
;
46 public NodeType NodeType
48 get { return nodeType; }
51 public String FullName
57 return string.Format("{0}.{1}", Parent
.FullName
, Name
);
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
;
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");
147 public Type ValueType
154 get { return type.IsArray; }
159 get { return value; }