Merge pull request #2098 from evincarofautumn/fix-gchandle-assert
[mono.git] / mcs / class / System.Web / System.Web / SiteMapNode.cs
index 545fa2f9f1b3aa4e98e15d6acb105364d8336183..b1660b8752c2365c8ff4981c3682921cafbd764c 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;
+using System.Security.Principal;
 
 namespace System.Web {
        public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
        
-               private SiteMapNode () {}
-               
-               public SiteMapNode (SiteMapProvider provider, string key) : this (provider, key, null, null, null, null, null, null) {}
-               public SiteMapNode (SiteMapProvider provider, string key, string url) : this (provider, key, url, null, null, null, null, null) {}
-               public SiteMapNode (SiteMapProvider provider, string key, string url, string title) : this (provider, key, url, title, 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) {}
-               public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection resourceKeys)
+               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;
@@ -57,7 +67,8 @@ namespace System.Web {
                        this.description = description;
                        this.roles = roles;
                        this.attributes = attributes;
-                       this.resourceKeys = resourceKeys;
+                       this.resourceKeys = explicitResourceKeys;
+                       this.resourceKey = implicitResourceKey;
                }
 
                public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
@@ -70,10 +81,9 @@ namespace System.Web {
                        return new SiteMapHierarchicalDataSourceView (this);
                }
                
-               [MonoTODO]
-               public bool IsAccessibleToUser (System.Web.HttpContext ctx)
+               public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
                {
-                       throw new NotImplementedException ();
+                       return provider.IsAccessibleToUser (ctx, this);
                }
                
                public override string ToString()
@@ -81,7 +91,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 ()
                {
@@ -95,9 +110,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);
                        }
@@ -160,11 +175,25 @@ namespace System.Web {
                                parent = value;
                        }
                }
-               
-               [MonoTODO ("set")]
+
                public virtual SiteMapNodeCollection ChildNodes {
-                       get { return provider.GetChildNodes (this); } 
-                       set { CheckWritable (); }
+                       get {
+                               if (provider.SecurityTrimmingEnabled) {
+                                       IPrincipal p = HttpContext.Current.User;
+                                       if ((user == null && user != p) || user != null && user != p) {
+                                               user = p;
+                                               childNodes = provider.GetChildNodes (this);
+                                       }
+                               } else if (childNodes == null) {
+                                       childNodes = provider.GetChildNodes (this);
+                               }
+                               return childNodes;
+                       } 
+                       set {
+                               CheckWritable ();
+                               user = null;
+                               childNodes = value;
+                       }
                }
 
                public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; }  }
@@ -178,31 +207,67 @@ namespace System.Web {
                        }
                }
                
-               [MonoTODO]
-               protected string GetExplicitResourceString (string attributeName, bool b)
+               protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
                {
-                       return null;
+                       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;
                }
-               
-               [MonoTODO]
+
                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;
                }
                
-               [MonoTODO ("resource string?")]
-               public string this [string key]
+               public virtual string this [string key]
                {
                        get {
-                               string val = null;
                                if (provider.EnableLocalization) {
-                                       val = GetExplicitResourceString (key, true);
-                                       if (val == null) val = GetImplicitResourceString (key);
+                                       string val = GetImplicitResourceString (key);
+                                       if (val == null)
+                                               val = GetExplicitResourceString (key, null, true);
+                                       if (val != null)
+                                               return val;
                                }
-                               if (val != null) return null;
                                if (attributes != null) return attributes [key];
                                return null;
                        }
+                       set {
+                               CheckWritable ();
+                               if (attributes == null) attributes = new NameValueCollection ();
+                               attributes [key] = value;
+                       }
                }
                
                object ICloneable.Clone ()
@@ -223,13 +288,15 @@ namespace System.Web {
                        node.url = url;
                        node.title = title;
                        node.description = description;
-                       node.roles = new ArrayList (roles);
-                       node.attributes = new NameValueCollection (attributes);
+                       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;
@@ -241,19 +308,30 @@ namespace System.Web {
                                        node.description != description) {
                                return false;
                        }
-                       
-                       if ((roles == null || node.roles == null) && (roles != node.roles)) return false;
-                       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) && (attributes != node.attributes)) return false;
-                       if (attributes.Count != node.attributes.Count) return false;
+                       if (roles == null || node.roles == null) {
+                               if (roles != node.roles)
+                                       return false;
+                       }
+                       else {
+                               if (roles.Count != node.roles.Count)
+                                       return false;
 
-                       foreach (string k in attributes)
-                               if (attributes [k] != node.attributes [k]) 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;
                }
                
@@ -270,19 +348,42 @@ namespace System.Web {
                                
                #region Field Accessors
                
-               public virtual NameValueCollection Attributes {
+               protected NameValueCollection Attributes {
                        get { return attributes; } 
                        set { CheckWritable (); attributes = value; }
                }
                
+               [Localizable (true)]
                public virtual string Description {
-                       get { return description != null ? 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 { return title != null ? 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; }
                }
                
@@ -291,7 +392,7 @@ namespace System.Web {
                        set { CheckWritable (); url = value; }
                }
                
-               public virtual IList Roles {
+               public IList Roles {
                        get { return roles; }
                        set { CheckWritable (); roles = value; }
                }
@@ -301,10 +402,13 @@ namespace System.Web {
                        set { readOnly = value; }
                }
                
-               [MonoTODO ("Do somethig with this")]
                public string ResourceKey {
                        get { return resourceKey; }
-                       set { resourceKey = value; }
+                       set {
+                               if (ReadOnly)
+                                       throw new InvalidOperationException ("The node is read-only.");
+                               resourceKey = value;
+                       }
                }
                
                public string Key { get { return key; } }
@@ -314,10 +418,9 @@ namespace System.Web {
                
                #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; } }
@@ -344,9 +447,11 @@ namespace System.Web {
                bool readOnly;
                string resourceKey;
                SiteMapNode parent;
+               SiteMapNodeCollection childNodes;
+               IPrincipal user;
                #endregion
                
        }
 }
-#endif
+