2001:11:09 Gaurav Vaish <gvaish@iitk.ac.in>
authorGaurav Vaish <gvaish@mono-cvs.ximian.com>
Fri, 9 Nov 2001 02:33:00 +0000 (02:33 -0000)
committerGaurav Vaish <gvaish@mono-cvs.ximian.com>
Fri, 9 Nov 2001 02:33:00 +0000 (02:33 -0000)
* UrlUtils.cs   Added the following functions:

             GetProtocol(string) -> Returns the protocol used (before ://)
             IsRootUrl(string)   -> Is the url supplied is RootUrl
             IsValidProtocol(string)
                                 -> Can the protocol represent a valid protocol
             RegionMatches(...)  -> Matches an inner part of a string

svn path=/trunk/mcs/; revision=1302

mcs/class/System.Web/System.Web.WebUtils/UrlUtils.cs [new file with mode: 0644]

diff --git a/mcs/class/System.Web/System.Web.WebUtils/UrlUtils.cs b/mcs/class/System.Web/System.Web.WebUtils/UrlUtils.cs
new file mode 100644 (file)
index 0000000..cdb21e9
--- /dev/null
@@ -0,0 +1,145 @@
+/**\r
+ * Namespace: System.Web.UI.WebUtils\r
+ * Class:     UrlUtils\r
+ * \r
+ * Author:  Gaurav Vaish\r
+ * Contact: <gvaish@iitk.ac.in>\r
+ * Status:  10??%\r
+ * \r
+ * (C) Gaurav Vaish (2001)\r
+ */\r
+\r
+using System;\r
+\r
+namespace System.Web.UI.WebUtils\r
+{\r
+       internal class UrlUtils\r
+       {\r
+               /*\r
+                * I could not find these functions in the class System.Uri\r
+                * Besides, an instance of Uri will not be formed until and unless the address is of\r
+                * the form protocol://[user:pass]host[:port]/[fullpath]\r
+                * ie, a protocol, and that too without any blanks before,\r
+                * is a must which may not be the case here.\r
+                */\r
+               public static string GetProtocol(string url)\r
+               {\r
+                       //String url = URL;\r
+                       if(url!=null)\r
+                       {\r
+                               if(url.Length>0)\r
+                               {\r
+                                       \r
+                                       int i, start = 0, limit;\r
+                                       limit = url.Length;\r
+                                       char c;\r
+                                       bool aRef = false;\r
+                                       while( (limit > 0) && (url[limit-1] <= ' '))\r
+                                       {\r
+                                               limit --;\r
+                                       }\r
+                                       while( (start < limit) && (url[start] <= ' '))\r
+                                       {\r
+                                               start++;\r
+                                       }\r
+                                       if(RegionMatches(true, url, start, "url:", 0, 4))\r
+                                       {\r
+                                               start += 4;\r
+                                       }\r
+                                       if(start < url.Length && url[start]=='#')\r
+                                       {\r
+                                               aRef = true;\r
+                                       }\r
+                                       for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)\r
+                                       {\r
+                                               if(c==':')\r
+                                               {\r
+                                                       return url.Substring(start, i - start);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+                       return String.Empty;\r
+               }\r
+               \r
+               public static bool IsRootUrl(string url)\r
+               {\r
+                       //Taking code from Java Class java.net.URL\r
+                       if(url!=null)\r
+                       {\r
+                               if(url.Length>0)\r
+                               {\r
+                                       return IsValidProtocol(GetProtocol(url).ToLower());\r
+                               }\r
+                       }\r
+                       return false;\r
+               }\r
+               \r
+               public static bool IsValidProtocol(string protocol)\r
+               {\r
+                       if(protocol.Length < 1)\r
+                               return false;\r
+                       char c = protocol[0];\r
+                       if(!Char.IsLetter(c))\r
+                       {\r
+                               System.Console.WriteLine("Character {0} is not a letter.", c);\r
+                               return false;\r
+                       }\r
+                       for(int i=1; i < protocol.Length; i++)\r
+                       {\r
+                               c = protocol[i];\r
+                               if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')\r
+                               {\r
+                                       System.Console.WriteLine("Character \"{0}\" is not a letter or a digit or something.", c);\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       return true;\r
+               }\r
+               \r
+               public static string MakeRelative(string from, string to)\r
+               {\r
+                       //Uri fromUri;\r
+                       //Uri toUri;\r
+                       return String.Empty;\r
+               }\r
+               \r
+               /*\r
+                * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)\r
+                * Could not find anything similar in the System.String class\r
+                */\r
+               public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)\r
+               {\r
+                       if(source!=null || match!=null)\r
+                       {\r
+                               if(source.Length>0 && match.Length>0)\r
+                               {\r
+                                       char[] ta = source.ToCharArray();\r
+                                       char[] pa = match.ToCharArray();\r
+                                       if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))\r
+                                       {\r
+                                               return false;\r
+                                       }\r
+                                       while(len-- > 0)\r
+                                       {\r
+                                               char c1 = ta[start++];\r
+                                               char c2 = pa[offset++];\r
+                                               if(c1==c2)\r
+                                                       continue;\r
+                                               if(ignoreCase)\r
+                                               {\r
+                                                       if(Char.ToUpper(c1)==Char.ToUpper(c2))\r
+                                                               continue;\r
+                                                       // Check for Gregorian Calendar where the above may not hold good\r
+                                                       if(Char.ToLower(c1)==Char.ToLower(c2))\r
+                                                               continue;\r
+                                               }\r
+                                               return false;\r
+                                       }\r
+                                       return true;\r
+                               }\r
+                       }\r
+                       return false;\r
+               }\r
+       }\r
+}\r