CharacterEncoder.st
changeset 7973 6dea491d56f7
parent 7972 91aa73f89491
child 7974 9905043988ee
equal deleted inserted replaced
7972:91aa73f89491 7973:6dea491d56f7
    12 
    12 
    13 "{ Package: 'stx:libbasic' }"
    13 "{ Package: 'stx:libbasic' }"
    14 
    14 
    15 Object subclass:#CharacterEncoder
    15 Object subclass:#CharacterEncoder
    16 	instanceVariableNames:''
    16 	instanceVariableNames:''
    17 	classVariableNames:'EncoderClassesByName EncodersByName LastEncoder'
    17 	classVariableNames:'EncoderClassesByName EncodersByName LastEncoder AccessLock'
    18 	poolDictionaries:''
    18 	poolDictionaries:''
    19 	category:'Collections-Text-Encodings'
    19 	category:'Collections-Text-Encodings'
    20 !
    20 !
    21 
    21 
    22 CharacterEncoder subclass:#SingleByteEncoder
    22 CharacterEncoder subclass:#SingleByteEncoder
   650     ].
   650     ].
   651 
   651 
   652     lcName := encodingNameSymbol asLowercase asSymbolIfInterned.
   652     lcName := encodingNameSymbol asLowercase asSymbolIfInterned.
   653     name := lcName ? encodingNameSymbol.
   653     name := lcName ? encodingNameSymbol.
   654 
   654 
   655     enc := EncodersByName at:name ifAbsent:nil.
   655     AccessLock critical:[
       
   656         enc := EncodersByName at:name ifAbsent:nil.
       
   657     ].
   656     enc notNil ifTrue:[^ enc ].
   658     enc notNil ifTrue:[^ enc ].
   657 
   659 
   658     cls := EncoderClassesByName at:name ifAbsent:nil.
   660     cls := EncoderClassesByName at:name ifAbsent:nil.
   659     cls notNil ifTrue:[^ cls new ].
   661     cls notNil ifTrue:[
       
   662         enc := cls new.
       
   663         AccessLock critical:[
       
   664             EncodersByName at:name put:enc.
       
   665         ].
       
   666         ^ enc 
       
   667     ].
   660 
   668 
   661     self allSubclassesDo:[:cls |
   669     self allSubclassesDo:[:cls |
   662         cls nameOfDecodedCode == #'unicode' ifTrue:[
   670         cls nameOfDecodedCode == #'unicode' ifTrue:[
   663             (cls namesOfEncoding includes:name) ifTrue:[
   671             (cls namesOfEncoding includes:name) ifTrue:[
   664                 enc := cls new.
   672                 enc := cls new.
   665                 EncodersByName at:name put:enc.    
   673                 AccessLock critical:[
       
   674                     EncodersByName at:name put:enc.    
       
   675                 ].
   666                 ^ enc.
   676                 ^ enc.
   667             ]
   677             ]
   668         ].
   678         ].
   669     ].
   679     ].
   670 
   680 
   671     self allSubclassesDo:[:cls |
   681     self allSubclassesDo:[:cls |
   672         (cls namesOfEncoding includes:name) ifTrue:[
   682         (cls namesOfEncoding includes:name) ifTrue:[
   673             "/ ok, found some other encoder - need a compound encoder then.
   683             "/ ok, found some other encoder - need a compound encoder then.
   674             "/ the one found encodes into what we need, but needs something else as input.
   684             "/ the one found encodes into what we need, but needs something else as input.
   675             
   685             
   676             ^ TwoStepEncoder new
   686             enc := TwoStepEncoder new
   677                 encoder1:(self encoderFor:(cls nameOfDecodedCode))
   687                 encoder1:(self encoderFor:(cls nameOfDecodedCode))
   678                 encoder2:(cls new).
   688                 encoder2:(cls new).
       
   689             AccessLock critical:[
       
   690                 EncodersByName at:name put:enc.    
       
   691             ].
       
   692             ^ enc.
   679         ].
   693         ].
   680     ].
   694     ].
   681     ^ exceptionValue value
   695     ^ exceptionValue value
   682 
   696 
   683     "
   697     "
   779 ! !
   793 ! !
   780 
   794 
   781 !CharacterEncoder class methodsFor:'class initialization'!
   795 !CharacterEncoder class methodsFor:'class initialization'!
   782 
   796 
   783 initialize
   797 initialize
       
   798     AccessLock := Semaphore forMutualExclusion.
       
   799 
   784     EncodersByName := IdentityDictionary new.
   800     EncodersByName := IdentityDictionary new.
   785     EncoderClassesByName := IdentityDictionary new.
   801     EncoderClassesByName := IdentityDictionary new.
   786 
   802 
   787     EncoderClassesByName at:#'iso8859-1' put:ISO8859_1.    
   803     EncoderClassesByName at:#'iso8859-1' put:ISO8859_1.    
   788     EncoderClassesByName at:#'iso8859-2' put:ISO8859_2.    
   804     EncoderClassesByName at:#'iso8859-2' put:ISO8859_2.    
 53106 ! !
 53122 ! !
 53107 
 53123 
 53108 !CharacterEncoder class methodsFor:'documentation'!
 53124 !CharacterEncoder class methodsFor:'documentation'!
 53109 
 53125 
 53110 version
 53126 version
 53111     ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoder.st,v 1.30 2004-02-20 10:38:57 cg Exp $'
 53127     ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoder.st,v 1.31 2004-02-20 10:45:15 cg Exp $'
 53112 ! !
 53128 ! !
 53113 
 53129 
 53114 CharacterEncoder initialize!
 53130 CharacterEncoder initialize!