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
|
//------------------------------------------------------------------------------
// <copyright file="SiteMapNode.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* SiteMapNode class definition
*
* Copyright (c) 2002 Microsoft Corporation
*/
namespace System.Web {
using System;
using System.Configuration.Provider;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Resources;
using System.Security.Permissions;
using System.Web.Configuration;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Util;
/// <devdoc>
/// <para></para>
/// </devdoc>
public class SiteMapNode : ICloneable, IHierarchyData, INavigateUIData {
private static readonly string _siteMapNodeType = typeof(SiteMapNode).Name;
private SiteMapProvider _provider;
private bool _readonly;
private bool _parentNodeSet;
private bool _childNodesSet;
private VirtualPath _virtualPath;
private string _title;
private string _description;
private string _url;
private string _key;
private string _resourceKey;
private IList _roles;
private NameValueCollection _attributes;
private NameValueCollection _resourceKeys;
private SiteMapNode _parentNode;
private SiteMapNodeCollection _childNodes;
public SiteMapNode(SiteMapProvider provider, string key) :
this(provider, key, null, null, null, null, null, null, null) {
}
public SiteMapNode(SiteMapProvider provider, string key, string url) :
this(provider, key, url, null, null, null, null, null, null) {
}
public SiteMapNode(SiteMapProvider provider, string key, string url, string title) :
this(provider, key, url, title, null, null, null, null, null) {
}
public SiteMapNode(SiteMapProvider provider, string key, string url, string title, string description) :
this(provider, key, url, title, description, null, null, null, null) {
}
public SiteMapNode(SiteMapProvider provider, string key, string url, string title, string description,
IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys, string implicitResourceKey) {
_provider = provider;
_title = title;
_description = description;
_roles = roles;
_attributes = attributes;
_key = key;
_resourceKeys = explicitResourceKeys;
_resourceKey = implicitResourceKey;
if (url != null) {
_url = url.Trim();
}
_virtualPath = CreateVirtualPathFromUrl(_url);
if (_key == null) {
throw new ArgumentNullException("key");
}
if (_provider == null) {
throw new ArgumentNullException("provider");
}
}
protected NameValueCollection Attributes {
get {
return _attributes;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Attributes"));
}
_attributes = value;
}
}
// Access custom attributes.
public virtual string this[string key] {
get {
string text = null;
if (_attributes != null) {
text = _attributes[key];
}
if (_provider.EnableLocalization) {
// Try the implicit resource first
string localizedText = GetImplicitResourceString(key);
if (localizedText != null) {
return localizedText;
}
// If not found, try the explicit resource.
localizedText = GetExplicitResourceString(key, text, true);
if (localizedText != null) {
return localizedText;
}
}
return text;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Item"));
}
if (_attributes == null) {
_attributes = new NameValueCollection();
}
_attributes[key] = value;
}
}
public virtual SiteMapNodeCollection ChildNodes {
get {
if (_childNodesSet)
return _childNodes;
return _provider.GetChildNodes(this);
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "ChildNodes"));
}
_childNodes = value;
_childNodesSet = true;
}
}
[
Localizable(true)
]
public virtual string Description {
get {
if (_provider.EnableLocalization) {
string localizedText = GetImplicitResourceString("description");
if (localizedText != null) {
return localizedText;
}
localizedText = GetExplicitResourceString("description", _description, true);
if (localizedText != null) {
return localizedText;
}
}
return _description == null? String.Empty : _description;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Description"));
}
_description = value;
}
}
public string Key {
get {
return _key;
}
}
public virtual bool HasChildNodes {
get {
IList children = ChildNodes;
return children != null && children.Count > 0;
}
}
public virtual SiteMapNode NextSibling {
get {
IList siblings = SiblingNodes;
if (siblings == null) {
return null;
}
int index = siblings.IndexOf(this);
if (index >= 0 && index < siblings.Count - 1) {
return (SiteMapNode)siblings[index + 1];
}
return null;
}
}
// Get parent node. If not found in current provider, search recursively in parent providers.
public virtual SiteMapNode ParentNode {
get {
if (_parentNodeSet)
return _parentNode;
return _provider.GetParentNode(this);
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "ParentNode"));
}
_parentNode = value;
_parentNodeSet = true;
}
}
public virtual SiteMapNode PreviousSibling {
get {
IList siblings = SiblingNodes;
if (siblings == null) {
return null;
}
int index = siblings.IndexOf(this);
if (index > 0 && index <= siblings.Count - 1) {
return (SiteMapNode)siblings[index - 1];
}
return null;
}
}
public SiteMapProvider Provider {
get {
return _provider;
}
}
public bool ReadOnly {
get {
return _readonly;
}
set {
_readonly = value;
}
}
public String ResourceKey {
get {
return _resourceKey;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "ResourceKey"));
}
_resourceKey = value;
}
}
public IList Roles {
get {
return _roles;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Roles"));
}
_roles = value;
}
}
public virtual SiteMapNode RootNode {
get {
SiteMapNode root = _provider.RootProvider.RootNode;
if (root == null) {
String name = ((ProviderBase)_provider.RootProvider).Name;
throw new InvalidOperationException(SR.GetString(SR.SiteMapProvider_Invalid_RootNode, name));
}
return root;
}
}
private SiteMapNodeCollection SiblingNodes {
get {
SiteMapNode parent = ParentNode;
return parent == null? null : parent.ChildNodes;
}
}
[
Localizable(true)
]
public virtual string Title {
get {
if (_provider.EnableLocalization) {
string localizedText = GetImplicitResourceString("title");
if (localizedText != null) {
return localizedText;
}
localizedText = GetExplicitResourceString("title", _title, true);
if (localizedText != null) {
return localizedText;
}
}
return _title == null? String.Empty : _title;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Title"));
}
_title = value;
}
}
public virtual string Url {
get {
return _url == null? String.Empty : _url;
}
set {
if (_readonly) {
throw new InvalidOperationException(SR.GetString(SR.SiteMapNode_readonly, "Url"));
}
if (value != null) {
_url = value.Trim();
}
_virtualPath = CreateVirtualPathFromUrl(_url);
}
}
internal VirtualPath VirtualPath {
get {
return _virtualPath;
}
}
private VirtualPath CreateVirtualPathFromUrl(string url) {
if (String.IsNullOrEmpty(url)) {
return null;
}
if (!UrlPath.IsValidVirtualPathWithoutProtocol(url)) {
return null;
}
if (UrlPath.IsAbsolutePhysicalPath(url)) {
return null;
}
// Do not generate the virtualPath class at designtime.
if (HttpRuntime.AppDomainAppVirtualPath == null) {
return null;
}
if (UrlPath.IsRelativeUrl(url) && !UrlPath.IsAppRelativePath(url)) {
url = UrlPath.Combine(HttpRuntime.AppDomainAppVirtualPathString, url);
}
// Remove the query string from url so the path can be validated by Authorization module.
int queryStringIndex = url.IndexOf('?');
if (queryStringIndex != -1) {
url = url.Substring(0, queryStringIndex);
}
return VirtualPath.Create(url,
VirtualPathOptions.AllowAbsolutePath | VirtualPathOptions.AllowAppRelativePath);
}
public virtual SiteMapNode Clone() {
ArrayList newRoles = null;
NameValueCollection newAttributes = null;
NameValueCollection newResourceKeys = null;
if (_roles != null) {
newRoles = new ArrayList(_roles);
}
if (_attributes != null) {
newAttributes = new NameValueCollection(_attributes);
}
if (_resourceKeys != null) {
newResourceKeys = new NameValueCollection(_resourceKeys);
}
SiteMapNode newNode = new SiteMapNode(_provider, Key, Url, Title, Description, newRoles, newAttributes, newResourceKeys, _resourceKey);
return newNode;
}
public virtual SiteMapNode Clone(bool cloneParentNodes) {
SiteMapNode current = Clone();
if (cloneParentNodes) {
SiteMapNode node = current;
SiteMapNode parent = ParentNode;
while (parent != null) {
SiteMapNode cloneParent = parent.Clone();
node.ParentNode = cloneParent;
cloneParent.ChildNodes = new SiteMapNodeCollection(node);
parent = parent.ParentNode;
node = cloneParent;
}
}
return current;
}
public override bool Equals(object obj) {
SiteMapNode node = obj as SiteMapNode;
return node != null && (_key == node.Key) &&
(String.Equals(_url, node._url, StringComparison.OrdinalIgnoreCase));
}
public SiteMapNodeCollection GetAllNodes() {
SiteMapNodeCollection collection = new SiteMapNodeCollection();
GetAllNodesRecursive(collection);
return SiteMapNodeCollection.ReadOnly(collection);
}
private void GetAllNodesRecursive(SiteMapNodeCollection collection) {
SiteMapNodeCollection childNodes = this.ChildNodes;
if (childNodes != null && childNodes.Count > 0) {
collection.AddRange(childNodes);
foreach(SiteMapNode node in childNodes)
node.GetAllNodesRecursive(collection);
}
}
public SiteMapDataSourceView GetDataSourceView(SiteMapDataSource owner, string viewName) {
return new SiteMapDataSourceView(owner, viewName, this);
}
public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView() {
return new SiteMapHierarchicalDataSourceView(this);
}
// Helpe method to retrieve localized string based on attribute name
protected string GetExplicitResourceString(string attributeName, string defaultValue, bool throwIfNotFound) {
if (attributeName == null) {
throw new ArgumentNullException("attributeName");
}
string text = null;
if (_resourceKeys != null) {
string[] keys = _resourceKeys.GetValues(attributeName);
if (keys != null && keys.Length > 1) {
try {
text = ResourceExpressionBuilder.GetGlobalResourceObject(keys[0], keys[1]) as string;
}
catch (MissingManifestResourceException) {
if (defaultValue != null) {
return defaultValue;
}
}
if (text == null && throwIfNotFound) {
// throw if default value is not specified.
throw new InvalidOperationException(
SR.GetString(SR.Res_not_found_with_class_and_key, keys[0], keys[1])); ;
}
}
}
return text;
}
// Only use the key to get the hashcode since url can be changed and makes the objects mutable.
public override int GetHashCode() {
return _key.GetHashCode();
}
// Helper method to retrieve localized string based on attribute name
protected string GetImplicitResourceString(string attributeName) {
if (attributeName == null) {
throw new ArgumentNullException("attributeName");
}
string text = null;
if (!String.IsNullOrEmpty(_resourceKey)) {
try {
text = ResourceExpressionBuilder.GetGlobalResourceObject(Provider.ResourceKey, ResourceKey + "." + attributeName) as String;
}
catch { }
}
return text;
}
public virtual bool IsAccessibleToUser(HttpContext context) {
return _provider.IsAccessibleToUser(context, this);
}
public virtual bool IsDescendantOf(SiteMapNode node) {
SiteMapNode parent = ParentNode;
while (parent != null) {
if (parent.Equals(node)) {
return true;
}
parent = parent.ParentNode;
}
return false;
}
public override string ToString() {
return Title;
}
#region ICloneable implementation
/// <internalonly/>
object ICloneable.Clone() {
return Clone();
}
#endregion
#region IHierarchyData implementation
/// <internalonly/>
bool IHierarchyData.HasChildren {
get {
return HasChildNodes;
}
}
/// <internalonly/>
object IHierarchyData.Item {
get {
return this;
}
}
/// <internalonly/>
string IHierarchyData.Path {
get {
return Key;
}
}
/// <internalonly/>
string IHierarchyData.Type {
get {
return _siteMapNodeType;
}
}
/// <internalonly/>
IHierarchicalEnumerable IHierarchyData.GetChildren() {
return ChildNodes;
}
/// <internalonly/>
IHierarchyData IHierarchyData.GetParent() {
SiteMapNode parentNode = ParentNode;
if (parentNode == null)
return null;
return parentNode;
}
#endregion
#region INavigateUIData implementations
string INavigateUIData.Description {
get {
return Description;
}
}
/// <internalonly/>
string INavigateUIData.Name {
get {
return Title;
}
}
/// <internalonly/>
string INavigateUIData.NavigateUrl {
get {
return Url;
}
}
/// <internalonly/>
string INavigateUIData.Value {
get {
return Title;
}
}
#endregion
}
}
|