2009-01-27 14:34:14 -05:00
|
|
|
! Copyright (C) 2005, 2009 Daniel Ehrenberg
|
2007-12-29 11:36:20 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-06-30 04:57:00 -04:00
|
|
|
USING: accessors kernel namespaces sequences words io assocs
|
2012-12-17 14:58:30 -05:00
|
|
|
quotations strings parser lexer arrays xml.data make
|
2009-01-27 14:34:14 -05:00
|
|
|
splitting vectors sequences.deep combinators fry memoize ;
|
2009-02-05 22:17:03 -05:00
|
|
|
IN: xml.traversal
|
2007-12-29 11:36:20 -05:00
|
|
|
|
|
|
|
: children>string ( tag -- string )
|
2008-08-27 18:02:54 -04:00
|
|
|
children>> {
|
2008-06-27 02:30:23 -04:00
|
|
|
{ [ dup empty? ] [ drop "" ] }
|
2009-01-31 22:01:55 -05:00
|
|
|
{
|
|
|
|
[ dup [ string? not ] any? ]
|
|
|
|
[ "XML tag unexpectedly contains non-text children" throw ]
|
|
|
|
}
|
2008-06-27 02:30:23 -04:00
|
|
|
[ concat ]
|
|
|
|
} cond ;
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2012-12-17 14:58:30 -05:00
|
|
|
: deep-children>string ( tag -- string )
|
|
|
|
children>> [
|
|
|
|
[ dup tag? [ deep-children>string ] when % ] each
|
|
|
|
] "" make ;
|
|
|
|
|
2007-12-29 11:36:20 -05:00
|
|
|
: children-tags ( tag -- sequence )
|
2008-08-27 18:02:54 -04:00
|
|
|
children>> [ tag? ] filter ;
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2010-02-18 18:31:52 -05:00
|
|
|
: first-child-tag ( tag -- child )
|
2008-08-27 18:02:54 -04:00
|
|
|
children>> [ tag? ] find nip ;
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2008-01-06 12:30:23 -05:00
|
|
|
: tag-named? ( name elem -- ? )
|
2007-12-29 11:36:20 -05:00
|
|
|
dup tag? [ names-match? ] [ 2drop f ] if ;
|
|
|
|
|
2009-01-31 22:01:55 -05:00
|
|
|
: tag-named ( tag name/string -- matching-tag )
|
|
|
|
assure-name '[ _ swap tag-named? ] find nip ;
|
2008-01-06 13:17:50 -05:00
|
|
|
|
2009-01-31 22:01:55 -05:00
|
|
|
: tags-named ( tag name/string -- tags-seq )
|
2012-07-11 17:19:07 -04:00
|
|
|
assure-name '[ _ swap tag-named? ] { } filter-as ;
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2009-01-31 22:01:55 -05:00
|
|
|
<PRIVATE
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2009-01-31 22:01:55 -05:00
|
|
|
: prepare-deep ( xml name/string -- tag name/string )
|
|
|
|
[ dup xml? [ body>> ] when ] [ assure-name ] bi* ;
|
2007-12-29 11:36:20 -05:00
|
|
|
|
2009-01-31 22:01:55 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: deep-tag-named ( tag name/string -- matching-tag )
|
|
|
|
prepare-deep '[ _ swap tag-named? ] deep-find ;
|
|
|
|
|
|
|
|
: deep-tags-named ( tag name/string -- tags-seq )
|
2012-07-11 17:19:07 -04:00
|
|
|
prepare-deep '[ _ swap tag-named? ] { } deep-filter-as ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
|
|
|
: tag-with-attr? ( elem attr-value attr-name -- ? )
|
2009-01-29 14:33:04 -05:00
|
|
|
rot dup tag? [ swap attr = ] [ 3drop f ] if ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
|
|
|
: tag-with-attr ( tag attr-value attr-name -- matching-tag )
|
2008-12-02 20:59:16 -05:00
|
|
|
assure-name '[ _ _ tag-with-attr? ] find nip ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
2011-12-14 17:05:57 -05:00
|
|
|
: tag-named-with-attr ( tag tag-name attr-value attr-name -- matching-tag )
|
|
|
|
[ tags-named ] 2dip '[ _ _ tag-with-attr? ] find nip ;
|
|
|
|
|
2008-01-06 12:30:23 -05:00
|
|
|
: tags-with-attr ( tag attr-value attr-name -- tags-seq )
|
2010-09-13 21:14:46 -04:00
|
|
|
assure-name '[ _ _ tag-with-attr? ] { } filter-as ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
2008-01-06 13:17:50 -05:00
|
|
|
: deep-tag-with-attr ( tag attr-value attr-name -- matching-tag )
|
2008-12-02 20:59:16 -05:00
|
|
|
assure-name '[ _ _ tag-with-attr? ] deep-find ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
2008-01-06 13:17:50 -05:00
|
|
|
: deep-tags-with-attr ( tag attr-value attr-name -- tags-seq )
|
2009-01-31 22:01:55 -05:00
|
|
|
assure-name '[ _ _ tag-with-attr? ] deep-filter ;
|
2008-01-06 12:30:23 -05:00
|
|
|
|
2009-01-27 19:16:35 -05:00
|
|
|
: get-id ( tag id -- elem )
|
2008-01-06 13:17:50 -05:00
|
|
|
"id" deep-tag-with-attr ;
|
|
|
|
|
|
|
|
: deep-tags-named-with-attr ( tag tag-name attr-value attr-name -- tags )
|
2008-12-02 20:59:16 -05:00
|
|
|
[ deep-tags-named ] 2dip tags-with-attr ;
|
2008-01-06 13:17:50 -05:00
|
|
|
|
|
|
|
: assert-tag ( name name -- )
|
|
|
|
names-match? [ "Unexpected XML tag found" throw ] unless ;
|