]> 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, 2009 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 strings ;
6 IN: xml.data
7
8 TUPLE: interpolated var ;
9 C: <interpolated> interpolated
10
11 UNION: nullable-string string POSTPONE: f ;
12
13 TUPLE: name
14     { space nullable-string }
15     { main string }
16     { url nullable-string } ;
17 C: <name> name
18
19 : ?= ( object/f object/f -- ? )
20     2dup and [ = ] [ 2drop t ] if ;
21
22 : names-match? ( name1 name2 -- ? )
23     [ [ space>> ] bi@ ?= ]
24     [ [ url>> ] bi@ ?= ]
25     [ [ main>> ] bi@ ?= ] 2tri and and ;
26
27 : <simple-name> ( string -- name )
28     "" swap f <name> ;
29
30 : <null-name> ( string -- name )
31     f swap f <name> ;
32
33 : assure-name ( string/name -- name )
34     dup name? [ <null-name> ] unless ;
35
36 TUPLE: attrs { alist sequence } ;
37 C: <attrs> attrs
38
39 : attr@ ( key alist -- index {key,value} )
40     [ assure-name ] dip alist>>
41     [ first names-match? ] with find ;
42
43 M: attrs at*
44     attr@ nip [ second t ] [ f f ] if* ;
45 M: attrs set-at
46     2dup attr@ nip [
47         2nip set-second
48     ] [
49         [ assure-name swap 2array ] dip
50         [ alist>> ?push ] keep (>>alist)
51     ] if* ;
52
53 M: attrs assoc-size alist>> length ;
54 M: attrs new-assoc drop V{ } new-sequence <attrs> ;
55 M: attrs >alist alist>> ;
56
57 : >attrs ( assoc -- attrs )
58     dup [
59         V{ } assoc-clone-like
60         [ [ assure-name ] dip ] assoc-map
61     ] when <attrs> ;
62 M: attrs assoc-like
63     drop dup attrs? [ >attrs ] unless ;
64
65 M: attrs clear-assoc
66     f >>alist drop ;
67 M: attrs delete-at
68     [ nip ] [ attr@ drop ] 2bi
69     [ swap alist>> delete-nth ] [ drop ] if* ;
70
71 M: attrs clone
72     alist>> clone <attrs> ;
73
74 INSTANCE: attrs assoc
75
76 TUPLE: opener { name name } { attrs attrs } ;
77 C: <opener> opener
78
79 TUPLE: closer { name name } ;
80 C: <closer> closer
81
82 TUPLE: contained { name name } { attrs attrs } ;
83 C: <contained> contained
84
85 TUPLE: comment { text string } ;
86 C: <comment> comment
87
88 TUPLE: directive ;
89
90 TUPLE: element-decl < directive
91     { name string }
92     { content-spec string } ;
93 C: <element-decl> element-decl
94
95 TUPLE: attlist-decl < directive
96     { name string }
97     { att-defs string } ;
98 C: <attlist-decl> attlist-decl
99
100 UNION: boolean t POSTPONE: f ;
101
102 TUPLE: entity-decl < directive
103     { name string }
104     { def string }
105     { pe? boolean } ;
106 C: <entity-decl> entity-decl
107
108 TUPLE: system-id { system-literal string } ;
109 C: <system-id> system-id
110
111 TUPLE: public-id { pubid-literal string } { system-literal string } ;
112 C: <public-id> public-id
113
114 UNION: id system-id public-id POSTPONE: f ;
115
116 TUPLE: dtd
117     { directives sequence }
118     { entities assoc }
119     { parameter-entities assoc } ;
120 C: <dtd> dtd
121
122 UNION: dtd/f dtd POSTPONE: f ;
123
124 TUPLE: doctype-decl < directive
125     { name string }
126     { external-id id }
127     { internal-subset dtd/f } ;
128 C: <doctype-decl> doctype-decl
129
130 TUPLE: notation-decl < directive
131     { name string }
132     { id string } ;
133 C: <notation-decl> notation-decl
134
135 TUPLE: instruction { text string } ;
136 C: <instruction> instruction
137
138 TUPLE: prolog
139     { version string }
140     { encoding string }
141     { standalone boolean } ;
142 C: <prolog> prolog
143
144 TUPLE: tag
145     { name name }
146     { attrs attrs }
147     { children sequence } ;
148
149 : <tag> ( name attrs children -- tag )
150     [ assure-name ] [ T{ attrs } assoc-like ] [ ] tri*
151     tag boa ;
152
153 ! For convenience, tags follow the assoc protocol too (for attrs)
154 CONSULT: assoc-protocol tag attrs>> ;
155 INSTANCE: tag assoc
156
157 ! They also follow the sequence protocol (for children)
158 CONSULT: sequence-protocol tag children>> ;
159 INSTANCE: tag sequence
160
161 CONSULT: name tag name>> ;
162
163 M: tag like
164     over tag? [ drop ] [
165         [ name>> ] keep attrs>>
166         rot dup [ V{ } like ] when <tag>
167     ] if ;
168
169 MACRO: clone-slots ( class -- tuple )
170     [
171         "slots" word-prop
172         [ name>> reader-word '[ _ execute clone ] ] map
173         '[ _ cleave ]
174     ] [ '[ _ boa ] ] bi compose ;
175
176 M: tag clone
177     tag clone-slots ;
178
179 TUPLE: xml
180     { prolog prolog }
181     { before sequence }
182     { body tag }
183     { after sequence } ;
184 C: <xml> xml
185
186 CONSULT: sequence-protocol xml body>> ;
187 INSTANCE: xml sequence
188
189 CONSULT: assoc-protocol xml body>> ;
190 INSTANCE: xml assoc
191
192 CONSULT: tag xml body>> ;
193
194 CONSULT: name xml body>> ;
195
196 <PRIVATE
197 : tag>xml ( xml tag -- newxml )
198     [ [ prolog>> ] [ before>> ] [ after>> ] tri ] dip
199     swap <xml> ;
200
201 : seq>xml ( xml seq -- newxml )
202     over body>> like tag>xml ;
203 PRIVATE>
204
205 M: xml clone
206    xml clone-slots ;
207
208 M: xml like
209     swap dup xml? [ nip ] [
210         dup tag? [ tag>xml ] [ seq>xml ] if
211     ] if ;
212
213 ! tag with children=f is contained
214 : <contained-tag> ( name attrs -- tag )
215     f <tag> ;
216
217 PREDICATE: contained-tag < tag children>> not ;
218 PREDICATE: open-tag < tag children>> ;
219
220 UNION: xml-data
221     tag comment string directive instruction ;