1/* xml_diagnosis.pl : XML exception diagnosis.
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_generation ).
16
17/* xml_fault( +Term, +Indentation, ?SubTerm, ?Path, ?Message ) identifies SubTerm
18 * as a sub-term of Term which cannot be serialized after Indentation.
19 * Message is an atom naming the type of error; Path is a string encoding a
20 * list of SubTerm's ancestor elements in the form <tag>{(id)}* where <tag> is the
21 * element tag and <id> is the value of any attribute _named_ id.
22 */
23xml_fault( Term, _Indent, Term, [], "Illegal Variable" ) :-
24	var( Term ).
25xml_fault( xml(Attributes,_Content), _Indent, Term, [], Message ) :-
26	member( Attribute, Attributes ),
27	attribute_fault( Attribute, Term, Message ).
28xml_fault( xml(_Attributes,Content), Indent, Culprit, Path, Message ) :-
29	xml_content_fault( Content, Indent, Culprit, Path, Message ).
30xml_fault( Term, _Indent, Term, [], "Illegal Term" ).
31
32xml_content_fault( Term, _Indent, Term, [], "Illegal Variable" ) :-
33	var( Term ).
34xml_content_fault( pcdata(Chars), _Indent, Chars, [], "Invalid Character Data" ) :-
35	\+ is_chars( Chars ).
36xml_content_fault( cdata(Chars), _Indent, Chars, [], "Invalid Character Data" ) :-
37	\+ is_chars( Chars ).
38xml_content_fault( [H|_T], Indent, Culprit, Path, Message ) :-
39	xml_content_fault( H, Indent, Culprit, Path, Message ).
40xml_content_fault( [_H|T], Indent, Culprit, Path, Message ) :-
41	xml_content_fault( T, Indent, Culprit, Path, Message ).
42xml_content_fault( namespace(_URI,_Prefix,Element), Indent, Culprit, Path, Message ) :-
43	element_fault( Element, [0' |Indent], Culprit, Path, Message ).
44xml_content_fault( Element, Indent, Culprit, Path, Message ) :-
45	element_fault( Element, [0' |Indent], Culprit, Path, Message ).
46xml_content_fault( Term, Indent, Term, [], "Illegal Term" ) :-
47	\+ generation(Term, "", false, Indent, _Format, _Plus, _Minus ).
48
49element_fault( element(Tag, _Attributes, _Contents), _Indent, Tag, [], "Tag must be an atom" ) :-
50	\+ atom( Tag ).
51element_fault( element(Tag, Attributes, _Contents), _Indent, Tag, [], "Attributes must be instantiated" ) :-
52	var( Attributes ).
53element_fault( element(Tag, Attributes, _Contents), _Indent, Faulty, Path, Message ) :-
54	fault_path( Tag, Attributes, Path, [] ),
55	member( Attribute, Attributes ),
56	attribute_fault( Attribute, Faulty, Message ).
57element_fault( element(Tag, Attributes, Contents), Indent, Culprit, Path, Message ) :-
58	fault_path( Tag, Attributes, Path, Path1 ),
59	xml_content_fault( Contents, Indent, Culprit, Path1, Message ).
60
61attribute_fault( Attribute, Attribute, "Illegal Variable" ) :-
62	var( Attribute ).
63attribute_fault( Name=Value, Name=Value, "Attribute Name must be atom" ) :-
64	\+ atom(Name).
65attribute_fault( Name=Value, Name=Value, "Attribute Value must be chars" ) :-
66	\+ is_chars( Value ).
67attribute_fault( Attribute, Attribute, "Malformed Attribute" ) :-
68	\+ Attribute = (_Name=_Value).
69
70is_chars( Chars ) :-
71	is_list( Chars ),
72	\+ (member( Char, Chars ), \+ (integer(Char), Char >=0, Char =< 255)).
73
74fault_path( Tag, Attributes ) -->
75	{atom_codes( Tag, Chars )},
76	chars( Chars ),
77	fault_id( Attributes ),
78	" ".
79
80fault_id( Attributes ) -->
81	{member( id=Chars, Attributes ), is_chars( Chars )},
82	!,
83	"(", chars(Chars), ")".
84fault_id( _Attributes ) --> "".
85