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