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