//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Mobile;
using System.Web.UI.MobileControls;
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
{
/*
* WmlPageAdapter base class contains wml specific methods.
*
* Copyright (c) 2000 Microsoft Corporation
*/
///
[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 WmlPageAdapter : WmlControlAdapter, IPageAdapter
{
private IList _renderableForms;
private static readonly String _headerBegin = "\r\n"
+ "";
private static readonly String _cacheExpiry
= "
\r\n"
+ "");
writer.Write("");
if (renderBackButton)
{
writer.Write("");
}
String stackTrace = ex.StackTrace;
if (stackTrace != null) {
int maximumStackTrace = OptimumPageWeight / 2;
if (stackTrace.Length > maximumStackTrace)
{
wmlWriter.WriteEncodedText(SR.GetString(SR.WmlPageAdapterStackTrace));
writer.Write("
");
stackTrace = stackTrace.Substring(0, maximumStackTrace);
}
else
{
wmlWriter.WriteEncodedText(SR.GetString(SR.WmlPageAdapterPartialStackTrace));
writer.Write("
");
}
int lineBegin = 0;
int lineEnd;
while (lineBegin < stackTrace.Length)
{
lineEnd = stackTrace.IndexOf("\r\n", lineBegin, StringComparison.Ordinal);
if (lineEnd == -1)
{
lineEnd = stackTrace.Length;
}
wmlWriter.WriteEncodedText(stackTrace.Substring(lineBegin, lineEnd - lineBegin));
writer.Write("
");
lineBegin = lineEnd + 2;
}
}
writer.WriteEndTag("p");
writer.WriteEndTag("card");
writer.WriteEndTag("wml");
wmlWriter.EndFile();
wmlWriter.EndResponse();
return true;
}
///
public virtual bool HandlePagePostBackEvent(String eventSource, String eventArgument)
{
return false;
}
///
public override void Render(WmlMobileTextWriter writer)
{
if (RequiresUTF8ContentEncoding())
{
Page.Response.ContentEncoding = UTF8Encoding;
}
writer.BeginResponse();
writer.BeginFile(Page.Request.Url.ToString(), _contentType, Page.Response.Charset);
RenderXmlHeader(writer);
writer.WriteFullBeginTag("wml");
if (Device.SupportsCacheControlMetaTag)
{
writer.Write(_cacheExpiry);
}
else
{
Page.Response.AppendHeader("Cache-Control", "max-age=0");
}
if (Device.CanCombineFormsInDeck)
{
_renderableForms = Page.ActiveForm.GetLinkedForms(OptimumPageWeight);
Debug.Assert(_renderableForms != null, "_renderableForms is null");
}
else
{
_renderableForms = new ArrayList();
_renderableForms.Add(Page.ActiveForm);
}
foreach (Form form in _renderableForms)
{
RenderForm(writer, form);
}
writer.RenderExtraCards();
writer.WriteEndTag("wml");
writer.EndFile();
writer.EndResponse();
}
///
protected virtual void RenderForm(WmlMobileTextWriter writer, Form form)
{
writer.AnalyzeMode = true;
form.RenderControl(writer);
writer.AnalyzeMode = false;
form.RenderControl(writer);
}
// Should be called by controls/adapters after _renderableForms is
// set in this.Render()
///
public virtual bool IsFormRendered(Form form)
{
Debug.Assert(_renderableForms != null, "_renderableForms is null");
return _renderableForms.Contains(form);
}
private void RenderXmlHeader(HtmlTextWriter writer)
{
writer.Write(_headerBegin);
String charset = Page.Response.Charset;
if (charset != null && charset.Length > 0 &&
String.Compare(charset, "utf-8", StringComparison.OrdinalIgnoreCase) != 0)
{
writer.Write(String.Format(CultureInfo.InvariantCulture, _headerEncoding, charset));
}
writer.Write(_headerEnd);
}
///
public virtual bool RendersMultipleForms()
{
return _renderableForms.Count > 1;
}
// '+' <-> '-'
// '=' <-> '.'
// '/' <-> '*'
private static readonly char[] _specialEncodingChars = new char[64]
{
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '/', '-', '\0', '+', '=', '*',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '.', '\0', '\0',
};
internal String EncodeSpecialViewState(String pageState)
{
char[] viewstate = pageState.ToCharArray();
for (int i = 0; i < viewstate.Length; i++)
{
char currentChar = viewstate[i];
// Only check character replacement if within the range
if (currentChar < _specialEncodingChars.Length)
{
char encodingChar = _specialEncodingChars[currentChar];
if (encodingChar != '\0')
{
viewstate[i] = encodingChar;
}
}
}
return new String(viewstate);
}
private static readonly Encoding UTF8Encoding = Encoding.GetEncoding("UTF-8");
private bool _requiresUTF8ContentEncoding = false;
private bool _haveRequiresUTF8ContentEncoding = false;
private bool RequiresUTF8ContentEncoding()
{
if (!_haveRequiresUTF8ContentEncoding)
{
String RequiresUTF8ContentEncodingString = Device["requiresUTF8ContentEncoding"];
if (RequiresUTF8ContentEncodingString == null)
{
if (IsKDDIPhone())
{
_requiresUTF8ContentEncoding = true;
}
}
else
{
_requiresUTF8ContentEncoding = Convert.ToBoolean(RequiresUTF8ContentEncodingString, CultureInfo.InvariantCulture);
}
_haveRequiresUTF8ContentEncoding = true;
}
return _requiresUTF8ContentEncoding;
}
private bool _requiresValueAttributeInInputTag = false;
private bool _haveRequiresValueAttributeInInputTag = false;
internal bool RequiresValueAttributeInInputTag()
{
if (!_haveRequiresValueAttributeInInputTag)
{
String RequiresValueAttributeInInputTagString = Device["requiresValueAttributeInInputTag"];
if (RequiresValueAttributeInInputTagString == null)
{
if (IsKDDIPhone())
{
_requiresValueAttributeInInputTag = true;
}
}
else
{
_requiresValueAttributeInInputTag = Convert.ToBoolean(RequiresValueAttributeInInputTagString, CultureInfo.InvariantCulture);
}
_haveRequiresValueAttributeInInputTag = true;
}
return _requiresValueAttributeInInputTag;
}
private bool _isKDDIPhone = false;
private bool _haveIsKDDIPhone = false;
internal bool IsKDDIPhone()
{
if (!_haveIsKDDIPhone)
{
if (Device.Browser == "Phone.com" &&
Device.MajorVersion == 3 &&
Device.MinorVersion < 0.3)
{
String charset = Page.Request.Headers["x-up-devcap-charset"];
if (charset != null &&
String.Compare(charset, "Shift_JIS", StringComparison.OrdinalIgnoreCase) == 0)
{
_isKDDIPhone = true;
}
}
_haveIsKDDIPhone = true;
}
return _isKDDIPhone;
}
}
}