Call SetEndOfSendNotification
[mono.git] / mcs / class / System.Web / System.Web / SiteMapNode.cs
index 3904264185e64b56f4da1c9d37305b40bab9c2ef..e1fb52011f9f6017f96a3eb4f6ee86fbda1e9d2f 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 using System.Collections;
 using System.Collections.Specialized;
 using System.Text;
 using System.Web.UI;
 using System.Web.UI.WebControls;
+using System.ComponentModel;
+using System.Resources;
 
 namespace System.Web {
-       public class SiteMapNode : IHierarchyData, INavigateUIData {
-               public SiteMapNode (ISiteMapProvider provider) : this (provider, null, null, null, null, null, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url) : this (provider, url, null, null, null, null, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url, string title) : this (provider, url, title, null, null, null, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url, string title, string description) : this (provider, url, title, description, null, null, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url, string title, string description, IList keywords) : this (provider, url, title, description, keywords, null, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url, string title, string description, IList keywords, IList roles) : this (provider, url, title, description, keywords, roles, null) {}
-               public SiteMapNode (ISiteMapProvider provider, string url, string title, string description, IList keywords, IList roles, NameValueCollection attributes)
+       public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
+       
+               SiteMapNode () {}
+               
+               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)
                {
                        if (provider == null)
                                throw new ArgumentNullException ("provider");
+                       if (key == null)
+                               throw new ArgumentNullException ("key");
                        
                        this.provider = provider;
+                       this.key = key;
                        this.url = url;
                        this.title = title;
                        this.description = description;
-                       this.keywords = keywords;
                        this.roles = roles;
                        this.attributes = attributes;
+                       this.resourceKeys = explicitResourceKeys;
+                       this.resourceKey = implicitResourceKey;
                }
 
-               public SiteMapDataSourceView GetDataSourceView ()
+               public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
+               {
+                       return new SiteMapDataSourceView (owner, viewName, this);
+               }
+               
+               public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
+               {
+                       return new SiteMapHierarchicalDataSourceView (this);
+               }
+               
+               public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
                {
-                       return new SiteMapDataSourceView (this);
+                       return provider.IsAccessibleToUser (ctx, this);
                }
                
                public override string ToString()
@@ -67,7 +90,12 @@ namespace System.Web {
                        return Title;
                }
 
-               public virtual bool HasChildNodes { get { return ChildNodes != null && ChildNodes.Count != 0; } }
+               public virtual bool HasChildNodes {
+                       get {
+                               SiteMapNodeCollection childNodes = ChildNodes;
+                               return childNodes != null && childNodes.Count > 0;
+                       }
+               }
 
                public SiteMapNodeCollection GetAllNodes ()
                {
@@ -81,9 +109,9 @@ namespace System.Web {
                void GetAllNodesRecursive(SiteMapNodeCollection c)
                {
                        SiteMapNodeCollection childNodes = this.ChildNodes;
-                       
-                       if (childNodes.Count > 0) {
-                               childNodes.AddRange (childNodes);
+
+                       if (childNodes != null && childNodes.Count > 0) {
+                               c.AddRange (childNodes);
                                foreach (SiteMapNode n in childNodes)
                                        n.GetAllNodesRecursive (c);
                        }
@@ -128,19 +156,36 @@ namespace System.Web {
                
                public virtual SiteMapNode ParentNode {
                        get {
-                               ISiteMapProvider provider = this.provider;
+                               if (parent != null) return parent;
+                               
+                               SiteMapProvider provider = this.provider;
                                
                                do {
-                                       SiteMapNode n = provider.GetParentNode (this);
-                                       if (n != null)
-                                               return n
+                                       parent = provider.GetParentNode (this);
+                                       if (parent != null)
+                                               return parent
                                        
                                        provider = provider.ParentProvider;
                                } while (provider != null);
                                return null;
                        }
+                       set {
+                               CheckWritable ();
+                               parent = value;
+                       }
+               }
+
+               public virtual SiteMapNodeCollection ChildNodes {
+                       get {
+                               if (childNodes == null)
+                                       childNodes = provider.GetChildNodes (this);
+                               return childNodes;
+                       } 
+                       set {
+                               CheckWritable ();
+                               childNodes = value;
+                       }
                }
-               public virtual SiteMapNodeCollection ChildNodes { get { return provider.GetChildNodes (this); } }
 
                public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; }  }
                
@@ -153,25 +198,222 @@ namespace System.Web {
                        }
                }
                
+               protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
+               {
+                       if (attributeName == null)
+                               throw new ArgumentNullException ("attributeName");
+                       
+                       if (resourceKeys != null){
+                               string[] values = resourceKeys.GetValues (attributeName);
+                               if (values != null && values.Length == 2) {
+                                       try {
+                                               object o = HttpContext.GetGlobalResourceObject (values [0], values [1]);
+                                               if (o is string)
+                                                       return (string) o;
+                                       }
+                                       catch (MissingManifestResourceException) {
+                                       }
+
+                                       if (throwIfNotFound && defaultValue == null)
+                                               throw new InvalidOperationException (String.Format ("The resource object with classname '{0}' and key '{1}' was not found.", values [0], values [1]));
+                               }
+                       }
+
+                       return defaultValue;
+               }
+
+               protected string GetImplicitResourceString (string attributeName)
+               {
+                       if (attributeName == null)
+                               throw new ArgumentNullException ("attributeName");
+
+                       string resourceKey = ResourceKey;
+                       if (String.IsNullOrEmpty (resourceKey))
+                               return null;
+
+                       try {
+                               object o = HttpContext.GetGlobalResourceObject (provider.ResourceKey, resourceKey + "." + attributeName);
+                               if (o is string)
+                                       return (string) o;
+                       } catch (MissingManifestResourceException) {
+                       }
+                       
+                       return null;
+               }
+               
+               public virtual string this [string key]
+               {
+                       get {
+                               if (provider.EnableLocalization) {
+                                       string val = GetImplicitResourceString (key);
+                                       if (val == null)
+                                               val = GetExplicitResourceString (key, null, true);
+                                       if (val != null)
+                                               return val;
+                               }
+                               if (attributes != null) return attributes [key];
+                               return null;
+                       }
+                       set {
+                               CheckWritable ();
+                               if (attributes == null) attributes = new NameValueCollection ();
+                               attributes [key] = value;
+                       }
+               }
+               
+               object ICloneable.Clone ()
+               {
+                       return Clone (false);
+               }
+               
+               public virtual SiteMapNode Clone ()
+               {
+                       return Clone (false);
+               }
+               
+               public virtual SiteMapNode Clone (bool cloneParentNodes)
+               {
+                       SiteMapNode node = new SiteMapNode ();
+                       node.provider = provider;
+                       node.key = key;
+                       node.url = url;
+                       node.title = title;
+                       node.description = description;
+                       if (roles != null)
+                               node.roles = new ArrayList (roles);
+                       if (attributes != null)
+                               node.attributes = new NameValueCollection (attributes);
+                       if (cloneParentNodes && ParentNode != null)
+                               node.parent = (SiteMapNode) ParentNode.Clone (true);
+                       return node;
+               }
+                               
+               public override bool Equals (object ob)
+               {
+                       SiteMapNode node = ob as SiteMapNode;
+                       if (node == null) return false;
+                       
+                       if (node.key != key ||
+                                       node.url != url ||
+                                       node.title != title ||
+                                       node.description != description) {
+                               return false;
+                       }
+
+                       if (roles == null || node.roles == null) {
+                               if (roles != node.roles)
+                                       return false;
+                       }
+                       else {
+                               if (roles.Count != node.roles.Count)
+                                       return false;
+
+                               foreach (object role in roles)
+                                       if (!node.roles.Contains (role)) return false;
+                       }
+                       if (attributes == null || node.attributes == null) {
+                               if (attributes != node.attributes)
+                                       return false;
+                       }
+                       else {
+                               if (attributes.Count != node.attributes.Count)
+                                       return false;
+
+                               foreach (string k in attributes)
+                                       if (attributes[k] != node.attributes[k])
+                                               return false;
+                       }
+                       return true;
+               }
+               
+               public override int GetHashCode ()
+               {
+                       return (key + url + title + description).GetHashCode ();
+               }
+               
+               void CheckWritable ()
+               {
+                       if (readOnly)
+                               throw new InvalidOperationException ("Can't modify read-only node");
+               }
+                               
                #region Field Accessors
-               public virtual NameValueCollection Attributes { get { return attributes; } }
-               public virtual string Description { get { return description != null ? description : ""; } }
-               public virtual IList Keywords { get { return keywords; } }
-               public virtual string Title { get { return title != null ? title : ""; } }
-               public virtual string Url { get { return url != null ? url : ""; } }
-               public virtual IList Roles { get { return roles; } }
+               
+               protected NameValueCollection Attributes {
+                       get { return attributes; } 
+                       set { CheckWritable (); attributes = value; }
+               }
+               
+               [Localizable (true)]
+               public virtual string Description {
+                       get {
+                               string ret = null;
+                               
+                               if (provider.EnableLocalization) {
+                                       ret = GetImplicitResourceString ("description");
+                                       if (ret == null)
+                                               ret = GetExplicitResourceString ("description", description, true);
+                               } else
+                                       ret = description;
+                               
+                               return ret != null ? ret : String.Empty;
+                       }
+                       set { CheckWritable (); description = value; }
+               }
+               
+               [LocalizableAttribute (true)]
+               public virtual string Title {
+                       get {
+                               string ret = null;
+
+                               if (provider.EnableLocalization) {
+                                       ret = GetImplicitResourceString ("title");
+                                       if (ret == null)
+                                               ret = GetExplicitResourceString ("title", title, true);
+                               } else
+                                       ret = title;
+                               
+                               return ret != null ? ret : String.Empty;
+                       }
+                       set { CheckWritable (); title = value; }
+               }
+               
+               public virtual string Url {
+                       get { return url != null ? url : ""; }
+                       set { CheckWritable (); url = value; }
+               }
+               
+               public IList Roles {
+                       get { return roles; }
+                       set { CheckWritable (); roles = value; }
+               }
+               
+               public bool ReadOnly {
+                       get { return readOnly; }
+                       set { readOnly = value; }
+               }
+               
+               public string ResourceKey {
+                       get { return resourceKey; }
+                       set {
+                               if (ReadOnly)
+                                       throw new InvalidOperationException ("The node is read-only.");
+                               resourceKey = value;
+                       }
+               }
+               
+               public string Key { get { return key; } }
+               public SiteMapProvider Provider { get { return provider; } }
+               
                #endregion
                
                #region INavigateUIData
                IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
-               IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetParent ()
+               IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
                {
-                       if (ParentNode == null) return null; 
-                       return SiteMapNodeCollection.ReadOnly (new SiteMapNodeCollection (ParentNode));
+                       return ParentNode;
                }
-               
 
-               
                bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
                object System.Web.UI.IHierarchyData.Item { get { return this; } }
                string System.Web.UI.IHierarchyData.Path { get { return Url; } }
@@ -185,16 +427,21 @@ namespace System.Web {
                #endregion
 
                #region Fields
-               ISiteMapProvider provider;
+               SiteMapProvider provider;
+               string key;
                string url;
                string title;
                string description;
-               IList keywords;
                IList roles;
                NameValueCollection attributes;
+               NameValueCollection resourceKeys;
+               bool readOnly;
+               string resourceKey;
+               SiteMapNode parent;
+               SiteMapNodeCollection childNodes;
                #endregion
                
        }
 }
-#endif
+