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
|
//------------------------------------------------------------------------------
// <copyright file="CharEntitiesEncodingFallback.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System.Text;
using System.Diagnostics;
using System.Globalization;
namespace System.Xml {
//
// CharEntityEncoderFallback
//
internal class CharEntityEncoderFallback : EncoderFallback {
private CharEntityEncoderFallbackBuffer fallbackBuffer;
private int[] textContentMarks;
private int endMarkPos;
private int curMarkPos;
private int startOffset;
internal CharEntityEncoderFallback() {
}
public override EncoderFallbackBuffer CreateFallbackBuffer() {
if ( fallbackBuffer == null ) {
fallbackBuffer = new CharEntityEncoderFallbackBuffer( this );
}
return fallbackBuffer;
}
public override int MaxCharCount {
get {
return 12;
}
}
internal int StartOffset {
get {
return startOffset;
}
set {
startOffset = value;
}
}
internal void Reset( int[] textContentMarks, int endMarkPos ) {
this.textContentMarks = textContentMarks;
this.endMarkPos = endMarkPos;
curMarkPos = 0;
}
internal bool CanReplaceAt( int index ) {
int mPos = curMarkPos;
int charPos = startOffset + index;
while ( mPos < endMarkPos && charPos >= textContentMarks[mPos+1] ) {
mPos++;
}
curMarkPos = mPos;
return (mPos & 1) != 0;
}
}
//
// CharEntityFallbackBuffer
//
internal class CharEntityEncoderFallbackBuffer : EncoderFallbackBuffer {
private CharEntityEncoderFallback parent;
private string charEntity = string.Empty;
private int charEntityIndex = -1;
internal CharEntityEncoderFallbackBuffer( CharEntityEncoderFallback parent ) {
this.parent = parent;
}
public override bool Fallback( char charUnknown, int index ) {
// If we are already in fallback, throw, it's probably at the suspect character in charEntity
if ( charEntityIndex >= 0 ) {
(new EncoderExceptionFallback()).CreateFallbackBuffer().Fallback( charUnknown, index );
}
// find out if we can replace the character with entity
if ( parent.CanReplaceAt( index ) ) {
// Create the replacement character entity
charEntity = string.Format( CultureInfo.InvariantCulture, "&#x{0:X};", new object[] { (int)charUnknown } );
charEntityIndex = 0;
return true;
}
else {
EncoderFallbackBuffer errorFallbackBuffer = ( new EncoderExceptionFallback() ).CreateFallbackBuffer();
errorFallbackBuffer.Fallback( charUnknown, index );
return false;
}
}
public override bool Fallback( char charUnknownHigh, char charUnknownLow, int index ) {
// check input surrogate pair
if ( !char.IsSurrogatePair( charUnknownHigh, charUnknownLow ) ) {
throw XmlConvert.CreateInvalidSurrogatePairException( charUnknownHigh, charUnknownLow );
}
// If we are already in fallback, throw, it's probably at the suspect character in charEntity
if ( charEntityIndex >= 0 ) {
(new EncoderExceptionFallback()).CreateFallbackBuffer().Fallback( charUnknownHigh, charUnknownLow, index );
}
if ( parent.CanReplaceAt( index ) ) {
// Create the replacement character entity
charEntity = string.Format( CultureInfo.InvariantCulture, "&#x{0:X};", new object[] { SurrogateCharToUtf32( charUnknownHigh, charUnknownLow ) } );
charEntityIndex = 0;
return true;
}
else {
EncoderFallbackBuffer errorFallbackBuffer = ( new EncoderExceptionFallback() ).CreateFallbackBuffer();
errorFallbackBuffer.Fallback( charUnknownHigh, charUnknownLow, index );
return false;
}
}
public override char GetNextChar() {
// Bug fix: 35637. The protocol using GetNextChar() and MovePrevious() called by Encoder is not well documented.
// Here we have to to signal to Encoder that the previous read was last character. Only AFTER we can
// mark ourself as done (-1). Otherwise MovePrevious() can still be called, but -1 is already incorrectly set
// and return false from MovePrevious(). Then Encoder ----ing the rest of the bytes.
if (charEntityIndex == charEntity.Length)
{
charEntityIndex = -1;
}
if ( charEntityIndex == -1 ) {
return (char)0;
}
else {
Debug.Assert( charEntityIndex < charEntity.Length );
char ch = charEntity[charEntityIndex++];
return ch;
}
}
public override bool MovePrevious() {
if ( charEntityIndex == -1 ) {
return false;
}
else {
// Could be == length if just read the last character
Debug.Assert(charEntityIndex <= charEntity.Length);
if (charEntityIndex > 0)
{
charEntityIndex--;
return true;
}
else {
return false;
}
}
}
public override int Remaining {
get {
if ( charEntityIndex == -1 ) {
return 0;
}
else {
return charEntity.Length - charEntityIndex;
}
}
}
public override void Reset() {
charEntityIndex = -1;
}
private int SurrogateCharToUtf32(char highSurrogate, char lowSurrogate) {
return XmlCharType.CombineSurrogateChar(lowSurrogate, highSurrogate);
}
}
}
|