Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ControlBuilder.cs « System.Web.UI « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cbb8fffd456b6aa14a37d8bf6e741479deef817 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// System.Web.UI.ControlBuilder.cs
//
// Authors:
// 	Duncan Mak  (duncan@ximian.com)
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002, 2003 Ximian, Inc. (http://www.ximian.com)
//

using System;
using System.Collections;
using System.Reflection;

namespace System.Web.UI {

	public class ControlBuilder
	{
		internal static BindingFlags flagsNoCase = BindingFlags.Public |
							   BindingFlags.Instance |
							   BindingFlags.Static |
							   BindingFlags.IgnoreCase;

		TemplateParser parser;
		ControlBuilder parentBuilder;
		Type type;	       
		string tagName;
		string id;
		IDictionary attribs;
		int line;
		string fileName;
		bool childrenAsProperties;
		bool isIParserAccessor;
		bool hasAspCode;
		ControlBuilder defaultPropertyBuilder;
		ArrayList children;

		public ControlBuilder ()
		{
		}

		internal ControlBuilder (TemplateParser parser,
					 ControlBuilder parentBuilder,
					 Type type,
					 string tagName,
					 string id,
					 IDictionary attribs,
					 int line,
					 string sourceFileName)

		{
			this.parser = parser;
			this.parentBuilder = parentBuilder;
			this.type = type;
			this.tagName = tagName;
			this.id = id;
			this.attribs = attribs;
			this.line = line;
			this.fileName = sourceFileName;
		}

		public Type ControlType {
			get { return type; }
		}

		public bool FChildrenAsProperties {
			get { return childrenAsProperties; }
		}

		public bool FIsNonParserAccessor {
			get { return isIParserAccessor; }
		}

		public bool HasAspCode {
			get { return hasAspCode; }
		}

		public string ID {
			get { return id; }
			set { id = value; }
		}

		protected void SetControlType (Type t)
		{
			type = t;
		}
		
		[MonoTODO]
		public bool InDesigner {
			get { return false; }
		}

		public Type NamingContainerType {
			get {
				if (parentBuilder == null)
					return typeof (Control);

				Type ptype = parentBuilder.ControlType;
				if (ptype == null)
					return typeof (Control);

				if (!typeof (INamingContainer).IsAssignableFrom (type))
					return parentBuilder.NamingContainerType;

				return type;
			}
		}

		protected TemplateParser Parser {
			get { return parser; }
		}

		public string TagName {
			get { return tagName; }
		}

		public virtual bool AllowWhitespaceLiterals ()
		{
			return true;
		}

		[MonoTODO]
		public virtual void AppendLiteralString (string s)
		{
			throw new NotImplementedException ();
		}

		public virtual void AppendSubBuilder (ControlBuilder subBuilder)
		{
			if (children == null)
				children = new ArrayList ();

			subBuilder.OnAppendToParentBuilder (this);
			children.Add (subBuilder);
		}

		public virtual void CloseControl ()
		{
		}

		public static ControlBuilder CreateBuilderFromType (TemplateParser parser,
								    ControlBuilder parentBuilder,
								    Type type,
								    string tagName,
								    string id,
								    IDictionary attribs,
								    int line,
								    string sourceFileName)
		{
			ControlBuilder  builder;
			object [] atts = type.GetCustomAttributes (typeof (ControlBuilderAttribute), true);
			if (atts != null && atts.Length > 0) {
				ControlBuilderAttribute att = (ControlBuilderAttribute) atts [0];
				builder = (ControlBuilder) Activator.CreateInstance (att.BuilderType);
			} else {
				builder = new ControlBuilder ();
			}

			builder.Init (parser, parentBuilder, type, tagName, id, attribs);
			builder.line = line;
			builder.fileName = sourceFileName;
			return builder;
		}

		public virtual Type GetChildControlType (string tagName, IDictionary attribs)
		{
			return null;
		}

		public virtual bool HasBody ()
		{
			return true;
		}

		public virtual bool HtmlDecodeLiterals ()
		{
			return false;
		}

		ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser)
		{
			PropertyInfo prop = type.GetProperty (propName, flagsNoCase);
			if (prop == null) {
				string msg = String.Format ("Property {0} not found in type {1}", propName, type);
				throw new HttpException (msg);
			}

			Type propType = prop.PropertyType;
			ControlBuilder builder = null;
			if (typeof (ICollection).IsAssignableFrom (propType))
				builder = new CollectionBuilder ();
			else if (typeof (ITemplate).IsAssignableFrom (propType))
				builder = new TemplateBuilder ();
			else
				return CreateBuilderFromType (parser,
							      parentBuilder,
							      propType,
							      propName,
							      null,
							      null,
							      line,
							      fileName);

			builder.Init (parser, this, null, tagName, null, null);
			builder.fileName = fileName;
			builder.line = line;
			return builder;
		}
		
		public virtual void Init (TemplateParser parser,
					  ControlBuilder parentBuilder,
					  Type type,
					  string tagName,
					  string id,
					  IDictionary attribs)
		{
			this.parser = parser;
			this.parentBuilder = parentBuilder;
			this.type = type;
			this.tagName = tagName;
			this.id = id;
			this.attribs = attribs;
			if (type == null)
				return;

			if (!typeof (IParserAccessor).IsAssignableFrom (type)) {
				isIParserAccessor = false;
				childrenAsProperties = true;
			} else {
				object [] atts = type.GetCustomAttributes (typeof (ParseChildrenAttribute), true);
				if (atts != null && atts.Length > 0) {
					ParseChildrenAttribute att = (ParseChildrenAttribute) atts [0];
					childrenAsProperties = att.ChildrenAsProperties;
					if (childrenAsProperties && att.DefaultProperty != "")
						defaultPropertyBuilder = CreatePropertyBuilder (att.DefaultProperty,
												parser);
				}
			}
		}

		public virtual bool NeedsTagInnerText ()
		{
			return false;
		}

		public virtual void OnAppendToParentBuilder (ControlBuilder parentBuilder)
		{
			if (parentBuilder.defaultPropertyBuilder == null)
				return;

			ControlBuilder old = parentBuilder.defaultPropertyBuilder;
			defaultPropertyBuilder = null;
			AppendSubBuilder (old);
		}

		public virtual void SetTagInnerText (string text)
		{
		}

		internal virtual object CreateInstance ()
		{
			// HtmlGenericControl, HtmlTableCell...
			object [] atts = type.GetCustomAttributes (typeof (ConstructorNeedsTagAttribute), true);
			object [] args = null;
			if (atts != null && atts.Length > 0) {
				ConstructorNeedsTagAttribute att = (ConstructorNeedsTagAttribute) atts [0];
				if (att.NeedsTag)
					args = new object [] {tagName};
			}

			return Activator.CreateInstance (type, args);
		}

		internal virtual void CreateChildren (object parent) 
		{
			if (children == null || children.Count == 0)
				return;

			IParserAccessor parser = parent as IParserAccessor;
			if (parser == null)
				return;

			foreach (object o in children) {
				if (o is string) {
					parser.AddParsedSubObject (new LiteralControl ((string) o));
				} else {
					parser.AddParsedSubObject (((ControlBuilder) o).CreateInstance ());
				}
			}
		}
	}
}