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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
|
//------------------------------------------------------------------------------
// <copyright file="MobileControlPersister.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.Design.MobileControls
{
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using AttributeCollection = System.Web.UI.AttributeCollection;
using System.Globalization;
/// <summary>
/// <para>
/// Provides helper functions used in persisting Controls.
/// </para>
/// </summary>
[
System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand,
Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
[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.")]
internal sealed class MobileControlPersister
{
/// <summary>
/// We don't want instances of this class to be created, so mark
/// the constructor as private.
/// </summary>
private MobileControlPersister() {
}
/// <summary>
/// <para>
/// Gets the delarative type for the
/// specified type.
/// </para>
/// </summary>
/// <param name='type'>
/// The type of the declarator.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
private static string GetDeclarativeType(Type type, IDesignerHost host) {
Debug.Assert(host != null, "Need an IDesignerHost to create declarative type names");
string declarativeType = null;
if (host != null) {
IWebFormReferenceManager refMgr =
(IWebFormReferenceManager)host.GetService(typeof(IWebFormReferenceManager));
Debug.Assert(refMgr != null, "Did not get back IWebFormReferenceManager service from host.");
if (refMgr != null) {
string tagPrefix = refMgr.GetTagPrefix(type);
if ((tagPrefix != null) && (tagPrefix.Length != 0)) {
declarativeType = tagPrefix + ":" + type.Name;
}
}
}
if (declarativeType == null)
{
/* Begin AUI 7201 */
/* Original declarativeType = type.FullName; */
if (type == typeof(System.Web.UI.MobileControls.Style))
{
declarativeType = type.Name;
}
else
{
declarativeType = type.FullName;
}
/* End AUI 7201 */
}
return declarativeType;
}
/// <summary>
/// <para>
/// Persists a collection property.
/// </para>
/// </summary>
/// <param name='persistMode'>
/// The persistance mode to use.
/// </param>
/// <param name=' sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name=' propDesc'>
/// A property descriptor for the collection property.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
private static void PersistCollectionProperty(TextWriter sw, object component, PropertyDescriptor propDesc, PersistenceMode persistMode, IDesignerHost host) {
Debug.Assert(typeof(ICollection).IsAssignableFrom(propDesc.PropertyType),
"Invalid collection property : " + propDesc.Name);
ICollection propValue = (ICollection)propDesc.GetValue(component);
if ((propValue == null) || (propValue.Count == 0))
return;
// Begin AUI Change #3785
// Original: sw.WriteLine();
if (!(component is DeviceSpecific))
{
sw.WriteLine();
}
// End of Change #3785
if (persistMode == PersistenceMode.InnerProperty) {
sw.Write('<');
sw.Write(propDesc.Name);
sw.WriteLine('>');
}
IEnumerator e = propValue.GetEnumerator();
while (e.MoveNext()) {
object collItem = e.Current;
// Begin of AUI Change
//string itemTypeName = GetDeclarativeType(collItem.GetType(), host);
string itemTypeName;
// AUI : To fix Hashtable objects used in Mobile Controls, only persist the value part
if (collItem is DictionaryEntry)
{
collItem = ((DictionaryEntry)collItem).Value;
}
// AUI : First check if the control already has a Default Persist Name,
// if not, use the Type as its name
PersistNameAttribute pna =
(PersistNameAttribute)TypeDescriptor.GetAttributes(collItem.GetType())[typeof(PersistNameAttribute)];
// attribute should returns default value if it's null.
// this is unlikely to happen, but just to be on the safe side.
Debug.Assert (pna != null, "PersistNameAttribute returns null!");
string persistName = (string)pna.Name;
if (persistName != null && persistName.Length > 0)
{
itemTypeName = persistName;
}
/* AUI Change #3911 */
/* Original : else if (collItem is Control) */
else if (collItem is Control || collItem.GetType() == typeof(System.Web.UI.MobileControls.Style))
{
itemTypeName = GetDeclarativeType(collItem.GetType(), host);
}
else
{
itemTypeName = collItem.GetType().Name;
}
// End of AUI Change
sw.Write("<");
sw.Write(itemTypeName);
PersistAttributes(sw, collItem, String.Empty, null);
sw.Write(">");
if (collItem is Control) {
PersistChildrenAttribute pca =
(PersistChildrenAttribute)TypeDescriptor.GetAttributes(collItem.GetType())[typeof(PersistChildrenAttribute)];
if (pca.Persist == true) {
// asurt 106696: ensure the parent control's visibility is set to true.
Control parentControl = (Control)collItem;
if (parentControl.HasControls())
{
bool oldVisible = parentControl.Visible;
try
{
parentControl.Visible = true;
PersistChildControls(sw, parentControl.Controls, host);
}
finally
{
parentControl.Visible = oldVisible;
}
}
}
else {
PersistInnerProperties(sw, collItem, host);
}
}
else {
PersistInnerProperties(sw, collItem, host);
}
sw.Write("</");
sw.Write(itemTypeName);
sw.WriteLine(">");
}
if (persistMode == PersistenceMode.InnerProperty) {
sw.Write("</");
sw.Write(propDesc.Name);
sw.WriteLine('>');
}
}
/// <summary>
/// <para>
/// Persists a complex property.
/// </para>
/// </summary>
/// <param name='persistMode'>
/// The persistance mode to use.
/// </param>
/// <param name=' sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name=' propDesc'>
/// A property descriptor for the complex property.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
private static void PersistComplexProperty(TextWriter sw, object component, PropertyDescriptor propDesc, IDesignerHost host) {
object propValue = propDesc.GetValue(component);
if (propValue == null) {
return;
}
StringWriter tagProps = new StringWriter(CultureInfo.InvariantCulture);
StringWriter innerProps = new StringWriter(CultureInfo.InvariantCulture);
PersistAttributes(tagProps, propValue, String.Empty, null);
PersistInnerProperties(innerProps, propValue, host);
// the rule here is that if a complex property has all its subproperties
// in the default state, then it itself is in the default state.
// When this is the case, there shouldn't be any tag properties or inner properties
if ((tagProps.GetStringBuilder().Length != 0) ||
(innerProps.GetStringBuilder().Length != 0)) {
sw.WriteLine();
sw.Write('<');
sw.Write(propDesc.Name);
sw.Write(tagProps.ToString());
sw.WriteLine(">");
string innerPropsString = innerProps.ToString();
sw.Write(innerPropsString);
if (innerPropsString.Length != 0) {
// Begin AUI Change #3785
// Original: sw.WriteLine();
if (!propDesc.Name.Equals("DeviceSpecific"))
{
sw.WriteLine();
}
// End AUI Change #3785
}
sw.Write("</");
sw.Write(propDesc.Name);
sw.WriteLine('>');
}
}
/// <summary>
/// <para>
/// Persists the data bindings of the specified control using the specified
/// string writer.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' control'>
/// The control to use.
/// </param>
private static void PersistDataBindings(TextWriter sw, Control control) {
DataBindingCollection bindings = ((IDataBindingsAccessor)control).DataBindings;
IEnumerator bindingEnum = bindings.GetEnumerator();
while (bindingEnum.MoveNext()) {
DataBinding db = (DataBinding)bindingEnum.Current;
string persistPropName = db.PropertyName.Replace('.', '-');
sw.Write(" ");
sw.Write(persistPropName);
sw.Write("='<%# ");
sw.Write(HttpUtility.HtmlEncode(db.Expression));
sw.Write(" %>'");
}
}
/// <overload>
/// <para>
/// Gets a string that can persist the inner properties of a control.
/// </para>
/// </overload>
/// <summary>
/// <para>
/// Gets a string that can persist the inner properties of a control.
/// </para>
/// </summary>
/// <param name='component'>
/// The component to persist.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
/// <returns>
/// <para>
/// A string that contains the persistable information about
/// the inner properties
/// of the control.
/// </para>
/// </returns>
internal static string PersistInnerProperties(object component, IDesignerHost host) {
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
PersistInnerProperties(sw, component, host);
return sw.ToString();
}
/// <summary>
/// <para>
/// Persists the inner properties of the control.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
internal static void PersistInnerProperties(TextWriter sw, object component, IDesignerHost host) {
PropertyDescriptorCollection propDescs = TypeDescriptor.GetProperties(component);
for (int i = 0; i < propDescs.Count; i++) {
// Only deal with inner attributes that need to be persisted
if (propDescs[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
continue;
PersistenceModeAttribute persistenceMode = (PersistenceModeAttribute)propDescs[i].Attributes[typeof(PersistenceModeAttribute)];
if (persistenceMode.Mode == PersistenceMode.Attribute) {
continue;
}
if (propDescs[i].PropertyType == typeof(string)) {
// String based property...
DataBindingCollection dataBindings = null;
if (component is IDataBindingsAccessor) {
dataBindings = ((IDataBindingsAccessor)component).DataBindings;
}
if (dataBindings == null || dataBindings[propDescs[i].Name] == null) {
PersistenceMode mode = persistenceMode.Mode;
if ((mode == PersistenceMode.InnerDefaultProperty) ||
(mode == PersistenceMode.EncodedInnerDefaultProperty)) {
PersistStringProperty(sw, component, propDescs[i], mode);
}
else {
Debug.Fail("Cannot persist inner string property marked with PersistenceMode.InnerProperty");
}
}
}
else if (typeof(ITemplate).IsAssignableFrom(propDescs[i].PropertyType)) {
// Template based property...
if (persistenceMode.Mode == PersistenceMode.InnerProperty) {
PersistTemplateProperty(sw, component, propDescs[i]);
}
else {
Debug.Fail("Cannot persist template property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
}
}
/* AUI change 03/21/01 */
else if (propDescs[i].DisplayName.Equals("Templates") &&
component is DeviceSpecificChoice &&
typeof(IDictionary).IsAssignableFrom(propDescs[i].PropertyType)) {
IDictionary templateCollection = (IDictionary)propDescs[i].GetValue(component);
foreach (String templateName in templateCollection.Keys) {
ITemplate template = (ITemplate)templateCollection[templateName];
PersistTemplateProperty(sw, templateName, template);
}
}
/* End of AUI change*/
else if (typeof(ICollection).IsAssignableFrom(propDescs[i].PropertyType)) {
// Collection based property...
if ((persistenceMode.Mode == PersistenceMode.InnerProperty) ||
(persistenceMode.Mode == PersistenceMode.InnerDefaultProperty)) {
PersistCollectionProperty(sw, component, propDescs[i], persistenceMode.Mode, host);
}
else {
Debug.Fail("Cannot persist collection property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty or PersistenceMode.InnerDefaultProperty");
}
}
else {
// Other complex property...
if (persistenceMode.Mode == PersistenceMode.InnerProperty) {
PersistComplexProperty(sw, component, propDescs[i], host);
}
else {
Debug.Fail("Cannot persist complex property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
}
}
}
}
/// <summary>
/// <para>
/// Persists the properties of a
/// string.
/// </para>
/// </summary>
/// <param name='persistMode'>
/// The persistance mode to use.
/// </param>
/// <param name=' sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name=' propDesc'>
/// A property descriptor for the string properties.
/// </param>
private static void PersistStringProperty(TextWriter sw, object component, PropertyDescriptor propDesc, PersistenceMode mode) {
Debug.Assert(propDesc.PropertyType == typeof(string),
"Invalid string property : " + propDesc.Name);
Debug.Assert((mode == PersistenceMode.InnerDefaultProperty) || (mode == PersistenceMode.EncodedInnerDefaultProperty),
"Inner string properties must be marked as either InnerDefaultProperty or EncodedInnerDefaultProperty");
object propValue = propDesc.GetValue(component);
if (propValue == null) {
return;
}
if (mode == PersistenceMode.InnerDefaultProperty) {
sw.Write((string)propValue);
}
else {
HttpUtility.HtmlEncode((string)propValue, sw);
}
}
/// <summary>
/// <para>
/// Persists the properties of a tag.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name=' prefix'>
/// The prefix to store.
/// </param>
/// <param name=' propDesc'>
/// A property descriptor for the tag properties.
/// </param>
private static void PersistAttributes(TextWriter sw, object component, string prefix, PropertyDescriptor propDesc) {
PropertyDescriptorCollection properties;
string persistPrefix = String.Empty;
object value = component;
if (propDesc != null) {
value = propDesc.GetValue(component);
properties = TypeDescriptor.GetProperties(propDesc.PropertyType,
new Attribute[] {
PersistenceModeAttribute.Attribute
});
}
else {
properties = TypeDescriptor.GetProperties(component,
new Attribute[] {
PersistenceModeAttribute.Attribute
});
}
if (value == null)
return;
if (prefix.Length != 0)
persistPrefix = prefix + "-";
DataBindingCollection dataBindings = null;
bool isControl = (component is Control);
if ((component is IDataBindingsAccessor))
dataBindings = ((IDataBindingsAccessor)component).DataBindings;
if (component is DeviceSpecificChoice)
{
properties = properties.Sort(new String[] {"Filter"});
}
for (int i = 0; i < properties.Count; i++) {
// Skip properties that are hidden to the serializer
if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Hidden) {
continue;
}
// Skip design-time only properties such as DefaultModifiers and Name
DesignOnlyAttribute doAttr = (DesignOnlyAttribute)properties[i].Attributes[typeof(DesignOnlyAttribute)];
if ((doAttr != null) && doAttr.IsDesignOnly) {
continue;
}
string propName = properties[i].Name;
Type propType = properties[i].PropertyType;
object obj = properties[i].GetValue(value);
if (obj == null)
continue;
DefaultValueAttribute defValAttr =
(DefaultValueAttribute)properties[i].Attributes[typeof(DefaultValueAttribute)];
if ((defValAttr != null) && (obj.Equals(defValAttr.Value)))
continue;
string persistName = propName;
/* AUI Change 3876 -- Change is taken out because of 4347
if (component is DeviceSpecificChoice && persistName.Equals("Xmlns"))
{
persistName = "xmlns";
}
End of Change */
if (prefix.Length != 0)
persistName = persistPrefix + persistName;
PropertyDescriptorCollection subProps = null;
if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Content) {
subProps = TypeDescriptor.GetProperties(propType);
}
if ((subProps == null) || (subProps.Count == 0)) {
string persistValue = null;
//
DataBinding db = null;
if (dataBindings != null)
db = dataBindings[persistName.Replace('.', '-')];
if (db == null) {
if (propType.IsEnum) {
persistValue = Enum.Format(propType, obj, "G");
}
else if (propType == typeof(string)) {
persistValue = HttpUtility.HtmlEncode(obj.ToString());
}
else {
TypeConverter converter = properties[i].Converter;
if (converter != null) {
persistValue = converter.ConvertToInvariantString(null, obj);
}
else {
persistValue = obj.ToString();
}
persistValue = HttpUtility.HtmlEncode(persistValue);
}
if ((persistValue == null) ||
(persistValue.Equals("NotSet")) ||
(propType.IsArray && (persistValue.Length == 0)))
continue;
sw.Write(" ");
sw.Write(persistName);
sw.Write("=\"");
sw.Write(persistValue);
sw.Write("\"");
}
}
else {
/*
* This will force all ListDictionary properties with DesignerSerializationVisibility.Content atttribute be
* persisted as a series of attributes eg. <PropertyFoo Key1="Value1" Key2="Value2" ... />. The
* WebControlPersistor is not able to handle this case and will return undesired results.
*/
// AUI Change to handle DeviceSpecificChoice.Contents
if (obj is ListDictionary)
{
IDictionaryEnumerator enumerator = ((ListDictionary)obj).GetEnumerator ();
String persistValue = null;
while (enumerator.MoveNext ())
{
propName = enumerator.Key as String;
persistValue = enumerator.Value as String;
Debug.Assert (propName != null,
"Non-string key in DeviceSpecificChoice Contents.");
if ((propName.Length == 0) ||
(persistValue == null))
continue;
sw.Write(" ");
sw.Write(propName);
sw.Write("=\"");
HttpUtility.HtmlEncode((String)persistValue, sw);
sw.Write("\"");
}
}
else
{
// End of AUI Change
// there are sub properties, don't persist this object, but
// recursively persist the subproperties.
PersistAttributes(sw, obj, persistName, null);
}
}
}
// Persist all the databindings on this control
if (isControl) {
PersistDataBindings(sw, (Control)component);
AttributeCollection expandos = null;
if (component is WebControl) {
expandos = ((WebControl)component).Attributes;
}
else if (component is HtmlControl) {
expandos = ((HtmlControl)component).Attributes;
}
else if (component is UserControl)
{
expandos = ((UserControl)component).Attributes;
}
if (expandos != null) {
foreach (string key in expandos.Keys) {
sw.Write(" ");
sw.Write(key);
sw.Write("=\"");
sw.Write(expandos[key]);
sw.Write("\"");
}
}
}
}
/// <summary>
/// <para>
/// Persists a template property including the specified persistance mode,
/// string writer and property descriptor.
/// </para>
/// </summary>
/// <param name='persistMode'>
/// The persistence mode to use.
/// </param>
/// <param name=' sw'>
/// The string writer to use.
/// </param>
/// <param name=' component'>
/// The component to persist.
/// </param>
/// <param name=' propDesc'>
/// A property descriptor for the property.
/// </param>
/* AUI change 03/21/01 added support for persisting Template collection */
private static void PersistTemplateProperty(TextWriter sw, object component, PropertyDescriptor propDesc)
{
Debug.Assert(typeof(ITemplate).IsAssignableFrom(propDesc.PropertyType),
"Invalid template property : " + propDesc.Name);
ITemplate template = (ITemplate)propDesc.GetValue(component);
String templateName = propDesc.Name;
PersistTemplateProperty(sw, templateName, template);
}
/* AUI change 03/21/01 made the following code a seperate method */
private static void PersistTemplateProperty(TextWriter sw, String templateName, ITemplate template)
{
if (template == null) {
return;
}
//string templateContent = ((TemplateBuilder)template).Text;
string templateContent;
Debug.Assert(template is TemplateBuilder, "Unexpected ITemplate implementation.");
if (template is TemplateBuilder) {
templateContent = ((TemplateBuilder)template).Text;
}
else {
templateContent = String.Empty;
}
sw.WriteLine();
sw.Write('<');
// changed propDesc.Name to templateName
sw.Write(templateName);
sw.Write('>');
if (!templateContent.StartsWith("\r\n", StringComparison.Ordinal)) {
sw.WriteLine();
}
sw.Write(templateContent);
if (!templateContent.EndsWith("\r\n", StringComparison.Ordinal)) {
sw.WriteLine();
}
sw.Write("</");
// changed propDesc.Name to templateName
sw.Write(templateName);
sw.WriteLine('>');
}
/// <overload>
/// <para>
/// Gets a string that can
/// persist a control.
/// </para>
/// </overload>
/// <summary>
/// <para>
/// Gets a string that can
/// persist a control.
/// </para>
/// </summary>
/// <param name='control'>
/// The control to persist.
/// </param>
/// <returns>
/// <para>
/// A string that contains the persistable information about
/// the control.
/// </para>
/// </returns>
internal static string PersistControl(Control control) {
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
PersistControl(sw, control);
return sw.ToString();
}
/// <overload>
/// <para>
/// Returns a string that can
/// persist a control.
/// </para>
/// </overload>
/// <summary>
/// <para>
/// Returns a string that can
/// persist a control.
/// </para>
/// </summary>
/// <param name='control'>
/// The control to persist.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
/// <returns>
/// <para>
/// A string that contains the persistable information about
/// the control.
/// </para>
/// </returns>
internal static string PersistControl(Control control, IDesignerHost host) {
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
PersistControl(sw, control, host);
return sw.ToString();
}
/// <summary>
/// <para>
/// Persists a control using the
/// specified string writer.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' control'>
/// The control to persist.
/// </param>
internal static void PersistControl(TextWriter sw, Control control) {
if (control is LiteralControl) {
PersistLiteralControl(sw, (LiteralControl)control);
return;
}
if (control is DesignerDataBoundLiteralControl) {
PersistDataBoundLiteralControl(sw, (DesignerDataBoundLiteralControl)control);
return;
}
ISite site = control.Site;
if (site == null) {
IComponent baseComponent = (IComponent)control.Page;
Debug.Assert(baseComponent != null, "Control does not have its Page set!");
if (baseComponent != null) {
site = baseComponent.Site;
}
}
IDesignerHost host = null;
if (site != null) {
host = (IDesignerHost)site.GetService(typeof(IDesignerHost));
}
Debug.Assert(host != null, "Did not get a valid IDesignerHost reference. Expect persistence problems!");
PersistControl(sw, control, host);
}
/// <summary>
/// <para>
/// Persists a control using the
/// specified string writer.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' control'>
/// The control to persist.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
internal static void PersistControl(TextWriter sw, Control control, IDesignerHost host) {
// Literals and DataBoundLiterals must be handled specially, since they
// don't have a tag around them
if (control is LiteralControl) {
PersistLiteralControl(sw, (LiteralControl)control);
return;
}
if (control is DesignerDataBoundLiteralControl) {
PersistDataBoundLiteralControl(sw, (DesignerDataBoundLiteralControl)control);
return;
}
Debug.Assert(host != null, "Did not get a valid IDesignerHost reference. Expect persistence problems!");
string tagName = null;
bool isUserControl = false;
if (control is HtmlControl) {
tagName = ((HtmlControl)control).TagName;
}
else if (control is UserControl)
{
tagName = ((IUserControlDesignerAccessor)control).TagName;
Debug.Assert((tagName != null) && (tagName.Length != 0));
if (tagName.Length == 0)
{
// not enough information to go any further... no options, other than to throw this control out
return;
}
isUserControl = true;
}
else {
tagName = GetDeclarativeType(control.GetType(), host);
}
sw.Write('<');
sw.Write(tagName);
sw.Write(" runat=\"server\"");
PersistAttributes(sw, control, String.Empty, null);
sw.Write('>');
if (isUserControl == false)
{
PersistChildrenAttribute pca =
(PersistChildrenAttribute)TypeDescriptor.GetAttributes(control.GetType())[typeof(PersistChildrenAttribute)];
if (pca.Persist == true)
{
if (control.HasControls())
{
// asurt 106696: Ensure parent control's visibility is true.
bool oldVisible = control.Visible;
try
{
control.Visible = true;
PersistChildControls(sw, control.Controls, host);
}
finally
{
control.Visible = oldVisible;
}
}
}
else
{
// controls marked with LiteralContent == true shouldn't have
// children in their persisted form. They only build children
// collections at runtime.
PersistInnerProperties(sw, control, host);
}
}
else
{
string innerText = ((IUserControlDesignerAccessor)control).InnerText;
if ((innerText != null) && (innerText.Length != 0))
{
sw.Write(innerText);
}
}
sw.Write("</");
sw.Write(tagName);
sw.WriteLine('>');
}
/// <summary>
/// <para>
/// Persists the child controls of
/// the control using the specified string writer.
/// </para>
/// </summary>
/// <param name='sw'>
/// The string writer to use.
/// </param>
/// <param name=' controls'>
/// The control collection to persist.
/// </param>
/// <param name='host'>
/// The services interface exposed by the webforms designer.
/// </param>
private static void PersistChildControls(TextWriter sw, ControlCollection controls, IDesignerHost host) {
int children = controls.Count;
for (int i = 0; i < children; i++) {
PersistControl(sw, controls[i], host);
}
}
private static void PersistDataBoundLiteralControl(TextWriter sw, DesignerDataBoundLiteralControl control) {
Debug.Assert(((IDataBindingsAccessor)control).HasDataBindings == true);
DataBindingCollection bindings = ((IDataBindingsAccessor)control).DataBindings;
DataBinding textBinding = bindings["Text"];
Debug.Assert(textBinding != null, "Did not get a Text databinding from DesignerDataBoundLiteralControl");
if (textBinding != null) {
sw.Write("<%# ");
sw.Write(textBinding.Expression);
sw.Write(" %>");
}
}
private static void PersistLiteralControl(TextWriter sw, LiteralControl control) {
Debug.Assert(control.Text != null);
sw.Write(control.Text);
}
}
}
|