Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Xml / System / Xml / Core / CharEntityEncoderFallback.cs / 1305376 / CharEntityEncoderFallback.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- 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, "{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, "{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() { if ( charEntityIndex == -1 ) { return (char)0; } else { Debug.Assert( charEntityIndex < charEntity.Length ); char ch = charEntity[charEntityIndex++]; if ( charEntityIndex == charEntity.Length ) { charEntityIndex = -1; } return ch; } } public override bool MovePrevious() { if ( charEntityIndex == -1 ) { return false; } else { 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- 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, "{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, "{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() { if ( charEntityIndex == -1 ) { return (char)0; } else { Debug.Assert( charEntityIndex < charEntity.Length ); char ch = charEntity[charEntityIndex++]; if ( charEntityIndex == charEntity.Length ) { charEntityIndex = -1; } return ch; } } public override bool MovePrevious() { if ( charEntityIndex == -1 ) { return false; } else { 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HttpPostProtocolImporter.cs
- TextBox.cs
- cache.cs
- XmlSchemaComplexType.cs
- Label.cs
- WindowsGraphicsWrapper.cs
- WindowsStreamSecurityBindingElement.cs
- SqlSelectStatement.cs
- MultiTrigger.cs
- RequestDescription.cs
- AsnEncodedData.cs
- OfTypeExpression.cs
- WindowsSolidBrush.cs
- HandlerBase.cs
- XmlMapping.cs
- BooleanExpr.cs
- SettingsAttributes.cs
- FileResponseElement.cs
- AsyncInvokeContext.cs
- SettingsPropertyNotFoundException.cs
- TransactionValidationBehavior.cs
- IntSecurity.cs
- FrameworkTemplate.cs
- JpegBitmapDecoder.cs
- XmlExceptionHelper.cs
- ItemCollection.cs
- StylusDownEventArgs.cs
- ObjectDataSourceView.cs
- ContextMarshalException.cs
- odbcmetadatacolumnnames.cs
- WaitHandleCannotBeOpenedException.cs
- ListViewSelectEventArgs.cs
- ContextDataSource.cs
- Point.cs
- SamlAction.cs
- HtmlListAdapter.cs
- LinearKeyFrames.cs
- ToolTipService.cs
- CategoryNameCollection.cs
- TextChange.cs
- RealProxy.cs
- IssuanceLicense.cs
- PathFigureCollectionValueSerializer.cs
- PreloadedPackages.cs
- SerialReceived.cs
- ToolStripPanel.cs
- XmlSchemaAppInfo.cs
- StaticExtension.cs
- DbMetaDataCollectionNames.cs
- DateTimeOffsetConverter.cs
- InvokeCompletedEventArgs.cs
- DataGridViewCellConverter.cs
- SubpageParagraph.cs
- TabControlEvent.cs
- DBCommandBuilder.cs
- Exceptions.cs
- SimpleHandlerFactory.cs
- CollectionViewProxy.cs
- Int64Storage.cs
- WindowsGrip.cs
- TextTreeNode.cs
- RIPEMD160Managed.cs
- HeaderUtility.cs
- CommandHelper.cs
- ExpandSegmentCollection.cs
- DataConnectionHelper.cs
- UserControlParser.cs
- ReachDocumentPageSerializerAsync.cs
- ChtmlCommandAdapter.cs
- TraceContext.cs
- MachineKeySection.cs
- GradientSpreadMethodValidation.cs
- ToolboxDataAttribute.cs
- TimelineClockCollection.cs
- BindingExpression.cs
- XPathNodeInfoAtom.cs
- ProviderBase.cs
- AssociationTypeEmitter.cs
- ResourceKey.cs
- FloatUtil.cs
- RowsCopiedEventArgs.cs
- ValidationEventArgs.cs
- Rotation3DKeyFrameCollection.cs
- XmlAnyAttributeAttribute.cs
- ProxyManager.cs
- SiteMapProvider.cs
- XmlComment.cs
- PeerOutputChannel.cs
- PropertyBuilder.cs
- DrawingBrush.cs
- Imaging.cs
- StylusDevice.cs
- CatalogZone.cs
- WebPartCollection.cs
- SingleAnimation.cs
- TextLineResult.cs
- TextSyndicationContent.cs
- Stylus.cs
- ClassHandlersStore.cs
- ManipulationLogic.cs