wasDAVClient – Blame information for rev 5
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
5 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | // Originally based on: WebDAV .NET client by Sergey Kazantsev |
||
7 | |||
8 | using System; |
||
1 | office | 9 | using System.Collections.Generic; |
10 | using System.IO; |
||
11 | using System.Linq; |
||
12 | using System.Net; |
||
13 | using System.Xml; |
||
14 | using wasDAVClient.Model; |
||
15 | |||
16 | namespace wasDAVClient.Helpers |
||
17 | { |
||
18 | /// <summary> |
||
3 | office | 19 | /// Represents the parser for response's results. |
1 | office | 20 | /// </summary> |
21 | internal static class ResponseParser |
||
22 | { |
||
3 | office | 23 | internal static XmlReaderSettings XmlReaderSettings = new XmlReaderSettings |
24 | { |
||
25 | IgnoreComments = true, |
||
26 | IgnoreProcessingInstructions = true, |
||
27 | IgnoreWhitespace = true |
||
28 | }; |
||
29 | |||
1 | office | 30 | /// <summary> |
3 | office | 31 | /// Parses the disk item. |
1 | office | 32 | /// </summary> |
33 | /// <param name="stream">The response text.</param> |
||
34 | /// <returns>The parsed item.</returns> |
||
35 | public static Item ParseItem(Stream stream) |
||
36 | { |
||
37 | return ParseItems(stream).FirstOrDefault(); |
||
38 | } |
||
39 | |||
40 | /// <summary> |
||
3 | office | 41 | /// Parses the disk items. |
1 | office | 42 | /// </summary> |
43 | /// <param name="stream">The response text.</param> |
||
44 | /// <returns>The list of parsed items.</returns> |
||
45 | public static IEnumerable<Item> ParseItems(Stream stream) |
||
46 | { |
||
47 | var items = new List<Item>(); |
||
48 | using (var reader = XmlReader.Create(stream, XmlReaderSettings)) |
||
49 | { |
||
50 | Item itemInfo = null; |
||
51 | while (reader.Read()) |
||
52 | { |
||
53 | if (reader.NodeType == XmlNodeType.Element) |
||
54 | { |
||
55 | switch (reader.LocalName.ToLower()) |
||
56 | { |
||
57 | case "response": |
||
58 | itemInfo = new Item(); |
||
59 | break; |
||
60 | case "href": |
||
61 | if (!reader.IsEmptyElement) |
||
62 | { |
||
63 | reader.Read(); |
||
64 | var value = reader.Value; |
||
65 | value = value.Replace("#", "%23"); |
||
66 | itemInfo.Href = value; |
||
67 | } |
||
68 | break; |
||
69 | case "creationdate": |
||
70 | if (!reader.IsEmptyElement) |
||
71 | { |
||
72 | reader.Read(); |
||
73 | DateTime creationdate; |
||
74 | if (DateTime.TryParse(reader.Value, out creationdate)) |
||
75 | itemInfo.CreationDate = creationdate; |
||
76 | } |
||
77 | break; |
||
78 | case "getlastmodified": |
||
79 | if (!reader.IsEmptyElement) |
||
80 | { |
||
81 | reader.Read(); |
||
82 | DateTime lastmodified; |
||
83 | if (DateTime.TryParse(reader.Value, out lastmodified)) |
||
84 | itemInfo.LastModified = lastmodified; |
||
85 | } |
||
86 | break; |
||
87 | case "displayname": |
||
88 | if (!reader.IsEmptyElement) |
||
89 | { |
||
90 | reader.Read(); |
||
91 | itemInfo.DisplayName = reader.Value; |
||
92 | } |
||
93 | break; |
||
94 | case "getcontentlength": |
||
95 | if (!reader.IsEmptyElement) |
||
96 | { |
||
97 | reader.Read(); |
||
98 | long contentLength; |
||
99 | if (long.TryParse(reader.Value, out contentLength)) |
||
100 | itemInfo.ContentLength = contentLength; |
||
101 | } |
||
102 | break; |
||
103 | case "getcontenttype": |
||
104 | if (!reader.IsEmptyElement) |
||
105 | { |
||
106 | reader.Read(); |
||
107 | itemInfo.ContentType = reader.Value; |
||
108 | } |
||
109 | break; |
||
110 | case "getetag": |
||
111 | if (!reader.IsEmptyElement) |
||
112 | { |
||
113 | reader.Read(); |
||
114 | itemInfo.Etag = reader.Value; |
||
115 | } |
||
116 | break; |
||
117 | case "iscollection": |
||
118 | if (!reader.IsEmptyElement) |
||
119 | { |
||
120 | reader.Read(); |
||
121 | bool isCollection; |
||
122 | if (bool.TryParse(reader.Value, out isCollection)) |
||
123 | itemInfo.IsCollection = isCollection; |
||
124 | int isCollectionInt; |
||
125 | if (int.TryParse(reader.Value, out isCollectionInt)) |
||
126 | itemInfo.IsCollection = isCollectionInt == 1; |
||
127 | } |
||
128 | break; |
||
129 | case "resourcetype": |
||
130 | if (!reader.IsEmptyElement) |
||
131 | { |
||
132 | reader.Read(); |
||
133 | var resourceType = reader.LocalName.ToLower(); |
||
3 | office | 134 | if (string.Equals(resourceType, "collection", |
135 | StringComparison.InvariantCultureIgnoreCase)) |
||
1 | office | 136 | itemInfo.IsCollection = true; |
137 | } |
||
138 | break; |
||
139 | case "hidden": |
||
140 | case "ishidden": |
||
141 | itemInfo.IsHidden = true; |
||
142 | break; |
||
143 | case "checked-in": |
||
144 | case "version-controlled-configuration": |
||
145 | reader.Skip(); |
||
146 | break; |
||
147 | } |
||
148 | } |
||
149 | else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName.ToLower() == "response") |
||
150 | { |
||
151 | // Remove trailing / if the item is not a collection |
||
152 | var href = itemInfo.Href.TrimEnd('/'); |
||
153 | if (!itemInfo.IsCollection) |
||
154 | { |
||
155 | itemInfo.Href = href; |
||
156 | } |
||
157 | if (string.IsNullOrEmpty(itemInfo.DisplayName)) |
||
158 | { |
||
159 | var name = href.Substring(href.LastIndexOf('/') + 1); |
||
160 | itemInfo.DisplayName = WebUtility.UrlDecode(name); |
||
161 | } |
||
162 | items.Add(itemInfo); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return items; |
||
168 | } |
||
169 | } |
||
3 | office | 170 | } |