aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/IML/NodeAddress.cs
blob: b9f8b902a1ecd7fe42bbbe32cb16e2666fb5cfca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// NodeAddress.cs
//
// Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace Crow.IML
{
	/// <summary>
	/// Node address is a list of nodes from root to leaf defining a unique node
	/// </summary>
	public class NodeAddress : List<Node>
	{
		#region CTOR
		public NodeAddress (Node[] nodes) : base(nodes) {}
		#endregion

		public Type NodeType { get { return Count == 0 ? null : this[this.Count -1].CrowType; }}

		/// <summary>
		/// Gets the node adress from binding expression starting from this node
		/// and return in expression remaining part
		/// </summary>
		public NodeAddress ResolveExpression (ref string expression){
			int ptr = this.Count - 1;
			string[] splitedExp = expression.Split ('/');

			if (splitedExp.Length < 2)//dataSource binding
				return null;

			if (string.IsNullOrEmpty (splitedExp [0]) || splitedExp [0] == ".") {//search template root
				ptr--;
				while (ptr >= 0) {
					if (typeof(TemplatedControl).IsAssignableFrom (this [ptr].CrowType))
						break;
					ptr--;
				}
			} else if (splitedExp [0] == "..") { //search starting at current node
				int levelUp = splitedExp.Length - 1;
				if (levelUp > ptr + 1)
					throw new Exception ("Binding error: try to bind outside IML source");
				ptr -= levelUp;
			}
			expression = splitedExp [splitedExp.Length - 1];
			//TODO:change Template special address identified with Nodecount = 0 to something not using array count to 0,
			//here linq is working without limits checking in compile option
			//but defining a 0 capacity array with limits cheking enabled, cause 'out of memory' error
			return new NodeAddress (this.Take(ptr+1).ToArray());//[ptr+1];
			//Array.Copy (sourceAddr.ToArray (), targetNode, ptr + 1);
			//return new NodeAddress (targetNode);
		}
		/// <summary>
		/// get BindingDefinition from binding expression
		/// </summary>
		public BindingDefinition GetBindingDef(string sourceMember, string expression){
			BindingDefinition bindingDef = new BindingDefinition(this, sourceMember);
			if (string.IsNullOrEmpty (expression)) {
				return bindingDef;
			} else {
				if (expression.StartsWith ("²")) {
					bindingDef.TwoWay = true;
					expression = expression.Substring (1);
				}

				string exp = expression;
				bindingDef.TargetNA = this.ResolveExpression (ref exp);

				string [] bindTrg = exp.Split ('.');

				if (bindTrg.Length == 0)
					throw new Exception ("invalid binding expression: " + expression);
				if (bindTrg.Length == 1)
					bindingDef.TargetMember = bindTrg [0];
				else {
					if (!string.IsNullOrEmpty(bindTrg[0]))//searchByName
						bindingDef.TargetName = bindTrg[0];

					bindingDef.TargetMember = exp.Substring (bindTrg[0].Length + 1);
				}
			}

			return bindingDef;
		}

		#region Object overrides
		public override bool Equals (object obj)
		{
			if (obj == null) 
				return false;
			
			NodeAddress na = (NodeAddress)obj;
			return this.SequenceEqual (na);
		}
		public override int GetHashCode ()
		{
			unchecked {
				int hash = 19;
				foreach (Node n in this)
					hash = hash * 31 + (n == null ? 0 : n.GetHashCode ());	
				return hash;
			}
		}
		public override string ToString ()
		{
			string tmp = "";
			foreach (Node n in this)
				tmp += string.Format ("{0};", n.Index);			
			return tmp;
		}
		#endregion
	}

	public class NamedNodeAddress : NodeAddress {
		public string Name;
		public NamedNodeAddress(string name, Node[] nodes) : base(nodes){
			Name = name;
		}
	}
}