1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays io io.encodings.binary io.files
4 io.streams.string kernel namespaces sequences strings io.encodings.utf8
5 xml.data xml.errors xml.elements ascii xml.entities
6 xml.writer xml.state xml.autoencoding assocs xml.tokenize
7 combinators.short-circuit xml.name ;
12 : add-child ( object -- )
13 xml-stack get peek second push ;
15 : push-xml ( object -- )
16 V{ } clone 2array xml-stack get push ;
18 : pop-xml ( -- object )
21 GENERIC: process ( object -- )
23 M: object process add-child ;
27 { V{ { f V{ "" } } } V{ { f V{ } } } } member?
28 [ bad-prolog ] unless drop ;
30 : before-main? ( -- ? )
33 [ first second [ tag? ] any? not ]
37 before-main? [ misplaced-directive ] unless add-child ;
40 [ name>> ] [ attrs>> ] bi
41 <contained-tag> add-child ;
43 M: opener process push-xml ;
45 : check-closer ( name opener -- name opener )
46 dup [ unopened ] unless
48 [ name>> swap mismatched ] unless ;
52 [ check-closer attrs>> ] dip
55 : init-xml-stack ( -- )
56 V{ } clone xml-stack set
59 : default-prolog ( -- prolog )
60 "1.0" "UTF-8" f <prolog> ;
64 extra-entities [ H{ } assoc-like ] change ;
66 : assert-blanks ( seq pre? -- )
67 swap [ string? ] filter
70 [ drop ] [ swap pre/post-content ] if
73 : no-pre/post ( pre post -- pre post/* )
74 ! this does *not* affect the contents of the stack
75 [ dup t assert-blanks ] [ dup f assert-blanks ] bi* ;
77 : no-post-tags ( post -- post/* )
78 ! this does *not* affect the contents of the stack
79 dup [ tag? ] any? [ multitags ] when ;
81 : assure-tags ( seq -- seq )
82 ! this does *not* affect the contents of the stack
85 : get-prolog ( seq -- prolog )
86 first dup prolog? [ drop default-prolog ] unless ;
88 : make-xml-doc ( seq -- xml-doc )
91 [ assure-tags cut rest no-pre/post no-post-tags ] dip
100 TUPLE: pull-xml scope ;
101 : <pull-xml> ( -- pull-xml )
103 input-stream [ ] change ! bring var in this scope
104 init-xml text-now? on
107 ! pull-xml needs to call start-document somewhere
109 : pull-event ( pull -- xml-event/f )
111 text-now? get [ parse-text f ] [
112 get-char [ make-tag t ] [ f f ] if
119 xml-stack get length 1 = ;
121 : (pull-elem) ( pull -- xml-elem/f )
122 dup pull-event dup closer? done? and [ nip ] [
124 [ drop xml-stack get first second ]
130 : pull-elem ( pull -- xml-elem/f )
131 [ init-xml-stack (pull-elem) ] with-scope ;
135 : call-under ( quot object -- quot )
136 swap dup slip ; inline
138 : xml-loop ( quot: ( xml-elem -- ) -- )
139 parse-text call-under
140 get-char [ make-tag call-under xml-loop ]
141 [ drop ] if ; inline recursive
143 : read-seq ( stream quot n -- seq )
146 init-xml init-xml-stack
149 done? [ unclosed ] unless
150 xml-stack get first second
151 ] with-state ; inline
155 : each-element ( stream quot: ( xml-elem -- ) -- )
158 start-document [ call-under ] when*
160 ] with-state ; inline
162 : read-xml ( stream -- xml )
163 [ start-document [ process ] when* ]
164 0 read-seq make-xml-doc ;
166 : read-xml-chunk ( stream -- seq )
167 [ check ] 1 read-seq <xml-chunk> ;
169 : string>xml ( string -- xml )
170 <string-reader> [ check ] 0 read-seq make-xml-doc ;
172 : string>xml-chunk ( string -- xml )
173 <string-reader> read-xml-chunk ;
175 : file>xml ( filename -- xml )
176 binary <file-reader> read-xml ;
178 : read-dtd ( stream -- dtd )
180 H{ } clone extra-entities set
184 : file>dtd ( filename -- dtd )
185 utf8 <file-reader> read-dtd ;
187 : string>dtd ( string -- dtd )
188 <string-reader> read-dtd ;