1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: kernel namespaces sequences words io assocs
\r
4 quotations strings parser arrays xml.data xml.writer debugger
\r
8 ! * System for words specialized on tag names
\r
10 TUPLE: process-missing process tag ;
\r
11 M: process-missing error.
\r
13 dup process-missing-tag print-name
\r
14 "> not implemented on process process " write
\r
15 process-missing-process word-name print ;
\r
17 : run-process ( tag word -- )
\r
18 2dup "xtable" word-prop
\r
19 >r dup name-tag r> at* [ 2nip call ] [
\r
20 drop \ process-missing construct-boa throw
\r
25 dup H{ } clone "xtable" set-word-prop
\r
26 dup [ run-process ] curry define-compound ; parsing
\r
31 swap "xtable" word-prop
\r
32 rot "/" split [ >r 2dup r> swap set-at ] each 2drop ;
\r
36 ! * Common utility functions
\r
38 : build-tag* ( items name -- tag )
\r
39 assure-name swap >r f r> <tag> ;
\r
41 : build-tag ( item name -- tag )
\r
42 >r 1array r> build-tag* ;
\r
44 : standard-prolog ( -- prolog )
\r
45 T{ prolog f "1.0" "iso-8859-1" f } ;
\r
47 : build-xml ( tag -- xml )
\r
48 standard-prolog { } rot { } <xml> ;
\r
50 : children>string ( tag -- string )
\r
52 dup [ string? ] all?
\r
53 [ "XML tag unexpectedly contains non-text children" throw ] unless
\r
56 : children-tags ( tag -- sequence )
\r
57 tag-children [ tag? ] subset ;
\r
59 : first-child-tag ( tag -- tag )
\r
60 tag-children [ tag? ] find nip ;
\r
62 ! * Utilities for searching through XML documents
\r
63 ! These all work from the outside in, top to bottom.
\r
65 : with-delegate ( object quot -- object )
\r
66 over clone >r >r delegate r> call r>
\r
67 [ set-delegate ] keep ; inline
\r
69 GENERIC# xml-each 1 ( quot tag -- ) inline
\r
72 swap tag-children [ swap xml-each ] curry* each ;
\r
76 >r delegate r> xml-each ;
\r
78 GENERIC# xml-map 1 ( quot tag -- tag ) inline
\r
80 swap clone over >r swap call r>
\r
81 swap [ tag-children [ swap xml-map ] curry* map ] keep
\r
82 [ set-tag-children ] keep ;
\r
86 swap [ swap xml-map ] with-delegate ;
\r
88 : xml-subset ( quot tag -- seq ) ! quot: tag -- ?
\r
90 swap >r [ swap call ] 2keep rot r>
\r
91 swap [ [ push ] keep ] [ nip ] if
\r
94 GENERIC# xml-find 1 ( quot tag -- tag ) inline
\r
96 [ call ] 2keep swap rot [
\r
98 [ nip over >r swap xml-find r> swap dup ] find
\r
99 2drop ! leaves result of quot
\r
104 >r delegate r> xml-find ;
\r
106 GENERIC# xml-inject 1 ( quot tag -- ) inline
\r
110 [ xml-inject ] keep
\r
112 M: object xml-inject 2drop ;
\r
113 M: xml xml-inject >r delegate >r xml-inject ;
\r
115 ! * Accessing part of an XML document
\r
116 ! for tag- words, a start means that it searches all children
\r
117 ! and no star searches only direct children
\r
119 : tag-named? ( name elem -- ? )
\r
120 dup tag? [ names-match? ] [ 2drop f ] if ;
\r
122 : tag-named* ( tag name/string -- matching-tag )
\r
123 assure-name swap [ dupd tag-named? ] xml-find nip ;
\r
125 : tags-named* ( tag name/string -- tags-seq )
\r
126 assure-name swap [ dupd tag-named? ] xml-subset nip ;
\r
128 : tag-named ( tag name/string -- matching-tag )
\r
129 ! like get-name-tag but only looks at direct children,
\r
130 ! not all the children down the tree.
\r
131 assure-name swap [ tag-named? ] curry* find nip ;
\r
133 : tags-named ( tag name/string -- tags-seq )
\r
134 assure-name swap [ tag-named? ] curry* subset ;
\r
136 : assert-tag ( name name -- )
\r
137 names-match? [ "Unexpected XML tag found" throw ] unless ;
\r
139 : insert-children ( children tag -- )
\r
140 dup tag-children [ push-all ]
\r
141 [ >r V{ } like r> set-tag-children ] if ;
\r
143 : insert-child ( child tag -- )
\r
144 >r 1vector r> insert-children ;
\r
146 : tag-with-attr? ( elem attr-value attr-name -- ? )
\r
147 rot dup tag? [ at = ] [ drop f ] if ;
\r
149 : tag-with-attr ( tag attr-value attr-name -- matching-tag )
\r
150 assure-name [ tag-with-attr? ] 2curry find nip ;
\r
152 : tags-with-attr ( tag attr-value attr-name -- tags-seq )
\r
153 assure-name [ tag-with-attr? ] 2curry subset ;
\r
155 : tag-with-attr* ( tag attr-value attr-name -- matching-tag )
\r
156 assure-name [ tag-with-attr? ] 2curry xml-find nip ;
\r
158 : tags-with-attr* ( tag attr-value attr-name -- tags-seq )
\r
159 assure-name [ tag-with-attr? ] 2curry xml-subset ;
\r
161 : get-id ( tag id -- elem ) ! elem=tag.getElementById(id)
\r
162 "id" tag-with-attr ;
\r
164 : tags-named-with-attr* ( tag tag-name attr-value attr-name -- tags )
\r
165 >r >r tags-named* r> r> tags-with-attr ;
\r