WebDAVProperty.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Xml;
  2. namespace WebDAVSharp.Server
  3. {
  4. /// <summary>
  5. /// This class implements the core WebDAV server.
  6. /// </summary>
  7. internal class WebDavProperty
  8. {
  9. #region Variables
  10. public enum StoreItemTypeFlag
  11. {
  12. Document,
  13. Collection,
  14. Any
  15. }
  16. /// <summary>
  17. /// Used as a prefix
  18. /// </summary>
  19. public string Extended;
  20. /// <summary>
  21. /// This class implements the core WebDAV server.
  22. /// </summary>
  23. public string Name;
  24. /// <summary>
  25. /// This class implements the core WebDAV server.
  26. /// </summary>
  27. public string Namespace;
  28. public StoreItemTypeFlag StoreItemType;
  29. /// <summary>
  30. /// This class implements the core WebDAV server.
  31. /// </summary>
  32. public string Value;
  33. #endregion
  34. #region Constructor
  35. /// <summary>
  36. /// Standard constructor
  37. /// </summary>
  38. public WebDavProperty()
  39. {
  40. Namespace = string.Empty;
  41. Name = string.Empty;
  42. Value = string.Empty;
  43. Extended = "";
  44. StoreItemType = StoreItemTypeFlag.Any;
  45. }
  46. /// <summary>
  47. /// Constructor for the WebDAVProperty class with "DAV:" as namespace and an empty value
  48. /// </summary>
  49. /// <param name="name">The name of the WebDAV property</param>
  50. /// <param name="type"></param>
  51. public WebDavProperty(string name, StoreItemTypeFlag? type = null)
  52. {
  53. Name = name;
  54. Value = string.Empty;
  55. Namespace = "DAV:";
  56. Extended = "";
  57. StoreItemType = type ?? StoreItemTypeFlag.Any;
  58. }
  59. /// <summary>
  60. /// Constructor for the WebDAVProperty class with "DAV:" as namespace
  61. /// </summary>
  62. /// <param name="name">The name of the WebDAV property</param>
  63. /// <param name="value">The value of the WebDAV property</param>
  64. /// <param name="type"></param>
  65. public WebDavProperty(string name, string value, StoreItemTypeFlag? type = null)
  66. {
  67. Name = name;
  68. Value = value;
  69. Namespace = "DAV:";
  70. Extended = "";
  71. StoreItemType = type ?? StoreItemTypeFlag.Any;
  72. }
  73. /// <summary>
  74. /// Constructor for the WebDAVProperty class
  75. /// </summary>
  76. /// <param name="name">The name of the WebDAV property</param>
  77. /// <param name="value">The value of the WebDAV property</param>
  78. /// <param name="ns">The namespace of the WebDAV property</param>
  79. /// <param name="type"></param>
  80. public WebDavProperty(string name, string value, string ns, StoreItemTypeFlag? type = null)
  81. {
  82. Name = name;
  83. Value = value;
  84. Namespace = ns;
  85. Extended = "";
  86. StoreItemType = type ?? StoreItemTypeFlag.Any;
  87. }
  88. public WebDavProperty(string name, string value, string ns, string extended, StoreItemTypeFlag? type = null)
  89. {
  90. Name = name;
  91. Value = value;
  92. Namespace = ns;
  93. Extended = extended;
  94. StoreItemType = type ?? StoreItemTypeFlag.Any;
  95. }
  96. #endregion
  97. #region Functions
  98. /// <summary>
  99. /// This class implements the core WebDAV server.
  100. /// </summary>
  101. /// <returns>
  102. /// A <see cref="System.String" /> that represents this instance.
  103. /// </returns>
  104. public override string ToString()
  105. {
  106. return StartString() + Value + EndString();
  107. }
  108. /// <summary>
  109. /// This class implements the core WebDAV server.
  110. /// </summary>
  111. /// <returns>The begin tag of an XML element as a string</returns>
  112. public string StartString()
  113. {
  114. if (Namespace == "DAV:")
  115. return "<D:" + Name + ">";
  116. if (!string.IsNullOrEmpty(Extended))
  117. return "<" + Extended + ":" + Name + ">";
  118. return "<" + Name + " xmlns=\"" + Namespace + "\">";
  119. }
  120. /// <summary>
  121. /// This class implements the core WebDAV server.
  122. /// </summary>
  123. /// <returns>An empty XML element as a string</returns>
  124. public string EmptyString()
  125. {
  126. if (Namespace == "DAV:")
  127. return "<D:" + Name + "/>";
  128. if (!string.IsNullOrEmpty(Extended))
  129. return "<" + Extended + ":" + Name + "/>";
  130. return "<" + Name + " xmlns=\"" + Namespace + "\"/>";
  131. }
  132. /// <summary>
  133. /// This class implements the core WebDAV server.
  134. /// </summary>
  135. /// <returns>The closing tag of an XML element as a string</returns>
  136. public string EndString()
  137. {
  138. if (Namespace == "DAV:")
  139. return "</D:" + Name + ">";
  140. if (string.IsNullOrEmpty(Extended))
  141. return "</" + Extended + ":" + Name + ">";
  142. return "</" + Name + ">";
  143. }
  144. ///// <summary>
  145. ///// Creates an XmlDocumentFragment from the current WebDAVProperty
  146. ///// </summary>
  147. ///// <param name="doc">The XmlDocument where a XmlDocumentFragment is needed</param>
  148. ///// <returns>
  149. ///// The XmlDocumentFragment of the current WebDAVProperty object
  150. ///// </returns>
  151. //public XmlDocumentFragment ToXmlDocumentFragment(XmlDocument doc)
  152. //{
  153. // XmlDocumentFragment fragment = doc.CreateDocumentFragment();
  154. // fragment.InnerXml = ToString();
  155. // return fragment;
  156. //}
  157. /// <summary>
  158. /// reates an XmlElement from the current WebDAVProperty
  159. /// </summary>
  160. /// <param name="doc">The XmlDocument where a XmlElement is needed</param>
  161. /// <returns>
  162. /// The XmlElement of the current WebDAVProperty object
  163. /// </returns>
  164. public XmlElement ToXmlElement(XmlDocument doc)
  165. {
  166. // if the DocumentElement is not null, return the XmlElement with namespace
  167. if (doc.DocumentElement == null)
  168. return doc.CreateElement(Name);
  169. // Get the prefix of the namespace
  170. string prefix = doc.DocumentElement.GetPrefixOfNamespace(Namespace);
  171. // Create the element
  172. XmlElement element = doc.CreateElement(prefix, Name, Namespace);
  173. element.InnerText = Value;
  174. return element;
  175. // else, return XmlElement without namespace
  176. }
  177. ///// <summary>
  178. ///// reates an XmlElement from the current WebDAVProperty
  179. ///// </summary>
  180. ///// <param name="doc">The XmlDocument where a XmlElement is needed</param>
  181. ///// <returns>
  182. ///// The XmlElement of the current WebDAVProperty object
  183. ///// </returns>
  184. //public XmlElement XmlToXmlElement(XmlDocument doc)
  185. //{
  186. // // if the DocumentElement is not null, return the XmlElement with namespace
  187. // if (doc.DocumentElement == null)
  188. // return doc.CreateElement(Name);
  189. // // Get the prefix of the namespace
  190. // string prefix = doc.DocumentElement.GetPrefixOfNamespace(Namespace);
  191. // // Create the element
  192. // XmlElement element = doc.CreateElement(prefix, Name, Namespace);
  193. // element.InnerXml = Value;
  194. // return element;
  195. // // else, return XmlElement without namespace
  196. //}
  197. #endregion
  198. }
  199. }