The Labs \ Source Viewer \ SSCLI \ System \ UriParser

  1. //------------------------------------------------------------------------------
  2. // <copyright file="UriScheme.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*++   
  16. Abstract:
  17.     Provides extensibility contract for System.Uri
  18.     The contains only public API definition.
  19.     For remaining internal stuff please refer to the _UriSyntax.cs file.
  20. Author:
  21.     Alexei Vopilov    19-Dec-2003
  22. Revision History:
  23.     Alexei Vopilov    60-July-2004  - Changed the extensiblity model to be based purely on derivation, also has cut Config extensibility option
  24. --*/
  25. namespace System
  26. {
  27.     using System.Net;
  28.     using System.Globalization;
  29.     using System.Security.Permissions;
  30.    
  31.     //
  32.     // The class is used as a base for custom uri parsing and derived Uri factoring.
  33.     // A set of protected .ctors allows to hookup on the builtin parser behaviors.
  34.     //
  35.     // A developer must implement at least internal default .ctor to participate in the Uri extensibility game.
  36.     //
  37.     public abstract partial class UriParser
  38.     {
  39.        
  40.         internal string SchemeName {
  41.             get { return m_Scheme; }
  42.         }
  43.         internal int DefaultPort {
  44.             get { return m_Port; }
  45.         }
  46.        
  47.         private const UriSyntaxFlags SchemeOnlyFlags = UriSyntaxFlags.MayHavePath;
  48.         // This is a "scheme-only" base parser, everything after the scheme is
  49.         // returned as the path component.
  50.         // The user parser will need to do the majority of the work itself.
  51.         //
  52.         // However when the ctor is called from OnCreateUri context the calling parser
  53.         // settings will later override the result on the base class
  54.         //
  55.         protected UriParser() : this(SchemeOnlyFlags)
  56.         {
  57.         }
  58.        
  59.         //
  60.         // Is called on each Uri ctor for every non-simple parser i.e. the one that does have
  61.         // user code.
  62.         //
  63.         protected virtual UriParser OnNewUri()
  64.         {
  65.             return this;
  66.         }
  67.         //
  68.         // Is called whenever a parser gets registered with some scheme
  69.         // The base implementaion is a nop.
  70.         //
  71.         protected virtual void OnRegister(string schemeName, int defaultPort)
  72.         {
  73.            
  74.         }
  75.         //
  76.         // Parses and validates a Uri object, is called at the Uri ctor time.
  77.         //
  78.         // This method returns a non null parsingError if Uri being created is invalid:
  79.         //
  80.         protected virtual void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
  81.         {
  82.             parsingError = uri.ParseMinimal();
  83.         }
  84.         //
  85.         // Resolves a relative Uri object into new AbsoluteUri.
  86.         //
  87.         // baseUri - The baseUri used to resolve this Uri.
  88.         // relativeuri - A relative Uri string passed by the application.
  89.         //
  90.         // This method returns:
  91.         // The result Uri value used to represent a new Uri
  92.         //
  93.         protected virtual string Resolve(Uri baseUri, Uri relativeUri, out UriFormatException parsingError)
  94.         {
  95.             if (baseUri.UserDrivenParsing)
  96.                 throw new InvalidOperationException(SR.GetString(SR.net_uri_UserDrivenParsing, this.GetType().FullName));
  97.            
  98.             if (!baseUri.IsAbsoluteUri)
  99.                 throw new InvalidOperationException(SR.GetString(SR.net_uri_NotAbsolute));
  100.            
  101.            
  102.             string newUriString = null;
  103.             bool userEscaped = false;
  104.             Uri result = Uri.ResolveHelper(baseUri, relativeUri, ref newUriString, ref userEscaped, out parsingError);
  105.            
  106.             if (parsingError != null)
  107.                 return null;
  108.            
  109.             if (result != null)
  110.                 return result.OriginalString;
  111.            
  112.             return newUriString;
  113.         }
  114.         //
  115.         //
  116.         //
  117.         protected virtual bool IsBaseOf(Uri baseUri, Uri relativeUri)
  118.         {
  119.             return baseUri.IsBaseOfHelper(relativeUri);
  120.         }
  121.         //
  122.         // This method is invoked to allow a cutsom parser to override the
  123.         // internal parser when serving application with Uri componenet strings.
  124.         // The output format depends on the "format" parameter
  125.         //
  126.         // Parameters:
  127.         // uriComponents - Which components are to be retrieved.
  128.         // uriFormat - The requested output format.
  129.         //
  130.         // This method returns:
  131.         // The final result. The base impementaion could be invoked to get a suggested value
  132.         //
  133.         protected virtual string GetComponents(Uri uri, UriComponents components, UriFormat format)
  134.         {
  135.             if (((components & UriComponents.SerializationInfoString) != 0) && components != UriComponents.SerializationInfoString)
  136.                 throw new ArgumentOutOfRangeException("UriComponents.SerializationInfoString");
  137.            
  138.             if ((format & ~UriFormat.SafeUnescaped) != 0)
  139.                 throw new ArgumentOutOfRangeException("format");
  140.            
  141.             if (uri.UserDrivenParsing)
  142.                 throw new InvalidOperationException(SR.GetString(SR.net_uri_UserDrivenParsing, this.GetType().FullName));
  143.            
  144.             if (!uri.IsAbsoluteUri)
  145.                 throw new InvalidOperationException(SR.GetString(SR.net_uri_NotAbsolute));
  146.            
  147.             return uri.GetComponentsHelper(components, format);
  148.         }
  149.         //
  150.         //
  151.         //
  152.         protected virtual bool IsWellFormedOriginalString(Uri uri)
  153.         {
  154.             return uri.InternalIsWellFormedOriginalString();
  155.         }
  156.        
  157.         //
  158.         // Static Registration methods
  159.         //
  160.         //
  161.         // Registers a custom Uri parser based on a scheme string
  162.         //
  163.         public static void Register(UriParser uriParser, string schemeName, int defaultPort)
  164.         {
  165.             ExceptionHelper.InfrastructurePermission.Demand();
  166.            
  167.             if (uriParser == null)
  168.                 throw new ArgumentNullException("uriParser");
  169.            
  170.             if (schemeName == null)
  171.                 throw new ArgumentNullException("schemeName");
  172.            
  173.             if (schemeName.Length == 1)
  174.                 throw new ArgumentOutOfRangeException("uriParser.SchemeName");
  175.            
  176.             if (!CheckSchemeName(schemeName))
  177.                 throw new ArgumentOutOfRangeException("schemeName");
  178.            
  179.             if ((defaultPort >= 65535 || defaultPort < 0) && defaultPort != -1)
  180.                 throw new ArgumentOutOfRangeException("defaultPort");
  181.            
  182.             schemeName = schemeName.ToLower(CultureInfo.InvariantCulture);
  183.             FetchSyntax(uriParser, schemeName, defaultPort);
  184.         }
  185.         //
  186.         // Is a Uri scheme known to System.Uri?
  187.         //
  188.         public static bool IsKnownScheme(string schemeName)
  189.         {
  190.             if (schemeName == null)
  191.                 throw new ArgumentNullException("schemeName");
  192.            
  193.             if (!CheckSchemeName(schemeName))
  194.                 throw new ArgumentOutOfRangeException("schemeName");
  195.            
  196.             UriParser syntax = UriParser.GetSyntax(schemeName.ToLower(CultureInfo.InvariantCulture));
  197.             return syntax != null && syntax.NotAny(UriSyntaxFlags.V1_UnknownUri);
  198.         }
  199.     }
  200. }

Developer Fusion