8171
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
8170
|
12 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
|
14 |
"{ NameSpace: CharacterEncoderImplementations }"
|
|
15 |
|
|
16 |
TwoByteEncoder subclass:#ISO10646_to_SGML
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Text-Encodings'
|
|
21 |
!
|
|
22 |
|
8171
|
23 |
!ISO10646_to_SGML class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
Incomplete - only knows how to encode/decode escaped decimal-code characters
|
|
42 |
(i.e. &#nnnn; )
|
|
43 |
|
|
44 |
TODO:
|
|
45 |
add all other characters
|
|
46 |
reuse this code in XML and HTML processing code.
|
|
47 |
"
|
|
48 |
! !
|
8170
|
49 |
|
|
50 |
!ISO10646_to_SGML methodsFor:'encoding & decoding'!
|
|
51 |
|
|
52 |
decode:aCode
|
|
53 |
self shouldNotImplement "/ no single byte conversion possible
|
|
54 |
!
|
|
55 |
|
|
56 |
decodeString:aStringOrByteCollection
|
|
57 |
"given a string in SGML encoding (i.e. with SGML escaped characters),
|
|
58 |
return a new string containing the same characters, in 16bit (or more) encoding.
|
|
59 |
Returns either a normal String, a TwoByteString or a FourByteString instance.
|
|
60 |
Only useful, when reading from external sources.
|
|
61 |
This only handles up-to 30bit characters."
|
|
62 |
|
|
63 |
|nBits ch
|
|
64 |
in out codePoint t|
|
|
65 |
|
|
66 |
nBits := 8.
|
|
67 |
in := aStringOrByteCollection readStream.
|
|
68 |
out := WriteStream on:(String new:10).
|
|
69 |
[in atEnd] whileFalse:[
|
|
70 |
ch := in next.
|
|
71 |
ch == $& ifTrue:[
|
|
72 |
in peekOrNil == $# ifTrue:[
|
|
73 |
in next.
|
|
74 |
codePoint := 0.
|
|
75 |
[ch := in peekOrNil.
|
|
76 |
ch notNil and:[ch isDigit]
|
|
77 |
] whileTrue:[
|
|
78 |
codePoint := (codePoint * 10) + ch digitValue.
|
|
79 |
in next.
|
|
80 |
].
|
|
81 |
codePoint > 16rFF ifTrue:[
|
|
82 |
codePoint > 16rFFFF ifTrue:[
|
|
83 |
nBits < 32 ifTrue:[
|
|
84 |
t := out contents.
|
|
85 |
out := WriteStream on:(Unicode32String fromString:t).
|
|
86 |
out position:t size.
|
|
87 |
nBits := 32.
|
|
88 |
]
|
|
89 |
] ifFalse:[
|
|
90 |
nBits < 16 ifTrue:[
|
|
91 |
t := out contents.
|
|
92 |
out := WriteStream on:(Unicode16String fromString:t).
|
|
93 |
out position:t size.
|
|
94 |
nBits := 16.
|
|
95 |
]
|
|
96 |
]
|
|
97 |
].
|
|
98 |
out nextPut:(Character value:codePoint).
|
|
99 |
in peekOrNil == $; ifTrue:[
|
|
100 |
in next.
|
|
101 |
]
|
|
102 |
] ifFalse:[
|
|
103 |
out nextPut:ch
|
|
104 |
]
|
|
105 |
] ifFalse:[
|
|
106 |
out nextPut:ch
|
|
107 |
].
|
|
108 |
].
|
|
109 |
^ out contents
|
|
110 |
|
|
111 |
"
|
|
112 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
|
113 |
decodeString:'Файл'
|
|
114 |
|
|
115 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
|
116 |
decodeString:'#197;&bn...'
|
|
117 |
"
|
|
118 |
!
|
|
119 |
|
|
120 |
encode:aCode
|
|
121 |
self shouldNotImplement "/ no single byte conversion possible
|
|
122 |
!
|
|
123 |
|
|
124 |
encodeString:aUnicodeString
|
10108
|
125 |
"return the SGML representation of aUnicodeString.
|
8170
|
126 |
The resulting string is only useful to be stored on some external file,
|
10108
|
127 |
not for being used inside ST/X."
|
8170
|
128 |
|
|
129 |
|ch in out codePoint|
|
|
130 |
|
|
131 |
in := aUnicodeString readStream.
|
|
132 |
out := WriteStream on:(String new:10).
|
|
133 |
[in atEnd] whileFalse:[
|
|
134 |
ch := in next.
|
|
135 |
codePoint := ch codePoint.
|
|
136 |
(codePoint between:16r20 and:16r7F) ifTrue:[
|
|
137 |
out nextPut:ch.
|
|
138 |
] ifFalse:[
|
|
139 |
out nextPutAll:'&#'.
|
|
140 |
out nextPutAll:(codePoint printString).
|
|
141 |
out nextPutAll:';'.
|
|
142 |
].
|
|
143 |
].
|
|
144 |
^ out contents
|
|
145 |
|
|
146 |
"
|
|
147 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
|
148 |
encodeString:'hello äöü'
|
|
149 |
"
|
10108
|
150 |
|
|
151 |
"Modified: / 23-10-2006 / 13:25:27 / cg"
|
8170
|
152 |
! !
|
|
153 |
|
|
154 |
!ISO10646_to_SGML class methodsFor:'documentation'!
|
|
155 |
|
|
156 |
version
|
10108
|
157 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_SGML.st,v 1.3 2006-10-23 11:25:11 cg Exp $'
|
8170
|
158 |
! !
|