%
% printing a Pillow XML tree in a pretty format
%
% By:	K. Hui
% Date:	12 Apr 2002
%

:- module(print_xml,[print_xml/1]).

print_xml(Xml) :-
	print_xml(0,Xml).

print_xml(_,[]) :-
	!.

print_xml(Tab,[X|Xs]) :-
	!,
	print_xml(Tab,X),
	print_xml(Tab,Xs).

print_xml(Tab,xmldecl(Attribs)) :-
	!,
	print_tab(Tab),
	write('<?xml'),
	print_attrib(Attribs),
	write('?>'),
	nl.

print_xml(Tab,comment(String)) :-
	print_tab(Tab),
	write('<!--'),
	print_string(String),
	write('-->'),
	nl.

print_xml(Tab,env(Tag,Attrib,Subtree)) :-
	!,
	print_tab(Tab),
	write('<'),
	write(Tag),
	print_attrib(Attrib),
	write('>'),
	nl,
	New_tab is Tab+1,
	print_xml(New_tab,Subtree),
	print_tab(Tab),
	write('</'),
	write(Tag),
	write('>'),
	nl.
print_xml(Tab,elem(Tag,Attrib)) :-
	!,
	print_tab(Tab),
	write('<'),
	write(Tag),
	print_attrib(Attrib),
	write('/>'),
	nl.
print_xml(Tab,Xml) :-
	print_tab(Tab),
	write(Xml),
	nl.


print_attrib([]).
print_attrib([X=Y|As]) :-
	write(' '),
	write(X),
	write('=\"'),
	print_string(Y),
%	write(Y),
	write('\"'),
	print_attrib(As).


print_tab(0).
print_tab(Tab) :-
	Tab>0,
	write('    '),
	New_tab is Tab-1,
	print_tab(New_tab).

print_string([]) :-
	!.
print_string([C|Cs]) :-
	!,
	put(C),
	print_string(Cs).
print_string(Atom) :-
	write(Atom).
