1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Imports
|
28
|
|
using System;
|
29
|
|
using System.Text;
|
30
|
|
|
31
|
|
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
|
32
|
|
using IBatisNet.Common.Utilities.Objects;
|
33
|
|
#endregion
|
34
|
|
|
35
|
|
|
36
|
|
namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers
|
37
|
|
{
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
public class IterateTagHandler : BaseTagHandler
|
42
|
|
{
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
32
|
public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, object parameterObject)
|
51
|
|
{
|
52
|
32
|
IterateContext iterate = (IterateContext) ctx.GetAttribute(tag);
|
53
|
32
|
if (iterate == null)
|
54
|
|
{
|
55
|
8
|
string propertyName = ((BaseTag)tag).Property;
|
56
|
8
|
object collection;
|
57
|
8
|
if (propertyName != null && propertyName.Length>0)
|
58
|
|
{
|
59
|
2
|
collection = ObjectProbe.GetPropertyValue(parameterObject, propertyName);
|
60
|
|
}
|
61
|
|
else
|
62
|
|
{
|
63
|
6
|
collection = parameterObject;
|
64
|
|
}
|
65
|
8
|
iterate = new IterateContext(collection);
|
66
|
8
|
ctx.AddAttribute(tag, iterate);
|
67
|
|
}
|
68
|
32
|
if (iterate != null && iterate.HasNext)
|
69
|
|
{
|
70
|
24
|
return BaseTagHandler.INCLUDE_BODY;
|
71
|
|
}
|
72
|
|
else
|
73
|
|
{
|
74
|
8
|
return BaseTagHandler.SKIP_BODY;
|
75
|
|
}
|
76
|
|
}
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
24
|
public override void DoPrepend(SqlTagContext ctx, SqlTag tag, object parameterObject, StringBuilder bodyContent)
|
87
|
|
{
|
88
|
24
|
IterateContext iterate = (IterateContext) ctx.GetAttribute(tag);
|
89
|
24
|
if (iterate.IsFirst)
|
90
|
|
{
|
91
|
8
|
base.DoPrepend(ctx, tag, parameterObject, bodyContent);
|
92
|
|
}
|
93
|
|
}
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
24
|
public override int DoEndFragment(SqlTagContext ctx, SqlTag tag,
|
104
|
|
object parameterObject, StringBuilder bodyContent)
|
105
|
|
{
|
106
|
24
|
IterateContext iterate = (IterateContext) ctx.GetAttribute(tag);
|
107
|
|
|
108
|
24
|
if (iterate.MoveNext())
|
109
|
|
{
|
110
|
24
|
string propertyName = ((BaseTag)tag).Property;
|
111
|
24
|
if (propertyName == null)
|
112
|
|
{
|
113
|
0
|
propertyName = "";
|
114
|
|
}
|
115
|
|
|
116
|
24
|
string find = propertyName + "[]";
|
117
|
24
|
string replace = propertyName + "[" + iterate.Index + "]";
|
118
|
24
|
Replace(bodyContent, find, replace);
|
119
|
|
|
120
|
24
|
if (iterate.IsFirst)
|
121
|
|
{
|
122
|
8
|
string open = ((Iterate)tag).Open;
|
123
|
8
|
if (open != null)
|
124
|
|
{
|
125
|
8
|
bodyContent.Insert(0,open);
|
126
|
8
|
bodyContent.Insert(0,' ');
|
127
|
|
}
|
128
|
|
}
|
129
|
24
|
if (!iterate.IsLast)
|
130
|
|
{
|
131
|
16
|
string conjunction = ((Iterate)tag).Conjunction;
|
132
|
16
|
if (conjunction != null)
|
133
|
|
{
|
134
|
16
|
bodyContent.Append(conjunction);
|
135
|
16
|
bodyContent.Append(' ');
|
136
|
|
}
|
137
|
|
}
|
138
|
24
|
if (iterate.IsLast)
|
139
|
|
{
|
140
|
8
|
string close = ((Iterate)tag).Close;
|
141
|
8
|
if (close != null)
|
142
|
|
{
|
143
|
8
|
bodyContent.Append(close);
|
144
|
|
}
|
145
|
|
}
|
146
|
|
|
147
|
24
|
return BaseTagHandler.REPEAT_BODY;
|
148
|
|
}
|
149
|
|
else
|
150
|
|
{
|
151
|
0
|
return BaseTagHandler.INCLUDE_BODY;
|
152
|
|
}
|
153
|
|
}
|
154
|
|
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
24
|
private static void Replace(StringBuilder buffer, string find, string replace)
|
162
|
|
{
|
163
|
24
|
int start = buffer.ToString().IndexOf(find);
|
164
|
24
|
int length = find.Length;
|
165
|
48
|
while (start > -1)
|
166
|
|
{
|
167
|
24
|
buffer = buffer.Replace(find, replace, start, length);
|
168
|
24
|
start = buffer.ToString().IndexOf(find);
|
169
|
|
}
|
170
|
|
}
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
|
public override bool IsPostParseRequired
|
176
|
|
{
|
177
|
1384
|
get
|
178
|
|
{
|
179
|
1384
|
return true;
|
180
|
|
}
|
181
|
|
}
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|