Fixing the build, will not add an interceptor twice when it was already added by...
[castle.git] / Core / Castle.Core / Resource / CustomUri.cs
blob915a9aaca2ad087145ea1c041bc03de9f9b352ff
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Text;
20 [Serializable]
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);
37 public bool IsUnc
39 get { return isUnc; }
42 public bool IsFile
44 get { return isFile; }
47 public bool IsAssembly
49 get { return isAssembly; }
52 public String AbsolutePath
54 get { throw new NotImplementedException(); }
57 public string Scheme
59 get { return scheme; }
62 public string Host
64 get { return host; }
67 public String Path
69 get { return path; }
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] == '\\')
85 // Unc
87 isUnc = true;
88 isFile = true;
89 scheme = UriSchemeFile;
90 translateSlashes = false;
92 else if (identifier[comma + 1] == '/' && identifier[comma + 2] == '/')
94 // Extract scheme
96 scheme = identifier.Substring(0, comma);
98 isFile = (scheme == UriSchemeFile);
99 isAssembly = (scheme == UriSchemeAssembly);
101 identifier = identifier.Substring(comma + SchemeDelimiter.Length);
103 else
105 isFile = true;
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();
118 sb.Length = 0;
121 sb.Append('/');
123 else
125 sb.Append(ch);
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");