File: xml_hierarchy.pl

package info (click to toggle)
swi-prolog-packages 5.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 50,688 kB
  • ctags: 25,904
  • sloc: ansic: 195,096; perl: 91,425; cpp: 7,660; sh: 3,046; makefile: 2,750; yacc: 843; awk: 14; sed: 12
file content (130 lines) | stat: -rw-r--r-- 4,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*  $Id: xml_hierarchy.pl,v 1.5 2002/02/01 15:04:51 jan Exp $

    Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (C): 1985-2002, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(xml_hierarchy, []).
:- use_module(library(pce)).
:- use_module(library(pce_toc)).
:- require([ memberchk/2
	   , append/3
	   , nth1/3
	   ]).

resource(open,	image, image('16x16/book2.xpm')).
resource(close,	image, image('16x16/manual.xpm')).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Show hierarchy of elements as produced by  xml2pl. This class is kept as
simple as possible. In general it  requires subclassing and defining the
select_node message to do anything sensible.   See  class toc_window for
details on programming this library.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- pce_begin_class(xml_hierarchy, toc_window,
		   "Browse XML document hierarchy").

variable(xml,	prolog,	get, "Represented XML term").

initialise(H, XML:prolog) :->
	send_super(H, initialise),
	send(H, xml, XML).

xml(H, XML:prolog) :->			% list of elements
	"Visualise an XML structure"::
	content(XML, Content),
	send(H, slot, xml, Content),
	get(H, icon, Content, @on, Icons),
	get(H, caption, Content, Caption),
	send(H, root, xml_node(Caption, [], Icons, @on)),
	send(H, expand_root).

content(document(_Type, [Content]), Content).
content([Content], Content).
content(element(A,B,C), element(A,B,C)).
content([pi(_XML), Content], Content).	% bit of a hack to handle XML files

expand_node(H, Node:xml_node) :->
	"Expand clicked node"::
	get(Node, xml, element(_Name, _Attributes, Content)),
	get(Node, path, Loc0),
	append(Loc0, [Index], Loc),
	(   nth1(Index, Content, Esub),
	    get(H, caption, Esub, Name),
	    arg(3, Esub, SubContent),
	    (	memberchk(element(_,_,_), SubContent)
	    ->	get(H, icon, Esub, @on, Icons),
		send(H, son, Node, xml_node(Name, Loc, Icons, @on))
	    ;   get(H, icon, Esub, @on, Icons),
		send(H, son, Node, xml_node(Name, Loc, Icons, @off))
	    ),
	    fail
	;   true
	).

:- pce_group(refine).

caption(_, XML:prolog, Title:name) :<-
	"Get title for a node"::
	XML = element(Title, _, _).

icon(_H, _XML:prolog, _HasSub:bool, Tuple:tuple) :<-
	"Return open/close icon"::
	new(Tuple, tuple(image(resource(open)),
			 image(resource(close)))).

:- pce_end_class.


:- pce_begin_class(xml_node, toc_folder,
		   "Show XML node with sub-nodes").

variable(path, prolog, get, "Path from root").

initialise(Node, Name:name, Path:prolog, Icons:tuple, CanExpand:bool) :->
	get(Icons, first, Open),
	get(Icons, second, Close),
	send_super(Node, initialise, Name, @default, Close, Open, CanExpand),
	send(Node, slot, path, Path).

xml(Node, XML:prolog) :<-
	"Get XML as Prolog term"::
	get(Node?tree, window, Window),
	get(Window, xml, Content),
	get(Node, path, Path),
	find_xml(Path, Content, XML).

find_xml([], XML, XML) :- !.
find_xml([H|T], element(_, _, Content), XML) :-
	nth1(H, Content, XML0),
	find_xml(T, XML0, XML).

:- pce_end_class.