4 // Copyright (C) 2004 Novell, Inc.
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
28 using System
.Collections
;
31 namespace Beagle
.Util
{
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
)
55 StringBuilder builder
= new StringBuilder ();
58 path
= Uri
.UriSchemeFile
+ Uri
.SchemeDelimiter
59 + StringFu
.HexEscape (uri
.LocalPath
);
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
]));
69 builder
.Append (path
[i
]);
72 builder
.Append (uri
.Fragment
);
74 return builder
.ToString ();
77 static public String
LocalPathFromUri (Uri uri
)
81 // FIXME: Can we assume "a directory", if it is not a file?
82 // If so, return the path of that directory.
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
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
) {
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] != "!@#")
150 ArrayList uri_array
= new ArrayList ();
151 for (int i
= 1; i
< parts
.Length
; ++i
) {
153 Uri uri
= UriStringToUri (parts
[i
]);
155 } catch (Exception ex
) {
156 Logger
.Log
.Debug ("Caught exception converting '{0}' to a Uri", parts
[i
]);