1/* xml_pp: "pretty print" an XML Document on the current output stream.
2 *
3 * Copyright (C) 2001-2005 Binding Time Limited
4 * Copyright (C) 2005, 2006 John Fletcher
5 *
6 * Current Release: $Revision: 1.2 $
7 *
8 * TERMS AND CONDITIONS:
9 *
10 * This program is offered free of charge, as unsupported source code. You may
11 * use it, copy it, distribute it, modify it or sell it without restriction,
12 * but entirely at your own risk.
13 */
14
15:- ensure_loaded( xml_utilities ).
16
17/* xml_pp( +XMLDocument ) "pretty prints" XMLDocument on the current
18 * output stream.
19 */
20xml_pp( xml(Attributes, Document) ) :-
21	write( 'xml( ' ), pp_attributes( Attributes, "" ), put( 0', ), nl,
22	xml_pp_list( Document, "	" ),
23	format( ' ).~n', [] ).
24xml_pp( malformed(Attributes, Document) ) :-
25	write( 'malformed( ' ), pp_attributes( Attributes, "" ), put( 0', ), nl,
26	xml_pp_list( Document, "	" ),
27	format( ' ).~n', [] ).
28
29xml_pp_indented( [], Indent ) :-
30	format( '~s[]', [Indent] ).
31xml_pp_indented( List, Indent ) :-
32	List = [_|_],
33	format( '~s', [Indent] ),
34	xml_pp_list( List, Indent ).
35xml_pp_indented( comment(Text), Indent ) :-
36	format( '~scomment(', [Indent] ), pp_string(Text), put( 0') ). %'
37xml_pp_indented( namespace(URI,Prefix,Element), Indent ) :-
38	format( '~snamespace( ~q, "~s",~n', [Indent,URI,Prefix] ),
39	xml_pp_indented( Element, [0'	|Indent] ),
40	format( '~n~s)', [[0'	|Indent]] ).
41xml_pp_indented( element(Tag,Attributes,Contents), Indent ) :-
42	format( '~selement( ~q,~n', [Indent,Tag] ),
43	pp_attributes( Attributes, [0'	|Indent] ), put(0',), nl,
44	xml_pp_list( Contents, [0'	|Indent] ), write( ' )' ). %'
45xml_pp_indented( instructions(Target, Processing), Indent ) :-
46	format( '~sinstructions( ~q, ', [Indent,Target] ),
47	pp_string(Processing), put( 0') ). %'
48xml_pp_indented( doctype(Name, DoctypeId), Indent ) :-
49	format( '~sdoctype( ~q, ', [Indent,Name] ),
50	xml_pp_indented( DoctypeId, [0'	|Indent] ), %'
51	write( ' )' ).
52xml_pp_indented( cdata(CData), Indent ) :-
53	format( '~scdata(', [Indent] ), pp_string(CData), put( 0') ). %'
54xml_pp_indented( pcdata(PCData), Indent ) :-
55	format( '~spcdata(', [Indent] ), pp_string(PCData), put( 0') ). %'
56xml_pp_indented( public(URN,URL), _Indent ) :-
57	format( 'public( "~s", "~s" )', [URN,URL] ).
58xml_pp_indented( public(URN,URL,Literals), Indent ) :-
59	format( 'public( "~s", "~s",~n', [URN,URL] ),
60	xml_pp_list( Literals, [0'	|Indent] ), write( ' )' ). %'
61xml_pp_indented( system(URL), _Indent ) :-
62	format( 'system( "~s" )', [URL] ).
63xml_pp_indented( system(URL,Literals), Indent ) :-
64	format( 'system( "~s",~n', [URL] ),
65	xml_pp_list( Literals, [0'	|Indent] ), write( ' )' ). %'
66xml_pp_indented( local, _Indent ) :-
67	write( local ).
68xml_pp_indented( local(Literals), Indent ) :-
69	write( 'local(' ), nl,
70	xml_pp_list( Literals, [0'	|Indent] ), write( ' )' ). %'
71xml_pp_indented( dtd_literal(String), Indent ) :-
72	format( '~sdtd_literal(', [Indent] ), pp_string(String), put( 0') ). %'
73xml_pp_indented( out_of_context(Tag), Indent ) :-
74	format( '~s/* SYNTAX ERROR */ out_of_context( ~q )', [Indent,Tag] ).
75xml_pp_indented( unparsed(String), Indent ) :-
76	format( '~s/* SYNTAX ERROR */ unparsed( ', [Indent] ),
77	pp_string(String), put( 0') ). %'
78
79xml_pp_list( [], Indent ) :-
80	format( '~s[]', [Indent] ).
81xml_pp_list( [H|T], Indent ) :-
82	format( '~s[~n', [Indent] ),
83	xml_pp_indented( H, Indent ),
84	xml_pp_list1( T, Indent ),
85	format( '~s]', [Indent] ).
86
87xml_pp_list1( [], _Indent ) :-
88	nl.
89xml_pp_list1( [H|T], Indent ) :-
90	put( 0', ), nl, %'
91	xml_pp_indented( H, Indent ),
92	xml_pp_list1( T, Indent ).
93
94pp_attributes( [], Indent ) :-
95	format( '~s[]', [Indent] ).
96pp_attributes( [Attribute|Attributes], Indent ) :-
97	format( '~s[', [Indent] ),
98	pp_attributes1( Attributes, Attribute ),
99	put( 0'] ). %'
100
101pp_attributes1( [], Name=Value ) :-
102	pp_name( Name ), pp_string( Value ).
103pp_attributes1( [H|T], Name=Value ) :-
104	pp_name( Name ), pp_string( Value ), write( ', ' ),
105	pp_attributes1( T, H ).
106
107
108pp_name( Name ) :-
109	( possible_operator( Name ) ->
110		format( '(~w)=', [Name] )
111	; otherwise ->
112		format( '~q=', [Name] )
113	).
114
115possible_operator( (abolish) ).
116possible_operator( (attribute) ).
117possible_operator( (check_advice) ).
118possible_operator( (compile_command) ).
119possible_operator( (delay) ).
120possible_operator( (demon) ).
121possible_operator( (discontiguous) ).
122possible_operator( (div) ).
123possible_operator( (do) ).
124possible_operator( (document_export) ).
125possible_operator( (document_import) ).
126possible_operator( (dy) ).
127possible_operator( (dynamic) ).
128possible_operator( (edb) ).
129possible_operator( (eexport) ).
130possible_operator( (else) ).
131possible_operator( (except) ).
132possible_operator( (export) ).
133possible_operator( (foreign_pred) ).
134possible_operator( (from) ).
135possible_operator( (from_chars) ).
136possible_operator( (from_file) ).
137possible_operator( (from_stream) ).
138possible_operator( (global) ).
139possible_operator( (help) ).
140possible_operator( (hilog) ).
141possible_operator( (if) ).
142possible_operator( (import) ).
143possible_operator( (index) ).
144possible_operator( (initialization) ).
145possible_operator( (is) ).
146possible_operator( (listing) ).
147possible_operator( (local) ).
148possible_operator( (locked) ).
149possible_operator( (meta_predicate) ).
150possible_operator( (mod) ).
151possible_operator( (mode) ).
152possible_operator( (module_transparent) ).
153possible_operator( (multifile) ).
154possible_operator( (namic) ).
155possible_operator( (nocheck_advice) ).
156possible_operator( (nospy) ).
157possible_operator( (not) ).
158possible_operator( (of) ).
159possible_operator( (once) ).
160possible_operator( (onto_chars) ).
161possible_operator( (onto_file) ).
162possible_operator( (onto_stream) ).
163possible_operator( (parallel) ).
164possible_operator( (public) ).
165possible_operator( (r) ).
166possible_operator( (rem) ).
167possible_operator( (skipped) ).
168possible_operator( (spy) ).
169possible_operator( (table) ).
170possible_operator( (then) ).
171possible_operator( (thread_local) ).
172possible_operator( (ti) ).
173possible_operator( (ti_off) ).
174possible_operator( (traceable) ).
175possible_operator( (unskipped) ).
176possible_operator( (untraceable) ).
177possible_operator( (use_subsumptive_tabling) ).
178possible_operator( (use_variant_tabling) ).
179possible_operator( (volatile) ).
180possible_operator( (with) ).
181possible_operator( (with_input_from_chars) ).
182possible_operator( (with_output_to_chars) ).
183possible_operator( (xor) ).
184