Lines 42-99
namespace Beagle.Util {
Link Here
|
42 |
|
42 |
|
43 |
static public Uri UriStringToUri (string path) |
43 |
static public Uri UriStringToUri (string path) |
44 |
{ |
44 |
{ |
45 |
// Decode our pre-encoded 'odd' characters into their real values |
45 |
// Our current hackery attempts to serialize Uri strings in |
46 |
int i = 0, pos = 0; |
46 |
// escaped and constructable form, so we don't require any |
47 |
while ((i = path.IndexOf ('%', pos)) != -1) { |
47 |
// extra processing on deserialization right now. |
48 |
pos = i; |
|
|
49 |
char unescaped = UriFu.HexUnescape (path, ref pos); |
50 |
if (unescaped < '!' || unescaped > '~') { |
51 |
path = path.Remove (i, 3); |
52 |
path = path.Insert (i, new String(unescaped, 1)); |
53 |
pos -= 2; |
54 |
} |
55 |
} |
56 |
|
57 |
// Paths from the file:// indexer need (re)quoting. For example, |
58 |
// valid characters such as @ need to be converted to their hex |
59 |
// values. |
60 |
if (path.StartsWith ("file://")) { |
61 |
// Remove the file:// prefix |
62 |
path = path.Substring (7); |
63 |
|
64 |
return PathToFileUri (path); |
65 |
} |
66 |
|
67 |
// Currently, no other protocols need extra processing |
68 |
return new Uri (path, true); |
48 |
return new Uri (path, true); |
69 |
} |
49 |
} |
70 |
|
50 |
|
71 |
static public String UriToSerializableString (Uri uri) |
51 |
static public String UriToSerializableString (Uri uri) |
72 |
{ |
52 |
{ |
73 |
int i; |
53 |
int i; |
74 |
string ret; |
54 |
string scheme, path; |
75 |
StringBuilder builder = new StringBuilder (); |
55 |
StringBuilder builder = new StringBuilder (); |
76 |
|
56 |
|
77 |
// The ToString() of a file:// URI is not always representative of |
57 |
scheme = uri.Scheme; |
78 |
// what it was constructed from. For example, it will return a |
58 |
path = StringFu.HexEscape (uri.LocalPath); |
79 |
// # (which was inputted as %23) as %23, whereas the more standard |
|
|
80 |
// behaviour for other escaped-characters is to return them as |
81 |
// their actual character. (e.g. %40 gets returned as @) |
82 |
// On the other hand, the LocalPath of a file:// URI does seem to |
83 |
// return the literal # so we use that instead. |
84 |
if (uri.IsFile) |
85 |
ret = Uri.UriSchemeFile + Uri.SchemeDelimiter + uri.LocalPath; |
86 |
else |
87 |
ret = uri.ToString (); |
88 |
|
59 |
|
89 |
// XmlSerializer is happy to serialize 'odd' characters, but doesn't |
60 |
// XmlSerializer is happy to serialize 'odd' characters, but doesn't |
90 |
// like to deserialize them. So we encode all 'odd' characters now. |
61 |
// like to deserialize them. So we encode all 'odd' characters now. |
91 |
for (i = 0; i < ret.Length; i++) |
62 |
for (i = 0; i < path.Length; i++) |
92 |
if ((ret [i] < '!') || (ret [i] > '~' && ret [i] < 256)) |
63 |
if ((path [i] < '!') || (path [i] > '~' && path [i] < 256)) |
93 |
builder.Append (Uri.HexEscape (ret [i])); |
64 |
builder.Append (Uri.HexEscape (path [i])); |
94 |
else |
65 |
else |
95 |
builder.Append (ret [i]); |
66 |
builder.Append (path [i]); |
|
|
67 |
|
68 |
if (scheme == "uid") |
69 |
builder.Insert (0, ':'); |
70 |
else |
71 |
builder.Insert (0, Uri.SchemeDelimiter); |
96 |
|
72 |
|
|
|
73 |
builder.Insert (0, scheme); |
74 |
builder.Append (uri.Fragment); |
75 |
|
97 |
return builder.ToString (); |
76 |
return builder.ToString (); |
98 |
} |
77 |
} |
99 |
|
78 |
|