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
|
//------------------------------------------------------------------------------
// <copyright file="XmlAttributeCache.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Xsl.Runtime {
using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
/// <summary>
/// This writer supports only writer methods which write attributes. Attributes are stored in a
/// data structure until StartElementContent() is called, at which time the attributes are flushed
/// to the wrapped writer. In the case of duplicate attributes, the last attribute's value is used.
/// </summary>
internal sealed class XmlAttributeCache : XmlRawWriter, IRemovableWriter {
private XmlRawWriter wrapped;
private OnRemoveWriter onRemove; // Event handler that is called when cached attributes are flushed to wrapped writer
private AttrNameVal[] arrAttrs; // List of cached attribute names and value parts
private int numEntries; // Number of attributes in the cache
private int idxLastName; // The entry containing the name of the last attribute to be cached
private int hashCodeUnion; // Set of hash bits that can quickly guarantee a name is not a duplicate
/// <summary>
/// Initialize the cache. Use this method instead of a constructor in order to reuse the cache.
/// </summary>
public void Init(XmlRawWriter wrapped) {
SetWrappedWriter(wrapped);
// Clear attribute list
this.numEntries = 0;
this.idxLastName = 0;
this.hashCodeUnion = 0;
}
/// <summary>
/// Return the number of cached attributes.
/// </summary>
public int Count {
get { return this.numEntries; }
}
//-----------------------------------------------
// IRemovableWriter interface
//-----------------------------------------------
/// <summary>
/// This writer will raise this event once cached attributes have been flushed in order to signal that the cache
/// no longer needs to be part of the pipeline.
/// </summary>
public OnRemoveWriter OnRemoveWriterEvent {
get { return this.onRemove; }
set { this.onRemove = value; }
}
/// <summary>
/// The wrapped writer will callback on this method if it wishes to remove itself from the pipeline.
/// </summary>
private void SetWrappedWriter(XmlRawWriter writer) {
// If new writer might remove itself from pipeline, have it callback on this method when its ready to go
IRemovableWriter removable = writer as IRemovableWriter;
if (removable != null)
removable.OnRemoveWriterEvent = SetWrappedWriter;
this.wrapped = writer;
}
//-----------------------------------------------
// XmlWriter interface
//-----------------------------------------------
/// <summary>
/// Add an attribute to the cache. If an attribute if the same name already exists, replace it.
/// </summary>
public override void WriteStartAttribute(string prefix, string localName, string ns) {
int hashCode;
int idx = 0;
Debug.Assert(localName != null && localName.Length != 0 && prefix != null && ns != null);
// Compute hashcode based on first letter of the localName
hashCode = (1 << ((int) localName[0] & 31));
// If the hashcode is not in the union, then name will not be found by a scan
if ((this.hashCodeUnion & hashCode) != 0) {
// The name may or may not be present, so scan for it
Debug.Assert(this.numEntries != 0);
do {
if (this.arrAttrs[idx].IsDuplicate(localName, ns, hashCode))
break;
// Next attribute name
idx = this.arrAttrs[idx].NextNameIndex;
}
while (idx != 0);
}
else {
// Insert hashcode into union
this.hashCodeUnion |= hashCode;
}
// Insert new attribute; link attribute names together in a list
EnsureAttributeCache();
if (this.numEntries != 0)
this.arrAttrs[this.idxLastName].NextNameIndex = this.numEntries;
this.idxLastName = this.numEntries++;
this.arrAttrs[this.idxLastName].Init(prefix, localName, ns, hashCode);
}
/// <summary>
/// No-op.
/// </summary>
public override void WriteEndAttribute() {
}
/// <summary>
/// Pass through namespaces to underlying writer. If any attributes have been cached, flush them.
/// </summary>
internal override void WriteNamespaceDeclaration(string prefix, string ns) {
FlushAttributes();
this.wrapped.WriteNamespaceDeclaration(prefix, ns);
}
/// <summary>
/// Add a block of text to the cache. This text block makes up some or all of the untyped string
/// value of the current attribute.
/// </summary>
public override void WriteString(string text) {
Debug.Assert(text != null);
Debug.Assert(this.arrAttrs != null && this.numEntries != 0);
EnsureAttributeCache();
this.arrAttrs[this.numEntries++].Init(text);
}
/// <summary>
/// All other WriteValue methods are implemented by XmlWriter to delegate to WriteValue(object) or WriteValue(string), so
/// only these two methods need to be implemented.
/// </summary>
public override void WriteValue(object value) {
Debug.Assert(value is XmlAtomicValue, "value should always be an XmlAtomicValue, as XmlAttributeCache is only used by XmlQueryOutput");
Debug.Assert(this.arrAttrs != null && this.numEntries != 0);
EnsureAttributeCache();
this.arrAttrs[this.numEntries++].Init((XmlAtomicValue) value);
}
public override void WriteValue(string value) {
WriteValue(value);
}
/// <summary>
/// Send cached, non-overriden attributes to the specified writer. Calling this method has
/// the side effect of clearing the attribute cache.
/// </summary>
internal override void StartElementContent() {
FlushAttributes();
// Call StartElementContent on wrapped writer
this.wrapped.StartElementContent();
}
public override void WriteStartElement(string prefix, string localName, string ns) {
Debug.Assert(false, "Should never be called on XmlAttributeCache.");
}
internal override void WriteEndElement(string prefix, string localName, string ns) {
Debug.Assert(false, "Should never be called on XmlAttributeCache.");
}
public override void WriteComment(string text) {
Debug.Assert(false, "Should never be called on XmlAttributeCache.");
}
public override void WriteProcessingInstruction(string name, string text) {
Debug.Assert(false, "Should never be called on XmlAttributeCache.");
}
public override void WriteEntityRef(string name) {
Debug.Assert(false, "Should never be called on XmlAttributeCache.");
}
/// <summary>
/// Forward call to wrapped writer.
/// </summary>
public override void Close() {
this.wrapped.Close();
}
/// <summary>
/// Forward call to wrapped writer.
/// </summary>
public override void Flush() {
this.wrapped.Flush();
}
//-----------------------------------------------
// Helper methods
//-----------------------------------------------
private void FlushAttributes() {
int idx = 0, idxNext;
string localName;
while (idx != this.numEntries) {
// Get index of next attribute's name (0 if this is the last attribute)
idxNext = this.arrAttrs[idx].NextNameIndex;
if (idxNext == 0)
idxNext = this.numEntries;
// If localName is null, then this is a duplicate attribute that has been marked as "deleted"
localName = this.arrAttrs[idx].LocalName;
if (localName != null) {
string prefix = this.arrAttrs[idx].Prefix;
string ns = this.arrAttrs[idx].Namespace;
this.wrapped.WriteStartAttribute(prefix, localName, ns);
// Output all of this attribute's text or typed values
while (++idx != idxNext) {
string text = this.arrAttrs[idx].Text;
if (text != null)
this.wrapped.WriteString(text);
else
this.wrapped.WriteValue(this.arrAttrs[idx].Value);
}
this.wrapped.WriteEndAttribute();
}
else {
// Skip over duplicate attributes
idx = idxNext;
}
}
// Notify event listener that attributes have been flushed
if (this.onRemove != null)
this.onRemove(this.wrapped);
}
private struct AttrNameVal {
private string localName;
private string prefix;
private string namespaceName;
private string text;
private XmlAtomicValue value;
private int hashCode;
private int nextNameIndex;
public string LocalName { get { return this.localName; } }
public string Prefix { get { return this.prefix; } }
public string Namespace { get { return this.namespaceName; } }
public string Text { get { return this.text; } }
public XmlAtomicValue Value { get { return this.value; } }
public int NextNameIndex { get { return this.nextNameIndex; } set { this.nextNameIndex = value; } }
/// <summary>
/// Cache an attribute's name and type.
/// </summary>
public void Init(string prefix, string localName, string ns, int hashCode) {
this.localName = localName;
this.prefix = prefix;
this.namespaceName = ns;
this.hashCode = hashCode;
this.nextNameIndex = 0;
}
/// <summary>
/// Cache all or part of the attribute's string value.
/// </summary>
public void Init(string text) {
this.text = text;
this.value = null;
}
/// <summary>
/// Cache all or part of the attribute's typed value.
/// </summary>
public void Init(XmlAtomicValue value) {
this.text = null;
this.value = value;
}
/// <summary>
/// Returns true if this attribute has the specified name (and thus is a duplicate).
/// </summary>
public bool IsDuplicate(string localName, string ns, int hashCode) {
// If attribute is not marked as deleted
if (this.localName != null) {
// And if hash codes match,
if (this.hashCode == hashCode) {
// And if local names match,
if (this.localName.Equals(localName)) {
// And if namespaces match,
if (this.namespaceName.Equals(ns)) {
// Then found duplicate attribute, so mark the attribute as deleted
this.localName = null;
return true;
}
}
}
}
return false;
}
}
#if DEBUG
private const int DefaultCacheSize = 2;
#else
private const int DefaultCacheSize = 32;
#endif
/// <summary>
/// Ensure that attribute array has been created and is large enough for at least one
/// additional entry.
/// </summary>
private void EnsureAttributeCache() {
if (this.arrAttrs == null) {
// Create caching array
this.arrAttrs = new AttrNameVal[DefaultCacheSize];
}
else if (this.numEntries >= this.arrAttrs.Length) {
// Resize caching array
Debug.Assert(this.numEntries == this.arrAttrs.Length);
AttrNameVal[] arrNew = new AttrNameVal[this.numEntries * 2];
Array.Copy(this.arrAttrs, arrNew, this.numEntries);
this.arrAttrs = arrNew;
}
}
}
}
|