Moving WindowsBase into the mcs tree
[mono.git] / mcs / class / WindowsBase / System.IO.Packaging / PackUriParser.cs
1 // PackUriParser.cs created with MonoDevelop
2 // User: alan at 14:50 31/10/2008
3 //
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
5 //
6
7 using System;
8 using System.Collections.Generic;
9 using System.Text;
10
11 namespace System.IO.Packaging
12 {
13     class PackUriParser : System.GenericUriParser
14     {
15         const string SchemaName = "pack";
16
17         StringBuilder builder = new StringBuilder();
18
19         public PackUriParser ()
20             : base (GenericUriParserOptions.Default)
21         {
22         }
23         
24         protected override string GetComponents(Uri uri, UriComponents components, UriFormat format)
25         {
26             string s = uri.OriginalString;
27             builder.Remove(0, builder.Length);
28
29             if ((components & UriComponents.Scheme) == UriComponents.Scheme)
30             {
31                 int start = 0;
32                 int end = s.IndexOf(':');
33                 builder.Append(s, start, end - start);
34             }
35
36             if ((components & UriComponents.Host) == UriComponents.Host)
37             {
38                 // Skip past pack://
39                 int start = 7;
40                 int end = s.IndexOf('/', start);
41                 if (end == -1)
42                     end = s.Length;
43
44                 if (builder.Length > 0)
45                     builder.Append("://");
46
47                 builder.Append(s, start, end - start);
48             }
49
50             // Port is always -1, so i think i can ignore both Port and StrongPort
51             // Normally they'd get parsed here
52
53             if ((components & UriComponents.Path) == UriComponents.Path)
54             {
55                 // Skip past pack://
56                 int start = s.IndexOf('/', 7);
57                 int end = s.IndexOf('?');
58                 if (end == -1)
59                     end = s.IndexOf('#');
60                 if (end == -1)
61                     end = s.Length;
62
63                 if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
64                     builder.Length == 0)
65                     start++;
66
67                 builder.Append(s, start, end - start);
68             }
69
70             if ((components & UriComponents.Query) == UriComponents.Query)
71             {
72                 int index = s.IndexOf('?');
73                 if (index == -1)
74                     return null;
75
76                 if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
77                     builder.Length == 0)
78                     index++;
79
80                 int fragIndex = s.IndexOf('#');
81                 int end = fragIndex == -1 ? s.Length : fragIndex;
82                 builder.Append(s, index, end - index);
83             }
84
85             if ((components & UriComponents.Fragment) == UriComponents.Fragment)
86             {
87                 int index = s.IndexOf('#');
88                 if (index == -1)
89                     return null;
90
91                 if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
92                     builder.Length == 0)
93                     index++;
94
95                 builder.Append(s, index, s.Length - index);
96             }
97
98             return builder.ToString();
99         }
100         
101         protected override void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
102         {
103             parsingError = null;
104         }
105
106         protected override UriParser OnNewUri()
107         {
108             return new PackUriParser();
109         }
110     }
111 }