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
|
//------------------------------------------------------------------------------
// <copyright file="RtfNavigator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Threading;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Text;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
namespace System.Xml.Xsl.Runtime {
/// <summary>
/// RtfNavigators store Xslt result-tree-fragments. At runtime, the Xslt library tests to see if a Navigator
/// is an RtfNavigator in order to enforce certain restrictions, such as prohibiting querying into Rtfs.
/// Furthermore, Rtfs must store extra serialization information required in order to properly implement the
/// Xslt disable-output-escaping flag.
/// </summary>
internal abstract class RtfNavigator : XPathNavigator {
//-----------------------------------------------
// RtfNavigator
//-----------------------------------------------
/// <summary>
/// Preserve serialization hints when deep copying.
/// </summary>
public abstract void CopyToWriter(XmlWriter writer);
/// <summary>
/// Discard serialization hints and return a navigator that actually allows navigation.
/// </summary>
public abstract XPathNavigator ToNavigator();
//-----------------------------------------------
// XPathNavigator
//-----------------------------------------------
/// <summary>
/// Get the XPath node type of the current node.
/// </summary>
public override XPathNodeType NodeType {
get { return XPathNodeType.Root; }
}
/// <summary>
/// Get the local name portion of the current node's name.
/// </summary>
public override string LocalName {
get { return string.Empty; }
}
/// <summary>
/// Get the namespace portion of the current node's name.
/// </summary>
public override string NamespaceURI {
get { return string.Empty; }
}
/// <summary>
/// Get the name of the current node.
/// </summary>
public override string Name {
get { return string.Empty; }
}
/// <summary>
/// Get the prefix portion of the current node's name.
/// </summary>
public override string Prefix {
get { return string.Empty; }
}
/// <summary>
/// Return true if this is an element which used a shortcut tag in its Xml 1.0 serialized form.
/// </summary>
public override bool IsEmptyElement {
get { return false; }
}
/// <summary>
/// Return the xml name table which was used to atomize all prefixes, local-names, and
/// namespace uris in the document.
/// </summary>
public override XmlNameTable NameTable {
get { throw new NotSupportedException(); }
}
/// <summary>
/// Position the navigator on the first attribute of the current node and return true. If no attributes
/// can be found, return false.
/// </summary>
public override bool MoveToFirstAttribute() {
throw new NotSupportedException();
}
/// <summary>
/// If positioned on an attribute, move to its next sibling attribute. If no attributes can be found,
/// return false.
/// </summary>
public override bool MoveToNextAttribute() {
throw new NotSupportedException();
}
/// <summary>
/// Position the navigator on the namespace within the specified scope. If no matching namespace
/// can be found, return false.
/// </summary>
public override bool MoveToFirstNamespace(XPathNamespaceScope namespaceScope) {
throw new NotSupportedException();
}
/// <summary>
/// Position the navigator on the next namespace within the specified scope. If no matching namespace
/// can be found, return false.
/// </summary>
public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope) {
throw new NotSupportedException();
}
/// <summary>
/// If the current node is an attribute or namespace (not content), return false. Otherwise,
/// move to the next content node. Return false if there are no more content nodes.
/// </summary>
public override bool MoveToNext() {
throw new NotSupportedException();
}
/// <summary>
/// If the current node is an attribute or namespace (not content), return false. Otherwise,
/// move to the previous (sibling) content node. Return false if there are no previous content nodes.
/// </summary>
public override bool MoveToPrevious() {
throw new NotSupportedException();
}
/// <summary>
/// Move to the first content-typed child of the current node. Return false if the current
/// node has no content children.
/// </summary>
public override bool MoveToFirstChild() {
throw new NotSupportedException();
}
/// <summary>
/// Position the navigator on the parent of the current node. If the current node has no parent,
/// return false.
/// </summary>
public override bool MoveToParent() {
throw new NotSupportedException();
}
/// <summary>
/// Position to the navigator to the element whose id is equal to the specified "id" string.
/// </summary>
public override bool MoveToId(string id) {
throw new NotSupportedException();
}
/// <summary>
/// Returns true if this navigator is positioned to the same node as the "other" navigator. Returns false
/// if not, or if the "other" navigator is not the same type as this navigator.
/// </summary>
public override bool IsSamePosition(XPathNavigator other) {
throw new NotSupportedException();
}
}
/// <summary>
/// This navigator is a cursor over a cache that stores Xslt disable-output-escaping flags.
/// </summary>
internal sealed class RtfTreeNavigator : RtfNavigator {
private XmlEventCache events;
private NavigatorConstructor constr;
private XmlNameTable nameTable;
//-----------------------------------------------
// Constructors
//-----------------------------------------------
/// <summary>
/// Create a new navigator over the specified cache of Xml events.
/// </summary>
public RtfTreeNavigator(XmlEventCache events, XmlNameTable nameTable) {
this.events = events;
this.constr = new NavigatorConstructor();
this.nameTable = nameTable;
}
/// <summary>
/// Copy constructor.
/// </summary>
public RtfTreeNavigator(RtfTreeNavigator that) {
this.events = that.events;
this.constr = that.constr;
this.nameTable = that.nameTable;
}
//-----------------------------------------------
// RtfNavigator
//-----------------------------------------------
/// <summary>
/// Preserve serialization hints when deep copying.
/// </summary>
public override void CopyToWriter(XmlWriter writer) {
this.events.EventsToWriter(writer);
}
/// <summary>
/// Discard serialization hints and return a navigator that actually allows navigation.
/// </summary>
public override XPathNavigator ToNavigator() {
return this.constr.GetNavigator(this.events, this.nameTable);
}
//-----------------------------------------------
// XPathItem
//-----------------------------------------------
/// <summary>
/// Get the string value of the current node, computed using data model dm:string-value rules.
/// If the node has a typed value, return the string representation of the value. If the node
/// is not a parent type (comment, text, pi, etc.), get its simple text value. Otherwise,
/// concatenate all text node descendants of the current node.
/// </summary>
public override string Value {
get { return this.events.EventsToString(); }
}
//-----------------------------------------------
// XPathNavigator
//-----------------------------------------------
/// <summary>
/// Get the base URI of the Rtf.
/// </summary>
public override string BaseURI {
get { return this.events.BaseUri; }
}
/// <summary>
/// Create a copy of this navigator, positioned to the same node in the tree.
/// </summary>
public override XPathNavigator Clone() {
return new RtfTreeNavigator(this);
}
/// <summary>
/// Position this navigator to the same position as the "other" navigator. If the "other" navigator
/// is not of the same type as this navigator, then return false.
/// </summary>
public override bool MoveTo(XPathNavigator other) {
RtfTreeNavigator that = other as RtfTreeNavigator;
if (that != null) {
this.events = that.events;
this.constr = that.constr;
this.nameTable = that.nameTable;
return true;
}
return false;
}
}
/// <summary>
/// This RtfNavigator specializes the case of a root node having a single text node child. This is a very common
/// case, such as in <xsl:variable name="foo">bar</xsl:variable>.
/// </summary>
internal sealed class RtfTextNavigator : RtfNavigator {
private string text, baseUri;
private NavigatorConstructor constr;
//-----------------------------------------------
// Constructors
//-----------------------------------------------
/// <summary>
/// Create a new navigator having a text node with value = "text" string.
/// </summary>
public RtfTextNavigator(string text, string baseUri) {
this.text = text;
this.baseUri = baseUri;
this.constr = new NavigatorConstructor();
}
/// <summary>
/// Copy constructor.
/// </summary>
public RtfTextNavigator(RtfTextNavigator that) {
this.text = that.text;
this.baseUri = that.baseUri;
this.constr = that.constr;
}
//-----------------------------------------------
// RtfNavigator
//-----------------------------------------------
/// <summary>
/// Preserve serialization hints when deep copying.
/// </summary>
public override void CopyToWriter(XmlWriter writer) {
writer.WriteString(Value);
}
/// <summary>
/// Discard serialization hints and return a navigator that actually allows navigation.
/// </summary>
public override XPathNavigator ToNavigator() {
return this.constr.GetNavigator(this.text, this.baseUri, new NameTable());
}
//-----------------------------------------------
// XPathItem
//-----------------------------------------------
/// <summary>
/// Get the string value of the current node, computed using data model dm:string-value rules.
/// If the node has a typed value, return the string representation of the value. If the node
/// is not a parent type (comment, text, pi, etc.), get its simple text value. Otherwise,
/// concatenate all text node descendants of the current node.
/// </summary>
public override string Value {
get { return this.text; }
}
//-----------------------------------------------
// XPathNavigator
//-----------------------------------------------
/// <summary>
/// Get the base URI of the Rtf.
/// </summary>
public override string BaseURI {
get { return this.baseUri; }
}
/// <summary>
/// Create a copy of this navigator, positioned to the same node in the tree.
/// </summary>
public override XPathNavigator Clone() {
return new RtfTextNavigator(this);
}
/// <summary>
/// Position this navigator to the same position as the "other" navigator. If the "other" navigator
/// is not of the same type as this navigator, then return false.
/// </summary>
public override bool MoveTo(XPathNavigator other) {
RtfTextNavigator that = other as RtfTextNavigator;
if (that != null) {
this.text = that.text;
this.baseUri = that.baseUri;
this.constr = that.constr;
return true;
}
return false;
}
}
/// <summary>
/// This class creates a document on the first call to GetNavigator(), and returns a Navigator from it. On
/// subsequent calls, Navigators from the same document are returned (no new document is created).
/// </summary>
internal sealed class NavigatorConstructor {
object cache;
/// <summary>
/// Create a document from the cache of events. If a document has already been created previously, return it.
/// This method is thread-safe, and is always guaranteed to return the exact same document, no matter how many
/// threads have called it concurrently.
/// </summary>
public XPathNavigator GetNavigator(XmlEventCache events, XmlNameTable nameTable) {
if (this.cache == null) {
// Create XPathDocument from event cache
XPathDocument doc = new XPathDocument(nameTable);
XmlRawWriter writer = doc.LoadFromWriter(XPathDocument.LoadFlags.AtomizeNames | (events.HasRootNode ? XPathDocument.LoadFlags.None : XPathDocument.LoadFlags.Fragment), events.BaseUri);
events.EventsToWriter(writer);
writer.Close();
this.cache = doc;
}
return ((XPathDocument) this.cache).CreateNavigator();
}
/// <summary>
/// Create a document containing a root node and a single text node child with "text" as its text value.
/// This method is thread-safe, and is always guaranteed to return the exact same document, no matter how many
/// threads have called it concurrently.
/// </summary>
public XPathNavigator GetNavigator(string text, string baseUri, XmlNameTable nameTable) {
if (this.cache == null) {
// Create XPathDocument
XPathDocument doc = new XPathDocument(nameTable);
XmlRawWriter writer = doc.LoadFromWriter(XPathDocument.LoadFlags.AtomizeNames, baseUri);
writer.WriteString(text);
writer.Close();
this.cache = doc;
}
return ((XPathDocument) this.cache).CreateNavigator();
}
}
}
|