DTMXPathDocumentBuilder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // Mono.Xml.XPath.DTMXPathDocumentBuilder
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. //#define DTM_CLASS
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.XPath;
  36. namespace Mono.Xml.XPath
  37. {
  38. #if OUTSIDE_SYSTEM_XML
  39. public
  40. #else
  41. internal
  42. #endif
  43. class DTMXPathDocumentBuilder
  44. {
  45. public DTMXPathDocumentBuilder (string url)
  46. : this (url, XmlSpace.None, 200)
  47. {
  48. }
  49. public DTMXPathDocumentBuilder (string url, XmlSpace space)
  50. : this (url, space, 200)
  51. {
  52. }
  53. public DTMXPathDocumentBuilder (string url, XmlSpace space, int defaultCapacity)
  54. {
  55. XmlReader r = null;
  56. try {
  57. r = new XmlTextReader (url);
  58. Init (r, space, defaultCapacity);
  59. } finally {
  60. if (r != null)
  61. r.Close ();
  62. }
  63. }
  64. public DTMXPathDocumentBuilder (XmlReader reader)
  65. : this (reader, XmlSpace.None, 200)
  66. {
  67. }
  68. public DTMXPathDocumentBuilder (XmlReader reader, XmlSpace space)
  69. : this (reader, space, 200)
  70. {
  71. }
  72. public DTMXPathDocumentBuilder (XmlReader reader, XmlSpace space, int defaultCapacity)
  73. {
  74. Init (reader, space, defaultCapacity);
  75. }
  76. private void Init (XmlReader reader, XmlSpace space, int defaultCapacity)
  77. {
  78. this.xmlReader = reader;
  79. this.validatingReader = reader as XmlValidatingReader;
  80. lineInfo = reader as IXmlLineInfo;
  81. this.xmlSpace = space;
  82. this.nameTable = reader.NameTable;
  83. nodeCapacity = defaultCapacity;
  84. attributeCapacity = nodeCapacity;
  85. idTable = new Hashtable ();
  86. nodes = new DTMXPathLinkedNode [nodeCapacity];
  87. attributes = new DTMXPathAttributeNode [attributeCapacity];
  88. namespaces = new DTMXPathNamespaceNode [0];
  89. Compile ();
  90. }
  91. XmlReader xmlReader;
  92. XmlValidatingReader validatingReader;
  93. XmlSpace xmlSpace;
  94. XmlNameTable nameTable;
  95. IXmlLineInfo lineInfo;
  96. int nodeCapacity = 200;
  97. int attributeCapacity = 200;
  98. int nsCapacity = 10;
  99. // Linked Node
  100. DTMXPathLinkedNode [] nodes;
  101. // Attribute
  102. DTMXPathAttributeNode [] attributes;
  103. // NamespaceNode
  104. DTMXPathNamespaceNode [] namespaces;
  105. // idTable [string value] -> int nodeId
  106. Hashtable idTable;
  107. int nodeIndex;
  108. int attributeIndex;
  109. int nsIndex;
  110. int parentForFirstChild;
  111. // for attribute processing; should be reset per each element.
  112. int firstAttributeIndex;
  113. int lastNsIndexInCurrent;
  114. int attrIndexAtStart;
  115. int nsIndexAtStart;
  116. int prevSibling;
  117. int lastNsInScope;
  118. bool skipRead = false;
  119. public DTMXPathDocument CreateDocument ()
  120. {
  121. return new DTMXPathDocument (nameTable,
  122. nodes,
  123. attributes,
  124. namespaces,
  125. idTable
  126. );
  127. }
  128. public void Compile ()
  129. {
  130. // index 0 is dummy. No node (including Root) is assigned to this index
  131. // So that we can easily compare index != 0 instead of index < 0.
  132. // (Difference between jnz or jbe in 80x86.)
  133. AddNode (0, 0, 0, 0, XPathNodeType.All, "", false, "", "", "", "", "", 0, 0, 0);
  134. nodeIndex++;
  135. AddAttribute (0, null, null, null, null, null, 0, 0);
  136. AddNsNode (0, null, null, 0);
  137. nsIndex++;
  138. AddNsNode (1, "xml", XmlNamespaces.XML, 0);
  139. // add root.
  140. AddNode (0, 0, 0, -1, XPathNodeType.Root, xmlReader.BaseURI, false, "", "", "", "", "", 1, 0, 0);
  141. this.nodeIndex = 1;
  142. this.lastNsInScope = 1;
  143. this.parentForFirstChild = nodeIndex;
  144. while (!xmlReader.EOF)
  145. Read ();
  146. SetNodeArrayLength (nodeIndex + 1);
  147. SetAttributeArrayLength (attributeIndex + 1);
  148. SetNsArrayLength (nsIndex + 1);
  149. xmlReader = null; // It is no more required.
  150. }
  151. public void Read ()
  152. {
  153. if (!skipRead)
  154. if (!xmlReader.Read ())
  155. return;
  156. skipRead = false;
  157. int parent = nodeIndex;
  158. if (nodes [nodeIndex].Depth >= xmlReader.Depth) {
  159. // if not, then current node is parent.
  160. while (xmlReader.Depth <= nodes [parent].Depth)
  161. parent = nodes [parent].Parent;
  162. }
  163. prevSibling = nodeIndex;
  164. switch (xmlReader.NodeType) {
  165. case XmlNodeType.Element:
  166. case XmlNodeType.CDATA:
  167. case XmlNodeType.SignificantWhitespace:
  168. case XmlNodeType.Comment:
  169. case XmlNodeType.Text:
  170. case XmlNodeType.ProcessingInstruction:
  171. if (parentForFirstChild >= 0)
  172. prevSibling = 0;
  173. else
  174. while (nodes [prevSibling].Depth != xmlReader.Depth)
  175. prevSibling = nodes [prevSibling].Parent;
  176. nodeIndex++;
  177. if (prevSibling != 0)
  178. nodes [prevSibling].NextSibling = nodeIndex;
  179. if (parentForFirstChild >= 0)
  180. nodes [parent].FirstChild = nodeIndex;
  181. break;
  182. case XmlNodeType.Whitespace:
  183. if (xmlSpace == XmlSpace.Preserve)
  184. goto case XmlNodeType.Text;
  185. else
  186. goto default;
  187. case XmlNodeType.EndElement:
  188. parentForFirstChild = -1;
  189. return;
  190. default:
  191. // No operations. Doctype, EntityReference,
  192. return;
  193. }
  194. parentForFirstChild = -1; // Might be changed in ProcessElement().
  195. string value = null;
  196. XPathNodeType nodeType = xmlReader.NodeType == XmlNodeType.Whitespace ?
  197. XPathNodeType.Whitespace : XPathNodeType.Text;
  198. switch (xmlReader.NodeType) {
  199. case XmlNodeType.Element:
  200. ProcessElement (parent, prevSibling);
  201. break;
  202. case XmlNodeType.CDATA:
  203. case XmlNodeType.SignificantWhitespace:
  204. case XmlNodeType.Text:
  205. case XmlNodeType.Whitespace:
  206. if (value == null)
  207. skipRead = true;
  208. AddNode (parent,
  209. 0,
  210. prevSibling,
  211. xmlReader.Depth,
  212. nodeType,
  213. xmlReader.BaseURI,
  214. xmlReader.IsEmptyElement,
  215. xmlReader.LocalName, // for PI
  216. xmlReader.NamespaceURI, // for PI
  217. xmlReader.Prefix,
  218. value,
  219. xmlReader.XmlLang,
  220. nsIndex,
  221. lineInfo != null ? lineInfo.LineNumber : 0,
  222. lineInfo != null ? lineInfo.LinePosition : 0);
  223. // this code is tricky, but after ReadString() invokation,
  224. // xmlReader is moved to next node!!
  225. if (value == null)
  226. nodes [nodeIndex].Value = xmlReader.ReadString ();
  227. break;
  228. case XmlNodeType.Comment:
  229. value = xmlReader.Value;
  230. nodeType = XPathNodeType.Comment;
  231. goto case XmlNodeType.Text;
  232. case XmlNodeType.ProcessingInstruction:
  233. value = xmlReader.Value;
  234. nodeType = XPathNodeType.ProcessingInstruction;
  235. goto case XmlNodeType.Text;
  236. }
  237. }
  238. private void ProcessElement (int parent, int previousSibling)
  239. {
  240. WriteStartElement (parent, previousSibling);
  241. // process namespaces and attributes.
  242. if (xmlReader.MoveToFirstAttribute ()) {
  243. do {
  244. string prefix = xmlReader.Prefix;
  245. string ns = xmlReader.NamespaceURI;
  246. if (ns == XmlNamespaces.XMLNS)
  247. ProcessNamespace ((prefix == null || prefix == String.Empty) ? "" : xmlReader.LocalName, xmlReader.Value);
  248. else
  249. ProcessAttribute (prefix, xmlReader.LocalName, ns, xmlReader.Value);
  250. } while (xmlReader.MoveToNextAttribute ());
  251. xmlReader.MoveToElement ();
  252. }
  253. CloseStartElement ();
  254. }
  255. private void PrepareStartElement (int previousSibling)
  256. {
  257. firstAttributeIndex = 0;
  258. lastNsIndexInCurrent = 0;
  259. attrIndexAtStart = attributeIndex;
  260. nsIndexAtStart = nsIndex;
  261. while (namespaces [lastNsInScope].DeclaredElement == previousSibling) {
  262. lastNsInScope = namespaces [lastNsInScope].NextNamespace;
  263. }
  264. }
  265. private void WriteStartElement (int parent, int previousSibling)
  266. {
  267. PrepareStartElement (previousSibling);
  268. AddNode (parent,
  269. 0, // dummy:firstAttribute
  270. previousSibling,
  271. xmlReader.Depth,
  272. XPathNodeType.Element,
  273. xmlReader.BaseURI,
  274. xmlReader.IsEmptyElement,
  275. xmlReader.LocalName,
  276. xmlReader.NamespaceURI,
  277. xmlReader.Prefix,
  278. "", // Element has no internal value.
  279. xmlReader.XmlLang,
  280. lastNsInScope,
  281. lineInfo != null ? lineInfo.LineNumber : 0,
  282. lineInfo != null ? lineInfo.LinePosition : 0);
  283. }
  284. private void CloseStartElement ()
  285. {
  286. if (attrIndexAtStart != attributeIndex)
  287. nodes [nodeIndex].FirstAttribute = attrIndexAtStart + 1;
  288. if (nsIndexAtStart != nsIndex) {
  289. nodes [nodeIndex].FirstNamespace = nsIndex;
  290. lastNsInScope = nsIndex;
  291. }
  292. if (!nodes [nodeIndex].IsEmptyElement)
  293. parentForFirstChild = nodeIndex;
  294. }
  295. private void ProcessNamespace (string prefix, string ns)
  296. {
  297. nsIndex++;
  298. int nextTmp = lastNsIndexInCurrent == 0 ? nodes [nodeIndex].FirstNamespace : lastNsIndexInCurrent;
  299. this.AddNsNode (nodeIndex,
  300. prefix,
  301. ns,
  302. nextTmp);
  303. lastNsIndexInCurrent = nsIndex;
  304. }
  305. private void ProcessAttribute (string prefix, string localName, string ns, string value)
  306. {
  307. attributeIndex ++;
  308. this.AddAttribute (nodeIndex,
  309. localName,
  310. ns,
  311. prefix != null ? prefix : String.Empty,
  312. value,
  313. null,
  314. lineInfo != null ? lineInfo.LineNumber : 0,
  315. lineInfo != null ? lineInfo.LinePosition : 0);
  316. if (firstAttributeIndex == 0)
  317. firstAttributeIndex = attributeIndex;
  318. else
  319. attributes [attributeIndex - 1].NextAttribute = attributeIndex;
  320. // Identity infoset
  321. if (validatingReader != null) {
  322. XmlSchemaDatatype dt = validatingReader.SchemaType as XmlSchemaDatatype;
  323. if (dt == null) {
  324. XmlSchemaType xsType = validatingReader.SchemaType as XmlSchemaType;
  325. if (xsType != null)
  326. dt = xsType.Datatype;
  327. }
  328. if (dt != null && dt.TokenizedType == XmlTokenizedType.ID)
  329. idTable.Add (value, nodeIndex);
  330. }
  331. }
  332. private void SetNodeArrayLength (int size)
  333. {
  334. DTMXPathLinkedNode [] newArr = new DTMXPathLinkedNode [size];
  335. Array.Copy (nodes, newArr, System.Math.Min (size, nodes.Length));
  336. nodes = newArr;
  337. }
  338. private void SetAttributeArrayLength (int size)
  339. {
  340. DTMXPathAttributeNode [] newArr =
  341. new DTMXPathAttributeNode [size];
  342. Array.Copy (attributes, newArr, System.Math.Min (size, attributes.Length));
  343. attributes = newArr;
  344. }
  345. private void SetNsArrayLength (int size)
  346. {
  347. DTMXPathNamespaceNode [] newArr =
  348. new DTMXPathNamespaceNode [size];
  349. Array.Copy (namespaces, newArr, System.Math.Min (size, namespaces.Length));
  350. namespaces = newArr;
  351. }
  352. // Here followings are skipped: firstChild, nextSibling,
  353. 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)
  354. {
  355. if (nodes.Length < nodeIndex + 1) {
  356. nodeCapacity *= 4;
  357. SetNodeArrayLength (nodeCapacity);
  358. }
  359. #if DTM_CLASS
  360. nodes [nodeIndex] = new DTMXPathLinkedNode ();
  361. #endif
  362. nodes [nodeIndex].FirstChild = 0; // dummy
  363. nodes [nodeIndex].Parent = parent;
  364. nodes [nodeIndex].FirstAttribute = firstAttribute;
  365. nodes [nodeIndex].PreviousSibling = previousSibling;
  366. nodes [nodeIndex].NextSibling = 0; // dummy
  367. nodes [nodeIndex].Depth = depth;
  368. nodes [nodeIndex].NodeType = nodeType;
  369. nodes [nodeIndex].BaseURI = baseUri;
  370. nodes [nodeIndex].IsEmptyElement = isEmptyElement;
  371. nodes [nodeIndex].LocalName = localName;
  372. nodes [nodeIndex].NamespaceURI = ns;
  373. nodes [nodeIndex].Prefix = prefix;
  374. nodes [nodeIndex].Value = value;
  375. nodes [nodeIndex].XmlLang = xmlLang;
  376. nodes [nodeIndex].FirstNamespace = namespaceNode;
  377. nodes [nodeIndex].LineNumber = lineNumber;
  378. nodes [nodeIndex].LinePosition = linePosition;
  379. }
  380. // Followings are skipped: nextAttribute,
  381. public void AddAttribute (int ownerElement, string localName, string ns, string prefix, string value, object schemaType, int lineNumber, int linePosition)
  382. {
  383. if (attributes.Length < attributeIndex + 1) {
  384. attributeCapacity *= 4;
  385. SetAttributeArrayLength (attributeCapacity);
  386. }
  387. #if DTM_CLASS
  388. attributes [attributeIndex] = new DTMXPathAttributeNode ();
  389. #endif
  390. attributes [attributeIndex].OwnerElement = ownerElement;
  391. attributes [attributeIndex].LocalName = localName;
  392. attributes [attributeIndex].NamespaceURI = ns;
  393. attributes [attributeIndex].Prefix = prefix;
  394. attributes [attributeIndex].Value = value;
  395. attributes [attributeIndex].SchemaType = schemaType;
  396. attributes [attributeIndex].LineNumber = lineNumber;
  397. attributes [attributeIndex].LinePosition = linePosition;
  398. }
  399. // Followings are skipped: nextNsNode (may be next attribute in the same element, or ancestors' nsNode)
  400. public void AddNsNode (int declaredElement, string name, string ns, int nextNs)
  401. {
  402. if (namespaces.Length < nsIndex + 1) {
  403. nsCapacity *= 4;
  404. SetNsArrayLength (nsCapacity);
  405. }
  406. #if DTM_CLASS
  407. namespaces [nsIndex] = new DTMXPathNamespaceNode ();
  408. #endif
  409. namespaces [nsIndex].DeclaredElement = declaredElement;
  410. namespaces [nsIndex].Name = name;
  411. namespaces [nsIndex].Namespace = ns;
  412. namespaces [nsIndex].NextNamespace = nextNs;
  413. }
  414. }
  415. }