2 // XPathNavigatorReader.cs
\r
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
\r
7 // 2003 Atsushi Enomoto. No rights reserved.
\r
8 // Copyright (C) 2004 Novell Inc.
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
35 using System.Xml.Schema;
\r
36 using System.Xml.XPath;
\r
38 namespace Mono.Xml.XPath
\r
41 internal class XPathNavigatorReader : XmlReader
\r
43 public XPathNavigatorReader (XPathNavigator nav)
\r
45 // It seems that this class have only to support linked
\r
46 // node as its parameter
\r
47 switch (nav.NodeType) {
\r
48 case XPathNodeType.Attribute:
\r
49 case XPathNodeType.Namespace:
\r
50 throw new InvalidOperationException (String.Format ("NodeType {0} is not supported to read as a subtree of an XPathNavigator.", nav.NodeType));
\r
52 root = nav.Clone ();
\r
53 current = nav.Clone ();
\r
56 XPathNavigator root;
\r
57 XPathNavigator current;
\r
61 bool attributeValueConsumed;
\r
62 StringBuilder readStringBuffer = new StringBuilder ();
\r
63 StringBuilder innerXmlBuilder = new StringBuilder ();
\r
66 int attributeCount = 0;
\r
71 public override XmlNodeType NodeType
\r
74 if (ReadState != ReadState.Interactive)
\r
75 return XmlNodeType.None;
\r
77 return XmlNodeType.EndElement;
\r
78 if (attributeValueConsumed)
\r
79 // Is there any way to get other kind of nodes than Text?
\r
80 return XmlNodeType.Text;
\r
82 switch (current.NodeType) {
\r
83 case XPathNodeType.Namespace:
\r
84 case XPathNodeType.Attribute:
\r
85 return XmlNodeType.Attribute;
\r
86 case XPathNodeType.Comment:
\r
87 return XmlNodeType.Comment;
\r
88 case XPathNodeType.Element:
\r
89 return XmlNodeType.Element;
\r
90 case XPathNodeType.ProcessingInstruction:
\r
91 return XmlNodeType.ProcessingInstruction;
\r
92 case XPathNodeType.Root:
\r
93 // It is actually Document, but in XmlReader there is no such situation to return Document.
\r
94 return XmlNodeType.None;
\r
95 case XPathNodeType.SignificantWhitespace:
\r
96 return XmlNodeType.SignificantWhitespace;
\r
97 case XPathNodeType.Text:
\r
98 return XmlNodeType.Text;
\r
99 case XPathNodeType.Whitespace:
\r
100 return XmlNodeType.Whitespace;
\r
102 throw new InvalidOperationException (String.Format ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.", current.NodeType));
\r
107 public override string Name {
\r
110 return String.Empty;
\r
111 else if (current.NodeType == XPathNodeType.Namespace)
\r
112 return current.Name == String.Empty ? "xmlns" : "xmlns:" + current.Name;
\r
114 return current.Name;
\r
118 public override string LocalName {
\r
121 return String.Empty;
\r
122 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName == String.Empty)
\r
125 return current.LocalName;
\r
129 public override string NamespaceURI {
\r
132 return String.Empty;
\r
133 else if (current.NodeType == XPathNodeType.Namespace)
\r
134 return XmlNamespaceManager.XmlnsXmlns;
\r
136 return current.NamespaceURI;
\r
140 public override string Prefix {
\r
143 return String.Empty;
\r
144 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName != String.Empty)
\r
147 return current.Prefix;
\r
151 public override bool HasValue {
\r
153 switch (current.NodeType) {
\r
154 case XPathNodeType.Namespace:
\r
155 case XPathNodeType.Attribute:
\r
156 case XPathNodeType.Comment:
\r
157 case XPathNodeType.ProcessingInstruction:
\r
158 case XPathNodeType.SignificantWhitespace:
\r
159 case XPathNodeType.Text:
\r
160 case XPathNodeType.Whitespace:
\r
167 public override int Depth {
\r
169 switch (ReadState) {
\r
170 case ReadState.EndOfFile:
\r
171 case ReadState.Initial:
\r
172 case ReadState.Closed:
\r
179 public override string Value {
\r
181 switch (current.NodeType) {
\r
182 case XPathNodeType.Namespace:
\r
183 case XPathNodeType.Attribute:
\r
184 case XPathNodeType.Comment:
\r
185 case XPathNodeType.ProcessingInstruction:
\r
186 case XPathNodeType.SignificantWhitespace:
\r
187 case XPathNodeType.Text:
\r
188 case XPathNodeType.Whitespace:
\r
189 return current.Value;
\r
190 case XPathNodeType.Element:
\r
191 case XPathNodeType.Root:
\r
192 return String.Empty;
\r
194 throw new InvalidOperationException ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.");
\r
199 public override string BaseURI {
\r
200 get { return current.BaseURI; }
\r
203 public override bool IsEmptyElement {
\r
204 get { return current.IsEmptyElement; }
\r
207 public override bool IsDefault {
\r
209 IXmlSchemaInfo si = current as IXmlSchemaInfo;
\r
210 return si != null && si.IsDefault;
\r
214 // It makes no sense.
\r
215 public override char QuoteChar {
\r
216 get { return '\"'; }
\r
219 public override string XmlLang {
\r
220 get { return current.XmlLang; }
\r
223 // It is meaningless.
\r
224 public override XmlSpace XmlSpace {
\r
225 get { return XmlSpace.None; }
\r
228 public override int AttributeCount {
\r
229 get { return attributeCount; }
\r
232 private int GetAttributeCount ()
\r
235 if (current.MoveToFirstAttribute ()) {
\r
238 } while (current.MoveToNextAttribute ());
\r
239 current.MoveToParent ();
\r
241 if (current.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
\r
244 } while (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
245 current.MoveToParent ();
\r
250 private bool MoveToAttributeNavigator (int i)
\r
252 switch (current.NodeType) {
\r
253 case XPathNodeType.Namespace:
\r
254 case XPathNodeType.Attribute:
\r
255 this.MoveToElement ();
\r
256 goto case XPathNodeType.Element;
\r
257 case XPathNodeType.Element:
\r
259 if (MoveToFirstAttribute ()) {
\r
263 for (count++; this.MoveToNextAttribute (); count++) {
\r
272 public override string this [int i] {
\r
274 XPathNavigator backup = current.Clone ();
\r
276 if (MoveToAttributeNavigator (i))
\r
279 throw new ArgumentOutOfRangeException ();
\r
281 current.MoveTo (backup);
\r
286 private void SplitName (string name, out string localName, out string ns)
\r
290 int colon = name.IndexOf (':');
\r
292 localName = name.Substring (colon + 1, name.Length - colon - 1);
\r
293 ns = this.LookupNamespace (name.Substring (0, colon));
\r
297 public override string this [string name] {
\r
301 SplitName (name, out localName, out ns);
\r
302 return this [localName, ns];
\r
306 public override string this [string localName, string namespaceURI] {
\r
308 string v = current.GetAttribute (localName, namespaceURI);
\r
309 if (v != String.Empty)
\r
311 XPathNavigator tmp = current.Clone ();
\r
312 return tmp.MoveToAttribute (localName, namespaceURI) ? String.Empty : null;
\r
316 public override bool EOF {
\r
317 get { return ReadState == ReadState.EndOfFile; }
\r
320 public override ReadState ReadState {
\r
323 return ReadState.EndOfFile;
\r
325 return ReadState.Closed;
\r
327 return ReadState.Initial;
\r
328 return ReadState.Interactive;
\r
332 public override XmlNameTable NameTable {
\r
333 get { return current.NameTable; }
\r
339 public override string GetAttribute (string name)
\r
343 SplitName (name, out localName, out ns);
\r
344 return this [localName, ns];
\r
347 public override string GetAttribute (string localName, string namespaceURI)
\r
349 return this [localName, namespaceURI];
\r
352 public override string GetAttribute (int i)
\r
357 private bool CheckAttributeMove (bool b)
\r
360 attributeValueConsumed = false;
\r
364 public override bool MoveToAttribute (string name)
\r
368 SplitName (name, out localName, out ns);
\r
369 return CheckAttributeMove (MoveToAttribute (localName, ns));
\r
372 public override bool MoveToAttribute (string localName, string namespaceURI)
\r
374 XPathNavigator backup = null;
\r
375 switch (current.NodeType) {
\r
376 case XPathNodeType.Attribute:
\r
377 backup = current.Clone ();
\r
378 this.MoveToElement ();
\r
379 goto case XPathNodeType.Element;
\r
380 case XPathNodeType.Element:
\r
381 while (MoveToNextAttribute ())
\r
382 if (current.LocalName == localName && current.NamespaceURI == namespaceURI) {
\r
383 attributeValueConsumed = false;
\r
388 if (backup != null)
\r
393 public override void MoveToAttribute (int i)
\r
395 if (!MoveToAttributeNavigator (i))
\r
396 throw new ArgumentOutOfRangeException ();
\r
399 public override bool MoveToFirstAttribute ()
\r
401 bool b = CheckAttributeMove (current.MoveToFirstNamespace (XPathNamespaceScope.Local));
\r
404 return CheckAttributeMove (current.MoveToFirstAttribute ());
\r
407 public override bool MoveToNextAttribute ()
\r
409 if (current.NodeType == XPathNodeType.Namespace) {
\r
410 bool b = CheckAttributeMove (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
413 current.MoveToParent ();
\r
414 b = CheckAttributeMove (current.MoveToFirstAttribute ());
\r
418 return CheckAttributeMove (current.MoveToNextAttribute ());
\r
421 public override bool MoveToElement ()
\r
423 if (current.NodeType == XPathNodeType.Attribute ||
\r
424 current.NodeType == XPathNodeType.Namespace) {
\r
425 attributeValueConsumed = false;
\r
426 return current.MoveToParent ();
\r
431 public override void Close ()
\r
437 public override bool Read ()
\r
439 switch (ReadState) {
\r
440 case ReadState.EndOfFile:
\r
441 case ReadState.Closed:
\r
442 case ReadState.Error:
\r
444 case ReadState.Initial:
\r
446 switch (current.NodeType) {
\r
447 case XPathNodeType.Root:
\r
448 // recurse, but as Interactive
\r
450 case XPathNodeType.Element:
\r
451 if (current.IsEmptyElement)
\r
453 attributeCount = GetAttributeCount ();
\r
470 if (endElement || !current.MoveToFirstChild ()) {
\r
471 if (current.IsSamePosition (root)) { // It should happen only when the root node was empty.
\r
475 if (!current.MoveToNext ()) {
\r
476 current.MoveToParent ();
\r
478 endElement = (current.NodeType == XPathNodeType.Element);
\r
479 if (current.IsSamePosition (root)) {
\r
480 if (current.NodeType == XPathNodeType.Element)
\r
483 endElement = false;
\r
489 endElement = false;
\r
491 else if (!endElement)
\r
493 attributeCount = GetAttributeCount ();
\r
497 public override string ReadString ()
\r
499 readStringBuffer.Length = 0;
\r
501 switch (NodeType) {
\r
503 return String.Empty;
\r
504 case XmlNodeType.Element:
\r
505 if (IsEmptyElement)
\r
506 return String.Empty;
\r
509 switch (NodeType) {
\r
510 case XmlNodeType.Text:
\r
511 case XmlNodeType.CDATA:
\r
512 case XmlNodeType.Whitespace:
\r
513 case XmlNodeType.SignificantWhitespace:
\r
514 readStringBuffer.Append (Value);
\r
520 case XmlNodeType.Text:
\r
521 case XmlNodeType.CDATA:
\r
522 case XmlNodeType.Whitespace:
\r
523 case XmlNodeType.SignificantWhitespace:
\r
525 switch (NodeType) {
\r
526 case XmlNodeType.Text:
\r
527 case XmlNodeType.CDATA:
\r
528 case XmlNodeType.Whitespace:
\r
529 case XmlNodeType.SignificantWhitespace:
\r
530 readStringBuffer.Append (Value);
\r
538 string ret = readStringBuffer.ToString ();
\r
539 readStringBuffer.Length = 0;
\r
545 public override string ReadInnerXml ()
\r
547 if (ReadState != ReadState.Interactive)
\r
548 return String.Empty;
\r
550 switch (NodeType) {
\r
551 case XmlNodeType.Attribute:
\r
553 case XmlNodeType.Element:
\r
554 if (IsEmptyElement)
\r
555 return String.Empty;
\r
557 int startDepth = Depth;
\r
559 innerXmlBuilder.Length = 0;
\r
563 if (NodeType ==XmlNodeType.None)
\r
564 throw new InvalidOperationException ("unexpected end of xml.");
\r
565 else if (NodeType == XmlNodeType.EndElement && Depth == startDepth) {
\r
570 innerXmlBuilder.Append (GetCurrentTagMarkup ());
\r
572 string xml = innerXmlBuilder.ToString ();
\r
573 innerXmlBuilder.Length = 0;
\r
575 case XmlNodeType.None:
\r
576 // MS document is incorrect. Seems not to progress.
\r
577 return String.Empty;
\r
580 return String.Empty;
\r
584 StringBuilder atts = new StringBuilder ();
\r
585 private string GetCurrentTagMarkup ()
\r
587 switch (NodeType) {
\r
588 case XmlNodeType.CDATA:
\r
589 return String.Format ("<![CDATA[{0}]]>", Value.Replace ("]]>", "]]>"));
\r
590 case XmlNodeType.Text:
\r
591 return Value.Replace ("<", "<");
\r
592 case XmlNodeType.Comment:
\r
593 return String.Format ("<!--{0}-->", Value);
\r
594 case XmlNodeType.SignificantWhitespace:
\r
595 case XmlNodeType.Whitespace:
\r
597 case XmlNodeType.EndElement:
\r
598 return String.Format ("</{0}>", Name);
\r
601 bool isEmpty = IsEmptyElement;
\r
602 string name = Name;
\r
604 XPathNavigator temp = current.Clone ();
\r
605 while (temp.MoveToNextAttribute ())
\r
606 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
607 if (!IsEmptyElement)
\r
608 return String.Format ("<{0}{1}>", name, atts);
\r
610 return String.Format ("<{0}{1} />", name, atts);
\r
613 // Arranged copy of XmlTextReader.ReadOuterXml()
\r
614 public override string ReadOuterXml ()
\r
616 if (ReadState != ReadState.Interactive)
\r
617 return String.Empty;
\r
619 switch (NodeType) {
\r
620 case XmlNodeType.Attribute:
\r
621 // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
\r
622 return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
\r
623 case XmlNodeType.Element:
\r
624 bool isEmpty = IsEmptyElement;
\r
625 string name = Name;
\r
626 StringBuilder atts = new StringBuilder ();
\r
627 XPathNavigator temp = current.Clone ();
\r
628 while (temp.MoveToNextAttribute ())
\r
629 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
632 return String.Format ("{0}{1}</{2}>", GetCurrentTagMarkup (), atts, ReadInnerXml (), name);
\r
634 return String.Format ("{0}", GetCurrentTagMarkup ());
\r
635 case XmlNodeType.None:
\r
636 // MS document is incorrect. Seems not to progress.
\r
637 return String.Empty;
\r
640 return String.Empty;
\r
645 public override string LookupNamespace (string prefix)
\r
647 XPathNavigator backup = current.Clone ();
\r
649 this.MoveToElement ();
\r
650 if (current.MoveToFirstNamespace ()) {
\r
652 if (current.LocalName == prefix)
\r
653 return current.Value;
\r
654 } while (current.MoveToNextNamespace ());
\r
662 // It does not support entity resolution.
\r
663 public override void ResolveEntity ()
\r
665 throw new InvalidOperationException ();
\r
668 public override bool ReadAttributeValue () {
\r
669 if (NodeType != XmlNodeType.Attribute)
\r
671 if (attributeValueConsumed)
\r
673 attributeValueConsumed = true;
\r