DTMXPathDocumentWriter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. public DTMXPathDocument CreateDocument ()
  83. {
  84. return new DTMXPathDocument (nameTable,
  85. nodes,
  86. attributes,
  87. namespaces,
  88. idTable
  89. );
  90. }
  91. public void Init ()
  92. {
  93. // index 0 is dummy. No node (including Root) is assigned to this index
  94. // So that we can easily compare index != 0 instead of index < 0.
  95. // (Difference between jnz or jbe in 80x86.)
  96. AddNode (0, 0, 0, 0, XPathNodeType.All, "", false, "", "", "", "", "", 0, 0, 0);
  97. nodeIndex++;
  98. AddAttribute (0, null, null, null, null, null, 0, 0);
  99. AddNsNode (0, null, null, 0);
  100. nsIndex++;
  101. AddNsNode (1, "xml", XmlNamespaces.XML, 0);
  102. // add root.
  103. AddNode (0, 0, 0, -1, XPathNodeType.Root, null, false, "", "", "", "", "", 1, 0, 0);
  104. this.nodeIndex = 1;
  105. this.lastNsInScope = 1;
  106. this.parentForFirstChild = nodeIndex;
  107. state = WriteState.Content;
  108. }
  109. private int GetParentIndex ()
  110. {
  111. if (parentForFirstChild >= 0)
  112. return parentForFirstChild;
  113. int parent = nodeIndex;
  114. if (nodes [nodeIndex].Depth >= writerDepth) {
  115. // if not, then current node is parent.
  116. while (writerDepth <= nodes [parent].Depth)
  117. parent = nodes [parent].Parent;
  118. }
  119. return parent;
  120. }
  121. private int GetPreviousSiblingIndex ()
  122. {
  123. int prevSibling = nodeIndex;
  124. if (parentForFirstChild >= 0)
  125. prevSibling = 0;
  126. else
  127. while (nodes [prevSibling].Depth != writerDepth)
  128. prevSibling = nodes [prevSibling].Parent;
  129. return prevSibling;
  130. }
  131. private void UpdateTreeForAddition ()
  132. {
  133. int parent = GetParentIndex ();
  134. prevSibling = GetPreviousSiblingIndex ();
  135. nodeIndex++;
  136. if (prevSibling != 0)
  137. nodes [prevSibling].NextSibling = nodeIndex;
  138. if (parentForFirstChild >= 0)
  139. nodes [parent].FirstChild = nodeIndex;
  140. parentForFirstChild = -1;
  141. }
  142. private void CloseStartElement ()
  143. {
  144. if (attrIndexAtStart != attributeIndex)
  145. nodes [nodeIndex].FirstAttribute = attrIndexAtStart + 1;
  146. if (nsIndexAtStart != nsIndex) {
  147. nodes [nodeIndex].FirstNamespace = nsIndex;
  148. lastNsInScope = nsIndex;
  149. }
  150. if (!nodes [nodeIndex].IsEmptyElement)
  151. parentForFirstChild = nodeIndex;
  152. state = WriteState.Content;
  153. writerDepth++;
  154. }
  155. #region Adding Nodes
  156. private void SetNodeArrayLength (int size)
  157. {
  158. DTMXPathLinkedNode [] newArr = new DTMXPathLinkedNode [size];
  159. Array.Copy (nodes, newArr, System.Math.Min (size, nodes.Length));
  160. nodes = newArr;
  161. }
  162. private void SetAttributeArrayLength (int size)
  163. {
  164. DTMXPathAttributeNode [] newArr =
  165. new DTMXPathAttributeNode [size];
  166. Array.Copy (attributes, newArr, System.Math.Min (size, attributes.Length));
  167. attributes = newArr;
  168. }
  169. private void SetNsArrayLength (int size)
  170. {
  171. DTMXPathNamespaceNode [] newArr =
  172. new DTMXPathNamespaceNode [size];
  173. Array.Copy (namespaces, newArr, System.Math.Min (size, namespaces.Length));
  174. namespaces = newArr;
  175. }
  176. // Here followings are skipped: firstChild, nextSibling,
  177. 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)
  178. {
  179. if (nodes.Length < nodeIndex + 1) {
  180. nodeCapacity *= 4;
  181. SetNodeArrayLength (nodeCapacity);
  182. }
  183. #if DTM_CLASS
  184. nodes [nodeIndex] = new DTMXPathLinkedNode ();
  185. #endif
  186. nodes [nodeIndex].FirstChild = 0; // dummy
  187. nodes [nodeIndex].Parent = parent;
  188. nodes [nodeIndex].FirstAttribute = firstAttribute;
  189. nodes [nodeIndex].PreviousSibling = previousSibling;
  190. nodes [nodeIndex].NextSibling = 0; // dummy
  191. nodes [nodeIndex].Depth = depth;
  192. nodes [nodeIndex].NodeType = nodeType;
  193. nodes [nodeIndex].BaseURI = baseUri;
  194. nodes [nodeIndex].IsEmptyElement = isEmptyElement;
  195. nodes [nodeIndex].LocalName = localName;
  196. nodes [nodeIndex].NamespaceURI = ns;
  197. nodes [nodeIndex].Prefix = prefix;
  198. nodes [nodeIndex].Value = value;
  199. nodes [nodeIndex].XmlLang = xmlLang;
  200. nodes [nodeIndex].FirstNamespace = namespaceNode;
  201. nodes [nodeIndex].LineNumber = lineNumber;
  202. nodes [nodeIndex].LinePosition = linePosition;
  203. }
  204. // Followings are skipped: nextAttribute,
  205. public void AddAttribute (int ownerElement, string localName, string ns, string prefix, string value, object schemaType, int lineNumber, int linePosition)
  206. {
  207. if (attributes.Length < attributeIndex + 1) {
  208. attributeCapacity *= 4;
  209. SetAttributeArrayLength (attributeCapacity);
  210. }
  211. #if DTM_CLASS
  212. attributes [attributeIndex] = new DTMXPathAttributeNode ();
  213. #endif
  214. attributes [attributeIndex].OwnerElement = ownerElement;
  215. attributes [attributeIndex].LocalName = localName;
  216. attributes [attributeIndex].NamespaceURI = ns;
  217. attributes [attributeIndex].Prefix = prefix;
  218. attributes [attributeIndex].Value = value;
  219. attributes [attributeIndex].SchemaType = schemaType;
  220. attributes [attributeIndex].LineNumber = lineNumber;
  221. attributes [attributeIndex].LinePosition = linePosition;
  222. }
  223. // Followings are skipped: nextNsNode (may be next attribute in the same element, or ancestors' nsNode)
  224. public void AddNsNode (int declaredElement, string name, string ns, int nextNs)
  225. {
  226. if (namespaces.Length < nsIndex + 1) {
  227. nsCapacity *= 4;
  228. SetNsArrayLength (nsCapacity);
  229. }
  230. #if DTM_CLASS
  231. namespaces [nsIndex] = new DTMXPathNamespaceNode ();
  232. #endif
  233. namespaces [nsIndex].DeclaredElement = declaredElement;
  234. namespaces [nsIndex].Name = name;
  235. namespaces [nsIndex].Namespace = ns;
  236. namespaces [nsIndex].NextNamespace = nextNs;
  237. }
  238. #endregion
  239. #region XmlWriter methods
  240. // They are not supported
  241. public override string XmlLang { get { return null; } }
  242. public override XmlSpace XmlSpace { get { return XmlSpace.None; } }
  243. public override WriteState WriteState { get { return state; } }
  244. public override void Close ()
  245. {
  246. // Fixup arrays
  247. SetNodeArrayLength (nodeIndex + 1);
  248. SetAttributeArrayLength (attributeIndex + 1);
  249. SetNsArrayLength (nsIndex + 1);
  250. }
  251. public override void Flush ()
  252. {
  253. // do nothing
  254. }
  255. public override string LookupPrefix (string ns)
  256. {
  257. int tmp = nsIndex;
  258. while (tmp != 0) {
  259. if (namespaces [tmp].Namespace == ns)
  260. return namespaces [tmp].Name;
  261. tmp = namespaces [tmp].NextNamespace;
  262. }
  263. return null;
  264. }
  265. public override void WriteCData (string data)
  266. {
  267. AddTextNode (data);
  268. }
  269. private void AddTextNode (string data)
  270. {
  271. switch (state) {
  272. case WriteState.Element:
  273. CloseStartElement ();
  274. break;
  275. case WriteState.Content:
  276. break;
  277. default:
  278. throw new InvalidOperationException ("Invalid document state for CDATA section: " + state);
  279. }
  280. // When text after text, just add the value, and return.
  281. if (nodes [nodeIndex].Depth == writerDepth) {
  282. switch (nodes [nodeIndex].NodeType) {
  283. case XPathNodeType.Text:
  284. case XPathNodeType.SignificantWhitespace:
  285. nodes [nodeIndex].Value += data;
  286. if (IsWhitespace (data))
  287. nodes [nodeIndex].NodeType = XPathNodeType.SignificantWhitespace;
  288. else
  289. nodes [nodeIndex].NodeType = XPathNodeType.Text;
  290. return;
  291. }
  292. }
  293. int parent = GetParentIndex ();
  294. UpdateTreeForAddition ();
  295. AddNode (parent,
  296. 0, // attribute
  297. prevSibling,
  298. writerDepth,
  299. XPathNodeType.Text,
  300. null,
  301. false,
  302. null,
  303. String.Empty,
  304. String.Empty,
  305. data,
  306. null,
  307. 0, // nsIndex
  308. 0, // line info
  309. 0);
  310. }
  311. private void CheckTopLevelNode ()
  312. {
  313. switch (state) {
  314. case WriteState.Element:
  315. CloseStartElement ();
  316. break;
  317. case WriteState.Content:
  318. case WriteState.Prolog:
  319. case WriteState.Start:
  320. break;
  321. default:
  322. throw new InvalidOperationException ("Invalid document state for CDATA section: " + state);
  323. }
  324. }
  325. public override void WriteComment (string data)
  326. {
  327. CheckTopLevelNode ();
  328. int parent = GetParentIndex ();
  329. UpdateTreeForAddition ();
  330. AddNode (parent,
  331. 0, // attribute
  332. prevSibling,
  333. writerDepth,
  334. XPathNodeType.Comment,
  335. null,
  336. false,
  337. null,
  338. String.Empty,
  339. String.Empty,
  340. data,
  341. null,
  342. 0, // nsIndex
  343. 0, // line info
  344. 0);
  345. }
  346. public override void WriteProcessingInstruction (string name, string data)
  347. {
  348. CheckTopLevelNode ();
  349. int parent = GetParentIndex ();
  350. UpdateTreeForAddition ();
  351. AddNode (parent,
  352. 0, // attribute
  353. prevSibling,
  354. writerDepth,
  355. XPathNodeType.ProcessingInstruction,
  356. null,
  357. false,
  358. name,
  359. String.Empty,
  360. String.Empty,
  361. data,
  362. null,
  363. 0, // nsIndex
  364. 0, // line info
  365. 0);
  366. }
  367. public override void WriteWhitespace (string data)
  368. {
  369. CheckTopLevelNode ();
  370. int parent = GetParentIndex ();
  371. UpdateTreeForAddition ();
  372. AddNode (parent,
  373. 0, // attribute
  374. prevSibling,
  375. writerDepth,
  376. XPathNodeType.Whitespace,
  377. null,
  378. false,
  379. null,
  380. String.Empty,
  381. String.Empty,
  382. data,
  383. null,
  384. 0, // nsIndex
  385. 0, // line info
  386. 0);
  387. }
  388. public override void WriteStartDocument ()
  389. {
  390. // do nothing
  391. }
  392. public override void WriteStartDocument (bool standalone)
  393. {
  394. // do nothing
  395. }
  396. public override void WriteEndDocument ()
  397. {
  398. // do nothing
  399. }
  400. public override void WriteStartElement (string prefix, string localName, string ns)
  401. {
  402. switch (state) {
  403. case WriteState.Element:
  404. CloseStartElement ();
  405. break;
  406. case WriteState.Start:
  407. case WriteState.Prolog:
  408. case WriteState.Content:
  409. break;
  410. default:
  411. throw new InvalidOperationException ("Invalid document state for writing element: " + state);
  412. }
  413. int parent = GetParentIndex ();
  414. UpdateTreeForAddition ();
  415. WriteStartElement (parent, prevSibling, prefix, localName, ns);
  416. state = WriteState.Element;
  417. }
  418. private void WriteStartElement (int parent, int previousSibling, string prefix, string localName, string ns)
  419. {
  420. PrepareStartElement (previousSibling);
  421. AddNode (parent,
  422. 0, // dummy:firstAttribute
  423. previousSibling,
  424. writerDepth,
  425. XPathNodeType.Element,
  426. null,
  427. false,
  428. localName,
  429. ns,
  430. prefix,
  431. "", // Element has no internal value.
  432. null,
  433. lastNsInScope,
  434. 0,
  435. 0);
  436. }
  437. private void PrepareStartElement (int previousSibling)
  438. {
  439. firstAttributeIndex = 0;
  440. lastNsIndexInCurrent = 0;
  441. attrIndexAtStart = attributeIndex;
  442. nsIndexAtStart = nsIndex;
  443. while (namespaces [lastNsInScope].DeclaredElement == previousSibling) {
  444. lastNsInScope = namespaces [lastNsInScope].NextNamespace;
  445. }
  446. }
  447. public override void WriteEndElement ()
  448. {
  449. WriteEndElement (false);
  450. }
  451. public override void WriteFullEndElement ()
  452. {
  453. WriteEndElement (true);
  454. }
  455. private void WriteEndElement (bool full)
  456. {
  457. switch (state) {
  458. case WriteState.Element:
  459. CloseStartElement ();
  460. break;
  461. case WriteState.Content:
  462. break;
  463. default:
  464. throw new InvalidOperationException ("Invalid state for writing EndElement: " + state);
  465. }
  466. parentForFirstChild = -1;
  467. if (nodes [nodeIndex].NodeType == XPathNodeType.Element) {
  468. if (!full)
  469. nodes [nodeIndex].IsEmptyElement = true;
  470. }
  471. writerDepth--;
  472. }
  473. public override void WriteStartAttribute (string prefix, string localName, string ns)
  474. {
  475. if (state != WriteState.Element)
  476. throw new InvalidOperationException ("Invalid document state for attribute: " + state);
  477. state = WriteState.Attribute;
  478. if (ns == XmlNamespaces.XMLNS)
  479. ProcessNamespace ((prefix == null || prefix == String.Empty) ? "" : localName, String.Empty); // dummy: Value should be completed
  480. else
  481. ProcessAttribute (prefix, localName, ns, String.Empty); // dummy: Value should be completed
  482. }
  483. private void ProcessNamespace (string prefix, string ns)
  484. {
  485. nsIndex++;
  486. int nextTmp = lastNsIndexInCurrent == 0 ? nodes [nodeIndex].FirstNamespace : lastNsIndexInCurrent;
  487. this.AddNsNode (nodeIndex,
  488. prefix,
  489. ns,
  490. nextTmp);
  491. lastNsIndexInCurrent = nsIndex;
  492. openNamespace = true;
  493. }
  494. private void ProcessAttribute (string prefix, string localName, string ns, string value)
  495. {
  496. attributeIndex ++;
  497. this.AddAttribute (nodeIndex,
  498. localName,
  499. ns,
  500. prefix != null ? prefix : String.Empty,
  501. value,
  502. null,
  503. 0,
  504. 0);
  505. if (firstAttributeIndex == 0)
  506. firstAttributeIndex = attributeIndex;
  507. else
  508. attributes [attributeIndex - 1].NextAttribute = attributeIndex;
  509. }
  510. public override void WriteEndAttribute ()
  511. {
  512. if (state != WriteState.Attribute)
  513. throw new InvalidOperationException ();
  514. openNamespace = false;
  515. state = WriteState.Element;
  516. }
  517. public override void WriteString (string text)
  518. {
  519. if (WriteState == WriteState.Attribute) {
  520. if (openNamespace)
  521. namespaces [nsIndex].Namespace += text;
  522. else
  523. attributes [attributeIndex].Value += text;
  524. }
  525. else
  526. AddTextNode (text);
  527. }
  528. // Well, they cannot be supported, but actually used to
  529. // disable-output-escaping = "true"
  530. public override void WriteRaw (string data)
  531. {
  532. WriteString (data);
  533. }
  534. public override void WriteRaw (char [] data, int start, int len)
  535. {
  536. WriteString (new string (data, start, len));
  537. }
  538. public override void WriteName (string name)
  539. {
  540. WriteString (name);
  541. }
  542. public override void WriteNmToken (string name)
  543. {
  544. WriteString (name);
  545. }
  546. public override void WriteBase64 (byte [] buffer, int index, int count)
  547. {
  548. throw new NotSupportedException ();
  549. }
  550. public override void WriteBinHex (byte [] buffer, int index, int count)
  551. {
  552. throw new NotSupportedException ();
  553. }
  554. public override void WriteChars (char [] buffer, int index, int count)
  555. {
  556. throw new NotSupportedException ();
  557. }
  558. public override void WriteCharEntity (char c)
  559. {
  560. throw new NotSupportedException ();
  561. }
  562. public override void WriteDocType (string name, string pub, string sys, string intSubset)
  563. {
  564. throw new NotSupportedException ();
  565. }
  566. public override void WriteEntityRef (string name)
  567. {
  568. throw new NotSupportedException ();
  569. }
  570. public override void WriteQualifiedName (string localName, string ns)
  571. {
  572. throw new NotSupportedException ();
  573. }
  574. public override void WriteSurrogateCharEntity (char high, char low)
  575. {
  576. throw new NotSupportedException ();
  577. }
  578. private bool IsWhitespace (string data)
  579. {
  580. for (int i = 0; i < data.Length; i++) {
  581. switch (data [i]) {
  582. case ' ':
  583. case '\r':
  584. case '\n':
  585. case '\t':
  586. continue;
  587. default:
  588. return false;
  589. }
  590. }
  591. return true;
  592. }
  593. #endregion
  594. }
  595. }