* tools/beagle-crawl-system.in: Use MONO_SHARED_DIR to point to a
[beagle.git] / Util / UriFu.cs
blob6c9add527380829777973e09d62887361cccedbd
1 //
2 // UriFu.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.Text;
31 namespace Beagle.Util {
33 public class UriFu {
35 private UriFu () { } // class is static
37 static public Uri PathToFileUri (string path)
39 string uriStr = StringFu.PathToQuotedFileUri (path);
40 return new Uri (uriStr, true);
43 static public Uri UriStringToUri (string path)
45 // Our current hackery attempts to serialize Uri strings in
46 // escaped and constructable form, so we don't require any
47 // extra processing on deserialization right now.
48 return new Uri (path, true);
51 static public String UriToSerializableString (Uri uri)
53 int i;
54 string path;
55 StringBuilder builder = new StringBuilder ();
57 if (uri.IsFile)
58 path = Uri.UriSchemeFile + Uri.SchemeDelimiter
59 + StringFu.HexEscape (uri.LocalPath);
60 else
61 path = uri.ToString ();
63 // XmlSerializer is happy to serialize 'odd' characters, but doesn't
64 // like to deserialize them. So we encode all 'odd' characters now.
65 for (i = 0; i < path.Length; i++)
66 if ((path [i] < '!') || (path [i] > '~' && path [i] < 256))
67 builder.Append (Uri.HexEscape (path [i]));
68 else
69 builder.Append (path [i]);
71 if (uri.IsFile)
72 builder.Append (uri.Fragment);
74 return builder.ToString ();
77 static public String LocalPathFromUri (Uri uri)
79 if (uri == null)
80 return "";
81 // FIXME: Can we assume "a directory", if it is not a file?
82 // If so, return the path of that directory.
83 if (uri.IsFile)
84 return uri.LocalPath;
85 else
86 return "";
89 //////////////////////////////////
91 static public bool Equals (Uri uri1, Uri uri2)
93 return uri1.ToString () == uri2.ToString ();
96 static public int Compare (Uri uri1, Uri uri2)
98 return String.Compare (uri1.ToString (), uri2.ToString ());
101 //////////////////////////////////
103 public class Comparer : IComparer
105 public int Compare(object uri1, object uri2)
107 return String.Compare(uri1.ToString(), uri2.ToString());
111 public class Hasher : IHashCodeProvider
113 public int GetHashCode(object o)
115 return o.ToString().GetHashCode();
119 static Comparer the_comparer = new Comparer ();
120 static Hasher the_hasher = new Hasher ();
122 // Returns a hash table that does the right thing when
123 // the key is a Uri.
124 static public Hashtable NewHashtable ()
126 return new Hashtable (the_hasher, the_comparer);
129 //////////////////////////////////
131 static public string UrisToString (ICollection list_of_uris)
133 StringBuilder sb = new StringBuilder ("!@#");
135 foreach (Uri uri in list_of_uris) {
136 sb.Append (" ");
137 sb.Append (UriToSerializableString (uri).Replace (" ", "%20"));
140 return sb.ToString ();
143 static public ICollection StringToUris (string list_of_uris_as_string)
145 string [] parts = list_of_uris_as_string.Split (' ');
147 if (parts.Length == 0 || parts [0] != "!@#")
148 return null;
150 ArrayList uri_array = new ArrayList ();
151 for (int i = 1; i < parts.Length; ++i) {
152 try {
153 Uri uri = UriStringToUri (parts [i]);
154 uri_array.Add (uri);
155 } catch (Exception ex) {
156 Logger.Log.Debug ("Caught exception converting '{0}' to a Uri", parts [i]);
160 return uri_array;