Merge pull request #1142 from edbprx/master
[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                 if (start > 0) builder.Append(s, start, end - start);
68             }
69
70             if ((components & UriComponents.Query) == UriComponents.Query)
71             {
72                 int index = s.IndexOf('?');
73
74                 if (index != -1)
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
86             if ((components & UriComponents.Fragment) == UriComponents.Fragment)
87             {
88                 int index = s.IndexOf('#');
89
90                 if (index != -1)
91                 {
92                         if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
93                             builder.Length == 0)
94                                 index++;
95
96                         builder.Append(s, index, s.Length - index);
97                 }
98             }
99
100             return builder.ToString();
101         }
102         
103         protected override void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
104         {
105             parsingError = null;
106         }
107
108         protected override UriParser OnNewUri()
109         {
110             return new PackUriParser();
111         }
112     }
113 }