factor/libs/xml/writer.factor

85 lines
2.1 KiB
Factor
Raw Normal View History

2006-11-09 16:01:57 -05:00
! Copyright (C) 2005, 2006 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
IN: xml
2006-11-23 19:37:41 -05:00
USING: hashtables kernel math namespaces sequences strings
io generic ;
2006-11-09 16:01:57 -05:00
2006-11-23 19:37:41 -05:00
GENERIC: write-str-elem ( elem -- )
2006-11-29 22:03:00 -05:00
: chars>entities ( str -- str )
2006-11-23 19:37:41 -05:00
#! Convert <, >, &, ' and " to HTML entities.
2006-11-29 22:03:00 -05:00
[ [ dup entities hash [ % ] [ , ] ?if ] each ] "" make ;
2006-11-23 19:37:41 -05:00
M: string write-str-elem
2006-11-29 22:03:00 -05:00
chars>entities write ;
2006-11-23 19:37:41 -05:00
M: entity write-str-elem
CHAR: & write1 entity-name write CHAR: ; write1 ;
M: reference write-str-elem
CHAR: % write1 reference-name write CHAR: ; write1 ;
2006-11-09 16:01:57 -05:00
2006-11-26 21:48:21 -05:00
UNION: str-elem string entity reference ;
2006-11-09 16:01:57 -05:00
: print-name ( name -- )
2006-11-23 19:37:41 -05:00
dup name-space dup "" = [ drop ]
[ write CHAR: : write1 ] if
name-tag write ;
2006-11-09 16:01:57 -05:00
: print-props ( hash -- )
[
2006-11-23 19:37:41 -05:00
" " write swap print-name "=\"" write
[ write-str-elem ] each "\"" write
2006-11-09 16:01:57 -05:00
] hash-each ;
2006-11-23 19:37:41 -05:00
GENERIC: (xml>string) ( object -- )
2006-11-09 16:01:57 -05:00
2006-11-26 21:48:21 -05:00
M: str-elem (xml>string) ! string element
2006-11-23 19:37:41 -05:00
write-str-elem ;
2006-11-09 16:01:57 -05:00
M: contained-tag (xml>string)
2006-11-23 19:37:41 -05:00
CHAR: < write1
dup print-name
2006-11-09 16:01:57 -05:00
tag-props print-props
2006-11-23 19:37:41 -05:00
"/>" write ;
2006-11-09 16:01:57 -05:00
M: open-tag (xml>string)
2006-11-23 19:37:41 -05:00
CHAR: < write1
dup print-name
2006-11-09 16:01:57 -05:00
dup tag-props print-props
2006-11-23 19:37:41 -05:00
CHAR: > write1
2006-11-09 16:01:57 -05:00
dup tag-children [ (xml>string) ] each
2006-11-23 19:37:41 -05:00
"</" write print-name CHAR: > write1 ;
2006-11-09 16:01:57 -05:00
M: comment (xml>string)
2006-11-23 19:37:41 -05:00
"<!--" write comment-text write "-->" write ;
2006-11-09 16:01:57 -05:00
M: directive (xml>string)
2006-11-23 19:37:41 -05:00
"<!" write directive-text write CHAR: > write1 ;
2006-11-09 16:01:57 -05:00
M: instruction (xml>string)
2006-11-23 19:37:41 -05:00
"<?" write instruction-text write "?>" write ;
2006-11-18 01:01:11 -05:00
2006-11-09 16:01:57 -05:00
: xml-preamble ( xml -- )
2006-11-23 19:37:41 -05:00
"<?xml version=\"" write dup prolog-version write
"\" encoding=\"" write dup prolog-encoding write
"\" standalone=\"" write
prolog-standalone "yes" "no" ? write
"\"?>" write ;
: write-xml ( xml-doc -- )
dup xml-doc-prolog xml-preamble
dup xml-doc-before [ (xml>string) ] each
dup delegate (xml>string)
xml-doc-after [ (xml>string) ] each ;
2006-11-09 16:01:57 -05:00
: print-xml ( xml-doc -- )
write-xml terpri ;
2006-11-09 16:01:57 -05:00
: xml>string ( xml-doc -- string )
2006-11-23 19:37:41 -05:00
[ write-xml ] string-out ;
2006-11-09 16:01:57 -05:00
: xml-reprint ( string -- )
string>xml print-xml ;
2006-11-09 16:01:57 -05:00