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
|
//------------------------------------------------------------------------------
// <copyright file="XmlCharCheckingWriter.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.Text;
using System.Xml.Schema;
using System.Collections;
using System.Diagnostics;
namespace System.Xml {
//
// XmlCharCheckingWriter
//
internal partial class XmlCharCheckingWriter : XmlWrappingWriter {
//
// Fields
//
bool checkValues;
bool checkNames;
bool replaceNewLines;
string newLineChars;
XmlCharType xmlCharType;
//
// Constructor
//
internal XmlCharCheckingWriter( XmlWriter baseWriter, bool checkValues, bool checkNames, bool replaceNewLines, string newLineChars )
: base( baseWriter ) {
Debug.Assert( checkValues || replaceNewLines );
this.checkValues = checkValues;
this.checkNames = checkNames;
this.replaceNewLines = replaceNewLines;
this.newLineChars = newLineChars;
if ( checkValues ) {
xmlCharType = XmlCharType.Instance;
}
}
//
// XmlWriter implementation
//
public override XmlWriterSettings Settings {
get {
XmlWriterSettings s = base.writer.Settings;
s = ( s != null) ? (XmlWriterSettings)s.Clone() : new XmlWriterSettings();
if ( checkValues ) {
s.CheckCharacters = true;
}
if ( replaceNewLines ) {
s.NewLineHandling = NewLineHandling.Replace;
s.NewLineChars = newLineChars;
}
s.ReadOnly = true;
return s;
}
}
public override void WriteDocType( string name, string pubid, string sysid, string subset ) {
if ( checkNames ) {
ValidateQName( name );
}
if ( checkValues ) {
if ( pubid != null ) {
int i;
if ( ( i = xmlCharType.IsPublicId( pubid ) ) >= 0 ) {
throw XmlConvert.CreateInvalidCharException( pubid, i );
}
}
if ( sysid != null ) {
CheckCharacters( sysid );
}
if ( subset != null ) {
CheckCharacters( subset );
}
}
if ( replaceNewLines ) {
sysid = ReplaceNewLines( sysid );
pubid = ReplaceNewLines( pubid );
subset = ReplaceNewLines( subset );
}
writer.WriteDocType( name, pubid, sysid, subset );
}
public override void WriteStartElement( string prefix, string localName, string ns ) {
if ( checkNames ) {
if ( localName == null || localName.Length == 0 ) {
throw new ArgumentException( Res.GetString( Res.Xml_EmptyLocalName ) );
}
ValidateNCName( localName );
if ( prefix != null && prefix.Length > 0 ) {
ValidateNCName( prefix );
}
}
writer.WriteStartElement( prefix, localName, ns );
}
public override void WriteStartAttribute( string prefix, string localName, string ns ) {
if ( checkNames ) {
if ( localName == null || localName.Length == 0 ) {
throw new ArgumentException( Res.GetString( Res.Xml_EmptyLocalName ) );
}
ValidateNCName( localName );
if ( prefix != null && prefix.Length > 0 ) {
ValidateNCName( prefix );
}
}
writer.WriteStartAttribute( prefix, localName, ns );
}
public override void WriteCData( string text ) {
if ( text != null ) {
if ( checkValues ) {
CheckCharacters( text );
}
if ( replaceNewLines ) {
text = ReplaceNewLines( text );
}
int i;
while ( ( i = text.IndexOf( "]]>", StringComparison.Ordinal ) ) >= 0 ) {
writer.WriteCData( text.Substring( 0, i + 2 ) );
text = text.Substring( i + 2 );
}
}
writer.WriteCData( text );
}
public override void WriteComment( string text ) {
if ( text != null ) {
if ( checkValues ) {
CheckCharacters( text );
text = InterleaveInvalidChars( text, '-', '-' );
}
if ( replaceNewLines ) {
text = ReplaceNewLines( text );
}
}
writer.WriteComment( text );
}
public override void WriteProcessingInstruction( string name, string text ) {
if ( checkNames ) {
ValidateNCName( name );
}
if ( text != null ) {
if ( checkValues ) {
CheckCharacters( text );
text = InterleaveInvalidChars( text, '?', '>' );
}
if ( replaceNewLines ) {
text = ReplaceNewLines( text );
}
}
writer.WriteProcessingInstruction( name, text );
}
public override void WriteEntityRef( string name ) {
if ( checkNames ) {
ValidateQName( name );
}
writer.WriteEntityRef( name );
}
public override void WriteWhitespace( string ws ) {
if ( ws == null ) {
ws = string.Empty;
}
// "checkNames" is intentional here; if false, the whitespaces are checked in XmlWellformedWriter
if ( checkNames ) {
int i;
if ( ( i = xmlCharType.IsOnlyWhitespaceWithPos( ws ) ) != -1 ) {
throw new ArgumentException( Res.GetString( Res.Xml_InvalidWhitespaceCharacter, XmlException.BuildCharExceptionArgs( ws, i ) ) );
}
}
if ( replaceNewLines ) {
ws = ReplaceNewLines( ws );
}
writer.WriteWhitespace( ws );
}
public override void WriteString( string text ) {
if ( text != null ) {
if ( checkValues ) {
CheckCharacters( text );
}
if ( replaceNewLines && WriteState != WriteState.Attribute ) {
text = ReplaceNewLines( text );
}
}
writer.WriteString( text );
}
public override void WriteSurrogateCharEntity( char lowChar, char highChar ) {
writer.WriteSurrogateCharEntity( lowChar, highChar );
}
public override void WriteChars( char[] buffer, int index, int count ) {
if (buffer == null) {
throw new ArgumentNullException("buffer");
}
if (index < 0) {
throw new ArgumentOutOfRangeException("index");
}
if (count < 0) {
throw new ArgumentOutOfRangeException("count");
}
if (count > buffer.Length - index) {
throw new ArgumentOutOfRangeException("count");
}
if (checkValues) {
CheckCharacters( buffer, index, count );
}
if ( replaceNewLines && WriteState != WriteState.Attribute ) {
string text = ReplaceNewLines( buffer, index, count );
if ( text != null ) {
WriteString( text );
return;
}
}
writer.WriteChars( buffer, index, count );
}
public override void WriteNmToken( string name ) {
if ( checkNames ) {
if ( name == null || name.Length == 0 ) {
throw new ArgumentException( Res.GetString( Res.Xml_EmptyName ) );
}
XmlConvert.VerifyNMTOKEN( name );
}
writer.WriteNmToken( name );
}
public override void WriteName( string name ) {
if ( checkNames ) {
XmlConvert.VerifyQName( name, ExceptionType.XmlException );
}
writer.WriteName( name );
}
public override void WriteQualifiedName( string localName, string ns ) {
if ( checkNames ) {
ValidateNCName( localName );
}
writer.WriteQualifiedName( localName, ns );
}
//
// Private methods
//
private void CheckCharacters( string str ) {
XmlConvert.VerifyCharData( str, ExceptionType.ArgumentException );
}
private void CheckCharacters( char[] data, int offset, int len ) {
XmlConvert.VerifyCharData( data, offset, len, ExceptionType.ArgumentException );
}
private void ValidateNCName( string ncname ) {
if ( ncname.Length == 0 ) {
throw new ArgumentException( Res.GetString( Res.Xml_EmptyName ) );
}
int len = ValidateNames.ParseNCName( ncname, 0 );
if ( len != ncname.Length ) {
throw new ArgumentException(Res.GetString(len == 0 ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(ncname, len)));
}
}
private void ValidateQName( string name ) {
if ( name.Length == 0 ) {
throw new ArgumentException( Res.GetString( Res.Xml_EmptyName ) );
}
int colonPos;
int len = ValidateNames.ParseQName( name, 0, out colonPos );
if ( len != name.Length ) {
string res = ( len == 0 || ( colonPos > -1 && len == colonPos + 1 ) ) ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar;
throw new ArgumentException( Res.GetString( res, XmlException.BuildCharExceptionArgs( name, len ) ) );
}
}
private string ReplaceNewLines( string str ) {
if ( str == null ) {
return null;
}
StringBuilder sb = null;
int start = 0;
int i;
for ( i = 0; i < str.Length; i++ ) {
char ch;
if ( ( ch = str[i] ) >= 0x20 ) {
continue;
}
if ( ch == '\n' ) {
if ( newLineChars == "\n" ) {
continue;
}
if ( sb == null ) {
sb = new StringBuilder( str.Length + 5 );
}
sb.Append( str, start, i - start );
}
else if ( ch == '\r' ) {
if ( i + 1 < str.Length && str[i+1] == '\n' ) {
if ( newLineChars == "\r\n" ) {
i++;
continue;
}
if ( sb == null ) {
sb = new StringBuilder( str.Length + 5 );
}
sb.Append( str, start, i - start );
i++;
}
else {
if ( newLineChars == "\r" ) {
continue;
}
if ( sb == null ) {
sb = new StringBuilder( str.Length + 5 );
}
sb.Append( str, start, i - start );
}
}
else {
continue;
}
sb.Append( newLineChars );
start = i + 1;
}
if ( sb == null ) {
return str;
}
else {
sb.Append( str, start, i - start );
return sb.ToString();
}
}
private string ReplaceNewLines( char[] data, int offset, int len ) {
if ( data == null ) {
return null;
}
StringBuilder sb = null;
int start = offset;
int endPos = offset + len;
int i;
for ( i = offset; i < endPos; i++ ) {
char ch;
if ( ( ch = data[i] ) >= 0x20 ) {
continue;
}
if ( ch == '\n' ) {
if ( newLineChars == "\n" ) {
continue;
}
if ( sb == null ) {
sb = new StringBuilder( len + 5 );
}
sb.Append( data, start, i - start );
}
else if ( ch == '\r' ) {
if ( i + 1 < endPos && data[i+1] == '\n' ) {
if ( newLineChars == "\r\n" ) {
i++;
continue;
}
if ( sb == null ) {
sb = new StringBuilder( len + 5 );
}
sb.Append( data, start, i - start );
i++;
}
else {
if ( newLineChars == "\r" ) {
continue;
}
if ( sb == null ) {
sb = new StringBuilder( len + 5 );
}
sb.Append( data, start, i - start );
}
}
else {
continue;
}
sb.Append( newLineChars );
start = i + 1;
}
if ( sb == null ) {
return null;
}
else {
sb.Append( data, start, i - start );
return sb.ToString();
}
}
// Interleave 2 adjacent invalid chars with a space. This is used for fixing invalid values of comments and PIs.
// Any "--" in comment must be replaced with "- -" and any "-" at the end must be appended with " ".
// Any "?>" in PI value must be replaced with "? >".
// This code has a bug SQL BU Defect Tracking #480848, which was triaged as Won't Fix because it is a breaking change
private string InterleaveInvalidChars( string text, char invChar1, char invChar2 ) {
StringBuilder sb = null;
int start = 0;
int i;
for ( i = 0; i < text.Length; i++ ) {
if ( text[i] != invChar2 ) {
continue;
}
if ( i > 0 && text[i-1] == invChar1 ) {
if ( sb == null ) {
sb = new StringBuilder( text.Length + 5 );
}
sb.Append( text, start, i - start );
sb.Append( ' ' );
start = i;
}
}
// check last char & return
if ( sb == null ) {
return (i == 0 || text[i - 1] != invChar1) ? text : (text + ' ');
}
else {
sb.Append( text, start, i - start );
if (i > 0 && text[i - 1] == invChar1) {
sb.Append(' ');
}
return sb.ToString();
}
}
}
}
|