The Labs \ Source Viewer \ SSCLI \ System \ GenericUriParserOptions

  1. //------------------------------------------------------------------------------
  2. // <copyright file="GenericUriParser.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.     This is a public sealed class that exposes various Uri parsing options
  18.     suported by built in Uri parser
  19. Author:
  20.     Alexei Vopilov    Jul 26 2004
  21. Revision History:
  22. --*/
  23. namespace System
  24. {
  25.    
  26.     using System.Globalization;
  27.     using System.Collections;
  28.     using System.Security.Permissions;
  29.    
  30.     //
  31.     // This enum specifies the public options used to customize a hierarchical built-in parser.
  32.     //
  33.     [Flags()]
  34.     public enum GenericUriParserOptions
  35.     {
  36.         // A hierarchical URI, allows a userinfo, non empty Inet-based authority, path, query and fragment
  37.         // The URI path gets agressively compressed means dots, slashes and backslashes are unescaped,
  38.         // backslashesare converted, and then it compresses the path. It also removes trailing dots,
  39.         // empty segments and dots-only segments
  40.         Default = 0,
  41.        
  42.         // Allows a free style authority that would terminate with '/'
  43.         GenericAuthority = 1,
  44.        
  45.         // Allows an empty authority foo:///
  46.         AllowEmptyAuthority = 2,
  47.        
  48.         // Disables a user info component, it implied in the case of GenericAuthority flag
  49.         NoUserInfo = 4,
  50.        
  51.         // Disables a port component, it is implied in the case of GenericAuthority flag
  52.         NoPort = 8,
  53.        
  54.         // Disables a query. A ? char is considered as part of the path and is escaped
  55.         NoQuery = 16,
  56.        
  57.         // Disables a fragment. A # char is considered as part of the path or query and is escaped
  58.         NoFragment = 32,
  59.        
  60.         // if false then converta \ to /, otheriwse does this conversion for the Path component.
  61.         DontConvertPathBackslashes = 64,
  62.        
  63.         // if false, then a/./b or a/.../b becomes a/b and /a/../b becomes /b
  64.         DontCompressPath = 128,
  65.        
  66.         // if false then a/%2e./b becomes a/../b and then usually compressed
  67.         DontUnescapePathDotsAndSlashes = 256
  68.     }
  69.     //
  70.     // A hierachical Uri parser that supports various customization options
  71.     //
  72.     // ATTN: This type must be compile-time registered with UriParser.CheckSetIsSimpleFlag() method
  73.     // to avoid calling into the user code if there is no one.
  74.     //
  75.     public class GenericUriParser : UriParser
  76.     {
  77.         //
  78.         // The only availabe .ctor.
  79.         //
  80.         public GenericUriParser(GenericUriParserOptions options) : base(MapGenericParserOptions(options))
  81.         {
  82.         }
  83.         //
  84.         private static UriSyntaxFlags MapGenericParserOptions(GenericUriParserOptions options)
  85.         {
  86.             //
  87.             // Here we map public flags to internal ones
  88.             // Note an instacne of this parser is always a "simple parser" since the class is sealed.
  89.             //
  90.             UriSyntaxFlags flags = DefaultGenericUriParserFlags;
  91.            
  92.             if ((options & GenericUriParserOptions.GenericAuthority) != 0) {
  93.                 // Disable some options that are not compatible with generic authority
  94.                 flags &= ~(UriSyntaxFlags.MayHaveUserInfo | UriSyntaxFlags.MayHavePort | UriSyntaxFlags.AllowUncHost | UriSyntaxFlags.AllowAnInternetHost);
  95.                 flags |= UriSyntaxFlags.AllowAnyOtherHost;
  96.             }
  97.            
  98.             if ((options & GenericUriParserOptions.AllowEmptyAuthority) != 0) {
  99.                 flags |= UriSyntaxFlags.AllowEmptyHost;
  100.             }
  101.            
  102.             if ((options & GenericUriParserOptions.NoUserInfo) != 0) {
  103.                 flags &= ~UriSyntaxFlags.MayHaveUserInfo;
  104.             }
  105.            
  106.             if ((options & GenericUriParserOptions.NoPort) != 0) {
  107.                 flags &= ~UriSyntaxFlags.MayHavePort;
  108.             }
  109.            
  110.             if ((options & GenericUriParserOptions.NoQuery) != 0) {
  111.                 flags &= ~UriSyntaxFlags.MayHaveQuery;
  112.             }
  113.            
  114.             if ((options & GenericUriParserOptions.NoFragment) != 0) {
  115.                 flags &= ~UriSyntaxFlags.MayHaveFragment;
  116.             }
  117.            
  118.             if ((options & GenericUriParserOptions.DontConvertPathBackslashes) != 0) {
  119.                 flags &= ~UriSyntaxFlags.ConvertPathSlashes;
  120.             }
  121.            
  122.             if ((options & GenericUriParserOptions.DontCompressPath) != 0) {
  123.                 flags &= ~(UriSyntaxFlags.CompressPath | UriSyntaxFlags.CanonicalizeAsFilePath);
  124.             }
  125.            
  126.             if ((options & GenericUriParserOptions.DontUnescapePathDotsAndSlashes) != 0) {
  127.                 flags &= ~UriSyntaxFlags.UnEscapeDotsAndSlashes;
  128.             }
  129.            
  130.             return flags;
  131.         }
  132.        
  133.             //
  134.             //
  135.             //
  136.             //
  137.         private const UriSyntaxFlags DefaultGenericUriParserFlags = UriSyntaxFlags.MustHaveAuthority | UriSyntaxFlags.MayHaveUserInfo | UriSyntaxFlags.MayHavePort | UriSyntaxFlags.MayHavePath | UriSyntaxFlags.MayHaveQuery | UriSyntaxFlags.MayHaveFragment | UriSyntaxFlags.AllowUncHost | UriSyntaxFlags.AllowAnInternetHost | UriSyntaxFlags.PathIsRooted | UriSyntaxFlags.ConvertPathSlashes | UriSyntaxFlags.CompressPath | UriSyntaxFlags.CanonicalizeAsFilePath | UriSyntaxFlags.UnEscapeDotsAndSlashes;
  138.        
  139.        
  140.     }
  141. }

Developer Fusion