Merge pull request #2700 from akoeplinger/monotouch-mobile-static
[mono.git] / mcs / class / System.Web / System.Web.UI / CollectionBuilder.cs
1 //
2 // System.Web.UI.CollectionBuilder.cs
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Reflection;
34 using System.Text;
35
36 namespace System.Web.UI
37 {
38         sealed class CollectionBuilder : ControlBuilder
39         {
40                 Type[] possibleElementTypes;
41
42                 internal CollectionBuilder ()
43                 {
44                 }
45
46                 public override void AppendLiteralString (string s)
47                 {
48                         if (s != null && s.Trim ().Length > 0)
49                                 throw new HttpException ("Literal content not allowed for " + ControlType);
50                 }
51
52                 public override Type GetChildControlType (string tagName, IDictionary attribs)
53                 {
54                         Type t = Root.GetChildControlType (tagName, attribs);
55                         if (possibleElementTypes != null) {
56                                bool foundMatchingType = false;
57                                for (int i = 0; i < possibleElementTypes.Length && !foundMatchingType; ++i)
58                                        foundMatchingType = possibleElementTypes[i].IsAssignableFrom (t);
59                                
60                                if (!foundMatchingType) {
61                                        StringBuilder possibleTypesString = new StringBuilder ();
62                                        for (int i = 0; i < possibleElementTypes.Length; i++) {
63                                                if (i != 0)
64                                                        possibleTypesString.Append (", ");
65                                                possibleTypesString.Append (possibleElementTypes[i]);
66                                        }
67                                        throw new HttpException ("Cannot add a " + t + " to " + possibleTypesString);
68                                }
69                         }
70
71                         return t;
72                 }
73
74                 public override void Init (TemplateParser parser,
75                                            ControlBuilder parentBuilder,
76                                            Type type,
77                                            string tagName,
78                                            string id,
79                                            IDictionary attribs)
80                 {                       
81                         base.Init (parser, parentBuilder, type, tagName, id, attribs);
82
83                         PropertyInfo prop = parentBuilder.ControlType.GetProperty (tagName, FlagsNoCase);
84                         SetControlType (prop.PropertyType);
85
86                         MemberInfo[] mems = ControlType.GetMember ("Item", MemberTypes.Property, FlagsNoCase & ~BindingFlags.IgnoreCase);
87                         if (mems.Length > 0) {
88                                possibleElementTypes = new Type [mems.Length];
89                                for (int i = 0; i < mems.Length; ++i)
90                                        possibleElementTypes [i] = ((PropertyInfo)mems [i]).PropertyType;
91                         } else
92                                 throw new HttpException ("Collection of type '" + ControlType + "' does not have an indexer.");
93                 }
94         }
95 }
96