File: ChtmlPageAdapter.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (385 lines) | stat: -rw-r--r-- 16,519 bytes parent folder | download | duplicates (7)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//------------------------------------------------------------------------------
// <copyright file="ChtmlPageAdapter.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Web.Mobile;
using System.Web.UI.MobileControls.Adapters;
using System.Security.Permissions;

#if COMPILING_FOR_SHIPPED_SOURCE
namespace System.Web.UI.MobileControls.ShippedAdapterSource
#else
namespace System.Web.UI.MobileControls.Adapters
#endif    

{

    /*
     * ChtmlPageAdapter class.
     *
     * Copyright (c) 2000 Microsoft Corporation
     */
    /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter"]/*' />
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
    public class ChtmlPageAdapter : HtmlPageAdapter
    {
        private const int DefaultPageWeight = 800;
        private const String _postedFromOtherFile = ".";

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.ChtmlPageAdapter"]/*' />
        public ChtmlPageAdapter() : base(DefaultPageWeight)
        {
        }

        /////////////////////////////////////////////////////////////////////
        //  Static method used for determining if device should use
        //  this adapter
        /////////////////////////////////////////////////////////////////////

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.DeviceQualifies"]/*' />
        public new static bool DeviceQualifies(HttpContext context)
        {
            String type = ((MobileCapabilities)context.Request.Browser).PreferredRenderingType;
            bool javascriptSupported = context.Request.Browser.JavaScript;
            bool qualifies = (type == MobileCapabilities.PreferredRenderingTypeHtml32 ||
                              type == MobileCapabilities.PreferredRenderingTypeChtml10)
                             && !javascriptSupported;
            return qualifies;
        }
        
        /////////////////////////////////////////////////////////////////////
        //  IControlAdapter implementation
        /////////////////////////////////////////////////////////////////////

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.RenderPostBackEvent"]/*' />
        public override void RenderPostBackEvent(HtmlMobileTextWriter writer, 
                                                String target, 
                                                String argument)
        {
            // Since it doesn't have scripts, the CHTML adapter
            // only supports URL postback events.

            RenderUrlPostBackEvent(writer, target, argument);
        }

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.EventSourceKey"]/*' />
        protected override String EventSourceKey
        {
            get
            {
                return Constants.EventSourceID;
            }
        }

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.EventArgumentKey"]/*' />
        protected override String EventArgumentKey
        {
            get
            {
                return Constants.EventArgumentID;
            }
        }

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.RenderPostBackHeader"]/*' />
        public override void RenderPostBackHeader(HtmlMobileTextWriter writer, Form form)
        {
            bool postBack = form.Action.Length == 0;

            RenderPageState(writer);
            if (!postBack)
            {
                writer.WriteHiddenField(EventSourceKey, _postedFromOtherFile);
            }
            else if (Page.ClientViewState == null)
            {
                // The empty event source variable is used to identify a
                // postback request
                writer.WriteHiddenField(EventSourceKey, String.Empty);
            }

            RenderHiddenVariables(writer);
        }

        /////////////////////////////////////////////////////////////////////
        //  IPageAdapter implementation
        /////////////////////////////////////////////////////////////////////

        // ==================================================================
        // For browser that doesn't support javascript, like cHTML browser,
        // control id and its corresponding value are specially encoded in
        // the post back data collection.  This method is to extract the
        // encoded info and put the info back to the collection in an
        // expected format that is understood by ASP.NET Frameworks so post
        // back event is raised correctly.
        // Note other control adapters should do the encoding accordinly so
        // the data can be decoded properly here.
        //
        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.DeterminePostBackMode"]/*' />
        public override NameValueCollection DeterminePostBackMode
        (
            HttpRequest request,
            String postEventSourceID,
            String postEventArgumentID,
            NameValueCollection baseCollection
        )
        {
            if (baseCollection != null && baseCollection[EventSourceKey] == _postedFromOtherFile)
            {
                return null;
            }
            else if (request == null)
            {
                return baseCollection;
            }
            else if (String.Compare(request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0)
            {
                return CollectionFromForm(request.Form,
                                          postEventSourceID,
                                          postEventArgumentID);
            }
            else if (request.QueryString.Count == 0)
            {
                return baseCollection;
            }
            else
            {
                return CollectionFromQueryString(request.QueryString,
                                                 postEventSourceID,
                                                 postEventArgumentID);
            }
        }

        /// <include file='doc\ChtmlPageAdapter.uex' path='docs/doc[@for="ChtmlPageAdapter.CreateTextWriter"]/*' />
        public override HtmlTextWriter CreateTextWriter(TextWriter writer)
        {
            return new ChtmlMobileTextWriter(writer, Device);
        }

        private NameValueCollection CollectionFromQueryString(
            NameValueCollection queryString,
            String postEventSourceID,
            String postEventArgumentID)
        {
            NameValueCollection collection = new NameValueCollection();
            bool isPostBack = false;

            for (int i = 0; i < queryString.Count; i++)
            {
                String name = queryString.GetKey(i);

                // Supposingly, we should double check if the control id
                // is real or not by checking against the control tree.
                // However, the tree can't be checked because it hasn't
                // been built at this stage.  And this is the only place
                // we can override the value collection.  We just need to
                // assume the control adapters are setting the id and
                // value accordingly.

                // ASSUMPTION: In query string, besides the expected
                // name/value pairs (ViewStateID, EventSource and
                // EventArgument), there are hidden variables, control
                // id/value pairs (if the form submit method is GET), unique
                // file path suffix variable and custom query string text.
                // They will be in the above order if any of them present.
                // Hidden variables and control id/value pairs should be added
                // back to the collection intactly, but the other 2 items
                // should not be added to the collection.

                // name can be null if there is a query name without equal
                // sign appended.  We should just ignored it in this case.
                if (name == null)
                {
                    continue;
                }
                else if (name == MobilePage.ViewStateID)
                {
                    collection.Add(MobilePage.ViewStateID, queryString.Get(i));
                    isPostBack = true;
                }
                else if (name == Constants.EventSourceID)
                {
                    collection.Add(postEventSourceID, queryString.Get(i));
                    isPostBack = true;
                }
                else if (name == Constants.EventArgumentID)
                {
                    collection.Add(postEventArgumentID, queryString.Get(i));
                }
                else if (Constants.UniqueFilePathSuffixVariable.StartsWith(name, StringComparison.Ordinal))
                {
                    // At this point we know that the rest of them is
                    // the custom query string text, so we are done.
                    break;
                }
                else
                {
                    AddValues(queryString, name, collection);
                }
            }

            if (collection.Count == 0 || !isPostBack)
            {
                // Returning null to indicate this is not a postback
                return null;
            }
            else
            {
                return collection;
            }
        }

        // ==================================================================
        // The complexity (multiple if statements) of this method is due to
        // workarounds for different targeted devices and limitation on non-
        // javascript html browser.
        //
        private NameValueCollection CollectionFromForm(
            NameValueCollection form,
            String postEventSourceID,
            String postEventArgumentID)
        {
            int i;
            int count = form.Count;
            NameValueCollection collection = new NameValueCollection();
            bool isPostBack = false;

            // continue statements are used below to simplify the logic and
            // make people easier to follow and maintain the code.
            for (i = 0; i < count; i++)
            {
                String name = form.GetKey(i);

                // 1. Some browser returns the name of a password textbox
                // only without the expected character "=" if the textbox is
                // empty.  This causes the key to be null and the name to be
                // the value of the collection item when the returned form
                // content is parsed in HttpValueCollection.  In this case,
                // we need to reverse the setting with the value as the name
                // and empty string as the value so subsequent manipulations
                // of the collection work correctly.
                if (name == null)
                {
                    if (AddEmptyStringValues(form.GetValues(i), collection)) {
                        isPostBack = true;
                    }
                    continue;
                }

                // 2. Pager navigation is rendered by buttons which have the
                // targeted page number appended to the form id after
                // PagePrefix which is a constant string to identify this
                // special case.  E.g. ControlID__PG_2
                int index = name.LastIndexOf(Constants.PagePrefix, StringComparison.Ordinal);
                if (index != -1)
                {
                    // We need to associate the form id with the event source
                    // id and the page number with the event argument id in
                    // order to have the event raised properly by ASP.NET
                    int pageBeginPos = index + Constants.PagePrefix.Length;
                    collection.Add(postEventSourceID,
                                   name.Substring(0, index));
                    collection.Add(postEventArgumentID,
                                   name.Substring(pageBeginPos,
                                              name.Length - pageBeginPos));
                    continue;
                }

                // 3. This special case happens when A. SelectionList control is
                // with property SelectType equal to CheckBox or
                // MultiSelectListBox, and the device itself doesn't handle
                // multiple check boxes correctly. or B. Browser requires the
                // ID of the input element to be unique during postbacks.
                //
                // In this case, the control (SelectionList or TextBox) adapter 
                // appended special characters as a suffix of the actual control
                // id. That should be stripped off when detected.
                if (Device.RequiresUniqueHtmlCheckboxNames ||
                    Device.RequiresUniqueHtmlInputNames)
                {
                    index = name.LastIndexOf(
                        Constants.SelectionListSpecialCharacter);

                    if (index != -1)
                    {
                        String value = form.Get(i);
                        if (!String.IsNullOrEmpty(value))
                        {
                            if(Device.RequiresAttributeColonSubstitution)
                            {
                                collection.Add(name.Substring(0, index).Replace(',',':'), value);
                            }
                            else
                            {
                                collection.Add(name.Substring(0, index), value);
                            }
                            continue;
                        }
                    }
                }

                // 4. This is to determine if the request is a postback from
                // the same mobile page.
                if (name == MobilePage.ViewStateID ||
                    name == EventSourceKey)
                {
                    isPostBack = true;
                }

                // Default case, just preserve the value(s)
                AddValues(form, name, collection);
            }

            if (collection.Count == 0 || !isPostBack)
            {
                // Returning null to indicate this is not a postback
                return null;
            }
            else
            {
                return collection;
            }
        }

        // Helper function to add empty string as value for the keys
        private bool AddEmptyStringValues(String [] keys,
                                        NameValueCollection targetCollection)
        {
            bool result = false;
            foreach (String key in keys)
            {
                if (key == MobilePage.ViewStateID ||
                    key == EventSourceKey) {
                    result = true;
                }
                targetCollection.Add(key, String.Empty);
            }
            return result;
        }

        // Helper function to add multiple values for the same key
        private void AddValues(NameValueCollection sourceCollection,
                               String sourceKey,
                               NameValueCollection targetCollection)
        {
            String [] values = sourceCollection.GetValues(sourceKey);
            foreach (String value in values)
            {
                if(Device.RequiresAttributeColonSubstitution)
                {
                    targetCollection.Add(sourceKey.Replace(',',':'), value);
                }
                else
                {
                    targetCollection.Add(sourceKey, value);
                }
            }
        }
    }
}