author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 16:00:23 +0100 | |
changeset 25135 | 98f799af1e48 |
parent 22407 | 09eea6d44c0e |
permissions | -rw-r--r-- |
15945 | 1 |
" |
2 |
COPYRIGHT (c) 2006 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 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
16 |
ISO10646_to_UTF8 subclass:#ISO10646_to_XMLUTF8 |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'ReplacementCharacter' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
23 |
!ISO10646_to_XMLUTF8 class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2006 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 |
This encoder encodes characters into utf8 characters that may |
|
42 |
occur in XML document. |
|
43 |
||
44 |
Not all UTF characters are valid in XML, whatever encoding |
|
45 |
is used. For a reference, see |
|
46 |
||
22407 | 47 |
https://www.w3.org/TR/xml/#NT-Char |
15945 | 48 |
|
49 |
Invalid characters are replaced by ReplacementCharacter |
|
50 |
with $? as default. |
|
51 |
||
52 |
[author:] |
|
53 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
|
54 |
||
55 |
[instance variables:] |
|
56 |
||
57 |
[class variables:] |
|
58 |
||
59 |
[see also:] |
|
22407 | 60 |
https://www.w3.org/TR/xml/#NT-Char |
15945 | 61 |
|
62 |
" |
|
63 |
! ! |
|
64 |
||
65 |
!ISO10646_to_XMLUTF8 class methodsFor:'initialization'! |
|
66 |
||
67 |
initialize |
|
68 |
"Invoked at system start or when the class is dynamically loaded." |
|
69 |
||
70 |
ReplacementCharacter := $?. |
|
71 |
||
72 |
"Modified: / 30-06-2012 / 19:55:00 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
73 |
! ! |
|
74 |
||
22407 | 75 |
!ISO10646_to_XMLUTF8 class methodsFor:'queries'! |
76 |
||
77 |
isValidXMLunicode: codePoint |
|
78 |
"Returns true, if given codePoint (Integer!!!!!!) is |
|
79 |
valid XML unicode." |
|
80 |
||
81 |
codePoint == 16r0009 ifTrue:[ ^ true ]. |
|
82 |
codePoint == 16r000A ifTrue:[ ^ true ]. |
|
83 |
codePoint == 16r000D ifTrue:[ ^ true ]. |
|
84 |
(codePoint between: 16r0020 and: 16rD7FF ) ifTrue:[ ^ true ]. |
|
85 |
(codePoint between: 16rE000 and: 16rFFFD ) ifTrue:[ ^ true ]. |
|
86 |
(codePoint between: 16r10000 and: 16r10FFFF) ifTrue:[ ^ true ]. |
|
87 |
||
88 |
^false. |
|
89 |
||
90 |
"Created: / 30-06-2012 / 20:11:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
91 |
! ! |
|
92 |
||
15945 | 93 |
!ISO10646_to_XMLUTF8 methodsFor:'encoding & decoding'! |
94 |
||
21479 | 95 |
encodeCharacter:aUnicodeCharacter on:aStream |
96 |
"return the UTF-8 representation of a aUnicodeCharacter. |
|
97 |
The resulting string contains only valid XML unicode |
|
98 |
characters. Invalid characters are replaced by a |
|
99 |
ReplacementCharacter. For details, please see |
|
100 |
||
22407 | 101 |
https://www.w3.org/TR/xml/#NT-Char" |
21479 | 102 |
|
103 |
|codePoint| |
|
104 |
||
105 |
codePoint := aUnicodeCharacter codePoint. |
|
22407 | 106 |
(self class isValidXMLunicode:codePoint) ifFalse:[ |
21479 | 107 |
codePoint := ReplacementCharacter codePoint. |
108 |
]. |
|
109 |
aStream nextPutUtf8:codePoint. |
|
110 |
||
111 |
"Created: / 16-02-2017 / 17:29:24 / stefan" |
|
22407 | 112 |
"Modified: / 04-01-2018 / 01:10:28 / stefan" |
21479 | 113 |
! |
114 |
||
15945 | 115 |
encodeString:aUnicodeString |
116 |
"return the UTF-8 representation of a aUnicodeString. |
|
117 |
The resulting string contains only valid XML unicode |
|
118 |
characters. Invalid characters are replaced by a |
|
119 |
ReplacementCharacter. For details, please see |
|
120 |
||
22407 | 121 |
https://www.w3.org/TR/xml/#NT-Char" |
15945 | 122 |
|
123 |
|s| |
|
124 |
||
125 |
s := WriteStream on:(String uninitializedNew:aUnicodeString size). |
|
21479 | 126 |
self encodeString:aUnicodeString on:s. |
15945 | 127 |
^ s contents |
128 |
||
129 |
" |
|
130 |
(self encodeString:'hello') asByteArray #[104 101 108 108 111] |
|
131 |
(self encodeString:(Character value:16r40) asString) asByteArray #[64] |
|
132 |
(self encodeString:(Character value:16r7F) asString) asByteArray #[127] |
|
133 |
(self encodeString:(Character value:16r80) asString) asByteArray #[194 128] |
|
134 |
(self encodeString:(Character value:16rFF) asString) asByteArray #[195 191] |
|
135 |
(self encodeString:(Character value:16r100) asString) asByteArray #[196 128] |
|
136 |
(self encodeString:(Character value:16r200) asString) asByteArray #[200 128] |
|
137 |
(self encodeString:(Character value:16r400) asString) asByteArray #[208 128] |
|
138 |
(self encodeString:(Character value:16r800) asString) asByteArray #[224 160 128] |
|
139 |
(self encodeString:(Character value:16r1000) asString) asByteArray #[225 128 128] |
|
140 |
(self encodeString:(Character value:16r2000) asString) asByteArray #[226 128 128] |
|
141 |
(self encodeString:(Character value:16r4000) asString) asByteArray #[228 128 128] |
|
142 |
(self encodeString:(Character value:16r8000) asString) asByteArray #[232 128 128] |
|
21479 | 143 |
(self encodeString:(Character value:16rFFFF) asString) '?' |
15945 | 144 |
" |
145 |
||
146 |
"Created: / 30-06-2012 / 20:07:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
22407 | 147 |
"Modified (comment): / 04-01-2018 / 01:08:11 / stefan" |
21479 | 148 |
! |
149 |
||
150 |
encodeString:aUnicodeString on:aStream |
|
151 |
"return the UTF-8 representation of a aUnicodeString. |
|
152 |
The resulting string contains only valid XML unicode |
|
153 |
characters. Invalid characters are replaced by a |
|
154 |
ReplacementCharacter. For details, please see |
|
155 |
||
22407 | 156 |
https://www.w3.org/TR/xml/#NT-Char" |
21479 | 157 |
|
158 |
|sz "{Class: SmallInteger}"| |
|
159 |
||
160 |
sz := aUnicodeString size. |
|
161 |
1 to:sz do:[:idx| |
|
162 |
self encodeCharacter:(aUnicodeString at:idx) on:aStream |
|
163 |
]. |
|
164 |
||
165 |
"Created: / 16-02-2017 / 17:18:47 / stefan" |
|
22407 | 166 |
"Modified (comment): / 04-01-2018 / 01:07:32 / stefan" |
15945 | 167 |
! ! |
168 |
||
169 |
!ISO10646_to_XMLUTF8 methodsFor:'queries'! |
|
170 |
||
17496
4dacd778d9c9
class: CharacterEncoderImplementations::ISO10646_to_XMLUTF8
Claus Gittinger <cg@exept.de>
parents:
15945
diff
changeset
|
171 |
nameOfEncoding |
4dacd778d9c9
class: CharacterEncoderImplementations::ISO10646_to_XMLUTF8
Claus Gittinger <cg@exept.de>
parents:
15945
diff
changeset
|
172 |
^ #'utf8-XML' |
15945 | 173 |
! ! |
174 |
||
175 |
!ISO10646_to_XMLUTF8 class methodsFor:'documentation'! |
|
176 |
||
177 |
version |
|
21479 | 178 |
^ '$Header$' |
15945 | 179 |
! |
180 |
||
181 |
version_CVS |
|
21479 | 182 |
^ '$Header$' |
15945 | 183 |
! ! |
184 |
||
185 |
||
186 |
ISO10646_to_XMLUTF8 initialize! |