CefXmlReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class that supports the reading of XML data via the libxml streaming API.
  10. /// The methods of this class should only be called on the thread that creates
  11. /// the object.
  12. /// </summary>
  13. public sealed unsafe partial class CefXmlReader
  14. {
  15. /// <summary>
  16. /// Create a new CefXmlReader object. The returned object's methods can only
  17. /// be called from the thread that created the object.
  18. /// </summary>
  19. public static CefXmlReader Create(CefStreamReader stream, CefXmlEncoding encodingType, string uri)
  20. {
  21. if (stream == null) throw new ArgumentNullException("stream");
  22. fixed (char* uri_str = uri)
  23. {
  24. var n_uri = new cef_string_t(uri_str, uri != null ? uri.Length : 0);
  25. return CefXmlReader.FromNative(
  26. cef_xml_reader_t.create(stream.ToNative(), encodingType, &n_uri)
  27. );
  28. }
  29. }
  30. /// <summary>
  31. /// Moves the cursor to the next node in the document. This method must be
  32. /// called at least once to set the current cursor position. Returns true if
  33. /// the cursor position was set successfully.
  34. /// </summary>
  35. public bool MoveToNextNode()
  36. {
  37. return cef_xml_reader_t.move_to_next_node(_self) != 0;
  38. }
  39. /// <summary>
  40. /// Close the document. This should be called directly to ensure that cleanup
  41. /// occurs on the correct thread.
  42. /// </summary>
  43. public bool Close()
  44. {
  45. return cef_xml_reader_t.close(_self) != 0;
  46. }
  47. /// <summary>
  48. /// Returns true if an error has been reported by the XML parser.
  49. /// </summary>
  50. public bool HasError
  51. {
  52. get { return cef_xml_reader_t.has_error(_self) != 0; }
  53. }
  54. /// <summary>
  55. /// Returns the error string.
  56. /// </summary>
  57. public string Error
  58. {
  59. get
  60. {
  61. var n_result = cef_xml_reader_t.get_error(_self);
  62. return cef_string_userfree.ToString(n_result);
  63. }
  64. }
  65. /// <summary>
  66. /// The below methods retrieve data for the node at the current cursor
  67. /// position.
  68. /// Returns the node type.
  69. /// </summary>
  70. public CefXmlNodeType NodeType
  71. {
  72. get { return cef_xml_reader_t.get_type(_self); }
  73. }
  74. /// <summary>
  75. /// Returns the node depth. Depth starts at 0 for the root node.
  76. /// </summary>
  77. public int Depth
  78. {
  79. get { return cef_xml_reader_t.get_depth(_self); }
  80. }
  81. /// <summary>
  82. /// Returns the local name. See
  83. /// http://www.w3.org/TR/REC-xml-names/#NT-LocalPart for additional details.
  84. /// </summary>
  85. public string LocalName
  86. {
  87. get
  88. {
  89. var n_result = cef_xml_reader_t.get_local_name(_self);
  90. return cef_string_userfree.ToString(n_result);
  91. }
  92. }
  93. /// <summary>
  94. /// Returns the namespace prefix. See http://www.w3.org/TR/REC-xml-names/ for
  95. /// additional details.
  96. /// </summary>
  97. public string Prefix
  98. {
  99. get
  100. {
  101. var n_result = cef_xml_reader_t.get_prefix(_self);
  102. return cef_string_userfree.ToString(n_result);
  103. }
  104. }
  105. /// <summary>
  106. /// Returns the qualified name, equal to (Prefix:)LocalName. See
  107. /// http://www.w3.org/TR/REC-xml-names/#ns-qualnames for additional details.
  108. /// </summary>
  109. public string QualifiedName
  110. {
  111. get
  112. {
  113. var n_result = cef_xml_reader_t.get_qualified_name(_self);
  114. return cef_string_userfree.ToString(n_result);
  115. }
  116. }
  117. /// <summary>
  118. /// Returns the URI defining the namespace associated with the node. See
  119. /// http://www.w3.org/TR/REC-xml-names/ for additional details.
  120. /// </summary>
  121. public string NamespaceUri
  122. {
  123. get
  124. {
  125. var n_result = cef_xml_reader_t.get_namespace_uri(_self);
  126. return cef_string_userfree.ToString(n_result);
  127. }
  128. }
  129. /// <summary>
  130. /// Returns the base URI of the node. See http://www.w3.org/TR/xmlbase/ for
  131. /// additional details.
  132. /// </summary>
  133. public string BaseUri
  134. {
  135. get
  136. {
  137. var n_result = cef_xml_reader_t.get_base_uri(_self);
  138. return cef_string_userfree.ToString(n_result);
  139. }
  140. }
  141. /// <summary>
  142. /// Returns the xml:lang scope within which the node resides. See
  143. /// http://www.w3.org/TR/REC-xml/#sec-lang-tag for additional details.
  144. /// </summary>
  145. public string XmlLang
  146. {
  147. get
  148. {
  149. var n_result = cef_xml_reader_t.get_xml_lang(_self);
  150. return cef_string_userfree.ToString(n_result);
  151. }
  152. }
  153. /// <summary>
  154. /// Returns true if the node represents an empty element. <a/> is considered
  155. /// empty but <a></a> is not.
  156. /// </summary>
  157. public bool IsEmptyElement
  158. {
  159. get { return cef_xml_reader_t.is_empty_element(_self) != 0; }
  160. }
  161. /// <summary>
  162. /// Returns true if the node has a text value.
  163. /// </summary>
  164. public bool HasValue
  165. {
  166. get { return cef_xml_reader_t.has_value(_self) != 0; }
  167. }
  168. /// <summary>
  169. /// Returns the text value.
  170. /// </summary>
  171. public string Value
  172. {
  173. get
  174. {
  175. var n_result = cef_xml_reader_t.get_value(_self);
  176. return cef_string_userfree.ToString(n_result);
  177. }
  178. }
  179. /// <summary>
  180. /// Returns true if the node has attributes.
  181. /// </summary>
  182. public bool HasAttributes
  183. {
  184. get { return cef_xml_reader_t.has_attributes(_self) != 0; }
  185. }
  186. /// <summary>
  187. /// Returns the number of attributes.
  188. /// </summary>
  189. public int AttributeCount
  190. {
  191. get { return (int)cef_xml_reader_t.get_attribute_count(_self); }
  192. }
  193. /// <summary>
  194. /// Returns the value of the attribute at the specified 0-based index.
  195. /// </summary>
  196. public string GetAttribute(int index)
  197. {
  198. var n_result = cef_xml_reader_t.get_attribute_byindex(_self, index);
  199. return cef_string_userfree.ToString(n_result);
  200. }
  201. /// <summary>
  202. /// Returns the value of the attribute with the specified qualified name.
  203. /// </summary>
  204. public string GetAttribute(string qualifiedName)
  205. {
  206. fixed (char* qualifiedName_str = qualifiedName)
  207. {
  208. var n_qualifiedName = new cef_string_t(qualifiedName_str, qualifiedName != null ? qualifiedName.Length : 0);
  209. var n_result = cef_xml_reader_t.get_attribute_byqname(_self, &n_qualifiedName);
  210. return cef_string_userfree.ToString(n_result);
  211. }
  212. }
  213. /// <summary>
  214. /// Returns the value of the attribute with the specified local name and
  215. /// namespace URI.
  216. /// </summary>
  217. public string GetAttribute(string localName, string namespaceUri)
  218. {
  219. fixed (char* localName_str = localName)
  220. fixed (char* namespaceUri_str = namespaceUri)
  221. {
  222. var n_localName = new cef_string_t(localName_str, localName != null ? localName.Length : 0);
  223. var n_namespaceUri = new cef_string_t(namespaceUri_str, namespaceUri != null ? namespaceUri.Length : 0);
  224. var n_result = cef_xml_reader_t.get_attribute_bylname(_self, &n_localName, &n_namespaceUri);
  225. return cef_string_userfree.ToString(n_result);
  226. }
  227. }
  228. /// <summary>
  229. /// Returns an XML representation of the current node's children.
  230. /// </summary>
  231. public string GetInnerXml()
  232. {
  233. var n_result = cef_xml_reader_t.get_inner_xml(_self);
  234. return cef_string_userfree.ToString(n_result);
  235. }
  236. /// <summary>
  237. /// Returns an XML representation of the current node including its children.
  238. /// </summary>
  239. public string GetOuterXml()
  240. {
  241. var n_result = cef_xml_reader_t.get_outer_xml(_self);
  242. return cef_string_userfree.ToString(n_result);
  243. }
  244. /// <summary>
  245. /// Returns the line number for the current node.
  246. /// </summary>
  247. public int LineNumber
  248. {
  249. get { return cef_xml_reader_t.get_line_number(_self); }
  250. }
  251. /// <summary>
  252. /// Attribute nodes are not traversed by default. The below methods can be
  253. /// used to move the cursor to an attribute node. MoveToCarryingElement() can
  254. /// be called afterwards to return the cursor to the carrying element. The
  255. /// depth of an attribute node will be 1 + the depth of the carrying element.
  256. /// Moves the cursor to the attribute at the specified 0-based index. Returns
  257. /// true if the cursor position was set successfully.
  258. /// </summary>
  259. public bool MoveToAttribute(int index)
  260. {
  261. return cef_xml_reader_t.move_to_attribute_byindex(_self, index) != 0;
  262. }
  263. /// <summary>
  264. /// Moves the cursor to the attribute with the specified qualified name.
  265. /// Returns true if the cursor position was set successfully.
  266. /// </summary>
  267. public bool MoveToAttribute(string qualifiedName)
  268. {
  269. fixed (char* qualifiedName_str = qualifiedName)
  270. {
  271. var n_qualifiedName = new cef_string_t(qualifiedName_str, qualifiedName != null ? qualifiedName.Length : 0);
  272. return cef_xml_reader_t.move_to_attribute_byqname(_self, &n_qualifiedName) != 0;
  273. }
  274. }
  275. /// <summary>
  276. /// Moves the cursor to the attribute with the specified local name and
  277. /// namespace URI. Returns true if the cursor position was set successfully.
  278. /// </summary>
  279. public bool MoveToAttribute(string localName, string namespaceUri)
  280. {
  281. fixed (char* localName_str = localName)
  282. fixed (char* namespaceUri_str = namespaceUri)
  283. {
  284. var n_localName = new cef_string_t(localName_str, localName != null ? localName.Length : 0);
  285. var n_namespaceUri = new cef_string_t(namespaceUri_str, namespaceUri != null ? namespaceUri.Length : 0);
  286. return cef_xml_reader_t.move_to_attribute_bylname(_self, &n_localName, &n_namespaceUri) != 0;
  287. }
  288. }
  289. /// <summary>
  290. /// Moves the cursor to the first attribute in the current element. Returns
  291. /// true if the cursor position was set successfully.
  292. /// </summary>
  293. public bool MoveToFirstAttribute()
  294. {
  295. return cef_xml_reader_t.move_to_first_attribute(_self) != 0;
  296. }
  297. /// <summary>
  298. /// Moves the cursor to the next attribute in the current element. Returns
  299. /// true if the cursor position was set successfully.
  300. /// </summary>
  301. public bool MoveToNextAttribute()
  302. {
  303. return cef_xml_reader_t.move_to_next_attribute(_self) != 0;
  304. }
  305. /// <summary>
  306. /// Moves the cursor back to the carrying element. Returns true if the cursor
  307. /// position was set successfully.
  308. /// </summary>
  309. public bool MoveToCarryingElement()
  310. {
  311. return cef_xml_reader_t.move_to_carrying_element(_self) != 0;
  312. }
  313. }
  314. }