DTMXPathDocumentWriter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //
  2. // Mono.Xml.XPath.DTMXPathDocumentWriter
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.XPath;
  35. namespace Mono.Xml.XPath
  36. {
  37. #if OUTSIDE_SYSTEM_XML
  38. public
  39. #else
  40. internal
  41. #endif
  42. class DTMXPathDocumentWriter : XmlWriter
  43. {
  44. public DTMXPathDocumentWriter (XmlNameTable nt, int defaultCapacity)
  45. {
  46. nameTable = nt == null ? new NameTable () : nt;
  47. nodeCapacity = defaultCapacity;
  48. attributeCapacity = nodeCapacity;
  49. idTable = new Hashtable ();
  50. nodes = new DTMXPathLinkedNode [nodeCapacity];
  51. attributes = new DTMXPathAttributeNode [attributeCapacity];
  52. namespaces = new DTMXPathNamespaceNode [0];
  53. Init ();
  54. }
  55. XmlNameTable nameTable;
  56. int nodeCapacity = 200;
  57. int attributeCapacity = 200;
  58. int nsCapacity = 10;
  59. // Linked Node
  60. DTMXPathLinkedNode [] nodes;
  61. // Attribute
  62. DTMXPathAttributeNode [] attributes;
  63. // NamespaceNode
  64. DTMXPathNamespaceNode [] namespaces;
  65. // idTable [string value] -> int nodeId
  66. Hashtable idTable;
  67. int nodeIndex;
  68. int attributeIndex;
  69. int nsIndex;
  70. int parentForFirstChild;
  71. // for attribute processing; should be reset per each element.
  72. int firstAttributeIndex;
  73. int lastNsIndexInCurrent;
  74. int attrIndexAtStart;
  75. int nsIndexAtStart;
  76. int prevSibling;
  77. int lastNsInScope;
  78. // They are only used in Writer
  79. int writerDepth;
  80. WriteState state;
  81. bool openNamespace;
  82. bool isClosed;
  83. public DTMXPathDocument CreateDocument ()
  84. {
  85. if (!isClosed)
  86. Close ();
  87. return new DTMXPathDocument (nameTable,
  88. nodes,
  89. attributes,
  90. namespaces,
  91. idTable
  92. );
  93. }
  94. public void Init ()
  95. {
  96. // index 0 is dummy. No node (including Root) is assigned to this index
  97. // So that we can easily compare index != 0 instead of index < 0.
  98. // (Difference between jnz or jbe in 80x86.)
  99. AddNode (0, 0, 0, 0, XPathNodeType.All, "", false, "", "", "", "", "", 0, 0, 0);
  100. nodeIndex++;
  101. AddAttribute (0, null, null, null, null, 0, 0);
  102. AddNsNode (0, null, null, 0);
  103. nsIndex++;
  104. AddNsNode (1, "xml", XmlNamespaces.XML, 0);
  105. // add root.
  106. AddNode (0, 0, 0, -1, XPathNodeType.Root, null, false, "", "", "", "", "", 1, 0, 0);
  107. this.nodeIndex = 1;
  108. this.lastNsInScope = 1;
  109. this.parentForFirstChild = nodeIndex;
  110. state = WriteState.Content;
  111. }
  112. private int GetParentIndex ()
  113. {
  114. if (parentForFirstChild >= 0)
  115. return parentForFirstChild;
  116. int parent = nodeIndex;
  117. if (nodes [nodeIndex].Depth >= writerDepth) {
  118. // if not, then current node is parent.
  119. while (writerDepth <= nodes [parent].Depth)
  120. parent = nodes [parent].Parent;
  121. }
  122. return parent;
  123. }
  124. private int GetPreviousSiblingIndex ()
  125. {
  126. int prevSibling = nodeIndex;
  127. if (parentForFirstChild >= 0)
  128. prevSibling = 0;
  129. else
  130. while (nodes [prevSibling].Depth != writerDepth)
  131. prevSibling = nodes [prevSibling].Parent;
  132. return prevSibling;
  133. }
  134. private void UpdateTreeForAddition ()
  135. {
  136. int parent = GetParentIndex ();
  137. prevSibling = GetPreviousSiblingIndex ();
  138. nodeIndex++;
  139. if (prevSibling != 0)
  140. nodes [prevSibling].NextSibling = nodeIndex;
  141. if (parentForFirstChild >= 0)
  142. nodes [parent].FirstChild = nodeIndex;
  143. parentForFirstChild = -1;
  144. }
  145. private void CloseStartElement ()
  146. {
  147. if (attrIndexAtStart != attributeIndex)
  148. nodes [nodeIndex].FirstAttribute = attrIndexAtStart + 1;
  149. if (nsIndexAtStart != nsIndex) {
  150. nodes [nodeIndex].FirstNamespace = nsIndex;
  151. lastNsInScope = nsIndex;
  152. }
  153. if (!nodes [nodeIndex].IsEmptyElement)
  154. parentForFirstChild = nodeIndex;
  155. state = WriteState.Content;
  156. writerDepth++;
  157. }
  158. #region Adding Nodes
  159. private void SetNodeArrayLength (int size)
  160. {
  161. DTMXPathLinkedNode [] newArr = new DTMXPathLinkedNode [size];
  162. Array.Copy (nodes, newArr, System.Math.Min (size, nodes.Length));
  163. nodes = newArr;
  164. }
  165. private void SetAttributeArrayLength (int size)
  166. {
  167. DTMXPathAttributeNode [] newArr =
  168. new DTMXPathAttributeNode [size];
  169. Array.Copy (attributes, newArr, System.Math.Min (size, attributes.Length));
  170. attributes = newArr;
  171. }
  172. private void SetNsArrayLength (int size)
  173. {
  174. DTMXPathNamespaceNode [] newArr =
  175. new DTMXPathNamespaceNode [size];
  176. Array.Copy (namespaces, newArr, System.Math.Min (size, namespaces.Length));
  177. namespaces = newArr;
  178. }
  179. // Here followings are skipped: firstChild, nextSibling,
  180. public void AddNode (int parent, int firstAttribute, int previousSibling, int depth, XPathNodeType nodeType, string baseUri, bool isEmptyElement, string localName, string ns, string prefix, string value, string xmlLang, int namespaceNode, int lineNumber, int linePosition)
  181. {
  182. if (nodes.Length < nodeIndex + 1) {
  183. nodeCapacity *= 4;
  184. SetNodeArrayLength (nodeCapacity);
  185. }
  186. #if DTM_CLASS
  187. nodes [nodeIndex] = new DTMXPathLinkedNode ();
  188. #endif
  189. nodes [nodeIndex].FirstChild = 0; // dummy
  190. nodes [nodeIndex].Parent = parent;
  191. nodes [nodeIndex].FirstAttribute = firstAttribute;
  192. nodes [nodeIndex].PreviousSibling = previousSibling;
  193. nodes [nodeIndex].NextSibling = 0; // dummy
  194. nodes [nodeIndex].Depth = depth;
  195. nodes [nodeIndex].NodeType = nodeType;
  196. nodes [nodeIndex].BaseURI = baseUri;
  197. nodes [nodeIndex].IsEmptyElement = isEmptyElement;
  198. nodes [nodeIndex].LocalName = localName;
  199. nodes [nodeIndex].NamespaceURI = ns;
  200. nodes [nodeIndex].Prefix = prefix;
  201. nodes [nodeIndex].Value = value;
  202. nodes [nodeIndex].XmlLang = xmlLang;
  203. nodes [nodeIndex].FirstNamespace = namespaceNode;
  204. nodes [nodeIndex].LineNumber = lineNumber;
  205. nodes [nodeIndex].LinePosition = linePosition;
  206. }
  207. // Followings are skipped: nextAttribute,
  208. public void AddAttribute (int ownerElement, string localName, string ns, string prefix, string value, int lineNumber, int linePosition)
  209. {
  210. if (attributes.Length < attributeIndex + 1) {
  211. attributeCapacity *= 4;
  212. SetAttributeArrayLength (attributeCapacity);
  213. }
  214. #if DTM_CLASS
  215. attributes [attributeIndex] = new DTMXPathAttributeNode ();
  216. #endif
  217. attributes [attributeIndex].OwnerElement = ownerElement;
  218. attributes [attributeIndex].LocalName = localName;
  219. attributes [attributeIndex].NamespaceURI = ns;
  220. attributes [attributeIndex].Prefix = prefix;
  221. attributes [attributeIndex].Value = value;
  222. attributes [attributeIndex].LineNumber = lineNumber;
  223. attributes [attributeIndex].LinePosition = linePosition;
  224. }
  225. // Followings are skipped: nextNsNode (may be next attribute in the same element, or ancestors' nsNode)
  226. public void AddNsNode (int declaredElement, string name, string ns, int nextNs)
  227. {
  228. if (namespaces.Length < nsIndex + 1) {
  229. nsCapacity *= 4;
  230. SetNsArrayLength (nsCapacity);
  231. }
  232. #if DTM_CLASS
  233. namespaces [nsIndex] = new DTMXPathNamespaceNode ();
  234. #endif
  235. namespaces [nsIndex].DeclaredElement = declaredElement;
  236. namespaces [nsIndex].Name = name;
  237. namespaces [nsIndex].Namespace = ns;
  238. namespaces [nsIndex].NextNamespace = nextNs;
  239. }
  240. #endregion
  241. #region XmlWriter methods
  242. // They are not supported
  243. public override string XmlLang { get { return null; } }
  244. public override XmlSpace XmlSpace { get { return XmlSpace.None; } }
  245. public override WriteState WriteState { get { return state; } }
  246. public override void Close ()
  247. {
  248. // Fixup arrays
  249. SetNodeArrayLength (nodeIndex + 1);
  250. SetAttributeArrayLength (attributeIndex + 1);
  251. SetNsArrayLength (nsIndex + 1);
  252. isClosed = true;
  253. }
  254. public override void Flush ()
  255. {
  256. // do nothing
  257. }
  258. public override string LookupPrefix (string ns)
  259. {
  260. int tmp = nsIndex;
  261. while (tmp != 0) {
  262. if (namespaces [tmp].Namespace == ns)
  263. return namespaces [tmp].Name;
  264. tmp = namespaces [tmp].NextNamespace;
  265. }
  266. return null;
  267. }
  268. public override void WriteCData (string data)
  269. {
  270. AddTextNode (data);
  271. }
  272. private void AddTextNode (string data)
  273. {
  274. switch (state) {
  275. case WriteState.Element:
  276. CloseStartElement ();
  277. break;
  278. case WriteState.Content:
  279. break;
  280. default:
  281. throw new InvalidOperationException ("Invalid document state for CDATA section: " + state);
  282. }
  283. // When text after text, just add the value, and return.
  284. if (nodes [nodeIndex].Depth == writerDepth) {
  285. switch (nodes [nodeIndex].NodeType) {
  286. case XPathNodeType.Text:
  287. case XPathNodeType.SignificantWhitespace:
  288. nodes [nodeIndex].Value += data;
  289. if (IsWhitespace (data))
  290. nodes [nodeIndex].NodeType = XPathNodeType.SignificantWhitespace;
  291. else
  292. nodes [nodeIndex].NodeType = XPathNodeType.Text;
  293. return;
  294. }
  295. }
  296. int parent = GetParentIndex ();
  297. UpdateTreeForAddition ();
  298. AddNode (parent,
  299. 0, // attribute
  300. prevSibling,
  301. writerDepth,
  302. XPathNodeType.Text,
  303. null,
  304. false,
  305. null,
  306. String.Empty,
  307. String.Empty,
  308. data,
  309. null,
  310. 0, // nsIndex
  311. 0, // line info
  312. 0);
  313. }
  314. private void CheckTopLevelNode ()
  315. {
  316. switch (state) {
  317. case WriteState.Element:
  318. CloseStartElement ();
  319. break;
  320. case WriteState.Content:
  321. case WriteState.Prolog:
  322. case WriteState.Start:
  323. break;
  324. default:
  325. throw new InvalidOperationException ("Invalid document state for CDATA section: " + state);
  326. }
  327. }
  328. public override void WriteComment (string data)
  329. {
  330. CheckTopLevelNode ();
  331. int parent = GetParentIndex ();
  332. UpdateTreeForAddition ();
  333. AddNode (parent,
  334. 0, // attribute
  335. prevSibling,
  336. writerDepth,
  337. XPathNodeType.Comment,
  338. null,
  339. false,
  340. null,
  341. String.Empty,
  342. String.Empty,
  343. data,
  344. null,
  345. 0, // nsIndex
  346. 0, // line info
  347. 0);
  348. }
  349. public override void WriteProcessingInstruction (string name, string data)
  350. {
  351. CheckTopLevelNode ();
  352. int parent = GetParentIndex ();
  353. UpdateTreeForAddition ();
  354. AddNode (parent,
  355. 0, // attribute
  356. prevSibling,
  357. writerDepth,
  358. XPathNodeType.ProcessingInstruction,
  359. null,
  360. false,
  361. name,
  362. String.Empty,
  363. String.Empty,
  364. data,
  365. null,
  366. 0, // nsIndex
  367. 0, // line info
  368. 0);
  369. }
  370. public override void WriteWhitespace (string data)
  371. {
  372. CheckTopLevelNode ();
  373. int parent = GetParentIndex ();
  374. UpdateTreeForAddition ();
  375. AddNode (parent,
  376. 0, // attribute
  377. prevSibling,
  378. writerDepth,
  379. XPathNodeType.Whitespace,
  380. null,
  381. false,
  382. null,
  383. String.Empty,
  384. String.Empty,
  385. data,
  386. null,
  387. 0, // nsIndex
  388. 0, // line info
  389. 0);
  390. }
  391. public override void WriteStartDocument ()
  392. {
  393. // do nothing
  394. }
  395. public override void WriteStartDocument (bool standalone)
  396. {
  397. // do nothing
  398. }
  399. public override void WriteEndDocument ()
  400. {
  401. // do nothing
  402. }
  403. public override void WriteStartElement (string prefix, string localName, string ns)
  404. {
  405. switch (state) {
  406. case WriteState.Element:
  407. CloseStartElement ();
  408. break;
  409. case WriteState.Start:
  410. case WriteState.Prolog:
  411. case WriteState.Content:
  412. break;
  413. default:
  414. throw new InvalidOperationException ("Invalid document state for writing element: " + state);
  415. }
  416. int parent = GetParentIndex ();
  417. UpdateTreeForAddition ();
  418. WriteStartElement (parent, prevSibling, prefix, localName, ns);
  419. state = WriteState.Element;
  420. }
  421. private void WriteStartElement (int parent, int previousSibling, string prefix, string localName, string ns)
  422. {
  423. PrepareStartElement (previousSibling);
  424. AddNode (parent,
  425. 0, // dummy:firstAttribute
  426. previousSibling,
  427. writerDepth,
  428. XPathNodeType.Element,
  429. null,
  430. false,
  431. localName,
  432. ns,
  433. prefix,
  434. "", // Element has no internal value.
  435. null,
  436. lastNsInScope,
  437. 0,
  438. 0);
  439. }
  440. private void PrepareStartElement (int previousSibling)
  441. {
  442. firstAttributeIndex = 0;
  443. lastNsIndexInCurrent = 0;
  444. attrIndexAtStart = attributeIndex;
  445. nsIndexAtStart = nsIndex;
  446. while (namespaces [lastNsInScope].DeclaredElement == previousSibling) {
  447. lastNsInScope = namespaces [lastNsInScope].NextNamespace;
  448. }
  449. }
  450. public override void WriteEndElement ()
  451. {
  452. WriteEndElement (false);
  453. }
  454. public override void WriteFullEndElement ()
  455. {
  456. WriteEndElement (true);
  457. }
  458. private void WriteEndElement (bool full)
  459. {
  460. switch (state) {
  461. case WriteState.Element:
  462. CloseStartElement ();
  463. break;
  464. case WriteState.Content:
  465. break;
  466. default:
  467. throw new InvalidOperationException ("Invalid state for writing EndElement: " + state);
  468. }
  469. parentForFirstChild = -1;
  470. if (nodes [nodeIndex].NodeType == XPathNodeType.Element) {
  471. if (!full)
  472. nodes [nodeIndex].IsEmptyElement = true;
  473. }
  474. writerDepth--;
  475. }
  476. public override void WriteStartAttribute (string prefix, string localName, string ns)
  477. {
  478. if (state != WriteState.Element)
  479. throw new InvalidOperationException ("Invalid document state for attribute: " + state);
  480. state = WriteState.Attribute;
  481. if (ns == XmlNamespaces.XMLNS)
  482. ProcessNamespace ((prefix == null || prefix == String.Empty) ? "" : localName, String.Empty); // dummy: Value should be completed
  483. else
  484. ProcessAttribute (prefix, localName, ns, String.Empty); // dummy: Value should be completed
  485. }
  486. private void ProcessNamespace (string prefix, string ns)
  487. {
  488. nsIndex++;
  489. int nextTmp = lastNsIndexInCurrent == 0 ? nodes [nodeIndex].FirstNamespace : lastNsIndexInCurrent;
  490. this.AddNsNode (nodeIndex,
  491. prefix,
  492. ns,
  493. nextTmp);
  494. lastNsIndexInCurrent = nsIndex;
  495. openNamespace = true;
  496. }
  497. private void ProcessAttribute (string prefix, string localName, string ns, string value)
  498. {
  499. attributeIndex ++;
  500. this.AddAttribute (nodeIndex,
  501. localName,
  502. ns,
  503. prefix != null ? prefix : String.Empty,
  504. value,
  505. 0,
  506. 0);
  507. if (firstAttributeIndex == 0)
  508. firstAttributeIndex = attributeIndex;
  509. else
  510. attributes [attributeIndex - 1].NextAttribute = attributeIndex;
  511. }
  512. public override void WriteEndAttribute ()
  513. {
  514. if (state != WriteState.Attribute)
  515. throw new InvalidOperationException ();
  516. openNamespace = false;
  517. state = WriteState.Element;
  518. }
  519. public override void WriteString (string text)
  520. {
  521. if (WriteState == WriteState.Attribute) {
  522. if (openNamespace)
  523. namespaces [nsIndex].Namespace += text;
  524. else
  525. attributes [attributeIndex].Value += text;
  526. }
  527. else
  528. AddTextNode (text);
  529. }
  530. // Well, they cannot be supported, but actually used to
  531. // disable-output-escaping = "true"
  532. public override void WriteRaw (string data)
  533. {
  534. WriteString (data);
  535. }
  536. public override void WriteRaw (char [] data, int start, int len)
  537. {
  538. WriteString (new string (data, start, len));
  539. }
  540. public override void WriteName (string name)
  541. {
  542. WriteString (name);
  543. }
  544. public override void WriteNmToken (string name)
  545. {
  546. WriteString (name);
  547. }
  548. public override void WriteBase64 (byte [] buffer, int index, int count)
  549. {
  550. throw new NotSupportedException ();
  551. }
  552. public override void WriteBinHex (byte [] buffer, int index, int count)
  553. {
  554. throw new NotSupportedException ();
  555. }
  556. public override void WriteChars (char [] buffer, int index, int count)
  557. {
  558. throw new NotSupportedException ();
  559. }
  560. public override void WriteCharEntity (char c)
  561. {
  562. throw new NotSupportedException ();
  563. }
  564. public override void WriteDocType (string name, string pub, string sys, string intSubset)
  565. {
  566. throw new NotSupportedException ();
  567. }
  568. public override void WriteEntityRef (string name)
  569. {
  570. throw new NotSupportedException ();
  571. }
  572. public override void WriteQualifiedName (string localName, string ns)
  573. {
  574. throw new NotSupportedException ();
  575. }
  576. public override void WriteSurrogateCharEntity (char high, char low)
  577. {
  578. throw new NotSupportedException ();
  579. }
  580. private bool IsWhitespace (string data)
  581. {
  582. for (int i = 0; i < data.Length; i++) {
  583. switch (data [i]) {
  584. case ' ':
  585. case '\r':
  586. case '\n':
  587. case '\t':
  588. continue;
  589. default:
  590. return false;
  591. }
  592. }
  593. return true;
  594. }
  595. #endregion
  596. }
  597. }