Author: spouliot
Date: 2005-11-25 10:43:25 -0500 (Fri, 25 Nov 2005)
New Revision: 53480

Added:
   trunk/mcs/class/System/System/DefaultUriParser.cs
Modified:
   trunk/mcs/class/System/System/ChangeLog
   trunk/mcs/class/System/System/Uri.cs
   trunk/mcs/class/System/System/UriParser.cs
Log:
2005-11-25  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * DefaultUriParser.cs: Added an internal default parser because (a)
        UriParser is abstract and (b) MS doesn't always use the new parser
        classes :(
        * Uri.cs: Implemented Compare method. Added some FIXME.
        * UriParser.cs: Implemented GetComponents and IsBaseOf methods. 
        Changed parsers to point to the new DefaultUriParser.



Modified: trunk/mcs/class/System/System/ChangeLog
===================================================================
--- trunk/mcs/class/System/System/ChangeLog     2005-11-25 15:39:15 UTC (rev 
53479)
+++ trunk/mcs/class/System/System/ChangeLog     2005-11-25 15:43:25 UTC (rev 
53480)
@@ -1,3 +1,12 @@
+2005-11-25  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * DefaultUriParser.cs: Added an internal default parser because (a)
+       UriParser is abstract and (b) MS doesn't always use the new parser
+       classes :(
+       * Uri.cs: Implemented Compare method. Added some FIXME.
+       * UriParser.cs: Implemented GetComponents and IsBaseOf methods. 
+       Changed parsers to point to the new DefaultUriParser.
+
 2005-11-15  Sebastien Pouliot  <[EMAIL PROTECTED]> 
  
        * Uri.cs: Temporary fix for monodoc (which current Uri system won't

Added: trunk/mcs/class/System/System/DefaultUriParser.cs
===================================================================
--- trunk/mcs/class/System/System/DefaultUriParser.cs   2005-11-25 15:39:15 UTC 
(rev 53479)
+++ trunk/mcs/class/System/System/DefaultUriParser.cs   2005-11-25 15:43:25 UTC 
(rev 53480)
@@ -0,0 +1,44 @@
+//
+// System.DefaultUriParser internal class
+//
+// Author:
+//     Sebastien Pouliot  <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if NET_2_0
+
+namespace System {
+
+       // LAMESPEC: Sadly MS doesn't seems to use all/most? the new Uri
+       // parser classes. See unit tests... :(
+
+       internal class DefaultUriParser : UriParser {
+
+               public DefaultUriParser ()
+               {
+               }
+       }
+}
+
+#endif

Modified: trunk/mcs/class/System/System/Uri.cs
===================================================================
--- trunk/mcs/class/System/System/Uri.cs        2005-11-25 15:39:15 UTC (rev 
53479)
+++ trunk/mcs/class/System/System/Uri.cs        2005-11-25 15:43:25 UTC (rev 
53480)
@@ -131,12 +131,16 @@
                                if (IsAbsoluteUri)
                                        throw new InvalidOperationException 
(Locale.GetText ("This isn't an relative URI."));
                                break;
+                       default:
+                               string msg = Locale.GetText ("Invalid UriKind 
value '{0}'.", uriKind);
+                               throw new ArgumentException ("uriKind", msg);
                        }
                }
 
                public Uri (Uri baseUri, Uri relativeUri)
                        : this (baseUri, relativeUri.OriginalString, false)
                {
+                       // FIXME: this should call UriParser.Resolve
                }
 
                // note: doc says that dontEscape is always false but tests 
show otherwise
@@ -158,7 +162,8 @@
 
                public Uri (Uri baseUri, string relativeUri) 
                        : this (baseUri, relativeUri, false) 
-               {                       
+               {
+                       // FIXME: this should call UriParser.Resolve
                }
 
 #if NET_2_0
@@ -996,7 +1001,7 @@
                        return Unescape (str, false);
                }
                
-               private string Unescape (string str, bool excludeSpecial) 
+               internal static string Unescape (string str, bool 
excludeSpecial) 
                {
                        if (str == null)
                                return String.Empty;
@@ -1574,7 +1579,11 @@
                private UriParser parser;
 
                private UriParser Parser {
-                       get { return parser; }
+                       get {
+                               if (parser == null)
+                                       parser = UriParser.GetParser (Scheme);
+                               return parser;
+                       }
                        set { parser = value; }
                }
 
@@ -1606,7 +1615,6 @@
 
                private const int MaxUriLength = 32766;
 
-               [MonoTODO]
                public static int Compare (Uri uri1, Uri uri2, UriComponents 
partsToCompare, UriFormat compareFormat, StringComparison comparisonType)
                {
                        if ((comparisonType < StringComparison.CurrentCulture) 
|| (comparisonType > StringComparison.OrdinalIgnoreCase)) {
@@ -1617,7 +1625,9 @@
                        if ((uri1 == null) && (uri2 == null))
                                return 0;
 
-                       throw new NotImplementedException ();
+                       string s1 = uri1.GetComponents (partsToCompare, 
compareFormat);
+                       string s2 = uri2.GetComponents (partsToCompare, 
compareFormat);
+                       return String.Compare (s1, s2, comparisonType);
                }
 
                [MonoTODO]
@@ -1648,16 +1658,14 @@
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public static bool IsWellFormedUriString (string uriString, 
UriKind uriKind)
                {
                        if (uriString == null)
-                               throw new ArgumentNullException ("uriString");
-
-                       throw new NotImplementedException ();
+                               return false;
+                       Uri uri = new Uri (uriString, uriKind);
+                       return uri.IsWellFormedOriginalString ();
                }
 
-
                [MonoTODO ("rework code to avoid exception catching")]
                public static bool TryCreate (string uriString, UriKind 
uriKind, out Uri result)
                {
@@ -1675,6 +1683,7 @@
                public static bool TryCreate (Uri baseUri, string relativeUri, 
out Uri result)
                {
                        try {
+                               // FIXME: this should call UriParser.Resolve
                                result = new Uri (baseUri, relativeUri);
                                return true;
                        }
@@ -1688,6 +1697,7 @@
                public static bool TryCreate (Uri baseUri, Uri relativeUri, out 
Uri result)
                {
                        try {
+                               // FIXME: this should call UriParser.Resolve
                                result = new Uri (baseUri, relativeUri);
                                return true;
                        }

Modified: trunk/mcs/class/System/System/UriParser.cs
===================================================================
--- trunk/mcs/class/System/System/UriParser.cs  2005-11-25 15:39:15 UTC (rev 
53479)
+++ trunk/mcs/class/System/System/UriParser.cs  2005-11-25 15:43:25 UTC (rev 
53480)
@@ -31,6 +31,7 @@
 using System.Collections;
 using System.Globalization;
 using System.Security.Permissions;
+using System.Text;
 
 namespace System {
 
@@ -49,13 +50,72 @@
 
                // protected methods
 
-               [MonoTODO]
                protected internal virtual string GetComponents (Uri uri, 
UriComponents components, UriFormat format)
                {
                        if ((format < UriFormat.UriEscaped) || (format > 
UriFormat.SafeUnescaped))
                                throw new ArgumentOutOfRangeException 
("format");
 
-                       throw new NotImplementedException ();
+                       // it's easier to answer some case directly (as the 
output isn't identical 
+                       // when mixed with others components, e.g. leading 
slash, # ...)
+                       switch (components) {
+                       case UriComponents.Scheme:
+                               return uri.Scheme;
+                       case UriComponents.UserInfo:
+                               return uri.UserInfo;
+                       case UriComponents.Port:
+                               if (uri.IsDefaultPort)
+                                       return String.Empty;
+                               return uri.Port.ToString ();
+                       case UriComponents.Path:
+                               return Format (IgnoreFirstCharIf 
(uri.LocalPath, '/'), format);
+                       case UriComponents.Fragment:
+                               return Format (IgnoreFirstCharIf (uri.Fragment, 
'#'), format);
+                       case UriComponents.StrongPort:
+                               return uri.Port.ToString ();
+                       case UriComponents.SerializationInfoString:
+                               components = UriComponents.AbsoluteUri;
+                               break;
+                       }
+
+                       // now we deal with multiple flags...
+
+                       StringBuilder sb = new StringBuilder ();
+                       if ((components & UriComponents.Scheme) != 0) {
+                               sb.Append (uri.Scheme);
+                               sb.Append (Uri.GetSchemeDelimiter (uri.Scheme));
+                       }
+
+                       if ((components & UriComponents.UserInfo) != 0) {
+                               string s = uri.UserInfo;
+                               if (s.Length > 0) {
+                                       sb.Append (s);
+                                       sb.Append ("@");
+                               }
+                       }
+
+                       if ((components & UriComponents.Host) != 0)
+                               sb.Append (uri.Host);
+
+                       // for StrongPort always show port - even if -1
+                       // otherwise only display if ut's not the default port
+                       if (((components & UriComponents.StrongPort) != 0) ||
+                               ((components & UriComponents.Port) != 0) && 
!uri.IsDefaultPort) {
+                               sb.Append (":");
+                               sb.Append (uri.Port);
+                       }
+
+                       if ((components & UriComponents.Path) != 0) {
+                               sb.Append (uri.LocalPath);
+                       }
+
+                       if ((components & UriComponents.Query) != 0)
+                               sb.Append (uri.Query);
+
+                       if ((components & UriComponents.Fragment) != 0) {
+                               sb.Append (uri.Fragment);
+                       }
+
+                       return Format (sb.ToString (), format);
                }
 
                [MonoTODO]
@@ -64,10 +124,15 @@
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                protected internal virtual bool IsBaseOf (Uri baseUri, Uri 
relativeUri)
                {
-                       throw new NotImplementedException ();
+                       // compare, not case sensitive, the scheme, host and 
port (+ user informations)
+                       if (Uri.Compare (baseUri, relativeUri, 
UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped, 
StringComparison.InvariantCultureIgnoreCase) != 0)
+                               return false;
+
+                       string base_string = baseUri.LocalPath;
+                       int last_slash = base_string.LastIndexOf ('/') + 1; // 
keep the slash
+                       return (String.Compare (base_string, 0, 
relativeUri.LocalPath, 0, last_slash, 
StringComparison.InvariantCultureIgnoreCase) == 0);
                }
 
                [MonoTODO]
@@ -93,6 +158,7 @@
                [MonoTODO]
                protected internal virtual string Resolve (Uri baseUri, Uri 
relativeUri, out UriFormatException parsingError)
                {
+                       // used by Uri.ctor and Uri.TryCreate
                        throw new NotImplementedException ();
                }
 
@@ -108,6 +174,36 @@
                        set { default_port = value; }
                }
 
+               // private stuff
+
+               private string IgnoreFirstCharIf (string s, char c)
+               {
+                       if (s.Length == 0)
+                               return String.Empty;
+                       if (s[0] == c)
+                               return s.Substring (1);
+                       return s;
+               }
+
+               private string Format (string s, UriFormat format)
+               {
+                       if (s.Length == 0)
+                               return String.Empty;
+
+                       switch (format) {
+                       case UriFormat.UriEscaped:
+                               return Uri.EscapeString (s, false, true, true);
+                       case UriFormat.SafeUnescaped:
+                               // TODO subset of escape rules
+                               s = Uri.Unescape (s, false);
+                               return s; //Uri.EscapeString (s, false, true, 
true);
+                       case UriFormat.Unescaped:
+                               return Uri.Unescape (s, false);
+                       default:
+                               throw new ArgumentOutOfRangeException 
("format");
+                       }
+               }
+
                // static methods
 
                private static void CreateDefaults ()
@@ -116,20 +212,18 @@
                                if (table == null) {
                                        table = new Hashtable ();
 
-                                       InternalRegister (new 
FileStyleUriParser (), Uri.UriSchemeFile, -1);
-                                       InternalRegister (new FtpStyleUriParser 
(), Uri.UriSchemeFtp, 21);
-                                       InternalRegister (new 
GopherStyleUriParser (), Uri.UriSchemeGopher, 70);
-                                       InternalRegister (new 
HttpStyleUriParser (), Uri.UriSchemeHttp, 80);
-                                       InternalRegister (new 
HttpStyleUriParser (), Uri.UriSchemeHttps, 443);
-                                       // FIXME ??? no MailToUriParser
-                                       InternalRegister (new GenericUriParser 
(GenericUriParserOptions.Default),
-                                               Uri.UriSchemeMailto, 25);
-                                       InternalRegister (new 
NetPipeStyleUriParser (), Uri.UriSchemeNetPipe, -1);
-                                       InternalRegister (new 
NetTcpStyleUriParser (), Uri.UriSchemeNetTcp, -1);
-                                       InternalRegister (new 
NewsStyleUriParser (), Uri.UriSchemeNews, 119);
-                                       InternalRegister (new 
NewsStyleUriParser (), Uri.UriSchemeNntp, 119);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeFile, -1);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeFtp, 21);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeGopher, 70);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeHttp, 80);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeHttps, 443);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeMailto, 25);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeNetPipe, -1);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeNetTcp, -1);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeNews, 119);
+                                       InternalRegister (new DefaultUriParser 
(), Uri.UriSchemeNntp, 119);
                                        // not defined in Uri.UriScheme* but a 
parser class exists
-                                       InternalRegister (new 
LdapStyleUriParser (), "ldap", 389);
+                                       InternalRegister (new DefaultUriParser 
(), "ldap", 389);
                                }
                        }
                }
@@ -151,7 +245,17 @@
                {
                        uriParser.SchemeName = schemeName;
                        uriParser.DefaultPort = defaultPort;
-                       table.Add (schemeName, uriParser);
+
+                       // FIXME: MS doesn't seems to call most inherited 
parsers
+                       if (uriParser is GenericUriParser) {
+                               table.Add (schemeName, uriParser);
+                       } else {
+                               DefaultUriParser parser = new DefaultUriParser 
();
+                               parser.SchemeName = schemeName;
+                               parser.DefaultPort = defaultPort;
+                               table.Add (schemeName, parser);
+                       }
+
                        // note: we cannot set schemeName and defaultPort 
inside OnRegister
                        uriParser.OnRegister (schemeName, defaultPort);
                }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to