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 public 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
62 StringBuilder innerXmlBuilder = new StringBuilder ();
\r
65 int attributeCount = 0;
\r
70 public override XmlNodeType NodeType
\r
73 if (ReadState != ReadState.Interactive)
\r
74 return XmlNodeType.None;
\r
76 return XmlNodeType.EndElement;
\r
77 if (attributeValueConsumed)
\r
78 // Is there any way to get other kind of nodes than Text?
\r
79 return XmlNodeType.Text;
\r
81 switch (current.NodeType) {
\r
82 case XPathNodeType.Namespace:
\r
83 case XPathNodeType.Attribute:
\r
84 return XmlNodeType.Attribute;
\r
85 case XPathNodeType.Comment:
\r
86 return XmlNodeType.Comment;
\r
87 case XPathNodeType.Element:
\r
88 return XmlNodeType.Element;
\r
89 case XPathNodeType.ProcessingInstruction:
\r
90 return XmlNodeType.ProcessingInstruction;
\r
91 case XPathNodeType.Root:
\r
92 // It is actually Document, but in XmlReader there is no such situation to return Document.
\r
93 return XmlNodeType.None;
\r
94 case XPathNodeType.SignificantWhitespace:
\r
95 return XmlNodeType.SignificantWhitespace;
\r
96 case XPathNodeType.Text:
\r
97 return XmlNodeType.Text;
\r
98 case XPathNodeType.Whitespace:
\r
99 return XmlNodeType.Whitespace;
\r
101 throw new InvalidOperationException (String.Format ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.", current.NodeType));
\r
106 public override string Name {
\r
109 return String.Empty;
\r
110 else if (current.NodeType == XPathNodeType.Namespace)
\r
111 return current.Name == String.Empty ? "xmlns" : "xmlns:" + current.Name;
\r
113 return current.Name;
\r
117 public override string LocalName {
\r
120 return String.Empty;
\r
121 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName == String.Empty)
\r
124 return current.LocalName;
\r
128 public override string NamespaceURI {
\r
131 return String.Empty;
\r
132 else if (current.NodeType == XPathNodeType.Namespace)
\r
133 return "http://www.w3.org/2000/xmlns/";
\r
135 return current.NamespaceURI;
\r
139 public override string Prefix {
\r
142 return String.Empty;
\r
143 else if (current.NodeType == XPathNodeType.Namespace && current.LocalName != String.Empty)
\r
146 return current.Prefix;
\r
150 public override bool HasValue {
\r
152 switch (current.NodeType) {
\r
153 case XPathNodeType.Namespace:
\r
154 case XPathNodeType.Attribute:
\r
155 case XPathNodeType.Comment:
\r
156 case XPathNodeType.ProcessingInstruction:
\r
157 case XPathNodeType.SignificantWhitespace:
\r
158 case XPathNodeType.Text:
\r
159 case XPathNodeType.Whitespace:
\r
166 public override int Depth {
\r
168 switch (ReadState) {
\r
169 case ReadState.EndOfFile:
\r
170 case ReadState.Initial:
\r
171 case ReadState.Closed:
\r
178 public override string Value {
\r
180 switch (current.NodeType) {
\r
181 case XPathNodeType.Namespace:
\r
182 case XPathNodeType.Attribute:
\r
183 case XPathNodeType.Comment:
\r
184 case XPathNodeType.ProcessingInstruction:
\r
185 case XPathNodeType.SignificantWhitespace:
\r
186 case XPathNodeType.Text:
\r
187 case XPathNodeType.Whitespace:
\r
188 return current.Value;
\r
189 case XPathNodeType.Element:
\r
190 case XPathNodeType.Root:
\r
191 return String.Empty;
\r
193 throw new InvalidOperationException ("Current XPathNavigator status is {0} which is not acceptable to XmlReader.");
\r
198 public override string BaseURI {
\r
199 get { return current.BaseURI; }
\r
202 public override bool IsEmptyElement {
\r
203 get { return current.IsEmptyElement; }
\r
206 public override bool IsDefault {
\r
209 IXmlSchemaInfo si = current as IXmlSchemaInfo;
\r
210 return si != null && si.IsDefault;
\r
212 return false; // no way to check this.
\r
217 // It makes no sense.
\r
218 public override char QuoteChar {
\r
219 get { return '\"'; }
\r
222 public override string XmlLang {
\r
223 get { return current.XmlLang; }
\r
226 // It is meaningless.
\r
227 public override XmlSpace XmlSpace {
\r
228 get { return XmlSpace.None; }
\r
231 public override int AttributeCount {
\r
232 get { return attributeCount; }
\r
235 private int GetAttributeCount ()
\r
238 if (current.MoveToFirstAttribute ()) {
\r
241 } while (current.MoveToNextAttribute ());
\r
242 current.MoveToParent ();
\r
244 if (current.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
\r
247 } while (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
248 current.MoveToParent ();
\r
253 private bool MoveToAttributeNavigator (int i)
\r
255 switch (current.NodeType) {
\r
256 case XPathNodeType.Namespace:
\r
257 case XPathNodeType.Attribute:
\r
258 this.MoveToElement ();
\r
259 goto case XPathNodeType.Element;
\r
260 case XPathNodeType.Element:
\r
262 if (MoveToFirstAttribute ()) {
\r
266 for (count++; this.MoveToNextAttribute (); count++) {
\r
275 public override string this [int i] {
\r
277 XPathNavigator backup = current.Clone ();
\r
279 if (MoveToAttributeNavigator (i))
\r
282 throw new ArgumentOutOfRangeException ();
\r
284 current.MoveTo (backup);
\r
289 private void SplitName (string name, out string localName, out string ns)
\r
293 int colon = name.IndexOf (':');
\r
295 localName = name.Substring (colon + 1, name.Length - colon - 1);
\r
296 ns = this.LookupNamespace (name.Substring (0, colon));
\r
300 public override string this [string name] {
\r
304 SplitName (name, out localName, out ns);
\r
305 return this [localName, ns];
\r
309 public override string this [string localName, string namespaceURI] {
\r
311 string v = current.GetAttribute (localName, namespaceURI);
\r
312 if (v != String.Empty)
\r
314 XPathNavigator tmp = current.Clone ();
\r
315 return tmp.MoveToAttribute (localName, namespaceURI) ? String.Empty : null;
\r
319 public override bool EOF {
\r
320 get { return ReadState == ReadState.EndOfFile; }
\r
323 public override ReadState ReadState {
\r
326 return ReadState.EndOfFile;
\r
328 return ReadState.Closed;
\r
330 return ReadState.Initial;
\r
331 return ReadState.Interactive;
\r
335 public override XmlNameTable NameTable {
\r
336 get { return current.NameTable; }
\r
342 public override string GetAttribute (string name)
\r
346 SplitName (name, out localName, out ns);
\r
347 return this [localName, ns];
\r
350 public override string GetAttribute (string localName, string namespaceURI)
\r
352 return this [localName, namespaceURI];
\r
355 public override string GetAttribute (int i)
\r
360 private bool CheckAttributeMove (bool b)
\r
363 attributeValueConsumed = false;
\r
367 public override bool MoveToAttribute (string name)
\r
371 SplitName (name, out localName, out ns);
\r
372 return CheckAttributeMove (MoveToAttribute (localName, ns));
\r
375 public override bool MoveToAttribute (string localName, string namespaceURI)
\r
377 XPathNavigator backup = null;
\r
378 switch (current.NodeType) {
\r
379 case XPathNodeType.Attribute:
\r
380 backup = current.Clone ();
\r
381 this.MoveToElement ();
\r
382 goto case XPathNodeType.Element;
\r
383 case XPathNodeType.Element:
\r
384 while (MoveToNextAttribute ())
\r
385 if (current.LocalName == localName && current.NamespaceURI == namespaceURI) {
\r
386 attributeValueConsumed = false;
\r
391 if (backup != null)
\r
396 public override void MoveToAttribute (int i)
\r
398 if (!MoveToAttributeNavigator (i))
\r
399 throw new ArgumentOutOfRangeException ();
\r
402 public override bool MoveToFirstAttribute ()
\r
404 bool b = CheckAttributeMove (current.MoveToFirstNamespace (XPathNamespaceScope.Local));
\r
407 return CheckAttributeMove (current.MoveToFirstAttribute ());
\r
410 public override bool MoveToNextAttribute ()
\r
412 if (current.NodeType == XPathNodeType.Namespace) {
\r
413 bool b = CheckAttributeMove (current.MoveToNextNamespace (XPathNamespaceScope.Local));
\r
416 current.MoveToParent ();
\r
417 b = CheckAttributeMove (current.MoveToFirstAttribute ());
\r
421 return CheckAttributeMove (current.MoveToNextAttribute ());
\r
424 public override bool MoveToElement ()
\r
426 if (current.NodeType == XPathNodeType.Attribute ||
\r
427 current.NodeType == XPathNodeType.Namespace) {
\r
428 attributeValueConsumed = false;
\r
429 return current.MoveToParent ();
\r
434 public override void Close ()
\r
440 public override bool Read ()
\r
442 switch (ReadState) {
\r
443 case ReadState.EndOfFile:
\r
444 case ReadState.Closed:
\r
445 case ReadState.Error:
\r
447 case ReadState.Initial:
\r
449 switch (current.NodeType) {
\r
450 case XPathNodeType.Root:
\r
451 // recurse, but as Interactive
\r
453 case XPathNodeType.Element:
\r
454 if (current.IsEmptyElement)
\r
456 attributeCount = GetAttributeCount ();
\r
473 if (endElement || !current.MoveToFirstChild ()) {
\r
474 if (current.IsSamePosition (root)) { // It should happen only when the root node was empty.
\r
478 if (!current.MoveToNext ()) {
\r
479 current.MoveToParent ();
\r
481 endElement = (current.NodeType == XPathNodeType.Element);
\r
482 if (current.IsSamePosition (root)) {
\r
483 if (current.NodeType == XPathNodeType.Element)
\r
486 endElement = false;
\r
492 endElement = false;
\r
494 else if (!endElement)
\r
496 attributeCount = GetAttributeCount ();
\r
500 public override string ReadString ()
\r
502 readStringBuffer.Length = 0;
\r
504 switch (NodeType) {
\r
506 return String.Empty;
\r
507 case XmlNodeType.Element:
\r
508 if (IsEmptyElement)
\r
509 return String.Empty;
\r
512 switch (NodeType) {
\r
513 case XmlNodeType.Text:
\r
514 case XmlNodeType.CDATA:
\r
515 case XmlNodeType.Whitespace:
\r
516 case XmlNodeType.SignificantWhitespace:
\r
517 readStringBuffer.Append (Value);
\r
523 case XmlNodeType.Text:
\r
524 case XmlNodeType.CDATA:
\r
525 case XmlNodeType.Whitespace:
\r
526 case XmlNodeType.SignificantWhitespace:
\r
528 switch (NodeType) {
\r
529 case XmlNodeType.Text:
\r
530 case XmlNodeType.CDATA:
\r
531 case XmlNodeType.Whitespace:
\r
532 case XmlNodeType.SignificantWhitespace:
\r
533 readStringBuffer.Append (Value);
\r
541 string ret = readStringBuffer.ToString ();
\r
542 readStringBuffer.Length = 0;
\r
546 public override string LookupNamespace (string prefix)
\r
548 XPathNavigator backup = current.Clone ();
\r
550 this.MoveToElement ();
\r
551 if (current.MoveToFirstNamespace ()) {
\r
553 if (current.LocalName == prefix)
\r
554 return current.Value;
\r
555 } while (current.MoveToNextNamespace ());
\r
563 // It does not support entity resolution.
\r
564 public override void ResolveEntity ()
\r
566 throw new InvalidOperationException ();
\r
569 public override bool ReadAttributeValue () {
\r
570 if (NodeType != XmlNodeType.Attribute)
\r
572 if (attributeValueConsumed)
\r
574 attributeValueConsumed = true;
\r