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 switch (nav.NodeType) {
\r
46 case XPathNodeType.Element:
\r
47 case XPathNodeType.Root:
\r
50 throw new InvalidOperationException (String.Format ("NodeType {0} is not supported to read as a subtree of an XPathNavigator.", nav.NodeType));
\r
52 current = nav.Clone ();
\r
55 XPathNavigator root;
\r
56 XPathNavigator current;
\r
60 bool attributeValueConsumed;
\r
61 StringBuilder readStringBuffer = new StringBuilder ();
\r
63 StringBuilder innerXmlBuilder = new StringBuilder ();
\r
67 int attributeCount = 0;
\r
73 public override bool CanReadBinaryContent {
\r
74 get { return true; }
\r
77 public override bool CanReadValueChunk {
\r
78 get { return true; }
\r
82 public override XmlNodeType NodeType {
\r
84 if (ReadState != ReadState.Interactive)
\r
85 return XmlNodeType.None;
\r
87 return XmlNodeType.EndElement;
\r
88 if (attributeValueConsumed)
\r
89 // Is there any way to get other kind of nodes than Text?
\r
90 return XmlNodeType.Text;
\r
92 switch (current.NodeType) {
\r
93 case XPathNodeType.Namespace:
\r
94 case XPathNodeType.Attribute:
\r
95 return XmlNodeType.Attribute;
\r
96 case XPathNodeType.Comment:
\r
97 return XmlNodeType.Comment;
\r
98 case XPathNodeType.Element:
\r
99 return XmlNodeType.Element;
\r
100 case XPathNodeType.ProcessingInstruction:
\r
101 return XmlNodeType.ProcessingInstruction;
\r
102 case XPathNodeType.Root:
\r
103 // It is actually Document, but in XmlReader there is no such situation to return Document.
\r
104 return XmlNodeType.None;
\r
105 case XPathNodeType.SignificantWhitespace:
\r
106 return XmlNodeType.SignificantWhitespace;
\r
107 case XPathNodeType.Text:
\r
108 return XmlNodeType.Text;
\r
109 case XPathNodeType.Whitespace:
\r
110 return XmlNodeType.Whitespace;
\r
112 throw new InvalidOperationException (String.Format ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.", current.NodeType));
\r
117 public override string Name {
\r
119 if (ReadState != ReadState.Interactive)
\r
120 return String.Empty;
\r
121 if (attributeValueConsumed)
\r
122 return String.Empty;
\r
123 else if (current.NodeType == XPathNodeType.Namespace)
\r
124 return current.Name == String.Empty ? "xmlns" : "xmlns:" + current.Name;
\r
126 return current.Name;
\r
130 public override string LocalName {
\r
132 if (ReadState != ReadState.Interactive)
\r
133 return String.Empty;
\r
134 if (attributeValueConsumed)
\r
135 return String.Empty;
\r
136 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName == String.Empty)
\r
139 return current.LocalName;
\r
143 public override string NamespaceURI {
\r
145 if (ReadState != ReadState.Interactive)
\r
146 return String.Empty;
\r
147 if (attributeValueConsumed)
\r
148 return String.Empty;
\r
149 else if (current.NodeType == XPathNodeType.Namespace)
\r
150 return "http://www.w3.org/2000/xmlns/";
\r
152 return current.NamespaceURI;
\r
156 public override string Prefix {
\r
158 if (ReadState != ReadState.Interactive)
\r
159 return String.Empty;
\r
160 if (attributeValueConsumed)
\r
161 return String.Empty;
\r
162 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName != String.Empty)
\r
165 return current.Prefix;
\r
169 public override bool HasValue {
\r
171 switch (current.NodeType) {
\r
172 case XPathNodeType.Namespace:
\r
173 case XPathNodeType.Attribute:
\r
174 case XPathNodeType.Comment:
\r
175 case XPathNodeType.ProcessingInstruction:
\r
176 case XPathNodeType.SignificantWhitespace:
\r
177 case XPathNodeType.Text:
\r
178 case XPathNodeType.Whitespace:
\r
185 public override int Depth {
\r
187 if (ReadState != ReadState.Interactive)
\r
190 if (NodeType == XmlNodeType.Attribute)
\r
192 if (attributeValueConsumed)
\r
198 public override string Value {
\r
200 if (ReadState != ReadState.Interactive)
\r
201 return String.Empty;
\r
202 switch (current.NodeType) {
\r
203 case XPathNodeType.Namespace:
\r
204 case XPathNodeType.Attribute:
\r
205 case XPathNodeType.Comment:
\r
206 case XPathNodeType.ProcessingInstruction:
\r
207 case XPathNodeType.SignificantWhitespace:
\r
208 case XPathNodeType.Text:
\r
209 case XPathNodeType.Whitespace:
\r
210 return current.Value;
\r
211 case XPathNodeType.Element:
\r
212 case XPathNodeType.Root:
\r
213 return String.Empty;
\r
215 throw new InvalidOperationException ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.");
\r
220 public override string BaseURI {
\r
221 get { return current.BaseURI; }
\r
224 public override bool IsEmptyElement {
\r
226 if (ReadState != ReadState.Interactive)
\r
228 return current.IsEmptyElement;
\r
232 public override bool IsDefault {
\r
235 IXmlSchemaInfo si = current as IXmlSchemaInfo;
\r
236 return si != null && si.IsDefault;
\r
238 return false; // no way to check this.
\r
243 // It makes no sense.
\r
244 public override char QuoteChar {
\r
245 get { return '\"'; }
\r
249 public override IXmlSchemaInfo SchemaInfo {
\r
250 get { return current.SchemaInfo; }
\r
254 public override string XmlLang {
\r
255 get { return current.XmlLang; }
\r
258 // It is meaningless.
\r
259 public override XmlSpace XmlSpace {
\r
260 get { return XmlSpace.None; }
\r
263 public override int AttributeCount {
\r
264 get { return attributeCount; }
\r
267 private int GetAttributeCount ()
\r
269 if (ReadState != ReadState.Interactive)
\r
272 if (current.MoveToFirstAttribute ()) {
\r
275 } while (current.MoveToNextAttribute ());
\r
276 current.MoveToParent ();
\r
278 if (current.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
\r
281 } while (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
282 current.MoveToParent ();
\r
287 private bool MoveToAttributeNavigator (int i)
\r
289 if (ReadState != ReadState.Interactive)
\r
291 switch (current.NodeType) {
\r
292 case XPathNodeType.Namespace:
\r
293 case XPathNodeType.Attribute:
\r
294 this.MoveToElement ();
\r
295 goto case XPathNodeType.Element;
\r
296 case XPathNodeType.Element:
\r
298 if (MoveToFirstAttribute ()) {
\r
302 for (count++; this.MoveToNextAttribute (); count++) {
\r
312 public override string this [int i] {
\r
314 XPathNavigator backup = current.Clone ();
\r
316 if (MoveToAttributeNavigator (i))
\r
319 throw new ArgumentOutOfRangeException ();
\r
321 current.MoveTo (backup);
\r
327 private void SplitName (string name, out string localName, out string ns)
\r
329 if (name == "xmlns") {
\r
330 localName = "xmlns";
\r
331 ns = XmlNamespaceManager.XmlnsXmlns;
\r
336 int colon = name.IndexOf (':');
\r
338 localName = name.Substring (colon + 1, name.Length - colon - 1);
\r
339 ns = this.LookupNamespace (name.Substring (0, colon));
\r
340 if (name.Substring (0, colon) == "xmlns")
\r
341 ns = XmlNamespaceManager.XmlnsXmlns;
\r
345 public override string this [string name] {
\r
349 SplitName (name, out localName, out ns);
\r
350 return this [localName, ns];
\r
354 public override string this [string localName, string namespaceURI] {
\r
356 string v = current.GetAttribute (localName, namespaceURI);
\r
357 if (v != String.Empty)
\r
359 XPathNavigator tmp = current.Clone ();
\r
360 return tmp.MoveToAttribute (localName, namespaceURI) ? String.Empty : null;
\r
364 public override bool EOF {
\r
365 get { return ReadState == ReadState.EndOfFile; }
\r
368 public override ReadState ReadState {
\r
371 return ReadState.EndOfFile;
\r
373 return ReadState.Closed;
\r
375 return ReadState.Initial;
\r
376 return ReadState.Interactive;
\r
380 public override XmlNameTable NameTable {
\r
381 get { return current.NameTable; }
\r
387 public override string GetAttribute (string name)
\r
391 SplitName (name, out localName, out ns);
\r
392 return this [localName, ns];
\r
395 public override string GetAttribute (string localName, string namespaceURI)
\r
397 return this [localName, namespaceURI];
\r
400 public override string GetAttribute (int i)
\r
405 private bool CheckAttributeMove (bool b)
\r
408 attributeValueConsumed = false;
\r
412 public override bool MoveToAttribute (string name)
\r
416 SplitName (name, out localName, out ns);
\r
417 return MoveToAttribute (localName, ns);
\r
420 public override bool MoveToAttribute (string localName, string namespaceURI)
\r
422 bool isNS = namespaceURI == "http://www.w3.org/2000/xmlns/";
\r
423 XPathNavigator backup = null;
\r
424 switch (current.NodeType) {
\r
425 case XPathNodeType.Namespace:
\r
426 case XPathNodeType.Attribute:
\r
427 backup = current.Clone ();
\r
428 this.MoveToElement ();
\r
429 goto case XPathNodeType.Element;
\r
430 case XPathNodeType.Element:
\r
431 if (MoveToFirstAttribute ()) {
\r
433 bool match = false;
\r
435 if (localName == "xmlns")
\r
436 match = current.Name == String.Empty;
\r
438 match = localName == current.Name;
\r
441 match = current.LocalName == localName &&
\r
442 current.NamespaceURI == namespaceURI;
\r
444 attributeValueConsumed = false;
\r
447 } while (MoveToNextAttribute ());
\r
452 if (backup != null)
\r
458 public override void MoveToAttribute (int i)
\r
460 if (!MoveToAttributeNavigator (i))
\r
461 throw new ArgumentOutOfRangeException ();
\r
465 public override bool MoveToFirstAttribute ()
\r
467 if (CheckAttributeMove (current.MoveToFirstNamespace (XPathNamespaceScope.Local)))
\r
469 return CheckAttributeMove (current.MoveToFirstAttribute ());
\r
472 public override bool MoveToNextAttribute ()
\r
474 switch (current.NodeType) {
\r
475 case XPathNodeType.Element:
\r
476 return MoveToFirstAttribute ();
\r
477 case XPathNodeType.Namespace:
\r
478 if (CheckAttributeMove (current.MoveToNextNamespace (XPathNamespaceScope.Local)))
\r
480 XPathNavigator bak = current.Clone ();
\r
481 current.MoveToParent ();
\r
482 if (CheckAttributeMove (current.MoveToFirstAttribute ()))
\r
484 current.MoveTo (bak);
\r
486 case XPathNodeType.Attribute:
\r
487 return CheckAttributeMove (current.MoveToNextAttribute ());
\r
493 public override bool MoveToElement ()
\r
495 if (current.NodeType == XPathNodeType.Attribute ||
\r
496 current.NodeType == XPathNodeType.Namespace) {
\r
497 attributeValueConsumed = false;
\r
498 return current.MoveToParent ();
\r
503 public override void Close ()
\r
509 public override bool Read ()
\r
514 if (Binary != null)
\r
518 switch (ReadState) {
\r
519 case ReadState.Interactive:
\r
520 if ((IsEmptyElement || endElement) && root.IsSamePosition (current)) {
\r
524 if (!current.IsEmptyElement && current.NodeType == XPathNodeType.Element)
\r
527 case ReadState.EndOfFile:
\r
528 case ReadState.Closed:
\r
529 case ReadState.Error:
\r
531 case ReadState.Initial:
\r
533 root = current.Clone ();
\r
534 if (current.NodeType == XPathNodeType.Root &&
\r
535 !current.MoveToFirstChild ()) {
\r
536 endElement = false;
\r
540 attributeCount = GetAttributeCount ();
\r
546 if (endElement || !current.MoveToFirstChild ()) {
\r
547 // EndElement of current element.
\r
548 if (!endElement && !current.IsEmptyElement &&
\r
549 current.NodeType == XPathNodeType.Element)
\r
551 else if (!current.MoveToNext ()) {
\r
552 current.MoveToParent ();
\r
553 if (current.NodeType == XPathNodeType.Root) {
\r
554 endElement = false;
\r
558 endElement = (current.NodeType == XPathNodeType.Element);
\r
561 endElement = false;
\r
564 if (!endElement && current.NodeType == XPathNodeType.Element)
\r
565 attributeCount = GetAttributeCount ();
\r
567 attributeCount = 0;
\r
575 public override string ReadString ()
\r
577 readStringBuffer.Length = 0;
\r
579 switch (NodeType) {
\r
581 return String.Empty;
\r
582 case XmlNodeType.Element:
\r
583 if (IsEmptyElement)
\r
584 return String.Empty;
\r
587 switch (NodeType) {
\r
588 case XmlNodeType.Text:
\r
589 case XmlNodeType.CDATA:
\r
590 case XmlNodeType.Whitespace:
\r
591 case XmlNodeType.SignificantWhitespace:
\r
592 readStringBuffer.Append (Value);
\r
598 case XmlNodeType.Text:
\r
599 case XmlNodeType.CDATA:
\r
600 case XmlNodeType.Whitespace:
\r
601 case XmlNodeType.SignificantWhitespace:
\r
603 switch (NodeType) {
\r
604 case XmlNodeType.Text:
\r
605 case XmlNodeType.CDATA:
\r
606 case XmlNodeType.Whitespace:
\r
607 case XmlNodeType.SignificantWhitespace:
\r
608 readStringBuffer.Append (Value);
\r
616 string ret = readStringBuffer.ToString ();
\r
617 readStringBuffer.Length = 0;
\r
623 public override string ReadInnerXml ()
\r
625 if (ReadState != ReadState.Interactive)
\r
626 return String.Empty;
\r
628 switch (NodeType) {
\r
629 case XmlNodeType.Attribute:
\r
631 case XmlNodeType.Element:
\r
632 if (IsEmptyElement)
\r
633 return String.Empty;
\r
635 int startDepth = Depth;
\r
637 innerXmlBuilder.Length = 0;
\r
641 if (NodeType ==XmlNodeType.None)
\r
642 throw new InvalidOperationException ("unexpected end of xml.");
\r
643 else if (NodeType == XmlNodeType.EndElement && Depth == startDepth) {
\r
648 innerXmlBuilder.Append (GetCurrentTagMarkup ());
\r
650 string xml = innerXmlBuilder.ToString ();
\r
651 innerXmlBuilder.Length = 0;
\r
653 case XmlNodeType.None:
\r
654 // MS document is incorrect. Seems not to progress.
\r
655 return String.Empty;
\r
658 return String.Empty;
\r
662 StringBuilder atts = new StringBuilder ();
\r
663 private string GetCurrentTagMarkup ()
\r
665 switch (NodeType) {
\r
666 case XmlNodeType.CDATA:
\r
667 return String.Format ("<![CDATA[{0}]]>", Value.Replace ("]]>", "]]>"));
\r
668 case XmlNodeType.Text:
\r
669 return Value.Replace ("<", "<");
\r
670 case XmlNodeType.Comment:
\r
671 return String.Format ("<!--{0}-->", Value);
\r
672 case XmlNodeType.SignificantWhitespace:
\r
673 case XmlNodeType.Whitespace:
\r
675 case XmlNodeType.EndElement:
\r
676 return String.Format ("</{0}>", Name);
\r
679 bool isEmpty = IsEmptyElement;
\r
680 string name = Name;
\r
682 XPathNavigator temp = current.Clone ();
\r
683 while (temp.MoveToNextAttribute ())
\r
684 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
685 if (!IsEmptyElement)
\r
686 return String.Format ("<{0}{1}>", name, atts);
\r
688 return String.Format ("<{0}{1} />", name, atts);
\r
691 // Arranged copy of XmlTextReader.ReadOuterXml()
\r
692 public override string ReadOuterXml ()
\r
694 if (ReadState != ReadState.Interactive)
\r
695 return String.Empty;
\r
697 switch (NodeType) {
\r
698 case XmlNodeType.Attribute:
\r
699 // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
\r
700 return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
\r
701 case XmlNodeType.Element:
\r
702 bool isEmpty = IsEmptyElement;
\r
703 string name = Name;
\r
704 StringBuilder atts = new StringBuilder ();
\r
705 XPathNavigator temp = current.Clone ();
\r
706 while (temp.MoveToNextAttribute ())
\r
707 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
710 return String.Format ("{0}{1}</{2}>", GetCurrentTagMarkup (), atts, ReadInnerXml (), name);
\r
712 return String.Format ("{0}", GetCurrentTagMarkup ());
\r
713 case XmlNodeType.None:
\r
714 // MS document is incorrect. Seems not to progress.
\r
715 return String.Empty;
\r
718 return String.Empty;
\r
723 public override string LookupNamespace (string prefix)
\r
725 XPathNavigator backup = current.Clone ();
\r
727 this.MoveToElement ();
\r
728 if (current.MoveToFirstNamespace ()) {
\r
730 if (current.LocalName == prefix)
\r
731 return current.Value;
\r
732 } while (current.MoveToNextNamespace ());
\r
740 // It does not support entity resolution.
\r
741 public override void ResolveEntity ()
\r
743 throw new InvalidOperationException ();
\r
746 public override bool ReadAttributeValue () {
\r
747 if (NodeType != XmlNodeType.Attribute)
\r
749 if (attributeValueConsumed)
\r
751 attributeValueConsumed = true;
\r