1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Core
.Resource
21 public sealed class CustomUri
23 public static readonly String SchemeDelimiter
= "://";
24 public static readonly String UriSchemeFile
= "file";
25 public static readonly String UriSchemeAssembly
= "assembly";
27 private String scheme
, host
, path
;
28 private bool isUnc
, isFile
, isAssembly
;
30 public CustomUri(String resourceIdentifier
)
32 SanityCheck(resourceIdentifier
);
34 ParseIdentifier(resourceIdentifier
);
44 get { return isFile; }
47 public bool IsAssembly
49 get { return isAssembly; }
52 public String AbsolutePath
54 get { throw new NotImplementedException(); }
59 get { return scheme; }
72 private void ParseIdentifier(String identifier
)
74 int comma
= identifier
.IndexOf(':');
76 if (comma
== -1 && !(identifier
[0] == '\\' && identifier
[1] == '\\') && !(identifier
[0] == '/'))
78 throw new ArgumentException("Invalid Uri: no scheme delimiter found on " + identifier
);
81 bool translateSlashes
= true;
83 if (identifier
[0] == '\\' && identifier
[1] == '\\')
89 scheme
= UriSchemeFile
;
90 translateSlashes
= false;
92 else if (identifier
[comma
+ 1] == '/' && identifier
[comma
+ 2] == '/')
96 scheme
= identifier
.Substring(0, comma
);
98 isFile
= (scheme
== UriSchemeFile
);
99 isAssembly
= (scheme
== UriSchemeAssembly
);
101 identifier
= identifier
.Substring(comma
+ SchemeDelimiter
.Length
);
106 scheme
= UriSchemeFile
;
109 StringBuilder sb
= new StringBuilder();
111 foreach(char ch
in identifier
.ToCharArray())
113 if (translateSlashes
&& (ch
== '\\' || ch
== '/'))
115 if (host
== null && !IsFile
)
117 host
= sb
.ToString();
129 path
= sb
.ToString();
132 private static void SanityCheck(String resourceIdentifier
)
134 if (resourceIdentifier
== null)
136 throw new ArgumentNullException("resourceIdentifier");
138 if (resourceIdentifier
== String
.Empty
)
140 throw new ArgumentException("Empty resource identifier is not allowed", "resourceIdentifier");