]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/traversal/traversal.factor
Factor source files should not be executable
[factor.git] / basis / xml / traversal / traversal.factor
1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel namespaces sequences words io assocs
4 quotations strings parser lexer arrays xml.data xml.writer debugger
5 splitting vectors sequences.deep combinators fry memoize ;
6 IN: xml.traversal
7
8 : children>string ( tag -- string )
9     children>> {
10         { [ dup empty? ] [ drop "" ] }
11         {
12             [ dup [ string? not ] any? ]
13             [ "XML tag unexpectedly contains non-text children" throw ]
14         }
15         [ concat ]
16     } cond ;
17
18 : children-tags ( tag -- sequence )
19     children>> [ tag? ] filter ;
20
21 : first-child-tag ( tag -- tag )
22     children>> [ tag? ] find nip ;
23
24 : tag-named? ( name elem -- ? )
25     dup tag? [ names-match? ] [ 2drop f ] if ;
26
27 : tag-named ( tag name/string -- matching-tag )
28     assure-name '[ _ swap tag-named? ] find nip ;
29
30 : tags-named ( tag name/string -- tags-seq )
31     assure-name '[ _ swap tag-named? ] filter { } like ;
32
33 <PRIVATE
34
35 : prepare-deep ( xml name/string -- tag name/string )
36     [ dup xml? [ body>> ] when ] [ assure-name ] bi* ;
37
38 PRIVATE>
39
40 : deep-tag-named ( tag name/string -- matching-tag )
41     prepare-deep '[ _ swap tag-named? ] deep-find ;
42
43 : deep-tags-named ( tag name/string -- tags-seq )
44     prepare-deep '[ _ swap tag-named? ] deep-filter { } like ;
45
46 : tag-with-attr? ( elem attr-value attr-name -- ? )
47     rot dup tag? [ swap attr = ] [ 3drop f ] if ;
48
49 : tag-with-attr ( tag attr-value attr-name -- matching-tag )
50     assure-name '[ _ _ tag-with-attr? ] find nip ;
51
52 : tags-with-attr ( tag attr-value attr-name -- tags-seq )
53     assure-name '[ _ _ tag-with-attr? ] filter children>> ;
54
55 : deep-tag-with-attr ( tag attr-value attr-name -- matching-tag )
56     assure-name '[ _ _ tag-with-attr? ] deep-find ;
57
58 : deep-tags-with-attr ( tag attr-value attr-name -- tags-seq )
59     assure-name '[ _ _ tag-with-attr? ] deep-filter ;
60
61 : get-id ( tag id -- elem )
62     "id" deep-tag-with-attr ;
63
64 : deep-tags-named-with-attr ( tag tag-name attr-value attr-name -- tags )
65     [ deep-tags-named ] 2dip tags-with-attr ;
66
67 : assert-tag ( name name -- )
68     names-match? [ "Unexpected XML tag found" throw ] unless ;