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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
//------------------------------------------------------------------------------
// <copyright file="XhtmlBasicPageAdapter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI;
using System.IO;
using System.Security.Permissions;
using System.Text;
using System.Web.Mobile;
using System.Web.UI.MobileControls;
using System.Web.UI.MobileControls.Adapters;
using System.Diagnostics;
using System.Globalization;
#if COMPILING_FOR_SHIPPED_SOURCE
namespace System.Web.UI.MobileControls.ShippedAdapterSource.XhtmlAdapters
#else
namespace System.Web.UI.MobileControls.Adapters.XhtmlAdapters
#endif
{
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter"]/*' />
[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 XhtmlPageAdapter : XhtmlControlAdapter, IPageAdapter {
private static readonly TimeSpan _cacheExpirationTime = new TimeSpan(0, 20, 0);
private const int DefaultPageWeight = 4000;
private IDictionary _cookielessDataDictionary = null;
private int _defaultPageWeight = DefaultPageWeight;
private int _optimumPageWeight = 0;
private MobilePage _page;
private bool _persistCookielessData = true;
private bool _pushedCssClassForBody = false;
public XhtmlPageAdapter() {
HtmlPageAdapter.SetPreferredEncodings(HttpContext.Current);
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.CacheVaryByHeaders"]/*' />
public virtual IList CacheVaryByHeaders {
get {
return null;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.CookielessDataDictionary"]/*' />
public IDictionary CookielessDataDictionary {
get {
return _cookielessDataDictionary;
}
set {
_cookielessDataDictionary = value;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.EventArgumentKey"]/*' />
public virtual String EventArgumentKey {
get {
return Constants.EventArgumentID;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.EventSourceKey"]/*' />
public virtual String EventSourceKey {
get {
return Constants.EventSourceID;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.OptimumPageWeight"]/*' />
public virtual int OptimumPageWeight {
get {
if (_optimumPageWeight == 0) {
_optimumPageWeight = CalculateOptimumPageWeight(_defaultPageWeight);
}
return _optimumPageWeight;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.Page"]/*' />
public override MobilePage Page {
get {
return _page;
}
set {
_page = value;
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.PersistCookielessData"]/*' />
public bool PersistCookielessData {
get {
return _persistCookielessData;
}
set {
_persistCookielessData = value;
}
}
// 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) {
targetCollection.Add(sourceKey, value);
}
}
private NameValueCollection CollectionFromForm(
NameValueCollection form,
String postEventSourceID,
String postEventArgumentID) {
int i;
int count = form.Count;
NameValueCollection collection = new NameValueCollection();
bool isPostBack = false;
for (i = 0; i < count; i++) {
String name = form.GetKey(i);
if (name == null || name.Length == 0) {
continue;
}
// 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;
}
// 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;
}
}
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);
// 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;
}
}
private void ConditionalRenderLinkElement (XhtmlMobileTextWriter writer) {
if (DoesDeviceRequireCssSuppression()) {
return;
}
String cssLocation = (String) Page.ActiveForm.CustomAttributes[XhtmlConstants.StyleSheetLocationCustomAttribute];
if (cssLocation != null &&
cssLocation.Length != 0) {
writer.WriteBeginTag ("link");
writer.WriteAttribute ("type", "text/css");
writer.WriteAttribute ("rel", "stylesheet");
writer.WriteAttribute("href", cssLocation, true);
writer.WriteLine("/>");
}
else if (!writer.IsStyleSheetEmpty () && CssLocation!=StyleSheetLocation.Internal) {
writer.WriteLine ();
writer.WriteBeginTag ("link");
writer.WriteAttribute ("type", "text/css");
writer.WriteAttribute ("rel", "stylesheet");
String queryStringValue = GetCssQueryStringValue(writer);
writer.Write(" href=\"" + XhtmlConstants.CssMappedFileName + "?" + XhtmlConstants.CssQueryStringName + "=" + queryStringValue + "\"/>");
writer.WriteLine();
}
}
private void ConditionalRenderStyleElement (XhtmlMobileTextWriter writer) {
if (!writer.IsStyleSheetEmpty () && CssLocation == StyleSheetLocation.Internal) {
bool requiresComments = (String)Device["requiresCommentInStyleElement"] == "true";
writer.WriteLine();
writer.WriteBeginTag("style");
writer.Write(" type=\"text/css\">");
writer.WriteLine();
if (requiresComments) {
writer.WriteLine("<!--");
}
writer.Write(writer.GetStyles());
if (requiresComments) {
writer.WriteLine("-->");
}
writer.WriteEndTag("style");
writer.WriteLine();
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.CreateTextWriter"]/*' />
public virtual HtmlTextWriter CreateTextWriter( TextWriter writer) {
return new XhtmlMobileTextWriter (writer, Device);
}
// Similar to CHTML.
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.DeterminePostBackMode"]/*' />
public virtual NameValueCollection DeterminePostBackMode(
HttpRequest request,
String postEventSourceID,
String postEventArgumentID,
NameValueCollection baseCollection) {
if (baseCollection != null && baseCollection[EventSourceKey] == XhtmlConstants.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\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.DeviceQualifies"]/*' />
public static bool DeviceQualifies(HttpContext context) {
String type = ((MobileCapabilities)context.Request.Browser).PreferredRenderingType;
return String.Equals(type, "xhtml-basic", StringComparison.OrdinalIgnoreCase) ||
String.Equals(type, "xhtml-mp", StringComparison.OrdinalIgnoreCase) ||
String.Equals(type, "wml20", StringComparison.OrdinalIgnoreCase);
}
private bool DoesDeviceRequireCssSuppression() {
return(String)Device[XhtmlConstants.RequiresXhtmlCssSuppression] == "true";
}
private String GetCssQueryStringValue(XhtmlMobileTextWriter writer) {
if (CssLocation == StyleSheetLocation.ApplicationCache) {
// Initialize the cache key
writer.SetCacheKey(Page.Cache);
return writer.CacheKey;
}
else if (CssLocation == StyleSheetLocation.SessionState) {
writer.SetSessionKey(Page.Session);
return writer.SessionKey;
}
Debug.Assert (StyleSheetLocationAttributeValue != null);
return StyleSheetLocationAttributeValue;
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.HandleError"]/*' />
public virtual bool HandleError (Exception e, HtmlTextWriter writer) {
return false;
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.HandlePagePostBackEvent"]/*' />
public virtual bool HandlePagePostBackEvent (string eventSource, string eventArgument) {
return false;
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.InitWriterState"]/*' />
protected virtual void InitWriterState(XhtmlMobileTextWriter writer) {
writer.UseDivsForBreaks = (String)Device[XhtmlConstants.BreaksOnInlineElements] == "true";
writer.SuppressNewLine = (String)Device[XhtmlConstants.RequiresNewLineSuppression] == "true";
writer.SupportsNoWrapStyle = (String)Device[XhtmlConstants.SupportsNoWrapStyle] != "false";
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.OnPreRender"]/*' />
public override void OnPreRender(EventArgs e) {
if (Page.ActiveForm.Paginate && Page.ActiveForm.Action.Length > 0) {
Page.ActiveForm.Paginate = false;
}
base.OnPreRender(e);
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.Render"]/*' />
public override void Render (XhtmlMobileTextWriter writer) {
writer.BeginResponse ();
if (Page.Request.Browser["requiresPragmaNoCacheHeader"] == "true") {
Page.Response.AppendHeader("Pragma", "no-cache");
}
InitWriterState(writer);
writer.BeginCachedRendering ();
// For consistency with HTML, we render the form style with body tag.
RenderOpeningBodyElement(writer);
// Certain WML 2.0 devices require that we write an onevent onenterforward setvar snippet.
// We cannot know the relevant variables until after the form is rendered, so we mark this
// position. The setvar elements will be inserted into the cached rendering here.
writer.MarkWmlOnEventLocation ();
Page.ActiveForm.RenderControl(writer);
RenderClosingBodyElement(writer);
writer.ClearPendingBreak ();
writer.EndCachedRendering ();
// Note: first and third arguments are not used.
writer.BeginFile (Page.Request.Url.ToString (), Page.Device.PreferredRenderingMime, Page.Response.Charset);
String supportsXmlDeclaration = Device["supportsXmlDeclaration"];
// Invariant culture not needed, included for best practices compliance.
if (supportsXmlDeclaration == null ||
!String.Equals(supportsXmlDeclaration, "false", StringComparison.OrdinalIgnoreCase)) {
writer.WriteXmlDeclaration ();
}
writer.WriteDoctypeDeclaration(DocumentType);
// Review: Hard coded xmlns.
writer.WriteFullBeginTag ("html xmlns=\"http://www.w3.org/1999/xhtml\"");
writer.WriteLine ();
writer.WriteFullBeginTag ("head");
writer.WriteLine ();
writer.WriteFullBeginTag ("title");
if (Page.ActiveForm.Title != null) {
writer.WriteEncodedText(Page.ActiveForm.Title);
}
writer.WriteEndTag ("title");
ConditionalRenderLinkElement (writer);
ConditionalRenderStyleElement (writer);
writer.WriteEndTag ("head");
writer.WriteLine ();
writer.WriteCachedMarkup (); // includes body tag.
writer.WriteLine ();
writer.WriteEndTag ("html");
writer.EndFile ();
if (!DoesDeviceRequireCssSuppression()) {
if (CssLocation == StyleSheetLocation.ApplicationCache && !writer.IsStyleSheetEmpty()) {
// Recall that Page.Cache has application scope
Page.Cache.Insert(writer.CacheKey, writer.GetStyles (), null, DateTime.MaxValue, _cacheExpirationTime);
}
else if (CssLocation == StyleSheetLocation.SessionState && !writer.IsStyleSheetEmpty()) {
Page.Session[writer.SessionKey] = writer.GetStyles();
}
}
writer.EndResponse ();
}
private void RenderClosingBodyElement(XhtmlMobileTextWriter writer) {
Style formStyle = ((ControlAdapter)Page.ActiveForm.Adapter).Style;
if (CssLocation == StyleSheetLocation.PhysicalFile) {
writer.WriteEndTag("body");
if (_pushedCssClassForBody) {
writer.PopPhysicalCssClass();
}
}
else if ((String)Device[XhtmlConstants.RequiresXhtmlCssSuppression] != "true" &&
(String)Device[XhtmlConstants.SupportsBodyClassAttribute] != "false") {
writer.ExitStyle(formStyle); // writes the closing body element.
}
else {
writer.WriteEndTag ("body");
}
}
private void RenderHiddenVariablesInUrl(XhtmlMobileTextWriter writer) {
if (Page.HasHiddenVariables()) {
String hiddenVariablePrefix = MobilePage.HiddenVariablePrefix;
foreach (DictionaryEntry entry in Page.HiddenVariables) {
writer.Write("&");
writer.WriteUrlParameter(hiddenVariablePrefix + (String)entry.Key,
(String)entry.Value);
}
}
}
private void RenderOpeningBodyElement(XhtmlMobileTextWriter writer) {
Form activeForm = Page.ActiveForm;
Style formStyle = ((ControlAdapter)activeForm.Adapter).Style;
if (CssLocation == StyleSheetLocation.PhysicalFile) {
String cssClass = (String) activeForm.CustomAttributes[XhtmlConstants.CssClassCustomAttribute];
writer.WriteBeginTag("body");
if (cssClass != null && (String)Device["supportsBodyClassAttribute"] != "false") {
writer.WriteAttribute("class", cssClass, true /* encode */);
writer.PushPhysicalCssClass(cssClass);
_pushedCssClassForBody = true;
}
writer.Write(">");
}
else if ((String)Device[XhtmlConstants.RequiresXhtmlCssSuppression] != "true" &&
(String)Device[XhtmlConstants.SupportsBodyClassAttribute] != "false") {
writer.EnterStyle(formStyle, "body");
}
else {
writer.WriteFullBeginTag("body");
if ((String)Device[XhtmlConstants.RequiresXhtmlCssSuppression] != "true" &&
(String)Device[XhtmlConstants.SupportsBodyClassAttribute] == "false") {
writer.SetBodyStyle(formStyle);
}
}
}
/// <include file='doc\XhtmlBasicPageAdapter.uex' path='docs/doc[@for="XhtmlPageAdapter.RenderUrlPostBackEvent"]/*' />
public virtual void RenderUrlPostBackEvent (XhtmlMobileTextWriter writer,
String target,
String argument) {
String amp = (String)Device[XhtmlConstants.SupportsUrlAttributeEncoding] == "false" ? "&" : "&";
if ((String)Device["requiresAbsolutePostbackUrl"] == "true") {
writer.WriteEncodedUrl(Page.AbsoluteFilePath);
}
else {
writer.WriteEncodedUrl(Page.RelativeFilePath);
}
writer.Write ("?");
// Encode ViewStateID=.....&__ET=controlid&__EA=value in URL
// Note: the encoding needs to be agreed with the page
// adapter which handles the post back info
String pageState = Page.ClientViewState;
if (pageState != null) {
writer.WriteUrlParameter (MobilePage.ViewStateID, pageState);
writer.Write (amp);
}
writer.WriteUrlParameter (EventSourceKey, target);
writer.Write (amp);
writer.WriteUrlParameter (EventArgumentKey, argument);
RenderHiddenVariablesInUrl (writer);
// Unique file path suffix is used for identify if query
// string text is present. Corresponding code needs to agree
// on this. Even if the query string is empty, we still need
// to output the suffix to indicate this. (this corresponds
// to the code that handles the postback)
writer.Write(amp);
writer.Write(Constants.UniqueFilePathSuffixVariable);
String queryStringText = PreprocessQueryString(Page.QueryStringText);
if (queryStringText != null && queryStringText.Length > 0) {
writer.Write (amp);
if ((String)Device[XhtmlConstants.SupportsUrlAttributeEncoding] != "false") {
writer.WriteEncodedAttributeValue(queryStringText);
}
else {
writer.Write (queryStringText);
}
}
}
}
}
|