]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/data/data.factor
factor: trim using lists
[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: accessors arrays assocs combinators
4 combinators.short-circuit delegate delegate.protocols kernel
5 sequences slots strings vectors words ;
6 IN: xml.data
7
8 TUPLE: interpolated var ;
9 C: <interpolated> interpolated
10
11 TUPLE: name
12     { space maybe{ string } }
13     { main string }
14     { url maybe{ string } } ;
15 C: <name> name
16
17 : ?= ( object/f object/f -- ? )
18     2dup and [ = ] [ 2drop t ] if ;
19
20 : names-match? ( name1 name2 -- ? )
21     {
22         [ [ space>> ] bi@ ?= ]
23         [ [ url>> ] bi@ ?= ]
24         [ [ main>> ] bi@ ?= ]
25     } 2&& ;
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 <vector> <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>> remove-nth! drop ] [ 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 TUPLE: entity-decl < directive
101     { name string }
102     { def string }
103     { pe? boolean } ;
104 C: <entity-decl> entity-decl
105
106 TUPLE: system-id { system-literal string } ;
107 C: <system-id> system-id
108
109 TUPLE: public-id { pubid-literal string } { system-literal string } ;
110 C: <public-id> public-id
111
112 UNION: id system-id public-id ;
113
114 TUPLE: dtd
115     { directives sequence }
116     { entities assoc }
117     { parameter-entities assoc } ;
118 C: <dtd> dtd
119
120 TUPLE: doctype-decl < directive
121     { name string }
122     { external-id maybe{ id } }
123     { internal-subset maybe{ dtd } } ;
124 C: <doctype-decl> doctype-decl
125
126 TUPLE: notation-decl < directive
127     { name string }
128     { id string } ;
129 C: <notation-decl> notation-decl
130
131 TUPLE: instruction { text string } ;
132 C: <instruction> instruction
133
134 TUPLE: prolog
135     { version string }
136     { encoding string }
137     { standalone boolean } ;
138 C: <prolog> prolog
139
140 TUPLE: tag
141     { name name }
142     { attrs attrs }
143     { children sequence } ;
144
145 : <tag> ( name attrs children -- tag )
146     [ assure-name ] [ T{ attrs } assoc-like ] [ ] tri*
147     tag boa ;
148
149 : attr ( tag/xml name -- string )
150     swap attrs>> at ;
151
152 : set-attr ( tag/xml value name -- )
153     rot attrs>> set-at ;
154
155 ! They also follow the sequence protocol (for children)
156 CONSULT: sequence-protocol tag children>> ;
157 INSTANCE: tag sequence
158
159 ! They also follow the assoc protocol (for attributes)
160 CONSULT: assoc-protocol tag attrs>> ;
161 INSTANCE: tag assoc
162
163 CONSULT: name tag name>> ;
164
165 M: tag like
166     over tag? [ drop ] [
167         [ name>> ] keep attrs>>
168         rot dup [ V{ } like ] when <tag>
169     ] if ;
170
171 MACRO: clone-slots ( class -- quot )
172     [
173         "slots" word-prop
174         [ name>> reader-word '[ _ execute clone ] ] map
175         '[ _ cleave ]
176     ] [ '[ _ boa ] ] bi compose ;
177
178 M: tag clone
179     tag clone-slots ;
180
181 TUPLE: xml
182     { prolog prolog }
183     { before sequence }
184     { body tag }
185     { after sequence } ;
186 C: <xml> xml
187
188 CONSULT: sequence-protocol xml body>> ;
189 INSTANCE: xml sequence
190
191 CONSULT: tag xml body>> ;
192
193 CONSULT: name xml body>> ;
194
195 <PRIVATE
196 : tag>xml ( xml tag -- newxml )
197     [ [ prolog>> ] [ before>> ] [ after>> ] tri ] dip
198     swap <xml> ;
199
200 : sequence>xml ( xml seq -- newxml )
201     over body>> like tag>xml ;
202 PRIVATE>
203
204 M: xml clone
205    xml clone-slots ;
206
207 M: xml like
208     swap dup xml? [ nip ] [
209         dup tag? [ tag>xml ] [ sequence>xml ] if
210     ] if ;
211
212 ! tag with children=f is contained
213 : <contained-tag> ( name attrs -- tag )
214     f <tag> ;
215
216 PREDICATE: contained-tag < tag children>> empty? ;
217 PREDICATE: open-tag < tag children>> empty? not ;
218
219 TUPLE: unescaped string ;
220 C: <unescaped> unescaped
221
222 UNION: xml-data
223     tag comment string directive instruction unescaped ;
224
225 TUPLE: xml-chunk seq ;
226 C: <xml-chunk> xml-chunk
227
228 CONSULT: sequence-protocol xml-chunk seq>> ;
229 INSTANCE: xml-chunk sequence