1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors ascii assocs combinators
4 combinators.short-circuit kernel make math namespaces sequences
5 xml.char-classes xml.data xml.errors xml.state xml.tokenize ;
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?
28 [ ns-stack get assoc-stack ]
29 [ nonexist-ns ] ?unless >>url drop ;
37 : init-ns-stack ( -- )
39 { "xml" "https://www.w3.org/XML/1998/namespace" }
40 { "xmlns" "https://www.w3.org/2000/xmlns" }
45 : tag-ns ( name attrs-alist -- name attrs )
47 [ dup add-ns ] dip dup [ drop add-ns ] assoc-each <attrs> ;
49 : valid-name? ( str -- ? )
53 [ rest-slice [ name-char? ] with all? ]
59 : valid-name-start? ( str -- ? )
60 [ f ] [ version-1.0? swap first name-start? ] if-empty ;
62 : maybe-name ( space main -- name/f )
64 [ drop valid-name-start? ]
65 [ nip valid-name-start? ]
66 } 2&& [ f <name> ] [ 2drop f ] if ;
68 : prefixed-name ( str -- name/f )
70 CHAR: : 2over 1 + swap index-from
72 [ [ head ] [ 1 + tail ] 2bi maybe-name ]
76 : interpret-name ( str -- name )
77 dup prefixed-name [ ] [ <simple-name> ] ?if ;
81 : take-name ( -- string )
82 version-1.0? '[ _ swap name-char? not ] take-until ;
84 : parse-name ( -- name )
85 take-name interpret-name ;
87 : parse-name-starting ( string -- name )
88 take-name append interpret-name ;
90 : take-system-id ( -- system-id )
91 parse-quote <system-id> ;
93 : take-public-id ( -- public-id )
94 parse-quote parse-quote <public-id> ;
96 : (take-external-id) ( token -- external-id )
98 { "SYSTEM" [ take-system-id ] }
99 { "PUBLIC" [ take-public-id ] }
103 : take-word ( -- string )
104 [ blank? ] take-until ;
106 : take-external-id ( -- external-id )
107 take-word (take-external-id) ;