added samples
[windows-sources.git] / sdk / samples / FrameworkSamples / CLR / EncodingApp / vb / encodingapp / encoderinfo.vb
blob861ea7430135c0b3480edf70b3408ee165ee5625
2 '-----------------------------------------------------------------------
3 ' This file is part of the Microsoft .NET SDK Code Samples.
4 '
5 ' Copyright (C) Microsoft Corporation. All rights reserved.
6 '
7 'This source code is intended only as a supplement to Microsoft
8 'Development Tools and/or on-line documentation. See these other
9 'materials for detailed information regarding Microsoft code samples.
11 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
12 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 'PARTICULAR PURPOSE.
15 '-----------------------------------------------------------------------
16 Imports System
17 Imports System.Globalization
18 Imports System.Text
21 '/ <summary>
22 '/ Class to encapsulate encoding options of Encoding type, Fallback mechanism and Normalization scheme
23 '/ </summary>
25 Public Class EncoderInfo
26 Public Shared NormalizationNone As String = "None"
27 'Different fallback schemes
28 Public Shared FallbackSchemes As String() = {"Replacement", "Exception", "Best Fit"}
30 'Code page of encoding selected in options
31 Private encodingCodePage As Integer = 0
33 'Encoding that we're using
34 Private targetEncoding As Encoding = Encoding.Default
36 'EncoderFallback selected in options
37 Private encFallback As EncoderFallback = EncoderFallback.ReplacementFallback
39 'Normalization scheme selected in options
40 Private normalizationForm As String
43 'Constructor initializing members to default values
44 Public Sub New()
45 'Initialize encoding to UTF8
46 SetEncodingCodePage(Encoding.UTF8.CodePage)
48 'Initialize fallback scheme to replacement
49 SetEncoderFallback(FallbackSchemes(0))
51 'Initialize normalization scheme to none
52 SetNormalizationForm(NormalizationNone)
54 End Sub 'New
56 'Property to get the encoding scheme
58 Public ReadOnly Property CurrentEncoding() As Encoding
59 Get
60 Return Me.targetEncoding.Clone()
61 End Get
62 End Property
65 'Method to set the code page of the selected encoding
66 Public Sub SetEncodingCodePage(ByVal encodingCodePage As Integer)
67 Me.encodingCodePage = encodingCodePage
68 Me.targetEncoding = Encoding.GetEncoding(encodingCodePage, Me.encFallback, DecoderFallback.ReplacementFallback)
69 If TypeOf Me.encFallback Is EncoderBestFitFallback Then
70 Dim bestFit As EncoderBestFitFallback = Me.encFallback
71 bestFit.ParentEncoding = Me.targetEncoding
72 End If
73 End Sub 'SetEncodingCodePage
76 'Method to set the fallback scheme of the destination encoding
77 Public Sub SetEncoderFallback(ByVal strFallback As String)
78 ' Replacement
79 If strFallback = FallbackSchemes(0) Then
80 Me.encFallback = New EncoderReplacementFallback("?")
81 Else ' Exception
82 If strFallback = FallbackSchemes(1) Then
83 Me.encFallback = New EncoderExceptionFallback()
84 Else
85 Me.encFallback = New EncoderBestFitFallback(Me.targetEncoding)
86 End If
87 End If
88 Me.targetEncoding.EncoderFallback = Me.encFallback
90 End Sub 'SetEncoderFallback
93 'Method to set the normalization scheme
94 Public Sub SetNormalizationForm(ByVal strNormalization As String)
95 normalizationForm = strNormalization
97 End Sub 'SetNormalizationForm
100 'Method to normalize a given string in the normalization form specified
101 Public Function GetNormalizedString(ByVal inputString As String) As String
102 If normalizationForm <> NormalizationNone Then
103 Dim n As NormalizationForm = CType([Enum].Parse(GetType(NormalizationForm), normalizationForm), NormalizationForm)
104 'Normalize string
105 inputString = inputString.Normalize(n)
106 End If
108 Return inputString
110 End Function 'GetNormalizedString
111 End Class 'EncoderInfo