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
|
//------------------------------------------------------------------------------
// <copyright file=QueryOutputWriter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
namespace System.Xml {
/// <summary>
/// This writer wraps an XmlWriter that was not build using the XmlRawWriter architecture (such as XmlTextWriter or a custom XmlWriter)
/// for use in the XslCompilerTransform. Depending on the Xsl stylesheet output settings (which gets transfered to this writer via the
/// internal properties of XmlWriterSettings) this writer will inserts additional lexical information into the resulting Xml 1.0 document:
///
/// 1. CData sections
/// 2. DocType declaration
/// 3. Standalone attribute
///
/// It also calls WriteStateDocument if standalone="yes" and/or a DocType declaration is written out in order to enforce document conformance
/// checking.
/// </summary>
internal class QueryOutputWriterV1 : XmlWriter {
private XmlWriter wrapped;
private bool inCDataSection;
private Dictionary<XmlQualifiedName, XmlQualifiedName> lookupCDataElems;
private BitStack bitsCData;
private XmlQualifiedName qnameCData;
private bool outputDocType, inAttr;
private string systemId, publicId;
private XmlStandalone standalone;
public QueryOutputWriterV1(XmlWriter writer, XmlWriterSettings settings) {
this.wrapped = writer;
this.systemId = settings.DocTypeSystem;
this.publicId = settings.DocTypePublic;
if (settings.OutputMethod == XmlOutputMethod.Xml) {
bool documentConformance = false;
// Xml output method shouldn't output doc-type-decl if system ID is not defined (even if public ID is)
// Only check for well-formed document if output method is xml
if (this.systemId != null) {
documentConformance = true;
this.outputDocType = true;
}
// Check for well-formed document if standalone="yes" in an auto-generated xml declaration
if (settings.Standalone == XmlStandalone.Yes) {
documentConformance = true;
this.standalone = settings.Standalone;
}
if (documentConformance) {
if (settings.Standalone == XmlStandalone.Yes) {
this.wrapped.WriteStartDocument(true);
}
else {
this.wrapped.WriteStartDocument();
}
}
if (settings.CDataSectionElements != null && settings.CDataSectionElements.Count > 0) {
this.bitsCData = new BitStack();
this.lookupCDataElems = new Dictionary<XmlQualifiedName, XmlQualifiedName>();
this.qnameCData = new XmlQualifiedName();
// Add each element name to the lookup table
foreach (XmlQualifiedName name in settings.CDataSectionElements) {
this.lookupCDataElems[name] = null;
}
this.bitsCData.PushBit(false);
}
}
else if (settings.OutputMethod == XmlOutputMethod.Html) {
// Html output method should output doc-type-decl if system ID or public ID is defined
if (this.systemId != null || this.publicId != null)
this.outputDocType = true;
}
}
//-----------------------------------------------
// XmlWriter interface
//-----------------------------------------------
public override WriteState WriteState {
get {
return this.wrapped.WriteState;
}
}
public override void WriteStartDocument() {
this.wrapped.WriteStartDocument();
}
public override void WriteStartDocument(bool standalone) {
this.wrapped.WriteStartDocument(standalone);
}
public override void WriteEndDocument() {
this.wrapped.WriteEndDocument();
}
/// <summary>
/// Suppress this explicit call to WriteDocType if information was provided by XmlWriterSettings.
/// </summary>
public override void WriteDocType(string name, string pubid, string sysid, string subset) {
if (this.publicId == null && this.systemId == null) {
Debug.Assert(!this.outputDocType);
this.wrapped.WriteDocType(name, pubid, sysid, subset);
}
}
/// <summary>
/// Output doc-type-decl on the first element, and determine whether this element is a
/// CData section element.
/// </summary>
public override void WriteStartElement(string prefix, string localName, string ns) {
EndCDataSection();
// Output doc-type declaration immediately before first element is output
if (this.outputDocType) {
WriteState ws = this.wrapped.WriteState;
if (ws == WriteState.Start || ws == WriteState.Prolog) {
this.wrapped.WriteDocType(
prefix.Length != 0 ? prefix + ":" + localName : localName,
this.publicId,
this.systemId,
null );
}
this.outputDocType = false;
}
this.wrapped.WriteStartElement(prefix, localName, ns);
if (this.lookupCDataElems != null) {
// Determine whether this element is a CData section element
this.qnameCData.Init(localName, ns);
this.bitsCData.PushBit(this.lookupCDataElems.ContainsKey(this.qnameCData));
}
}
public override void WriteEndElement() {
EndCDataSection();
this.wrapped.WriteEndElement();
if (this.lookupCDataElems != null)
this.bitsCData.PopBit();
}
public override void WriteFullEndElement() {
EndCDataSection();
this.wrapped.WriteFullEndElement();
if (this.lookupCDataElems != null)
this.bitsCData.PopBit();
}
public override void WriteStartAttribute(string prefix, string localName, string ns) {
this.inAttr = true;
this.wrapped.WriteStartAttribute(prefix, localName, ns);
}
public override void WriteEndAttribute() {
this.inAttr = false;
this.wrapped.WriteEndAttribute();
}
public override void WriteCData(string text) {
this.wrapped.WriteCData(text);
}
public override void WriteComment(string text) {
EndCDataSection();
this.wrapped.WriteComment(text);
}
public override void WriteProcessingInstruction(string name, string text) {
EndCDataSection();
this.wrapped.WriteProcessingInstruction(name, text);
}
public override void WriteWhitespace(string ws) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteCData(ws);
else
this.wrapped.WriteWhitespace(ws);
}
public override void WriteString(string text) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteCData(text);
else
this.wrapped.WriteString(text);
}
public override void WriteChars(char[] buffer, int index, int count) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteCData(new string(buffer, index, count));
else
this.wrapped.WriteChars(buffer, index, count);
}
public override void WriteBase64(byte[] buffer, int index, int count) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteBase64(buffer, index, count);
else
this.wrapped.WriteBase64(buffer, index, count);
}
public override void WriteEntityRef(string name) {
EndCDataSection();
this.wrapped.WriteEntityRef(name);
}
public override void WriteCharEntity(char ch) {
EndCDataSection();
this.wrapped.WriteCharEntity(ch);
}
public override void WriteSurrogateCharEntity(char lowChar, char highChar) {
EndCDataSection();
this.wrapped.WriteSurrogateCharEntity(lowChar, highChar);
}
public override void WriteRaw(char[] buffer, int index, int count) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteCData(new string(buffer, index, count));
else
this.wrapped.WriteRaw(buffer, index, count);
}
public override void WriteRaw(string data) {
if (!this.inAttr && (this.inCDataSection || StartCDataSection()))
this.wrapped.WriteCData(data);
else
this.wrapped.WriteRaw(data);
}
public override void Close() {
this.wrapped.Close();
}
public override void Flush() {
this.wrapped.Flush();
}
public override string LookupPrefix(string ns) {
return this.wrapped.LookupPrefix(ns);
}
//-----------------------------------------------
// Helper methods
//-----------------------------------------------
/// <summary>
/// Write CData text if element is a CData element. Return true if text should be written
/// within a CData section.
/// </summary>
private bool StartCDataSection() {
Debug.Assert(!this.inCDataSection);
if (this.lookupCDataElems != null && this.bitsCData.PeekBit()) {
this.inCDataSection = true;
return true;
}
return false;
}
/// <summary>
/// No longer write CData text.
/// </summary>
private void EndCDataSection() {
this.inCDataSection = false;
}
}
}
|