XmlDocument.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. //
  2. // System.Xml.XmlDocument
  3. //
  4. // Author:
  5. // Daniel Weber ([email protected])
  6. //
  7. // (C) 2001 Daniel Weber
  8. using System;
  9. using System.IO;
  10. using System.Xml.XPath;
  11. using System.Diagnostics;
  12. namespace System.Xml
  13. {
  14. public class XmlDocument : XmlNode
  15. {
  16. #region Fields
  17. XmlLinkedNode lastLinkedChild;
  18. XmlNameTable nameTable;
  19. #endregion
  20. #region Constructors
  21. public XmlDocument () : base (null) { }
  22. [MonoTODO]
  23. protected internal XmlDocument (XmlImplementation imp) : base (null)
  24. {
  25. throw new NotImplementedException ();
  26. }
  27. public XmlDocument (XmlNameTable nt) : base (null)
  28. {
  29. nameTable = nt;
  30. }
  31. #endregion
  32. #region Events
  33. public event XmlNodeChangedEventHandler NodeChanged;
  34. public event XmlNodeChangedEventHandler NodeChanging;
  35. public event XmlNodeChangedEventHandler NodeInserted;
  36. public event XmlNodeChangedEventHandler NodeInserting;
  37. public event XmlNodeChangedEventHandler NodeRemoved;
  38. public event XmlNodeChangedEventHandler NodeRemoving;
  39. #endregion
  40. #region Properties
  41. [MonoTODO]
  42. public override string BaseURI {
  43. get { throw new NotImplementedException(); }
  44. }
  45. public XmlElement DocumentElement {
  46. get {
  47. XmlNode node = FirstChild;
  48. while (node != null) {
  49. if (node is XmlElement)
  50. break;
  51. node = node.NextSibling;
  52. }
  53. return node != null ? node as XmlElement : null;
  54. }
  55. }
  56. [MonoTODO]
  57. public virtual XmlDocumentType DocumentType {
  58. get { throw new NotImplementedException(); }
  59. }
  60. [MonoTODO]
  61. public XmlImplementation Implementation {
  62. get { throw new NotImplementedException(); }
  63. }
  64. [MonoTODO ("Setter.")]
  65. public override string InnerXml {
  66. get {
  67. // Not sure why this is an override. Passing through for now.
  68. return base.InnerXml;
  69. }
  70. set { throw new NotImplementedException(); }
  71. }
  72. public override bool IsReadOnly {
  73. get { return false; }
  74. }
  75. internal override XmlLinkedNode LastLinkedChild {
  76. get {
  77. return lastLinkedChild;
  78. }
  79. set {
  80. lastLinkedChild = value;
  81. }
  82. }
  83. public override string LocalName {
  84. get { return "#document"; }
  85. }
  86. public override string Name {
  87. get { return "#document"; }
  88. }
  89. public XmlNameTable NameTable {
  90. get { return nameTable; }
  91. }
  92. public override XmlNodeType NodeType {
  93. get { return XmlNodeType.Document; }
  94. }
  95. public override XmlDocument OwnerDocument {
  96. get { return null; }
  97. }
  98. [MonoTODO]
  99. public bool PreserveWhitespace {
  100. get { throw new NotImplementedException(); }
  101. set { throw new NotImplementedException(); }
  102. }
  103. [MonoTODO]
  104. public virtual XmlResolver XmlResolver {
  105. set { throw new NotImplementedException(); }
  106. }
  107. #endregion
  108. #region Methods
  109. [MonoTODO]
  110. public override XmlNode CloneNode (bool deep)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. public XmlAttribute CreateAttribute (string name)
  115. {
  116. return CreateAttribute (name, String.Empty);
  117. }
  118. public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
  119. {
  120. string prefix;
  121. string localName;
  122. ParseName (qualifiedName, out prefix, out localName);
  123. return CreateAttribute (prefix, localName, namespaceURI);
  124. }
  125. public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
  126. {
  127. if ((localName == null) || (localName == String.Empty))
  128. throw new ArgumentException ("The attribute local name cannot be empty.");
  129. return new XmlAttribute (prefix, localName, namespaceURI, this);
  130. }
  131. public virtual XmlCDataSection CreateCDataSection (string data)
  132. {
  133. return new XmlCDataSection (data, this);
  134. }
  135. public virtual XmlComment CreateComment (string data)
  136. {
  137. return new XmlComment(data, this);
  138. }
  139. [MonoTODO]
  140. protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. [MonoTODO]
  145. public virtual XmlDocumentFragment CreateDocumentFragment ()
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
  150. string systemId, string internalSubset)
  151. {
  152. return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
  153. }
  154. public XmlElement CreateElement (string name)
  155. {
  156. return CreateElement (name, String.Empty);
  157. }
  158. public XmlElement CreateElement (
  159. string qualifiedName,
  160. string namespaceURI)
  161. {
  162. string prefix;
  163. string localName;
  164. ParseName (qualifiedName, out prefix, out localName);
  165. return CreateElement (prefix, localName, namespaceURI);
  166. }
  167. public virtual XmlElement CreateElement (
  168. string prefix,
  169. string localName,
  170. string namespaceURI)
  171. {
  172. if ((localName == null) || (localName == String.Empty))
  173. throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
  174. return new XmlElement (prefix, localName, namespaceURI, this);
  175. }
  176. [MonoTODO]
  177. public virtual XmlEntityReference CreateEntityReference (string name)
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. [MonoTODO]
  182. protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. public virtual XmlNode CreateNode (
  187. string nodeTypeString,
  188. string name,
  189. string namespaceURI)
  190. {
  191. return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
  192. }
  193. public virtual XmlNode CreateNode (
  194. XmlNodeType type,
  195. string name,
  196. string namespaceURI)
  197. {
  198. string prefix = null;
  199. string localName = name;
  200. if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
  201. ParseName (name, out prefix, out localName);
  202. return CreateNode (type, prefix, localName, namespaceURI);
  203. }
  204. public virtual XmlNode CreateNode (
  205. XmlNodeType type,
  206. string prefix,
  207. string name,
  208. string namespaceURI)
  209. {
  210. switch (type) {
  211. case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
  212. case XmlNodeType.CDATA: return CreateCDataSection (null);
  213. case XmlNodeType.Comment: return CreateComment (null);
  214. case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
  215. case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
  216. case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
  217. case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
  218. case XmlNodeType.EntityReference: return CreateEntityReference (null);
  219. case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
  220. case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
  221. case XmlNodeType.Text: return CreateTextNode (null);
  222. case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
  223. case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
  224. default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
  225. "Specified argument was out of the range of valid values", type.ToString ()));
  226. }
  227. }
  228. public virtual XmlProcessingInstruction CreateProcessingInstruction (
  229. string target,
  230. string data)
  231. {
  232. return new XmlProcessingInstruction (target, data, this);
  233. }
  234. public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
  235. {
  236. foreach (char c in text)
  237. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  238. throw new ArgumentException ("Invalid whitespace characters.");
  239. return new XmlSignificantWhitespace (text, this);
  240. }
  241. public virtual XmlText CreateTextNode (string text)
  242. {
  243. return new XmlText (text, this);
  244. }
  245. public virtual XmlWhitespace CreateWhitespace (string text)
  246. {
  247. foreach (char c in text)
  248. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  249. throw new ArgumentException ("Invalid whitespace characters.");
  250. return new XmlWhitespace (text, this);
  251. }
  252. public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
  253. string standalone)
  254. {
  255. if (version != "1.0")
  256. throw new ArgumentException ("version string is not correct.");
  257. if ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
  258. throw new ArgumentException ("standalone string is not correct.");
  259. return new XmlDeclaration (version, encoding, standalone, this);
  260. }
  261. [MonoTODO]
  262. public virtual XmlElement GetElementById (string elementId)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. [MonoTODO]
  267. public virtual XmlNodeList GetElementsByTagName (string name)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. [MonoTODO]
  272. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  273. {
  274. throw new NotImplementedException();
  275. }
  276. private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
  277. {
  278. switch (nodeTypeString) {
  279. case "attribute": return XmlNodeType.Attribute;
  280. case "cdatasection": return XmlNodeType.CDATA;
  281. case "comment": return XmlNodeType.Comment;
  282. case "document": return XmlNodeType.Document;
  283. case "documentfragment": return XmlNodeType.DocumentFragment;
  284. case "documenttype": return XmlNodeType.DocumentType;
  285. case "element": return XmlNodeType.Element;
  286. case "entityreference": return XmlNodeType.EntityReference;
  287. case "processinginstruction": return XmlNodeType.ProcessingInstruction;
  288. case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
  289. case "text": return XmlNodeType.Text;
  290. case "whitespace": return XmlNodeType.Whitespace;
  291. default:
  292. throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
  293. }
  294. }
  295. [MonoTODO]
  296. public virtual XmlNode ImportNode (XmlNode node, bool deep)
  297. {
  298. throw new NotImplementedException ();
  299. }
  300. [MonoTODO]
  301. public virtual void Load (Stream inStream)
  302. {
  303. throw new NotImplementedException ();
  304. }
  305. public virtual void Load (string filename)
  306. {
  307. XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
  308. Load (xmlReader);
  309. }
  310. [MonoTODO]
  311. public virtual void Load (TextReader txtReader)
  312. {
  313. throw new NotImplementedException ();
  314. }
  315. public virtual void Load (XmlReader xmlReader)
  316. {
  317. // Reset our document
  318. // For now this just means removing all our children but later this
  319. // may turn out o need to call a private method that resets other things
  320. // like properties we have, etc.
  321. RemoveAll ();
  322. XmlNode currentNode = this;
  323. XmlNode newNode;
  324. while (xmlReader.Read ())
  325. {
  326. switch (xmlReader.NodeType) {
  327. case XmlNodeType.CDATA:
  328. newNode = CreateCDataSection(xmlReader.Value);
  329. currentNode.AppendChild (newNode);
  330. break;
  331. case XmlNodeType.Comment:
  332. newNode = CreateComment (xmlReader.Value);
  333. currentNode.AppendChild (newNode);
  334. break;
  335. case XmlNodeType.Element:
  336. XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
  337. currentNode.AppendChild (element);
  338. // set the element's attributes.
  339. while (xmlReader.MoveToNextAttribute ())
  340. element.SetAttribute (xmlReader.Name, xmlReader.Value);
  341. xmlReader.MoveToElement ();
  342. // if this element isn't empty, push it onto our "stack".
  343. if (!xmlReader.IsEmptyElement)
  344. currentNode = element;
  345. break;
  346. case XmlNodeType.EndElement:
  347. currentNode = currentNode.ParentNode;
  348. break;
  349. case XmlNodeType.ProcessingInstruction:
  350. newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
  351. currentNode.AppendChild (newNode);
  352. break;
  353. case XmlNodeType.Text:
  354. newNode = CreateTextNode (xmlReader.Value);
  355. currentNode.AppendChild (newNode);
  356. break;
  357. }
  358. }
  359. }
  360. public virtual void LoadXml (string xml)
  361. {
  362. XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
  363. Load (xmlReader);
  364. }
  365. internal void onNodeChanged (XmlNode node, XmlNode Parent)
  366. {
  367. if (NodeChanged != null)
  368. NodeInserted (node, new XmlNodeChangedEventArgs
  369. (XmlNodeChangedAction.Change,
  370. node, Parent, Parent));
  371. }
  372. internal void onNodeChanging(XmlNode node, XmlNode Parent)
  373. {
  374. if (NodeInserting != null)
  375. NodeChanging (node, new XmlNodeChangedEventArgs
  376. (XmlNodeChangedAction.Change,
  377. node, Parent, Parent));
  378. }
  379. internal void onNodeInserted (XmlNode node, XmlNode newParent)
  380. {
  381. if (NodeInserted != null)
  382. NodeInserted (node, new XmlNodeChangedEventArgs
  383. (XmlNodeChangedAction.Insert,
  384. node, null, newParent));
  385. }
  386. internal void onNodeInserting (XmlNode node, XmlNode newParent)
  387. {
  388. if (NodeInserting != null)
  389. NodeInserting (node, new XmlNodeChangedEventArgs
  390. (XmlNodeChangedAction.Insert,
  391. node, null, newParent));
  392. }
  393. internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
  394. {
  395. if (NodeRemoved != null)
  396. NodeRemoved (node, new XmlNodeChangedEventArgs
  397. (XmlNodeChangedAction.Remove,
  398. node, oldParent, null));
  399. }
  400. internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
  401. {
  402. if (NodeRemoving != null)
  403. NodeRemoving (node, new XmlNodeChangedEventArgs
  404. (XmlNodeChangedAction.Remove,
  405. node, oldParent, null));
  406. }
  407. private void ParseName (string name, out string prefix, out string localName)
  408. {
  409. int indexOfColon = name.IndexOf (':');
  410. if (indexOfColon != -1) {
  411. prefix = name.Substring (0, indexOfColon);
  412. localName = name.Substring (indexOfColon + 1);
  413. } else {
  414. prefix = "";
  415. localName = name;
  416. }
  417. }
  418. [MonoTODO]
  419. public virtual XmlNode ReadNode(XmlReader reader)
  420. {
  421. throw new NotImplementedException ();
  422. }
  423. [MonoTODO]
  424. public virtual void Save(Stream outStream)
  425. {
  426. throw new NotImplementedException ();
  427. }
  428. [MonoTODO]
  429. public virtual void Save (string filename)
  430. {
  431. throw new NotImplementedException ();
  432. }
  433. [MonoTODO]
  434. public virtual void Save (TextWriter writer)
  435. {
  436. throw new NotImplementedException ();
  437. }
  438. [MonoTODO]
  439. public virtual void Save (XmlWriter writer)
  440. {
  441. throw new NotImplementedException ();
  442. }
  443. public override void WriteContentTo (XmlWriter w)
  444. {
  445. foreach(XmlNode childNode in ChildNodes)
  446. childNode.WriteTo(w);
  447. }
  448. public override void WriteTo (XmlWriter w)
  449. {
  450. WriteContentTo(w);
  451. }
  452. #endregion
  453. }
  454. }