1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel namespaces accessors xml.tokenize xml.data assocs
4 xml.errors xml.char-classes combinators.short-circuit splitting
5 fry xml.state sequences combinators ascii ;
8 ! XML namespace processing: ns = namespace
10 ! A stack of hashtables
13 : attrs>ns ( attrs-alist -- hash )
14 ! this should check to make sure URIs are valid
17 swap dup space>> "xmlns" =
20 T{ name f "" "xmlns" f } names-match?
21 [ "" set ] [ drop ] if
24 ] { } make-assoc f like ;
27 dup space>> dup ns-stack get assoc-stack
28 [ nip ] [ nonexist-ns ] if* >>url drop ;
36 : init-ns-stack ( -- )
38 { "xml" "http://www.w3.org/XML/1998/namespace" }
39 { "xmlns" "http://www.w3.org/2000/xmlns" }
44 : tag-ns ( name attrs-alist -- name attrs )
46 [ dup add-ns ] dip dup [ drop add-ns ] assoc-each <attrs> ;
48 : valid-name? ( str -- ? )
52 [ rest-slice [ name-char? ] with all? ]
56 : prefixed-name ( str -- name/f )
57 ":" split dup length 2 = [
58 [ [ valid-name? ] all? ]
59 [ first2 f <name> ] bi and
62 : interpret-name ( str -- name )
63 dup prefixed-name [ ] [
65 [ <simple-name> ] [ bad-name ] if
68 : take-name ( -- string )
69 version-1.0? '[ _ swap name-char? not ] take-until ;
71 : parse-name ( -- name )
72 take-name interpret-name ;
74 : parse-name-starting ( string -- name )
75 take-name append interpret-name ;
77 : take-system-id ( -- system-id )
78 parse-quote <system-id> ;
80 : take-public-id ( -- public-id )
81 parse-quote parse-quote <public-id> ;
83 : (take-external-id) ( token -- external-id )
85 { "SYSTEM" [ take-system-id ] }
86 { "PUBLIC" [ take-public-id ] }
90 : take-word ( -- string )
91 [ blank? ] take-until ;
93 : take-external-id ( -- external-id )
94 take-word (take-external-id) ;