1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@ximian.com)
28 namespace System.IO.Packaging {
30 public static class PackUriHelper
32 public static readonly string UriSchemePack = "pack";
33 static readonly Uri PackSchemeUri = new Uri("pack://", UriKind.Absolute);
34 static readonly char[] _escapedChars = new char[] { '%', ',', '?', '@' };
37 static PackUriHelper ()
39 if (!UriParser.IsKnownScheme (UriSchemePack))
40 UriParser.Register (new PackUriParser (), UriSchemePack, -1);
43 public static int ComparePackUri (Uri firstPackUri, Uri secondPackUri)
45 // FIXME: Do i need to do validation that it is a pack:// uri?
46 if (firstPackUri == null)
47 return secondPackUri == null ? 0 : -1;
48 if (secondPackUri == null)
51 // FIXME: What exactly is compared. Lets assume originalstring
52 return firstPackUri.OriginalString.CompareTo (secondPackUri.OriginalString);
55 public static int ComparePartUri (Uri firstPartUri, Uri secondPartUri)
57 // FIXME: Do i need to do validation that it is a part URI?
58 if (firstPartUri == null)
59 return secondPartUri == null ? 0 : -1;
60 if (secondPartUri == null)
63 return firstPartUri.OriginalString.CompareTo (secondPartUri.OriginalString);
66 public static Uri Create (Uri packageUri)
68 return Create (packageUri, null, null);
71 public static Uri Create (Uri packageUri, Uri partUri)
73 return Create (packageUri, partUri, null);
76 public static Uri Create (Uri packageUri, Uri partUri, string fragment)
78 Check.PackageUri (packageUri);
79 Check.PackageUriIsValid (packageUri);
82 Check.PartUriIsValid (partUri);
84 if (fragment != null && (fragment.Length == 0 || fragment[0] != '#'))
85 throw new ArgumentException ("Fragment", "Fragment must not be empty and must start with '#'");
87 // FIXME: Validate that partUri is a valid one? Must be relative, must start with '/'
89 // First replace the slashes, then escape the special characters
90 //string orig = packageUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
91 string orig = packageUri.OriginalString;
93 foreach (var ch in _escapedChars)
95 orig = !orig.Contains(ch.ToString()) ? orig : orig.Replace(ch.ToString(), Uri.HexEscape(ch));
98 orig = orig.Replace('/', ',');
101 orig += partUri.OriginalString;
103 if ((fragment == null && partUri == null)&& orig[orig.Length - 1] != '/')
106 if (fragment != null)
109 return new Uri ("pack://" + orig);
112 public static Uri CreatePartUri (Uri partUri)
114 Check.PartUri (partUri);
116 if (partUri.OriginalString[0] != '/')
117 partUri = new Uri("/" + partUri.ToString (), UriKind.Relative);
121 public static Uri GetNormalizedPartUri (Uri partUri)
123 Check.PartUri (partUri);
124 return new Uri (partUri.ToString ().ToUpperInvariant (), UriKind.Relative);
127 public static Uri GetPackageUri (Uri packUri)
129 Check.PackUri (packUri);
130 Check.PackUriIsValid (packUri);
132 string s = packUri.Host.Replace(',', '/');
133 return new Uri (Uri.UnescapeDataString(s), UriKind.RelativeOrAbsolute);
136 public static Uri GetPartUri (Uri packUri)
138 Check.PackUri(packUri);
139 Check.PackUriIsValid(packUri);
141 if (string.IsNullOrEmpty(packUri.AbsolutePath) || packUri.AbsolutePath == "/")
144 return new Uri(packUri.AbsolutePath, UriKind.Relative);
147 public static Uri GetRelationshipPartUri (Uri partUri)
149 Check.PartUri (partUri);
150 Check.PartUriIsValid (partUri);
152 int index = partUri.OriginalString.LastIndexOf ("/");
153 string s = partUri.OriginalString.Substring (0, index);
154 s += "/_rels" + partUri.OriginalString.Substring (index) + ".rels";
155 return new Uri (s, UriKind.Relative);
158 public static Uri GetRelativeUri (Uri sourcePartUri, Uri targetPartUri)
160 Check.SourcePartUri (sourcePartUri);
161 Check.TargetPartUri (targetPartUri);
163 Uri uri = new Uri ("http://fake.com");
164 Uri a = new Uri (uri, sourcePartUri.OriginalString);
165 Uri b = new Uri (uri, targetPartUri.OriginalString);
167 return a.MakeRelativeUri(b);
170 public static Uri GetSourcePartUriFromRelationshipPartUri (Uri relationshipPartUri)
172 //Check.RelationshipPartUri (relationshipPartUri);
173 if (!IsRelationshipPartUri (relationshipPartUri))
174 throw new Exception ("is not a relationship part!?");
178 public static bool IsRelationshipPartUri (Uri partUri)
180 Check.PartUri (partUri);
181 return partUri.OriginalString.StartsWith ("/_rels") && partUri.OriginalString.EndsWith (".rels");
184 public static Uri ResolvePartUri (Uri sourcePartUri, Uri targetUri)
186 Check.SourcePartUri (sourcePartUri);
187 Check.TargetUri (targetUri);
189 Check.PartUriIsValid (sourcePartUri);
190 if (targetUri.IsAbsoluteUri)
191 throw new ArgumentException ("targetUri", "Absolute URIs are not supported");
193 Uri uri = new Uri ("http://fake.com");
194 uri = new Uri (uri, sourcePartUri);
195 uri = new Uri (uri, targetUri);
197 // Trim out 'http://fake.com'
198 return new Uri (uri.OriginalString.Substring (15), UriKind.Relative);