1 //depot/DevDiv/private/Whidbey_Xws/ndp/fx/src/Net/System/UriScheme.cs#4 - edit change 914456 (text)
3 Copyright (c) Microsoft Corporation
10 Provides extensibility contract for System.Uri
11 The contains only public API definition.
13 For remaining internal stuff please refer to the _UriSyntax.cs file.
17 Alexei Vopilov 19-Dec-2003
20 Alexei Vopilov 60-July-2004 - Changed the extensiblity model to be based purely on derivation, also has cut Config extensibility option
26 using System.Globalization;
27 using System.Security.Permissions;
30 // The class is used as a base for custom uri parsing and derived Uri factoring.
31 // A set of protected .ctors allows to hookup on the builtin parser behaviors.
33 // A developer must implement at least internal default .ctor to participate in the Uri extensibility game.
35 public abstract partial class UriParser {
37 internal string SchemeName
44 internal int DefaultPort {
50 private const UriSyntaxFlags SchemeOnlyFlags = UriSyntaxFlags.MayHavePath;
51 // This is a "scheme-only" base parser, everything after the scheme is
52 // returned as the path component.
53 // The user parser will need to do the majority of the work itself.
55 // However when the ctor is called from OnCreateUri context the calling parser
56 // settings will later override the result on the base class
58 protected UriParser(): this (SchemeOnlyFlags) { }
61 // Is called on each Uri ctor for every non-simple parser i.e. the one that does have
64 protected virtual UriParser OnNewUri()
69 // Is called whenever a parser gets registered with some scheme
70 // The base implementaion is a nop.
72 protected virtual void OnRegister(string schemeName, int defaultPort)
77 // Parses and validates a Uri object, is called at the Uri ctor time.
79 // This method returns a non null parsingError if Uri being created is invalid:
81 protected virtual void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
83 parsingError = uri.ParseMinimal();
86 // Resolves a relative Uri object into new AbsoluteUri.
88 // baseUri - The baseUri used to resolve this Uri.
89 // relativeuri - A relative Uri string passed by the application.
91 // This method returns:
92 // The result Uri value used to represent a new Uri
94 protected virtual string Resolve(Uri baseUri, Uri relativeUri, out UriFormatException parsingError)
96 if (baseUri.UserDrivenParsing)
97 throw new InvalidOperationException(SR.GetString(SR.net_uri_UserDrivenParsing, this.GetType().FullName));
99 if (!baseUri.IsAbsoluteUri)
100 throw new InvalidOperationException(SR.GetString(SR.net_uri_NotAbsolute));
103 string newUriString = null;
104 bool userEscaped = false;
105 Uri result = Uri.ResolveHelper(baseUri, relativeUri, ref newUriString, ref userEscaped, out parsingError);
107 if (parsingError != null)
111 return result.OriginalString;
119 protected virtual bool IsBaseOf(Uri baseUri, Uri relativeUri)
121 return baseUri.IsBaseOfHelper(relativeUri);
125 // This method is invoked to allow a cutsom parser to override the
126 // internal parser when serving application with Uri componenet strings.
127 // The output format depends on the "format" parameter
130 // uriComponents - Which components are to be retrieved.
131 // uriFormat - The requested output format.
133 // This method returns:
134 // The final result. The base impementaion could be invoked to get a suggested value
136 protected virtual string GetComponents(Uri uri, UriComponents components, UriFormat format)
138 if (((components & UriComponents.SerializationInfoString) != 0) && components != UriComponents.SerializationInfoString)
139 throw new ArgumentOutOfRangeException("components", components, SR.GetString(SR.net_uri_NotJustSerialization));
141 if ((format & ~UriFormat.SafeUnescaped) != 0)
142 throw new ArgumentOutOfRangeException("format");
144 if (uri.UserDrivenParsing)
145 throw new InvalidOperationException(SR.GetString(SR.net_uri_UserDrivenParsing, this.GetType().FullName));
147 if (!uri.IsAbsoluteUri)
148 throw new InvalidOperationException(SR.GetString(SR.net_uri_NotAbsolute));
150 return uri.GetComponentsHelper(components, format);
156 protected virtual bool IsWellFormedOriginalString(Uri uri)
158 return uri.InternalIsWellFormedOriginalString();
162 // Static Registration methods
165 // Registers a custom Uri parser based on a scheme string
167 public static void Register(UriParser uriParser, string schemeName, int defaultPort)
169 ExceptionHelper.InfrastructurePermission.Demand();
171 if (uriParser == null)
172 throw new ArgumentNullException("uriParser");
174 if (schemeName == null)
175 throw new ArgumentNullException("schemeName");
177 if (schemeName.Length == 1)
178 throw new ArgumentOutOfRangeException("schemeName");
180 if (!Uri.CheckSchemeName(schemeName))
181 throw new ArgumentOutOfRangeException("schemeName");
183 if ((defaultPort >= 0xFFFF || defaultPort < 0) && defaultPort != -1)
184 throw new ArgumentOutOfRangeException("defaultPort");
186 schemeName = schemeName.ToLower(CultureInfo.InvariantCulture);
187 FetchSyntax(uriParser, schemeName, defaultPort);
190 // Is a Uri scheme known to System.Uri?
192 public static bool IsKnownScheme(string schemeName)
194 if (schemeName == null)
195 throw new ArgumentNullException("schemeName");
197 if (!Uri.CheckSchemeName(schemeName))
198 throw new ArgumentOutOfRangeException("schemeName");
200 UriParser syntax = UriParser.GetSyntax(schemeName.ToLower(CultureInfo.InvariantCulture));
201 return syntax != null && syntax.NotAny(UriSyntaxFlags.V1_UnknownUri);