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
34 using System.Xml.Schema;
\r
35 using System.Xml.XPath;
\r
37 namespace Mono.Xml.XPath
\r
40 internal class XPathNavigatorReader : XmlReader
\r
42 public XPathNavigatorReader (XPathNavigator nav)
\r
44 // It seems that this class have only to support linked
\r
45 // node as its parameter
\r
46 switch (nav.NodeType) {
\r
47 case XPathNodeType.Attribute:
\r
48 case XPathNodeType.Namespace:
\r
49 throw new InvalidOperationException (String.Format ("NodeType {0} is not supported to read as a subtree of an XPathNavigator.", nav.NodeType));
\r
51 root = nav.Clone ();
\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
74 public override bool CanReadBinaryContent {
\r
75 get { return true; }
\r
78 public override bool CanReadValueChunk {
\r
79 get { return true; }
\r
83 public override XmlNodeType NodeType
\r
86 if (ReadState != ReadState.Interactive)
\r
87 return XmlNodeType.None;
\r
89 return XmlNodeType.EndElement;
\r
90 if (attributeValueConsumed)
\r
91 // Is there any way to get other kind of nodes than Text?
\r
92 return XmlNodeType.Text;
\r
94 switch (current.NodeType) {
\r
95 case XPathNodeType.Namespace:
\r
96 case XPathNodeType.Attribute:
\r
97 return XmlNodeType.Attribute;
\r
98 case XPathNodeType.Comment:
\r
99 return XmlNodeType.Comment;
\r
100 case XPathNodeType.Element:
\r
101 return XmlNodeType.Element;
\r
102 case XPathNodeType.ProcessingInstruction:
\r
103 return XmlNodeType.ProcessingInstruction;
\r
104 case XPathNodeType.Root:
\r
105 // It is actually Document, but in XmlReader there is no such situation to return Document.
\r
106 return XmlNodeType.None;
\r
107 case XPathNodeType.SignificantWhitespace:
\r
108 return XmlNodeType.SignificantWhitespace;
\r
109 case XPathNodeType.Text:
\r
110 return XmlNodeType.Text;
\r
111 case XPathNodeType.Whitespace:
\r
112 return XmlNodeType.Whitespace;
\r
114 throw new InvalidOperationException (String.Format ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.", current.NodeType));
\r
119 public override string Name {
\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
133 return String.Empty;
\r
134 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName == String.Empty)
\r
137 return current.LocalName;
\r
141 public override string NamespaceURI {
\r
144 return String.Empty;
\r
145 else if (current.NodeType == XPathNodeType.Namespace)
\r
146 return "http://www.w3.org/2000/xmlns/";
\r
148 return current.NamespaceURI;
\r
152 public override string Prefix {
\r
155 return String.Empty;
\r
156 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName != String.Empty)
\r
159 return current.Prefix;
\r
163 public override bool HasValue {
\r
165 switch (current.NodeType) {
\r
166 case XPathNodeType.Namespace:
\r
167 case XPathNodeType.Attribute:
\r
168 case XPathNodeType.Comment:
\r
169 case XPathNodeType.ProcessingInstruction:
\r
170 case XPathNodeType.SignificantWhitespace:
\r
171 case XPathNodeType.Text:
\r
172 case XPathNodeType.Whitespace:
\r
179 public override int Depth {
\r
181 switch (ReadState) {
\r
182 case ReadState.EndOfFile:
\r
183 case ReadState.Initial:
\r
184 case ReadState.Closed:
\r
191 public override string Value {
\r
193 switch (current.NodeType) {
\r
194 case XPathNodeType.Namespace:
\r
195 case XPathNodeType.Attribute:
\r
196 case XPathNodeType.Comment:
\r
197 case XPathNodeType.ProcessingInstruction:
\r
198 case XPathNodeType.SignificantWhitespace:
\r
199 case XPathNodeType.Text:
\r
200 case XPathNodeType.Whitespace:
\r
201 return current.Value;
\r
202 case XPathNodeType.Element:
\r
203 case XPathNodeType.Root:
\r
204 return String.Empty;
\r
206 throw new InvalidOperationException ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.");
\r
211 public override string BaseURI {
\r
212 get { return current.BaseURI; }
\r
215 public override bool IsEmptyElement {
\r
216 get { return current.IsEmptyElement; }
\r
219 public override bool IsDefault {
\r
222 IXmlSchemaInfo si = current as IXmlSchemaInfo;
\r
223 return si != null && si.IsDefault;
\r
225 return false; // no way to check this.
\r
230 // It makes no sense.
\r
231 public override char QuoteChar {
\r
232 get { return '\"'; }
\r
236 public override IXmlSchemaInfo SchemaInfo {
\r
237 get { return current.SchemaInfo; }
\r
241 public override string XmlLang {
\r
242 get { return current.XmlLang; }
\r
245 // It is meaningless.
\r
246 public override XmlSpace XmlSpace {
\r
247 get { return XmlSpace.None; }
\r
250 public override int AttributeCount {
\r
251 get { return attributeCount; }
\r
254 private int GetAttributeCount ()
\r
257 if (current.MoveToFirstAttribute ()) {
\r
260 } while (current.MoveToNextAttribute ());
\r
261 current.MoveToParent ();
\r
263 if (current.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
\r
266 } while (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
267 current.MoveToParent ();
\r
272 private bool MoveToAttributeNavigator (int i)
\r
274 switch (current.NodeType) {
\r
275 case XPathNodeType.Namespace:
\r
276 case XPathNodeType.Attribute:
\r
277 this.MoveToElement ();
\r
278 goto case XPathNodeType.Element;
\r
279 case XPathNodeType.Element:
\r
281 if (MoveToFirstAttribute ()) {
\r
285 for (count++; this.MoveToNextAttribute (); count++) {
\r
294 public override string this [int i] {
\r
296 XPathNavigator backup = current.Clone ();
\r
298 if (MoveToAttributeNavigator (i))
\r
301 throw new ArgumentOutOfRangeException ();
\r
303 current.MoveTo (backup);
\r
308 private void SplitName (string name, out string localName, out string ns)
\r
312 int colon = name.IndexOf (':');
\r
314 localName = name.Substring (colon + 1, name.Length - colon - 1);
\r
315 ns = this.LookupNamespace (name.Substring (0, colon));
\r
319 public override string this [string name] {
\r
323 SplitName (name, out localName, out ns);
\r
324 return this [localName, ns];
\r
328 public override string this [string localName, string namespaceURI] {
\r
330 string v = current.GetAttribute (localName, namespaceURI);
\r
331 if (v != String.Empty)
\r
333 XPathNavigator tmp = current.Clone ();
\r
334 return tmp.MoveToAttribute (localName, namespaceURI) ? String.Empty : null;
\r
338 public override bool EOF {
\r
339 get { return ReadState == ReadState.EndOfFile; }
\r
342 public override ReadState ReadState {
\r
345 return ReadState.EndOfFile;
\r
347 return ReadState.Closed;
\r
349 return ReadState.Initial;
\r
350 return ReadState.Interactive;
\r
354 public override XmlNameTable NameTable {
\r
355 get { return current.NameTable; }
\r
361 public override string GetAttribute (string name)
\r
365 SplitName (name, out localName, out ns);
\r
366 return this [localName, ns];
\r
369 public override string GetAttribute (string localName, string namespaceURI)
\r
371 return this [localName, namespaceURI];
\r
374 public override string GetAttribute (int i)
\r
379 private bool CheckAttributeMove (bool b)
\r
382 attributeValueConsumed = false;
\r
386 public override bool MoveToAttribute (string name)
\r
390 SplitName (name, out localName, out ns);
\r
391 return CheckAttributeMove (MoveToAttribute (localName, ns));
\r
394 public override bool MoveToAttribute (string localName, string namespaceURI)
\r
396 XPathNavigator backup = null;
\r
397 switch (current.NodeType) {
\r
398 case XPathNodeType.Attribute:
\r
399 backup = current.Clone ();
\r
400 this.MoveToElement ();
\r
401 goto case XPathNodeType.Element;
\r
402 case XPathNodeType.Element:
\r
403 while (MoveToNextAttribute ())
\r
404 if (current.LocalName == localName && current.NamespaceURI == namespaceURI) {
\r
405 attributeValueConsumed = false;
\r
410 if (backup != null)
\r
415 public override void MoveToAttribute (int i)
\r
417 if (!MoveToAttributeNavigator (i))
\r
418 throw new ArgumentOutOfRangeException ();
\r
421 public override bool MoveToFirstAttribute ()
\r
423 bool b = CheckAttributeMove (current.MoveToFirstNamespace (XPathNamespaceScope.Local));
\r
426 return CheckAttributeMove (current.MoveToFirstAttribute ());
\r
429 public override bool MoveToNextAttribute ()
\r
431 if (current.NodeType == XPathNodeType.Namespace) {
\r
432 bool b = CheckAttributeMove (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
435 current.MoveToParent ();
\r
436 b = CheckAttributeMove (current.MoveToFirstAttribute ());
\r
440 return CheckAttributeMove (current.MoveToNextAttribute ());
\r
443 public override bool MoveToElement ()
\r
445 if (current.NodeType == XPathNodeType.Attribute ||
\r
446 current.NodeType == XPathNodeType.Namespace) {
\r
447 attributeValueConsumed = false;
\r
448 return current.MoveToParent ();
\r
453 public override void Close ()
\r
459 public override bool Read ()
\r
462 if (Binary != null)
\r
466 switch (ReadState) {
\r
467 case ReadState.EndOfFile:
\r
468 case ReadState.Closed:
\r
469 case ReadState.Error:
\r
471 case ReadState.Initial:
\r
473 switch (current.NodeType) {
\r
474 case XPathNodeType.Root:
\r
475 // recurse, but as Interactive
\r
477 case XPathNodeType.Element:
\r
478 if (current.IsEmptyElement)
\r
480 attributeCount = GetAttributeCount ();
\r
497 if (endElement || !current.MoveToFirstChild ()) {
\r
498 if (current.IsSamePosition (root)) { // It should happen only when the root node was empty.
\r
502 if (!current.MoveToNext ()) {
\r
503 current.MoveToParent ();
\r
505 endElement = (current.NodeType == XPathNodeType.Element);
\r
506 if (current.IsSamePosition (root)) {
\r
507 if (current.NodeType == XPathNodeType.Element)
\r
510 endElement = false;
\r
516 endElement = false;
\r
518 else if (!endElement)
\r
520 attributeCount = GetAttributeCount ();
\r
524 public override string ReadString ()
\r
526 readStringBuffer.Length = 0;
\r
528 switch (NodeType) {
\r
530 return String.Empty;
\r
531 case XmlNodeType.Element:
\r
532 if (IsEmptyElement)
\r
533 return String.Empty;
\r
536 switch (NodeType) {
\r
537 case XmlNodeType.Text:
\r
538 case XmlNodeType.CDATA:
\r
539 case XmlNodeType.Whitespace:
\r
540 case XmlNodeType.SignificantWhitespace:
\r
541 readStringBuffer.Append (Value);
\r
547 case XmlNodeType.Text:
\r
548 case XmlNodeType.CDATA:
\r
549 case XmlNodeType.Whitespace:
\r
550 case XmlNodeType.SignificantWhitespace:
\r
552 switch (NodeType) {
\r
553 case XmlNodeType.Text:
\r
554 case XmlNodeType.CDATA:
\r
555 case XmlNodeType.Whitespace:
\r
556 case XmlNodeType.SignificantWhitespace:
\r
557 readStringBuffer.Append (Value);
\r
565 string ret = readStringBuffer.ToString ();
\r
566 readStringBuffer.Length = 0;
\r
572 public override string ReadInnerXml ()
\r
574 if (ReadState != ReadState.Interactive)
\r
575 return String.Empty;
\r
577 switch (NodeType) {
\r
578 case XmlNodeType.Attribute:
\r
580 case XmlNodeType.Element:
\r
581 if (IsEmptyElement)
\r
582 return String.Empty;
\r
584 int startDepth = Depth;
\r
586 innerXmlBuilder.Length = 0;
\r
590 if (NodeType ==XmlNodeType.None)
\r
591 throw new InvalidOperationException ("unexpected end of xml.");
\r
592 else if (NodeType == XmlNodeType.EndElement && Depth == startDepth) {
\r
597 innerXmlBuilder.Append (GetCurrentTagMarkup ());
\r
599 string xml = innerXmlBuilder.ToString ();
\r
600 innerXmlBuilder.Length = 0;
\r
602 case XmlNodeType.None:
\r
603 // MS document is incorrect. Seems not to progress.
\r
604 return String.Empty;
\r
607 return String.Empty;
\r
611 StringBuilder atts = new StringBuilder ();
\r
612 private string GetCurrentTagMarkup ()
\r
614 switch (NodeType) {
\r
615 case XmlNodeType.CDATA:
\r
616 return String.Format ("<![CDATA[{0}]]>", Value.Replace ("]]>", "]]>"));
\r
617 case XmlNodeType.Text:
\r
618 return Value.Replace ("<", "<");
\r
619 case XmlNodeType.Comment:
\r
620 return String.Format ("<!--{0}-->", Value);
\r
621 case XmlNodeType.SignificantWhitespace:
\r
622 case XmlNodeType.Whitespace:
\r
624 case XmlNodeType.EndElement:
\r
625 return String.Format ("</{0}>", Name);
\r
628 bool isEmpty = IsEmptyElement;
\r
629 string name = Name;
\r
631 XPathNavigator temp = current.Clone ();
\r
632 while (temp.MoveToNextAttribute ())
\r
633 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
634 if (!IsEmptyElement)
\r
635 return String.Format ("<{0}{1}>", name, atts);
\r
637 return String.Format ("<{0}{1} />", name, atts);
\r
640 // Arranged copy of XmlTextReader.ReadOuterXml()
\r
641 public override string ReadOuterXml ()
\r
643 if (ReadState != ReadState.Interactive)
\r
644 return String.Empty;
\r
646 switch (NodeType) {
\r
647 case XmlNodeType.Attribute:
\r
648 // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
\r
649 return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
\r
650 case XmlNodeType.Element:
\r
651 bool isEmpty = IsEmptyElement;
\r
652 string name = Name;
\r
653 StringBuilder atts = new StringBuilder ();
\r
654 XPathNavigator temp = current.Clone ();
\r
655 while (temp.MoveToNextAttribute ())
\r
656 atts.AppendFormat (" {0}='{1}'", temp.Name, temp.Value.Replace ("'", "'"));
\r
659 return String.Format ("{0}{1}</{2}>", GetCurrentTagMarkup (), atts, ReadInnerXml (), name);
\r
661 return String.Format ("{0}", GetCurrentTagMarkup ());
\r
662 case XmlNodeType.None:
\r
663 // MS document is incorrect. Seems not to progress.
\r
664 return String.Empty;
\r
667 return String.Empty;
\r
672 public override string LookupNamespace (string prefix)
\r
674 XPathNavigator backup = current.Clone ();
\r
676 this.MoveToElement ();
\r
677 if (current.MoveToFirstNamespace ()) {
\r
679 if (current.LocalName == prefix)
\r
680 return current.Value;
\r
681 } while (current.MoveToNextNamespace ());
\r
689 // It does not support entity resolution.
\r
690 public override void ResolveEntity ()
\r
692 throw new InvalidOperationException ();
\r
695 public override bool ReadAttributeValue () {
\r
696 if (NodeType != XmlNodeType.Attribute)
\r
698 if (attributeValueConsumed)
\r
700 attributeValueConsumed = true;
\r