1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io io.streams.string io.files kernel math namespaces
4 prettyprint sequences arrays generic strings vectors
5 xml.char-classes xml.data xml.errors xml.tokenize xml.writer
6 xml.utilities state-parser assocs ascii io.encodings.utf8
7 accessors xml.backend ;
10 ! -- Overall parser with data tree
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 ;
26 xml-stack get V{ { f V{ "" } } } =
27 [ <bad-prolog> throw ] unless drop ;
29 M: instruction process
30 xml-stack get length 1 =
31 [ <bad-instruction> throw ] unless
35 xml-stack get dup length 1 =
36 swap first second [ tag? ] contains? not and
37 [ <bad-directive> throw ] unless
41 [ contained-name ] keep contained-attrs
42 <contained-tag> add-child ;
44 M: opener process push-xml ;
46 : check-closer ( name opener -- name opener )
47 dup [ <unopened> throw ] unless
49 [ opener-name swap <mismatched> throw ] unless ;
52 closer-name pop-xml first2
53 >r check-closer opener-attrs r>
56 : init-xml-stack ( -- )
57 V{ } clone xml-stack set f push-xml ;
59 : default-prolog ( -- prolog )
60 "1.0" "UTF-8" f <prolog> ;
63 default-prolog prolog-data set ;
66 reset-prolog init-xml-stack init-ns-stack ;
68 : assert-blanks ( seq pre? -- )
69 swap [ string? ] filter
72 [ drop ] [ swap <pre/post-content> throw ] if
75 : no-pre/post ( pre post -- pre post/* )
76 ! this does *not* affect the contents of the stack
77 >r dup t assert-blanks r>
80 : no-post-tags ( post -- post/* )
81 ! this does *not* affect the contents of the stack
82 dup [ tag? ] contains? [ <multitags> throw ] when ;
84 : assure-tags ( seq -- seq )
85 ! this does *not* affect the contents of the stack
86 [ <notags> throw ] unless* ;
88 : make-xml-doc ( prolog seq -- xml-doc )
90 >r assure-tags cut rest
91 no-pre/post no-post-tags
98 TUPLE: pull-xml scope ;
99 : <pull-xml> ( -- pull-xml )
101 input-stream [ ] change ! bring var in this scope
102 init-parser reset-prolog init-ns-stack
105 { set-pull-xml-scope } pull-xml construct ;
107 : pull-event ( pull -- xml-event/f )
109 text-now? get [ parse-text f ] [
110 get-char [ make-tag t ] [ f f ] if
115 xml-stack get length 1 = ;
117 : (pull-elem) ( pull -- xml-elem/f )
118 dup pull-event dup closer? done? and [ nip ] [
120 [ drop xml-stack get first second ]
124 : pull-elem ( pull -- xml-elem/f )
125 [ init-xml-stack (pull-elem) ] with-scope ;
127 : call-under ( quot object -- quot )
128 swap dup slip ; inline
130 : sax-loop ( quot -- ) ! quot: xml-elem --
131 parse-text call-under
132 get-char [ make-tag call-under sax-loop ]
135 : sax ( stream quot -- ) ! quot: xml-elem --
137 reset-prolog init-ns-stack
138 prolog-data get call-under
140 ] state-parse ; inline
143 [ process ] sax-loop ; inline
145 : (xml-chunk) ( stream -- prolog seq )
148 done? [ <unclosed> throw ] unless
149 xml-stack get first second
153 : read-xml ( stream -- xml )
154 #! Produces a tree of XML nodes
155 (xml-chunk) make-xml-doc ;
157 : xml-chunk ( stream -- seq )
160 : string>xml ( string -- xml )
161 <string-reader> read-xml ;
163 : file>xml ( filename -- xml )
164 ! Autodetect encoding!
165 utf8 <file-reader> read-xml ;
167 : xml-reprint ( string -- )
168 string>xml print-xml ;