1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://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?
27 dup space>> dup ns-stack get assoc-stack
28 [ ] [ 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? ]
58 : valid-name-start? ( str -- ? )
59 [ f ] [ version-1.0? swap first name-start? ] if-empty ;
61 : maybe-name ( space main -- name/f )
63 [ drop valid-name-start? ]
64 [ nip valid-name-start? ]
65 } 2&& [ f <name> ] [ 2drop f ] if ;
67 : prefixed-name ( str -- name/f )
69 CHAR: : 2over 1 + swap index-from
71 [ [ head ] [ 1 + tail ] 2bi maybe-name ]
75 : interpret-name ( str -- name )
76 dup prefixed-name [ ] [ <simple-name> ] ?if ;
80 : take-name ( -- string )
81 version-1.0? '[ _ swap name-char? not ] take-until ;
83 : parse-name ( -- name )
84 take-name interpret-name ;
86 : parse-name-starting ( string -- name )
87 take-name append interpret-name ;
89 : take-system-id ( -- system-id )
90 parse-quote <system-id> ;
92 : take-public-id ( -- public-id )
93 parse-quote parse-quote <public-id> ;
95 : (take-external-id) ( token -- external-id )
97 { "SYSTEM" [ take-system-id ] }
98 { "PUBLIC" [ take-public-id ] }
102 : take-word ( -- string )
103 [ blank? ] take-until ;
105 : take-external-id ( -- external-id )
106 take-word (take-external-id) ;