]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/data/data.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / xml / data / data.factor
1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.private assocs arrays
4 delegate.protocols delegate vectors accessors multiline
5 macros words quotations combinators slots fry ;
6 IN: xml.data
7
8 TUPLE: name space main url ;
9 C: <name> name
10
11 : ?= ( object/f object/f -- ? )
12     2dup and [ = ] [ 2drop t ] if ;
13
14 : names-match? ( name1 name2 -- ? )
15     [ [ space>> ] bi@ ?= ]
16     [ [ url>> ] bi@ ?= ]
17     [ [ main>> ] bi@ ?= ] 2tri and and ;
18
19 : <simple-name> ( string -- name )
20     f swap f <name> ;
21
22 : assure-name ( string/name -- name )
23     dup name? [ <simple-name> ] unless ;
24
25 TUPLE: opener name attrs ;
26 C: <opener> opener
27
28 TUPLE: closer name ;
29 C: <closer> closer
30
31 TUPLE: contained name attrs ;
32 C: <contained> contained
33
34 TUPLE: comment text ;
35 C: <comment> comment
36
37 TUPLE: directive ;
38
39 TUPLE: element-decl < directive name content-spec ;
40 C: <element-decl> element-decl
41
42 TUPLE: attlist-decl < directive name att-defs ;
43 C: <attlist-decl> attlist-decl
44
45 TUPLE: entity-decl < directive name def ;
46 C: <entity-decl> entity-decl
47
48 TUPLE: system-id system-literal ;
49 C: <system-id> system-id
50
51 TUPLE: public-id pubid-literal system-literal ;
52 C: <public-id> public-id
53
54 TUPLE: doctype-decl < directive name external-id internal-subset ;
55 C: <doctype-decl> doctype-decl
56
57 TUPLE: instruction text ;
58 C: <instruction> instruction
59
60 TUPLE: prolog version encoding standalone ;
61 C: <prolog> prolog
62
63 TUPLE: attrs alist ;
64 C: <attrs> attrs
65
66 : attr@ ( key alist -- index {key,value} )
67     [ assure-name ] dip alist>>
68     [ first names-match? ] with find ;
69
70 M: attrs at*
71     attr@ nip [ second t ] [ f f ] if* ;
72 M: attrs set-at
73     2dup attr@ nip [
74         2nip set-second
75     ] [
76         [ assure-name swap 2array ] dip
77         [ alist>> ?push ] keep (>>alist)
78     ] if* ;
79
80 M: attrs assoc-size alist>> length ;
81 M: attrs new-assoc drop V{ } new-sequence <attrs> ;
82 M: attrs >alist alist>> ;
83
84 : >attrs ( assoc -- attrs )
85     dup [
86         V{ } assoc-clone-like
87         [ [ assure-name ] dip ] assoc-map
88     ] when <attrs> ;
89 M: attrs assoc-like
90     drop dup attrs? [ >attrs ] unless ;
91
92 M: attrs clear-assoc
93     f >>alist drop ;
94 M: attrs delete-at
95     tuck attr@ drop [ swap alist>> delete-nth ] [ drop ] if* ;
96
97 M: attrs clone
98     alist>> clone <attrs> ;
99
100 INSTANCE: attrs assoc
101
102 TUPLE: tag name attrs children ;
103
104 : <tag> ( name attrs children -- tag )
105     [ assure-name ] [ T{ attrs } assoc-like ] [ ] tri*
106     tag boa ;
107
108 ! For convenience, tags follow the assoc protocol too (for attrs)
109 CONSULT: assoc-protocol tag attrs>> ;
110 INSTANCE: tag assoc
111
112 ! They also follow the sequence protocol (for children)
113 CONSULT: sequence-protocol tag children>> ;
114 INSTANCE: tag sequence
115
116 CONSULT: name tag name>> ;
117
118 M: tag like
119     over tag? [ drop ] [
120         [ name>> ] keep attrs>>
121         rot dup [ V{ } like ] when <tag>
122     ] if ;
123
124 MACRO: clone-slots ( class -- tuple )
125     [
126         "slots" word-prop
127         [ name>> reader-word '[ _ execute clone ] ] map
128         '[ _ cleave ]
129     ] [ '[ _ boa ] ] bi compose ;
130
131 M: tag clone
132     tag clone-slots ;
133
134 TUPLE: xml prolog before body after ;
135 C: <xml> xml
136
137 CONSULT: sequence-protocol xml body>> ;
138 INSTANCE: xml sequence
139
140 CONSULT: assoc-protocol xml body>> ;
141 INSTANCE: xml assoc
142
143 CONSULT: tag xml body>> ;
144
145 CONSULT: name xml body>> ;
146
147 <PRIVATE
148 : tag>xml ( xml tag -- newxml )
149     [ [ prolog>> ] [ before>> ] [ after>> ] tri ] dip
150     swap <xml> ;
151
152 : seq>xml ( xml seq -- newxml )
153     over body>> like tag>xml ;
154 PRIVATE>
155
156 M: xml clone
157    xml clone-slots ;
158
159 M: xml like
160     swap dup xml? [ nip ] [
161         dup tag? [ tag>xml ] [ seq>xml ] if
162     ] if ;
163
164 ! tag with children=f is contained
165 : <contained-tag> ( name attrs -- tag )
166     f <tag> ;
167
168 PREDICATE: contained-tag < tag children>> not ;
169 PREDICATE: open-tag < tag children>> ;