1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
|
//------------------------------------------------------------------------------
// <copyright file="ReaderOutput.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Xsl.XsltOld {
using Res = System.Xml.Utils.Res;
using System;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
internal class ReaderOutput : XmlReader, RecordOutput {
private Processor processor;
private XmlNameTable nameTable;
// Main node + Fields Collection
private RecordBuilder builder;
private BuilderInfo mainNode;
private ArrayList attributeList;
private int attributeCount;
private BuilderInfo attributeValue;
// OutputScopeManager
private OutputScopeManager manager;
// Current position in the list
private int currentIndex;
private BuilderInfo currentInfo;
// Reader state
private ReadState state = ReadState.Initial;
private bool haveRecord;
// Static default record
static BuilderInfo s_DefaultInfo = new BuilderInfo();
XmlEncoder encoder = new XmlEncoder();
XmlCharType xmlCharType = XmlCharType.Instance;
internal ReaderOutput(Processor processor) {
Debug.Assert(processor != null);
Debug.Assert(processor.NameTable != null);
this.processor = processor;
this.nameTable = processor.NameTable;
Reset();
}
// XmlReader abstract methods implementation
public override XmlNodeType NodeType {
get {
CheckCurrentInfo();
return this.currentInfo.NodeType;
}
}
public override string Name {
get {
CheckCurrentInfo();
string prefix = Prefix;
string localName = LocalName;
if (prefix != null && prefix.Length > 0) {
if (localName.Length > 0) {
return nameTable.Add(prefix + ":" + localName);
}
else {
return prefix;
}
}
else {
return localName;
}
}
}
public override string LocalName {
get {
CheckCurrentInfo();
return this.currentInfo.LocalName;
}
}
public override string NamespaceURI {
get {
CheckCurrentInfo();
return this.currentInfo.NamespaceURI;
}
}
public override string Prefix {
get {
CheckCurrentInfo();
return this.currentInfo.Prefix;
}
}
public override bool HasValue {
get {
return XmlReader.HasValueInternal(NodeType);
}
}
public override string Value {
get {
CheckCurrentInfo();
return this.currentInfo.Value;
}
}
public override int Depth {
get {
CheckCurrentInfo();
return this.currentInfo.Depth;
}
}
public override string BaseURI {
get {
return string.Empty;
}
}
public override bool IsEmptyElement {
get {
CheckCurrentInfo();
return this.currentInfo.IsEmptyTag;
}
}
public override char QuoteChar {
get { return encoder.QuoteChar; }
}
public override bool IsDefault {
get { return false; }
}
public override XmlSpace XmlSpace {
get { return this.manager != null ? this.manager.XmlSpace : XmlSpace.None; }
}
public override string XmlLang {
get { return this.manager != null ? this.manager.XmlLang : string.Empty; }
}
// Attribute Accessors
public override int AttributeCount {
get { return this.attributeCount; }
}
public override string GetAttribute(string name) {
int ordinal;
if (FindAttribute(name, out ordinal)) {
Debug.Assert(ordinal >= 0);
return((BuilderInfo)this.attributeList[ordinal]).Value;
}
else {
Debug.Assert(ordinal == -1);
return null;
}
}
public override string GetAttribute(string localName, string namespaceURI) {
int ordinal;
if (FindAttribute(localName, namespaceURI, out ordinal)) {
Debug.Assert(ordinal >= 0);
return((BuilderInfo)this.attributeList[ordinal]).Value;
}
else {
Debug.Assert(ordinal == -1);
return null;
}
}
public override string GetAttribute(int i) {
BuilderInfo attribute = GetBuilderInfo(i);
return attribute.Value;
}
public override string this [int i] {
get { return GetAttribute(i); }
}
public override string this [string name] {
get { return GetAttribute(name); }
}
public override string this [string name, string namespaceURI] {
get { return GetAttribute(name, namespaceURI); }
}
public override bool MoveToAttribute(string name) {
int ordinal;
if (FindAttribute(name, out ordinal)) {
Debug.Assert(ordinal >= 0);
SetAttribute(ordinal);
return true;
}
else {
Debug.Assert(ordinal == -1);
return false;
}
}
public override bool MoveToAttribute(string localName, string namespaceURI) {
int ordinal;
if (FindAttribute(localName, namespaceURI, out ordinal)) {
Debug.Assert(ordinal >= 0);
SetAttribute(ordinal);
return true;
}
else {
Debug.Assert(ordinal == -1);
return false;
}
}
public override void MoveToAttribute(int i) {
if (i < 0 || this.attributeCount <= i) {
throw new ArgumentOutOfRangeException("i");
}
SetAttribute(i);
}
public override bool MoveToFirstAttribute() {
if (this.attributeCount <= 0) {
Debug.Assert(this.attributeCount == 0);
return false;
}
else {
SetAttribute(0);
return true;
}
}
public override bool MoveToNextAttribute() {
if (this.currentIndex + 1 < this.attributeCount) {
SetAttribute(this.currentIndex + 1);
return true;
}
return false;
}
public override bool MoveToElement() {
if (NodeType == XmlNodeType.Attribute || this.currentInfo == this.attributeValue) {
SetMainNode();
return true;
}
return false;
}
// Moving through the Stream
public override bool Read() {
Debug.Assert(this.processor != null || this.state == ReadState.Closed);
if (this.state != ReadState.Interactive) {
if (this.state == ReadState.Initial) {
state = ReadState.Interactive;
}
else {
return false;
}
}
while (true) { // while -- to ignor empty whitespace nodes.
if (this.haveRecord) {
this.processor.ResetOutput();
this.haveRecord = false;
}
this.processor.Execute();
if (this.haveRecord) {
CheckCurrentInfo();
// check text nodes on whitespaces;
switch (this.NodeType) {
case XmlNodeType.Text :
if (xmlCharType.IsOnlyWhitespace(this.Value)) {
this.currentInfo.NodeType = XmlNodeType.Whitespace;
goto case XmlNodeType.Whitespace;
}
Debug.Assert(this.Value.Length != 0, "It whould be Whitespace in this case");
break;
case XmlNodeType.Whitespace :
if(this.Value.Length == 0) {
continue; // ignoring emty text nodes
}
if (this.XmlSpace == XmlSpace.Preserve) {
this.currentInfo.NodeType = XmlNodeType.SignificantWhitespace;
}
break;
}
}
else {
Debug.Assert(this.processor.ExecutionDone);
this.state = ReadState.EndOfFile;
Reset();
}
return this.haveRecord;
}
}
public override bool EOF {
get { return this.state == ReadState.EndOfFile; }
}
public override void Close() {
this.processor = null;
this.state = ReadState.Closed;
Reset();
}
public override ReadState ReadState {
get { return this.state; }
}
// Whole Content Read Methods
public override string ReadString() {
string result = string.Empty;
if (NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || this.currentInfo == this.attributeValue) {
if(this.mainNode.IsEmptyTag) {
return result;
}
if (! Read()) {
throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
}
}
StringBuilder sb = null;
bool first = true;
while(true) {
switch (NodeType) {
case XmlNodeType.Text:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
// case XmlNodeType.CharacterEntity:
if (first) {
result = this.Value;
first = false;
} else {
if (sb == null) {
sb = new StringBuilder(result);
}
sb.Append(this.Value);
}
if (! Read())
throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
break;
default:
return (sb == null) ? result : sb.ToString();
}
}
}
public override string ReadInnerXml() {
if (ReadState == ReadState.Interactive) {
if (NodeType == XmlNodeType.Element && ! IsEmptyElement) {
StringOutput output = new StringOutput(this.processor);
output.OmitXmlDecl();
int depth = Depth;
Read(); // skeep begin Element
while (depth < Depth) { // process content
Debug.Assert(this.builder != null);
output.RecordDone(this.builder);
Read();
}
Debug.Assert(NodeType == XmlNodeType.EndElement);
Read(); // skeep end element
output.TheEnd();
return output.Result;
}
else if(NodeType == XmlNodeType.Attribute) {
return encoder.AtributeInnerXml(Value);
}
else {
Read();
}
}
return string.Empty;
}
public override string ReadOuterXml() {
if (ReadState == ReadState.Interactive) {
if (NodeType == XmlNodeType.Element) {
StringOutput output = new StringOutput(this.processor);
output.OmitXmlDecl();
bool emptyElement = IsEmptyElement;
int depth = Depth;
// process current record
output.RecordDone(this.builder);
Read();
// process internal elements & text nodes
while(depth < Depth) {
Debug.Assert(this.builder != null);
output.RecordDone(this.builder);
Read();
}
// process end element
if (! emptyElement) {
output.RecordDone(this.builder);
Read();
}
output.TheEnd();
return output.Result;
}
else if(NodeType == XmlNodeType.Attribute) {
return encoder.AtributeOuterXml(Name, Value);
}
else {
Read();
}
}
return string.Empty;
}
//
// Nametable and Namespace Helpers
//
public override XmlNameTable NameTable {
get {
Debug.Assert(this.nameTable != null);
return this.nameTable;
}
}
public override string LookupNamespace(string prefix) {
prefix = this.nameTable.Get(prefix);
if (this.manager != null && prefix != null) {
return this.manager.ResolveNamespace(prefix);
}
return null;
}
public override void ResolveEntity() {
Debug.Assert(NodeType != XmlNodeType.EntityReference);
if (NodeType != XmlNodeType.EntityReference) {
throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
}
}
public override bool ReadAttributeValue() {
if (ReadState != ReadState.Interactive || NodeType != XmlNodeType.Attribute) {
return false;
}
if (this.attributeValue == null) {
this.attributeValue = new BuilderInfo();
this.attributeValue.NodeType = XmlNodeType.Text;
}
if (this.currentInfo == this.attributeValue) {
return false;
}
this.attributeValue.Value = this.currentInfo.Value;
this.attributeValue.Depth = this.currentInfo.Depth + 1;
this.currentInfo = this.attributeValue;
return true;
}
//
// RecordOutput interface method implementation
//
public Processor.OutputResult RecordDone(RecordBuilder record) {
this.builder = record;
this.mainNode = record.MainNode;
this.attributeList = record.AttributeList;
this.attributeCount = record.AttributeCount;
this.manager = record.Manager;
this.haveRecord = true;
SetMainNode();
return Processor.OutputResult.Interrupt;
}
public void TheEnd() {
// nothing here, was taken care of by RecordBuilder
}
//
// Implementation internals
//
private void SetMainNode() {
this.currentIndex = -1;
this.currentInfo = this.mainNode;
}
private void SetAttribute(int attrib) {
Debug.Assert(0 <= attrib && attrib < this.attributeCount);
Debug.Assert(0 <= attrib && attrib < this.attributeList.Count);
Debug.Assert(this.attributeList[attrib] is BuilderInfo);
this.currentIndex = attrib;
this.currentInfo = (BuilderInfo) this.attributeList[attrib];
}
private BuilderInfo GetBuilderInfo(int attrib) {
if (attrib < 0 || this.attributeCount <= attrib) {
throw new ArgumentOutOfRangeException("attrib");
}
Debug.Assert(this.attributeList[attrib] is BuilderInfo);
return(BuilderInfo) this.attributeList[attrib];
}
private bool FindAttribute(String localName, String namespaceURI, out int attrIndex) {
if (namespaceURI == null) {
namespaceURI = string.Empty;
}
if (localName == null) {
localName = string.Empty;
}
for (int index = 0; index < this.attributeCount; index ++) {
Debug.Assert(this.attributeList[index] is BuilderInfo);
BuilderInfo attribute = (BuilderInfo) this.attributeList[index];
if (attribute.NamespaceURI == namespaceURI && attribute.LocalName == localName) {
attrIndex = index;
return true;
}
}
attrIndex = -1;
return false;
}
private bool FindAttribute(String name, out int attrIndex) {
if (name == null) {
name = string.Empty;
}
for (int index = 0; index < this.attributeCount; index ++) {
Debug.Assert(this.attributeList[index] is BuilderInfo);
BuilderInfo attribute = (BuilderInfo) this.attributeList[index];
if (attribute.Name == name) {
attrIndex = index;
return true;
}
}
attrIndex = -1;
return false;
}
private void Reset() {
this.currentIndex = -1;
this.currentInfo = s_DefaultInfo;
this.mainNode = s_DefaultInfo;
this.manager = null;
}
[System.Diagnostics.Conditional("DEBUG")]
private void CheckCurrentInfo() {
Debug.Assert(this.currentInfo != null);
Debug.Assert(this.attributeCount == 0 || this.attributeList != null);
Debug.Assert((this.currentIndex == -1) == (this.currentInfo == this.mainNode));
Debug.Assert((this.currentIndex == -1) || (this.currentInfo == this.attributeValue || this.attributeList[this.currentIndex] is BuilderInfo && this.attributeList[this.currentIndex] == this.currentInfo));
}
private class XmlEncoder {
private StringBuilder buffer = null;
private XmlTextEncoder encoder = null;
private void Init() {
buffer = new StringBuilder();
encoder = new XmlTextEncoder(new StringWriter(buffer, CultureInfo.InvariantCulture));
}
public string AtributeInnerXml(string value) {
if(encoder == null) Init();
buffer .Length = 0; // clean buffer
encoder.StartAttribute(/*save:*/false);
encoder.Write(value);
encoder.EndAttribute();
return buffer.ToString();
}
public string AtributeOuterXml(string name, string value) {
if(encoder == null) Init();
buffer .Length = 0; // clean buffer
buffer .Append(name);
buffer .Append('=');
buffer .Append(QuoteChar);
encoder.StartAttribute(/*save:*/false);
encoder.Write(value);
encoder.EndAttribute();
buffer .Append(QuoteChar);
return buffer.ToString();
}
public char QuoteChar {
get { return '"'; }
}
}
}
}
|