author | Claus Gittinger <cg@exept.de> |
Sat, 15 Nov 1997 14:40:12 +0100 | |
changeset 1956 | d15780bd1b47 |
parent 1942 | 1dd0aedfed03 |
child 2033 | dad8f0ee02f4 |
permissions | -rw-r--r-- |
5 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 3 |
All Rights Reserved |
5 | 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 |
" |
|
0 | 12 |
|
13 |
Dictionary subclass:#ResourcePack |
|
684 | 14 |
instanceVariableNames:'elements dependents packsClassName fileReadFailed' |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
15 |
classVariableNames:'Packs' |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
16 |
poolDictionaries:'' |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
17 |
category:'Interface-Support' |
0 | 18 |
! |
19 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
20 |
!ResourcePack class methodsFor:'documentation'! |
46 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 25 |
All Rights Reserved |
5 | 26 |
|
46 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
5 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
1942 | 38 |
This class supports easy customization of smalltalk code (i.e. internationalization |
39 |
and viewStyle adaption). |
|
145 | 40 |
ResourcePacks are class specific, meaning that every subclass of View |
1942 | 41 |
and ApplicationModel has an instance of ResourcePack (instVar called 'resources') |
42 |
which is created when the first instance of the view/app is created, |
|
43 |
and cached in a class-instVar (so the file is only read once). |
|
54 | 44 |
|
50 | 45 |
The resourcePack consists of a mapping from strings to values, which are |
46 |
then used in labels, buttons, menus etc. |
|
47 |
The resourcePack initializes itself from a file found in 'resources/<className>.rs', |
|
48 |
where 'className' is built by the usual abbreviation mechanism (see abbrev-files). |
|
1942 | 49 |
|
50 | 50 |
Conditional mappings are possible, by including lines as: |
612 | 51 |
#if <expression> |
52 |
#endif |
|
50 | 53 |
in the resourcefile. Example: |
54 |
file 'foo.rs': |
|
612 | 55 |
#if Language == #german |
56 |
'abort' 'Abbruch' |
|
57 |
#endif |
|
58 |
#if Language == #french |
|
59 |
'abort' 'canceller' |
|
60 |
#endif |
|
145 | 61 |
|
62 |
the corresponding resource-strings are accessed (from methods within the class) |
|
63 |
using: |
|
1942 | 64 |
resources string:'abort' |
145 | 65 |
|
54 | 66 |
returning the mapped string (i.e. 'Abbruch' if the global Language is set |
67 |
to #german).. |
|
145 | 68 |
|
1942 | 69 |
If no corresponding entry is found in the resources, the key is returned; |
70 |
alternatively, use: |
|
71 |
resources string:'foo' default:'bar' |
|
72 |
which returns 'bar', if no resource definition for 'foo' is found. |
|
73 |
||
54 | 74 |
Translations can also include arguments, such as: |
612 | 75 |
resources string:'really delete %1' with:fileName |
145 | 76 |
|
77 |
This scheme has the advantage, that you can write your programs using your |
|
78 |
native language strings. Later, when new languages are to be supported, |
|
79 |
simply create a resource file for the class and add translations for |
|
80 |
all those strings. (find the keys by looking at users of resource or senders |
|
1942 | 81 |
of 'string:*'). |
82 |
Notice, that the grammar of different languages may imply a reordering, |
|
83 |
so the above string becomes the german 'wollen Sie %1 wirklich löschen'; |
|
84 |
so using percent-placeholders is much better than simple concatenations of |
|
85 |
arguments to the question. |
|
145 | 86 |
|
87 |
More languages can be added later without any change in the code, |
|
88 |
recompilation or the like. |
|
89 |
Even by people without access to the source code i.e. which only have the |
|
90 |
applications binary. |
|
91 |
||
92 |
Also, unsupported languages' strings are simply left unchanged - if you |
|
93 |
write your application in (say) english, and only plan to use it in english, |
|
94 |
no additional work is required (i.e you dont need a resource file then). |
|
95 |
Strings for unknown languages will come in english |
|
96 |
(which is better than nothing or empty button labels ;-) |
|
97 |
||
98 |
Summary: |
|
612 | 99 |
in subclasses of View and ApplicationModel, |
100 |
instead of writing: |
|
145 | 101 |
|
612 | 102 |
... |
103 |
b := Button label:'press me' |
|
104 |
... |
|
145 | 105 |
|
612 | 106 |
always write: |
107 |
||
108 |
... |
|
109 |
b := Button label:(resources string:'press me') |
|
110 |
... |
|
145 | 111 |
|
612 | 112 |
if your class is not a subclass of one of the above, AND you need |
113 |
resource translations, you wont inherit the resources variable |
|
114 |
(which is automatically initialized). |
|
115 |
In this case, you have to ask the ResourcePack class explicitely for |
|
116 |
a corresponding package: |
|
145 | 117 |
|
612 | 118 |
ResourcePack for:aClassName |
145 | 119 |
|
612 | 120 |
as an example, see how the Date class gets the national names of |
121 |
week & monthnames. |
|
145 | 122 |
|
612 | 123 |
[author:] |
124 |
Claus Gittinger |
|
145 | 125 |
" |
126 |
! |
|
127 |
||
128 |
examples |
|
129 |
" |
|
130 |
normally, resources are found in files named after their classes sourcefile |
|
131 |
For example, the FileBrowsers resources are found in 'FBrowser.rs'. |
|
132 |
For the examples below, we process resources from a constant string; |
|
620 | 133 |
this is NOT representative. |
134 |
[exBegin] |
|
135 |
|stream res| |
|
145 | 136 |
|
620 | 137 |
stream := ReadStream on:' |
145 | 138 |
foo ''the translation for foo'' |
139 |
#if Language == #german |
|
140 |
bar ''die deutsche uebersetzung von bar'' |
|
141 |
baz ''baz hat den Wert %1'' |
|
142 |
#endif |
|
143 |
#if Language == #french |
|
144 |
bar ''bar en francaise'' |
|
145 |
baz ''%1, c''''est baz'' |
|
146 |
#endif |
|
147 |
||
148 |
'. |
|
149 |
||
620 | 150 |
res := ResourcePack new readFromResourceStream:stream in:nil. |
145 | 151 |
|
697 | 152 |
Transcript showCR:'baz is translated to: ' , (res string:'baz' with:'1234'). |
153 |
Transcript showCR:'bar is translated to: ' , (res string:'bar'). |
|
154 |
Transcript showCR:'foo is translated to: ' , (res string:'foo'). |
|
155 |
Transcript showCR:'fooBar is translated to: ' , (res string:'fooBar'). |
|
620 | 156 |
[exEnd] |
145 | 157 |
set the Language to french: |
620 | 158 |
[exBegin] |
159 |
Language := #french |
|
160 |
[exEnd] |
|
145 | 161 |
and repeat the above. |
620 | 162 |
back to english: |
163 |
[exBegin] |
|
164 |
Language := #english |
|
165 |
[exEnd] |
|
166 |
back to german: |
|
167 |
[exBegin] |
|
168 |
Language := #german |
|
169 |
[exEnd] |
|
46 | 170 |
" |
171 |
! ! |
|
5 | 172 |
|
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
173 |
!ResourcePack class methodsFor:'initialization'! |
0 | 174 |
|
72 | 175 |
flushCachedResourcePacks |
24 | 176 |
"forget all cached resources - needed after a style change" |
177 |
||
178 |
Packs := nil. |
|
179 |
self initialize |
|
180 |
||
72 | 181 |
"ResourcePack flushCachedResourcePacks" |
244 | 182 |
! |
183 |
||
184 |
initialize |
|
185 |
Packs isNil ifTrue:[ |
|
186 |
Packs := WeakArray new:30 |
|
187 |
]. |
|
188 |
||
189 |
"ResourcePack initialize" |
|
190 |
! ! |
|
191 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
192 |
!ResourcePack class methodsFor:'instance creation'! |
244 | 193 |
|
194 |
for:aClass |
|
195 |
"get the full resource definitions for aClass (i.e. with super packs). |
|
196 |
Also leave the resulting pack in the cache for faster access next time." |
|
197 |
||
198 |
|nm pack superPack| |
|
199 |
||
200 |
nm := aClass name. |
|
201 |
pack := self searchCacheFor:nm. |
|
202 |
pack notNil ifTrue:[^ pack]. |
|
203 |
||
204 |
pack := self fromFile:(Smalltalk fileNameForClass:nm) , '.rs'. |
|
205 |
aClass == Object ifFalse:[ |
|
206 |
superPack := self for:(aClass superclass). |
|
207 |
pack := pack merge:superPack |
|
208 |
]. |
|
209 |
pack packsClassName:nm. |
|
210 |
self addToCache:pack. |
|
211 |
^ pack |
|
212 |
||
213 |
" |
|
214 |
ResourcePack for:TextView |
|
215 |
ResourcePack for:CodeView |
|
216 |
ResourcePack for:Workspace |
|
217 |
Workspace classResources |
|
218 |
" |
|
219 |
! |
|
220 |
||
221 |
fromFile:aFileName |
|
222 |
"get the resource definitions from a file in the default directory. |
|
223 |
Uncached low-level entry." |
|
224 |
||
225 |
^ self fromFile:aFileName directory:'resources' |
|
226 |
||
227 |
" |
|
228 |
ResourcePack fromFile:'SBrowser.rs' |
|
229 |
ResourcePack fromFile:'FBrowser.rs' |
|
230 |
ResourcePack fromFile:'Smalltalk.rs' |
|
231 |
" |
|
232 |
! |
|
233 |
||
234 |
fromFile:aFileName directory:dirName |
|
235 |
"get the resource definitions from a file in a directory. |
|
236 |
Uncached low-level entry." |
|
237 |
||
238 |
|newPack| |
|
239 |
||
240 |
newPack := self new. |
|
241 |
newPack readFromFile:aFileName directory:dirName. |
|
242 |
^ newPack |
|
0 | 243 |
! ! |
244 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
245 |
!ResourcePack class methodsFor:'private'! |
54 | 246 |
|
247 |
addToCache:aPack |
|
248 |
|idx| |
|
0 | 249 |
|
54 | 250 |
idx := Packs identityIndexOf:nil. |
251 |
idx == 0 ifTrue:[ |
|
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
252 |
idx := Packs identityIndexOf:0 |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
253 |
]. |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
254 |
idx == 0 ifTrue:[ |
72 | 255 |
" |
256 |
throw away oldest |
|
257 |
" |
|
258 |
idx := Packs size. |
|
259 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2. |
|
54 | 260 |
]. |
261 |
Packs at:idx put:aPack |
|
262 |
! |
|
0 | 263 |
|
54 | 264 |
searchCacheFor:aClassname |
998 | 265 |
|sz "{ Class: SmallInteger }" | |
0 | 266 |
|
24 | 267 |
Packs isNil ifTrue:[ |
72 | 268 |
self initialize. |
269 |
^ nil |
|
0 | 270 |
]. |
54 | 271 |
|
272 |
sz := Packs size. |
|
273 |
1 to:sz do:[:idx | |
|
72 | 274 |
|aPack| |
54 | 275 |
|
72 | 276 |
aPack := Packs at:idx. |
1078
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
277 |
(aPack notNil |
f7b6f15899ab
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1042
diff
changeset
|
278 |
and:[aPack ~~ 0]) ifTrue:[ |
72 | 279 |
aClassname = aPack packsClassName ifTrue:[ |
280 |
" |
|
281 |
bring to end for LRU |
|
282 |
" |
|
283 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1. |
|
284 |
Packs at:sz put:aPack. |
|
285 |
^ aPack |
|
286 |
] |
|
287 |
] |
|
0 | 288 |
]. |
54 | 289 |
^ nil |
123 | 290 |
|
291 |
" |
|
292 |
ResourcePack searchCacheFor:'TextView' |
|
293 |
" |
|
54 | 294 |
! ! |
295 |
||
0 | 296 |
!ResourcePack methodsFor:'accessing'! |
297 |
||
244 | 298 |
array:anArray |
299 |
"translate a collection of strings" |
|
0 | 300 |
|
244 | 301 |
^ anArray collect:[:r | self at:r default:r] |
0 | 302 |
! |
303 |
||
2 | 304 |
at:aKey |
123 | 305 |
"translate a string" |
306 |
||
72 | 307 |
^ self at:aKey ifAbsent:aKey |
0 | 308 |
! |
309 |
||
310 |
at:aKey default:default |
|
123 | 311 |
"translate a string" |
312 |
||
72 | 313 |
^ self at:aKey ifAbsent:default |
0 | 314 |
! |
315 |
||
244 | 316 |
dependents |
1936
6943714d3ab2
empty dependents now always a collection.
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
317 |
^ dependents ? #() |
6943714d3ab2
empty dependents now always a collection.
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
318 |
|
6943714d3ab2
empty dependents now always a collection.
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
319 |
"Modified: / 27.10.1997 / 14:05:28 / cg" |
244 | 320 |
! |
321 |
||
322 |
dependents:aCollection |
|
323 |
dependents := aCollection |
|
324 |
! |
|
325 |
||
2 | 326 |
name:aKey |
123 | 327 |
"translate a string" |
328 |
||
72 | 329 |
^ self at:aKey ifAbsent:aKey |
2 | 330 |
! |
331 |
||
332 |
name:aKey default:default |
|
123 | 333 |
"translate a string" |
334 |
||
72 | 335 |
^ self at:aKey ifAbsent:default |
2 | 336 |
! |
337 |
||
244 | 338 |
packsClassName |
339 |
^ packsClassName |
|
340 |
! |
|
123 | 341 |
|
244 | 342 |
packsClassName:aString |
343 |
packsClassName := aString |
|
0 | 344 |
! |
345 |
||
346 |
string:s |
|
123 | 347 |
"translate a string" |
348 |
||
72 | 349 |
^ self at:s ifAbsent:s |
0 | 350 |
! |
351 |
||
352 |
string:s with:arg |
|
123 | 353 |
"translate and expand arg" |
354 |
||
0 | 355 |
^ self string:s withArgs:(Array with:arg) |
356 |
! |
|
357 |
||
358 |
string:s with:arg1 with:arg2 |
|
123 | 359 |
"translate and expand args" |
360 |
||
0 | 361 |
^ self string:s withArgs:(Array with:arg1 with:arg2) |
362 |
! |
|
363 |
||
292
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
364 |
string:s with:arg1 with:arg2 with:arg3 |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
365 |
"translate and expand args" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
366 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
367 |
^ self string:s withArgs:(Array with:arg1 with:arg2 with:arg3) |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
368 |
|
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
369 |
"Created: 9.12.1995 / 19:08:50 / cg" |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
370 |
! |
62799b2ab9ae
new method: string:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
371 |
|
1042 | 372 |
string:s with:arg1 with:arg2 with:arg3 with:arg4 |
373 |
"translate and expand args" |
|
374 |
||
375 |
^ self string:s withArgs:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
376 |
||
377 |
"Created: 9.9.1996 / 18:52:14 / cg" |
|
378 |
! |
|
379 |
||
0 | 380 |
string:s withArgs:argArray |
123 | 381 |
"translate and expand args" |
382 |
||
135 | 383 |
|template| |
0 | 384 |
|
385 |
template := self at:s ifAbsent:[s]. |
|
135 | 386 |
^ template expandPlaceholdersWith:argArray |
0 | 387 |
! ! |
388 |
||
389 |
!ResourcePack methodsFor:'file reading'! |
|
390 |
||
684 | 391 |
fileReadFailed |
687 | 392 |
^ fileReadFailed ~~ false |
684 | 393 |
|
394 |
"Created: 14.5.1996 / 10:19:26 / cg" |
|
687 | 395 |
"Modified: 14.5.1996 / 12:00:59 / cg" |
684 | 396 |
! |
397 |
||
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
398 |
nonexistingFileRead |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
399 |
"asked to read definitions for a non-existing file. |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
400 |
Here, this is legal and ignored (typically using inherited resources). |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
401 |
However, subclasses (such as styleSheet) may flag it as an error." |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
402 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
403 |
^ nil |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
404 |
|
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
405 |
"Created: 6.9.1997 / 11:38:53 / cg" |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
406 |
! |
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
407 |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
408 |
processLine:aLine encoding:encoding |
145 | 409 |
"process a single valid line (i.e. #ifdef & #include has already been processed)" |
410 |
||
144 | 411 |
|lineString name stream l rest macroName value| |
412 |
||
145 | 413 |
lineString := aLine withoutSeparators. |
144 | 414 |
name := nil. |
415 |
(lineString at:1) == $' ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
416 |
stream := ReadStream on:lineString. |
1956 | 417 |
stream signalAtEnd:false. |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
418 |
name := String |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
419 |
readFrom:stream |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
420 |
onError:[('ResourcePack [warning]: invalid line <',lineString,'>') errorPrintCR. nil]. |
158 | 421 |
"/ OLD: l := stream position |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
422 |
l := stream position + 1. |
144 | 423 |
|
424 |
"/ l := lineString indexOf:$' startingAt:2. |
|
425 |
"/ l ~~ 0 ifTrue:[ |
|
426 |
"/ name := (lineString copyFrom:2 to:l-1). |
|
427 |
"/ l := l + 1 |
|
428 |
"/ ] |
|
429 |
] ifFalse:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
430 |
l := lineString indexOfSeparatorStartingAt:1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
431 |
l ~~ 0 ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
432 |
name := lineString copyFrom:1 to:l-1. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
433 |
] |
144 | 434 |
]. |
435 |
name notNil ifTrue:[ |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
436 |
rest := (lineString copyFrom:l) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
437 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
438 |
skip <type> if present |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
439 |
" |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
440 |
(rest startsWith:$<) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
441 |
l := lineString indexOf:$> startingAt:l. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
442 |
rest := (lineString copyFrom:l+1) withoutSeparators. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
443 |
]. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
444 |
(rest startsWith:$=) ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
445 |
rest := rest copyFrom:2. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
446 |
stream := ReadStream on:rest. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
447 |
macroName := stream nextAlphaNumericWord. |
1942 | 448 |
[stream peek == $.] whileTrue:[ |
449 |
stream next. |
|
450 |
stream peek notNil ifTrue:[ |
|
451 |
macroName := macroName , '.' , (stream nextAlphaNumericWord) |
|
452 |
] |
|
453 |
]. |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
454 |
rest := stream upToEnd. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
455 |
value := self at:macroName. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
456 |
value := Compiler evaluate:('self ' , rest) |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
457 |
receiver:value |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
458 |
notifying:nil |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
459 |
compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
460 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
461 |
value := Compiler evaluate:rest compile:false. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
462 |
(value == #Error) ifTrue:[ |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
463 |
Transcript show:('ResourcePack: error in resource:' , name). |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
464 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
465 |
encoding notNil ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
466 |
value isString ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
467 |
value := value decodeFrom:encoding |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
468 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
469 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
470 |
] |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
471 |
]. |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
472 |
self at:name put:value. |
144 | 473 |
] |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
474 |
|
1942 | 475 |
"Created: / 26.2.1996 / 19:17:07 / cg" |
476 |
"Modified: / 31.10.1997 / 14:17:50 / cg" |
|
144 | 477 |
! |
478 |
||
244 | 479 |
readFromFile:fileName directory:dirName |
480 |
"read definitions from a file in a directory" |
|
481 |
||
482 |
|inStream ok| |
|
483 |
||
686 | 484 |
fileReadFailed := false. |
244 | 485 |
dirName = 'resources' ifTrue:[ |
684 | 486 |
inStream := Smalltalk resourceFileStreamFor:fileName |
244 | 487 |
] ifFalse:[ |
684 | 488 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName). |
244 | 489 |
]. |
490 |
inStream isNil ifTrue:[ |
|
684 | 491 |
" |
492 |
an empty pack |
|
493 |
" |
|
1894
9982e96dff75
better behavior if no stylesheet is present.
Claus Gittinger <cg@exept.de>
parents:
1803
diff
changeset
|
494 |
^ self nonexistingFileRead |
244 | 495 |
]. |
496 |
ok := self readFromResourceStream:inStream in:dirName. |
|
497 |
inStream close. |
|
686 | 498 |
|
244 | 499 |
ok ifFalse:[ |
684 | 500 |
fileReadFailed := true. |
501 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
502 |
('ResourcePack [warning]: ''' , fileName , ''' contains error(s) - data may be incomplete.') errorPrintCR. |
244 | 503 |
]. |
504 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
505 |
"Modified: 10.1.1997 / 18:05:17 / cg" |
244 | 506 |
! |
507 |
||
72 | 508 |
readFromResourceStream:inStream in:dirName |
145 | 509 |
"read definitions from a stream. THe dirName argument is required to |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
510 |
specify where #include files are searched for. |
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
511 |
Return true, if the style sheet could be read without errors, false otherwise." |
145 | 512 |
|
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
513 |
|lineString l lNo rest value ifLevel skipping first ok encoding| |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
514 |
|
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
515 |
encoding := nil. |
0 | 516 |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
517 |
ok := true. |
0 | 518 |
ifLevel := 0. |
519 |
skipping := false. |
|
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
520 |
lNo := 0. |
0 | 521 |
[inStream atEnd] whileFalse:[ |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
522 |
lineString := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
523 |
[lineString notNil and:[lineString endsWith:$\]] whileTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
524 |
lineString := lineString copyWithoutLast:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
525 |
l := inStream nextLine. lNo := lNo + 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
526 |
l notNil ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
527 |
lineString := lineString , l. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
528 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
529 |
]. |
177 | 530 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
531 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
532 |
first := lineString at:1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
533 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
534 |
"/ for your convenience: treat ; AND "/ as comment-line |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
535 |
"/ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
536 |
((first == $;) or:[lineString startsWith:'"/']) ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
537 |
first == $# ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
538 |
lineString := (lineString copyFrom:2) withoutSpaces. |
72 | 539 |
|
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
540 |
(lineString startsWith:'if') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
541 |
skipping ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
542 |
rest := lineString copyFrom:3. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
543 |
value := Compiler evaluate:rest compile:false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
544 |
(value == #Error) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
545 |
Transcript show:('error in resource:' , lineString). |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
546 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
547 |
(value == false) ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
548 |
skipping := true |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
549 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
550 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
551 |
ifLevel := ifLevel + 1 |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
552 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
553 |
(lineString startsWith:'endif') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
554 |
ifLevel := ifLevel - 1. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
555 |
ifLevel == 0 ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
556 |
skipping := false |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
557 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
558 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
559 |
(lineString startsWith:'else') ifTrue:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
560 |
skipping := skipping not |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
561 |
] ifFalse:[ |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
562 |
skipping ifFalse:[ |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
563 |
(lineString startsWith:'include') ifTrue:[ |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
564 |
rest := lineString copyFrom:8. |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
565 |
value := Compiler evaluate:rest compile:false. |
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
566 |
self readFromFile:value directory:dirName |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
567 |
] ifFalse:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
568 |
(lineString startsWith:'encoding') ifTrue:[ |
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
569 |
rest := lineString copyFrom:9. |
1803 | 570 |
encoding := rest withoutSeparators asSymbol |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
571 |
] |
444
1f67d369190d
only #include if not skipping (i.e. not in #if/#endif)
Claus Gittinger <cg@exept.de>
parents:
426
diff
changeset
|
572 |
] |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
573 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
574 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
575 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
576 |
]. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
577 |
] ifFalse:[ |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
578 |
skipping ifFalse:[ |
426 | 579 |
ErrorSignal handle:[:ex | |
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
580 |
('ResourcePack [warning]: ''' , ex errorString , '''') errorPrintCR. |
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
581 |
('ResourcePack [info]: line ' , lNo printString , ': ''' , lineString , '''') infoPrintCR. |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
582 |
ok := false. |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
583 |
] do:[ |
462
423f8426fefc
added #encoding directive (I am lazy)
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
584 |
self processLine:lineString encoding:encoding |
308
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
585 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
586 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
587 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
588 |
] |
54fa63d274fa
also allow "/ for comment lines
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
589 |
] |
0 | 590 |
]. |
210
0f942dafc85a
gracefully handle errors when reading resource files
Claus Gittinger <cg@exept.de>
parents:
177
diff
changeset
|
591 |
^ ok |
177 | 592 |
|
593 |
"Modified: 31.8.1995 / 02:33:45 / claus" |
|
1803 | 594 |
"Modified: 1.7.1997 / 00:59:22 / cg" |
0 | 595 |
! ! |
596 |
||
244 | 597 |
!ResourcePack methodsFor:'merging'! |
598 |
||
599 |
merge:anotherPack |
|
1942 | 600 |
"merge the contents of another pack into the receiver, |
601 |
keys which are already present in the receiver are not |
|
602 |
overwritten by the other pack" |
|
603 |
||
244 | 604 |
anotherPack keysAndValuesDo:[:key :value | |
1942 | 605 |
(self includesKey:key) ifFalse:[ |
606 |
self at:key put:value |
|
607 |
] |
|
244 | 608 |
] |
1942 | 609 |
|
610 |
"Modified: / 31.10.1997 / 14:16:03 / cg" |
|
244 | 611 |
! ! |
612 |
||
1173
86a212598655
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1078
diff
changeset
|
613 |
!ResourcePack class methodsFor:'documentation'! |
251 | 614 |
|
615 |
version |
|
1956 | 616 |
^ '$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.43 1997-11-15 13:40:12 cg Exp $' |
251 | 617 |
! ! |
0 | 618 |
ResourcePack initialize! |