author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 10 Apr 2008 09:14:47 +0000 | |
changeset 3 | 7909b6680107 |
parent 2 | 06f508a6f55c |
child 69 | 6e7602aefbb9 |
permissions | -rw-r--r-- |
3
7909b6680107
Loaded into & commited from 5.3.6
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2
diff
changeset
|
1 |
"{ Package: 'stx:goodies/xmlsuite/core' }" |
0 | 2 |
|
3 |
"{ NameSpace: XMLv2 }" |
|
4 |
||
5 |
Object subclass:#OldXMLWriter |
|
6 |
instanceVariableNames:'stream depth namespaces namespaceId nsStack |
|
7 |
pendingNamespaceDeclarations formatOutput' |
|
8 |
classVariableNames:'' |
|
9 |
poolDictionaries:'' |
|
10 |
category:'XML Suite-XML Writer' |
|
11 |
! |
|
12 |
||
13 |
ContentHandler subclass:#ContentHandler |
|
14 |
instanceVariableNames:'writer' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
privateIn:OldXMLWriter |
|
18 |
! |
|
19 |
||
20 |
||
21 |
!OldXMLWriter class methodsFor:'instance creation'! |
|
22 |
||
23 |
new |
|
24 |
^ self basicNew initialize. |
|
25 |
||
26 |
"Created: / 16-04-2005 / 20:38:09 / janfrog" |
|
27 |
! |
|
28 |
||
29 |
on:aStream |
|
30 |
||
31 |
^self new initializeOn:aStream |
|
32 |
||
33 |
"Created: / 22-04-2005 / 22:13:11 / janfrog" |
|
34 |
! ! |
|
35 |
||
36 |
!OldXMLWriter class methodsFor:'utilities'! |
|
37 |
||
38 |
write:aDOMNode |
|
39 |
||
40 |
self new write:aDOMNode |
|
41 |
||
42 |
"Created: / 22-04-2005 / 22:12:17 / janfrog" |
|
43 |
! |
|
44 |
||
45 |
write:aDOMNode on:aStream |
|
46 |
||
47 |
^(self on:aStream) write:aDOMNode |
|
48 |
||
49 |
"Created: / 22-04-2005 / 22:14:23 / janfrog" |
|
50 |
! |
|
51 |
||
52 |
write:aDOMNode on:aStream format:aBoolean |
|
53 |
||
54 |
^(self on:aStream) |
|
55 |
formatOutput:aBoolean; |
|
56 |
write:aDOMNode |
|
57 |
||
58 |
"Created: / 15-06-2005 / 19:12:58 / janfrog" |
|
59 |
! ! |
|
60 |
||
61 |
!OldXMLWriter methodsFor:'accessing'! |
|
62 |
||
63 |
encoding:encodingName |
|
64 |
||
65 |
stream ifNil:[self error:'No stream yet!!']. |
|
66 |
stream encoder:(CharacterEncoder encoderFor:encodingName) |
|
67 |
||
68 |
"Created: / 15-06-2005 / 19:28:03 / janfrog" |
|
69 |
! |
|
70 |
||
71 |
formatOutput |
|
72 |
^ formatOutput |
|
73 |
||
74 |
"Created: / 15-06-2005 / 19:00:15 / janfrog" |
|
75 |
! |
|
76 |
||
77 |
formatOutput:aBoolean |
|
78 |
formatOutput := aBoolean. |
|
79 |
||
80 |
"Created: / 15-06-2005 / 19:00:15 / janfrog" |
|
81 |
! |
|
82 |
||
83 |
stream |
|
84 |
^ stream |
|
85 |
||
86 |
"Created: / 22-04-2005 / 22:14:39 / janfrog" |
|
87 |
! |
|
88 |
||
89 |
stream:aStream |
|
90 |
self initializeOn:aStream |
|
91 |
||
92 |
"Created: / 15-06-2005 / 19:27:49 / janfrog" |
|
93 |
! ! |
|
94 |
||
95 |
!OldXMLWriter methodsFor:'accessing - sax'! |
|
96 |
||
97 |
contentHandler |
|
98 |
||
99 |
^XMLWriter::ContentHandler new writer:self |
|
100 |
||
101 |
"Created: / 16-04-2005 / 20:37:06 / janfrog" |
|
102 |
! ! |
|
103 |
||
104 |
!OldXMLWriter methodsFor:'initialization'! |
|
105 |
||
106 |
initialize |
|
107 |
||
108 |
self initializeOn:Transcript |
|
109 |
||
110 |
"Created: / 22-04-2005 / 22:13:23 / janfrog" |
|
111 |
! |
|
112 |
||
113 |
initializeOn:aStream |
|
114 |
||
115 |
stream := EncodedStream |
|
116 |
stream:aStream |
|
117 |
encoder:(CharacterEncoder encoderForUTF8). |
|
118 |
depth := 0. |
|
119 |
namespaces := Dictionary new. |
|
120 |
namespaceId := 0. |
|
121 |
formatOutput := true. |
|
122 |
||
123 |
namespaces at:'http://www.w3.org/XML/1998/namespace' put: 'xml' |
|
124 |
||
125 |
"Created: / 22-04-2005 / 22:13:23 / janfrog" |
|
126 |
"Modified: / 15-06-2005 / 19:03:54 / janfrog" |
|
127 |
! ! |
|
128 |
||
129 |
!OldXMLWriter methodsFor:'utilities'! |
|
130 |
||
131 |
decIndent |
|
132 |
||
133 |
depth := depth - 1 |
|
134 |
||
135 |
"Created: / 16-04-2005 / 20:40:55 / janfrog" |
|
136 |
! |
|
137 |
||
138 |
ensureNamespaceDeclaredFor: aNodeTag |
|
139 |
||
140 |
| qualifier | |
|
141 |
||
142 |
aNodeTag namespace isNilOrEmptyCollection ifTrue:[^self]. |
|
143 |
||
144 |
"(namespaces includesKey: aNodeTag namespace)" |
|
145 |
(nsStack top includes:aNodeTag namespace) ifTrue: [ |
|
146 |
aNodeTag qualifier: (namespaces at: aNodeTag namespace). |
|
147 |
^self]. |
|
148 |
||
149 |
qualifier := aNodeTag qualifier. |
|
150 |
(qualifier isNilOrEmptyCollection) ifTrue:[ |
|
151 |
qualifier := namespaces at:aNodeTag namespace |
|
152 |
ifAbsent:[ |
|
153 |
| q | |
|
154 |
(namespaces values includes:'') ifFalse:[ |
|
155 |
q := ''. |
|
156 |
] ifTrue:[ |
|
157 |
q := 'ns' , (namespaceId := namespaceId + 1) printString |
|
158 |
]. |
|
159 |
q |
|
160 |
] |
|
161 |
]. |
|
162 |
namespaces at: aNodeTag namespace put: qualifier. |
|
163 |
nsStack top add:aNodeTag namespace. |
|
164 |
aNodeTag qualifier:qualifier. |
|
165 |
||
166 |
||
167 |
pendingNamespaceDeclarations := pendingNamespaceDeclarations , |
|
168 |
(' xmlns' , (qualifier isEmpty ifTrue: [''] ifFalse: [':' , qualifier])) , |
|
169 |
'="' , |
|
170 |
aNodeTag namespace, |
|
171 |
'"' |
|
172 |
||
173 |
"Created: / 03-05-2005 / 11:07:41 / janfrog" |
|
174 |
"Modified: / 30-05-2005 / 16:42:15 / masca" |
|
175 |
"Modified: / 15-06-2005 / 11:08:56 / janfrog" |
|
176 |
! |
|
177 |
||
178 |
format:aDocument |
|
179 |
||
180 |
aDocument acceptVisitor:self |
|
181 |
||
182 |
"Created: / 22-04-2005 / 22:11:41 / janfrog" |
|
183 |
! |
|
184 |
||
185 |
incIndent |
|
186 |
||
187 |
depth := depth + 1 |
|
188 |
||
189 |
"Created: / 16-04-2005 / 20:40:49 / janfrog" |
|
190 |
! |
|
191 |
||
192 |
write:aDOMNode |
|
193 |
||
194 |
aDOMNode acceptVisitor:self |
|
195 |
||
196 |
"Created: / 22-04-2005 / 22:12:31 / janfrog" |
|
197 |
! ! |
|
198 |
||
199 |
!OldXMLWriter methodsFor:'visiting'! |
|
200 |
||
201 |
visitComment:aComment |
|
202 |
||
203 |
self |
|
204 |
indent; |
|
205 |
nextPutComment:aComment text; |
|
206 |
cr. |
|
207 |
||
208 |
"Created: / 22-04-2005 / 22:11:10 / janfrog" |
|
209 |
! |
|
210 |
||
211 |
visitDocument:aDocument |
|
212 |
||
213 |
nsStack := Stack new. |
|
214 |
nsStack push:(Set new addAll:(namespaces keys); yourself). |
|
215 |
self nextPutXMLDecl ; cr. |
|
216 |
aDocument elements do:[:e|e acceptVisitor:self]. |
|
217 |
nsStack := nil. |
|
218 |
||
219 |
"Created: / 22-04-2005 / 22:05:49 / janfrog" |
|
220 |
"Modified: / 15-06-2005 / 11:11:59 / janfrog" |
|
221 |
! |
|
222 |
||
223 |
visitElement:anElement |
|
224 |
||
225 |
| nsStackWasNil | |
|
226 |
||
227 |
pendingNamespaceDeclarations := ''. |
|
228 |
nsStack ifNil:[ |
|
229 |
nsStackWasNil := true. |
|
230 |
nsStack := Stack new. |
|
231 |
nsStack push:(Set new addAll:(namespaces keys); yourself). |
|
232 |
] ifNotNil:[ |
|
233 |
nsStackWasNil := false |
|
234 |
]. |
|
235 |
nsStack push:(nsStack top copy). |
|
236 |
anElement elements isEmpty |
|
237 |
ifTrue:[self visitEmptyElement:anElement] |
|
238 |
ifFalse:[self visitElementWithChildren:anElement]. |
|
239 |
nsStack pop. |
|
240 |
nsStackWasNil ifTrue:[nsStack := nil]. |
|
241 |
||
242 |
"Created: / 22-04-2005 / 22:10:02 / janfrog" |
|
243 |
"Modified: / 30-05-2005 / 16:30:11 / masca" |
|
244 |
"Modified: / 15-06-2005 / 11:14:40 / janfrog" |
|
245 |
! |
|
246 |
||
247 |
visitElementWithChildren:anElement |
|
248 |
|text| |
|
249 |
||
250 |
self ensureNamespaceDeclaredFor:anElement tag. |
|
251 |
self |
|
252 |
indent; |
|
253 |
nextPutOpenElement; |
|
254 |
nextPutElementPrefix:anElement tag qualifiedName |
|
255 |
localName:anElement tag type; |
|
256 |
nextPutAttributes:anElement attributes |
|
257 |
implicitNamespace:anElement tag namespace; |
|
258 |
nextPutAll:pendingNamespaceDeclarations; |
|
259 |
nextPutCloseElement. |
|
260 |
text := anElement elements size = 1 |
|
261 |
and:[ anElement elements anyOne isText ]. |
|
262 |
text ifFalse:[ |
|
263 |
self |
|
264 |
cr; |
|
265 |
incIndent |
|
266 |
]. |
|
267 |
anElement elements do:[:e | |
|
268 |
e acceptVisitor:self |
|
269 |
]. |
|
270 |
text ifFalse:[ |
|
271 |
self |
|
272 |
decIndent; |
|
273 |
indent |
|
274 |
]. |
|
275 |
self |
|
276 |
nextPutOpenElement; |
|
277 |
nextPut:$/; |
|
278 |
nextPutElementPrefix:anElement tag asString localName:''; |
|
279 |
nextPutCloseElement; |
|
280 |
cr. |
|
281 |
||
282 |
"Created: / 22-04-2005 / 22:32:18 / janfrog" |
|
283 |
"Modified: / 09-04-2007 / 11:20:13 / janfrog" |
|
284 |
! |
|
285 |
||
286 |
visitEmptyElement:anElement |
|
287 |
self ensureNamespaceDeclaredFor:anElement tag. |
|
288 |
self |
|
289 |
indent; |
|
290 |
nextPutOpenElement; |
|
291 |
nextPutElementPrefix:anElement tag qualifier localName:anElement tag type; |
|
292 |
nextPutAttributes:anElement attributes |
|
293 |
implicitNamespace:anElement tag namespace; |
|
294 |
nextPutAll:pendingNamespaceDeclarations; |
|
295 |
nextPut:$/; |
|
296 |
nextPutCloseElement; |
|
297 |
cr. |
|
298 |
||
299 |
"Created: / 22-04-2005 / 22:29:32 / janfrog" |
|
300 |
"Modified: / 09-04-2007 / 11:20:13 / janfrog" |
|
301 |
! |
|
302 |
||
303 |
visitPI:aPI |
|
304 |
||
305 |
self |
|
306 |
indent; |
|
307 |
nextPutPI:aPI name data:aPI text; |
|
308 |
cr |
|
309 |
||
310 |
"Created: / 22-04-2005 / 22:19:16 / janfrog" |
|
311 |
"Modified: / 23-04-2005 / 09:18:21 / janfrog" |
|
312 |
! |
|
313 |
||
314 |
visitText:aText |
|
315 |
||
316 |
self |
|
317 |
"/indent; |
|
318 |
nextPutAll:aText text |
|
319 |
"/cr. |
|
320 |
||
321 |
"Created: / 22-04-2005 / 22:10:42 / janfrog" |
|
322 |
"Modified: / 03-05-2005 / 10:58:47 / janfrog" |
|
323 |
! ! |
|
324 |
||
325 |
!OldXMLWriter methodsFor:'writing'! |
|
326 |
||
327 |
cr |
|
328 |
formatOutput ifTrue:[stream cr] |
|
329 |
||
330 |
"Created: / 16-04-2005 / 20:39:25 / janfrog" |
|
331 |
"Modified: / 15-06-2005 / 19:04:59 / janfrog" |
|
332 |
! |
|
333 |
||
334 |
indent |
|
335 |
||
336 |
formatOutput ifTrue:[ |
|
337 |
stream next:depth*2 put:Character space |
|
338 |
] |
|
339 |
||
340 |
"Created: / 16-04-2005 / 20:40:01 / janfrog" |
|
341 |
"Modified: / 15-06-2005 / 19:04:50 / janfrog" |
|
342 |
! |
|
343 |
||
344 |
nextPut:aCharacter |
|
345 |
||
346 |
stream nextPut:aCharacter |
|
347 |
||
348 |
"Created: / 16-04-2005 / 20:53:34 / janfrog" |
|
349 |
! |
|
350 |
||
351 |
nextPutAll:aString |
|
352 |
||
353 |
stream nextPutAll:aString |
|
354 |
||
355 |
"Created: / 16-04-2005 / 20:41:42 / janfrog" |
|
356 |
"Modified: / 16-05-2005 / 14:29:46 / masca" |
|
357 |
! |
|
358 |
||
359 |
nextPutAttribute:anAttr implicitNamespace:anObject |
|
360 |
self ensureNamespaceDeclaredFor:anAttr tag. |
|
361 |
self |
|
362 |
nextPutAttributeName:anAttr tag qualifiedName; |
|
363 |
nextPut:$=; |
|
364 |
nextPutAttributeValue:anAttr value. |
|
365 |
||
366 |
"Created: / 09-04-2007 / 11:19:59 / janfrog" |
|
367 |
! |
|
368 |
||
369 |
nextPutAttributeName:qName |
|
370 |
||
371 |
self |
|
372 |
nextPutAll:qName |
|
373 |
||
374 |
"Created: / 17-04-2005 / 13:42:46 / janfrog" |
|
375 |
! |
|
376 |
||
377 |
nextPutAttributeValue:value |
|
378 |
||
379 |
self |
|
380 |
nextPut:$"; |
|
381 |
nextPutAll:value; |
|
382 |
nextPut:$" |
|
383 |
||
384 |
"Created: / 17-04-2005 / 13:42:57 / janfrog" |
|
385 |
"Modified: / 22-04-2005 / 22:48:24 / janfrog" |
|
386 |
! |
|
387 |
||
388 |
nextPutAttributes:anAttributes implicitNamespace:defaultNS |
|
389 |
anAttributes do:[:a | |
|
390 |
self |
|
391 |
space; |
|
392 |
nextPutAttribute:a implicitNamespace:defaultNS |
|
393 |
] |
|
394 |
||
395 |
"Created: / 09-04-2007 / 11:20:13 / janfrog" |
|
396 |
! |
|
397 |
||
398 |
nextPutCloseElement |
|
399 |
||
400 |
stream nextPut:$> |
|
401 |
||
402 |
"Created: / 17-04-2005 / 09:37:40 / janfrog" |
|
403 |
! |
|
404 |
||
405 |
nextPutComment:aString |
|
406 |
||
407 |
self |
|
408 |
nextPutAll:'<!!-- '; |
|
409 |
nextPutAll:aString; |
|
410 |
nextPutAll:' -->'. |
|
411 |
||
412 |
"Created: / 17-04-2005 / 13:37:41 / janfrog" |
|
413 |
! |
|
414 |
||
415 |
nextPutElementPrefix:prefix localName:localName |
|
416 |
||
417 |
prefix isNilOrEmptyCollection ifTrue:[ |
|
418 |
self |
|
419 |
nextPutAll:localName |
|
420 |
] ifFalse: [ |
|
421 |
self |
|
422 |
nextPutAll:prefix; |
|
423 |
nextPut:$:; |
|
424 |
nextPutAll:localName |
|
425 |
] |
|
426 |
||
427 |
"Created: / 18-10-2005 / 10:50:19 / janfrog" |
|
428 |
! |
|
429 |
||
430 |
nextPutOpenElement |
|
431 |
||
432 |
stream nextPut:$< |
|
433 |
||
434 |
"Created: / 17-04-2005 / 09:37:32 / janfrog" |
|
435 |
! |
|
436 |
||
437 |
nextPutPI:target data:data |
|
438 |
||
439 |
stream |
|
440 |
nextPut:$<; |
|
441 |
nextPut:$?; |
|
442 |
nextPutAll:target; |
|
443 |
space; |
|
444 |
nextPutAll:data; |
|
445 |
nextPut:$?; |
|
446 |
nextPut:$> |
|
447 |
||
448 |
"Created: / 17-04-2005 / 09:36:14 / janfrog" |
|
449 |
"Modified: / 15-06-2005 / 19:56:48 / janfrog" |
|
450 |
! |
|
451 |
||
452 |
nextPutXMLDecl |
|
453 |
||
454 |
self nextPutPI:'xml' data:'version="1.0" encoding="' |
|
455 |
, stream encoder nameOfEncoding |
|
456 |
, '"' |
|
457 |
||
458 |
"Created: / 17-04-2005 / 09:34:41 / janfrog" |
|
459 |
"Modified: / 15-06-2005 / 11:03:58 / janfrog" |
|
460 |
! |
|
461 |
||
462 |
space |
|
463 |
stream space |
|
464 |
||
465 |
"Created: / 16-04-2005 / 22:19:46 / janfrog" |
|
466 |
! ! |
|
467 |
||
468 |
!OldXMLWriter::ContentHandler methodsFor:'SAX2 interface'! |
|
469 |
||
470 |
characters:aString |
|
471 |
||
472 |
writer |
|
473 |
indent; |
|
474 |
nextPutAll:aString withoutSeparators; |
|
475 |
cr. |
|
476 |
||
477 |
"Created: / 17-04-2005 / 09:08:07 / janfrog" |
|
478 |
! |
|
479 |
||
480 |
comment:string |
|
481 |
||
482 |
writer |
|
483 |
indent; |
|
484 |
nextPutComment:string; |
|
485 |
cr |
|
486 |
||
487 |
"Created: / 17-04-2005 / 13:36:58 / janfrog" |
|
488 |
! |
|
489 |
||
490 |
endElement:localName namespace:namespace prefix:prefix |
|
491 |
writer |
|
492 |
decIndent; |
|
493 |
indent; |
|
494 |
nextPutOpenElement; |
|
495 |
nextPut:$/; |
|
496 |
nextPutElementPrefix:prefix localName:localName; |
|
497 |
nextPutCloseElement; |
|
498 |
cr |
|
499 |
||
500 |
"Created: / 22-04-2005 / 21:41:16 / janfrog" |
|
501 |
"Modified: / 18-10-2005 / 10:50:52 / janfrog" |
|
502 |
! |
|
503 |
||
504 |
processingInstruction:target data:data |
|
505 |
||
506 |
writer |
|
507 |
indent; |
|
508 |
nextPutPI:target data:data; |
|
509 |
cr |
|
510 |
||
511 |
"Created: / 17-04-2005 / 13:36:38 / janfrog" |
|
512 |
! |
|
513 |
||
514 |
startDocument |
|
515 |
||
516 |
writer |
|
517 |
nextPutXMLDecl; |
|
518 |
cr. |
|
519 |
||
520 |
"Created: / 17-04-2005 / 09:33:58 / janfrog" |
|
521 |
! |
|
522 |
||
523 |
startElement:localName namespace:namespace prefix:prefix attributes:anAttributes |
|
524 |
writer |
|
525 |
indent; |
|
526 |
nextPutOpenElement; |
|
527 |
nextPutElementPrefix:prefix localName:localName; |
|
528 |
nextPutAttributes:anAttributes implicitNamespace:namespace; |
|
529 |
nextPutCloseElement; |
|
530 |
cr; |
|
531 |
incIndent. |
|
532 |
||
533 |
"/nextPutAll:' <!!-- ns:',namespace,' -->' |
|
534 |
||
535 |
"Created: / 22-04-2005 / 21:40:32 / janfrog" |
|
536 |
"Modified: / 09-04-2007 / 11:20:13 / janfrog" |
|
537 |
! ! |
|
538 |
||
539 |
!OldXMLWriter::ContentHandler methodsFor:'accessing'! |
|
540 |
||
541 |
writer |
|
542 |
^ writer |
|
543 |
||
544 |
"Created: / 16-04-2005 / 20:37:10 / janfrog" |
|
545 |
! |
|
546 |
||
547 |
writer:something |
|
548 |
writer := something. |
|
549 |
||
550 |
"Created: / 16-04-2005 / 20:37:10 / janfrog" |
|
551 |
! ! |
|
552 |
||
553 |
!OldXMLWriter class methodsFor:'documentation'! |
|
554 |
||
555 |
version |
|
556 |
^ '$Header: /opt/data/cvs/stx/goodies/xmlsuite/XMLv2__OldXMLWriter.st,v 1.3 2007-04-10 08:36:13 vranyj1 Exp $' |
|
557 |
! ! |