package FrontC

  1. Overview
  2. Docs

Source file cxml.ml

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(* 
 *	$Id$
 *	Copyright (c) 2003, Hugues Cassé <hugues.casse@laposte.net>
 *
 *	Pretty printer of XML document.
 *)


(** Provide types and pretty printing for XML.
 *)


(** Attribute representation.
 *)
type attr = string * string


(** Representation of nodes.
 *)
type node =
	|	TEXT of string							(** Simple text *)
	| 	COM of string							(** Commentary *)
	|	PI of string * string					(** Processing instruction *)
	|	ELT of string * attr list * node list	(** Element *)


(** Representation of an XML document.
 *)
type document = {
	version: string;	(** Version, usually 1.0 *)
	encoding: string;	(** Encoding (only current encoding supported now) *)
	standalone: bool;	(** Standalone attribute *)
	element: node;		(** Document root element *)
}


(** Build a simple document with default initialization.
	@param elt	Main element of the document.
*)
let new_simple_doc elt: document =
	{
		version = "1.0";
		encoding = "iso-8859-1";
		standalone = true;
		element = elt
	}


(** Build a full document.
	@param vers	XML version.
	@param enc	Document encoding.
	@param sa	Stand-alone attribute.
	@param elt	Document element.
 *)
let new_doc vers enc sa elt: document =
	{
		version = vers;
		encoding = enc;
		standalone = sa;
		element = elt
	}


(** Build an attribute.
	@param name	Name of the attribute.
	@param cont	Content of the attribute.
 *)
let new_attr name cont: attr = (name, cont)


(** Build a new element.
	@param name		Name of the element.
	@param attrs	Attributes.
	@param children	Children nodes.
 *)
let new_elt name attrs children = ELT(name, attrs, children)


(** Build a new text node.
	@param text	Content of the node.
 *)
let new_text text = TEXT text


(** Add children to an element node.
	@param node		Element to add to.
	@param children	Children to add.
	@return			Passed element with children added to the end.
*)
let add_children node children =
	match node with
	  ELT (name, attrs, orig_children)
	  	-> ELT (name, attrs, List.append orig_children children)
	| _ -> raise (Invalid_argument "not an element")


(** Escape the given attribute value for output.
	@param text		Text of the attribute.
	@param quote	Quote character used for the attribute, either '"' or '\''.
*)
let escape_attr text quote =
	let buf = Buffer.create 32 in
	let rec perform i =
		if i >= String.length text then Buffer.contents buf else
		begin
			(match String.get text i with
			  '&'
				-> Buffer.add_string buf "&amp;"
			| '<'
				-> Buffer.add_string buf "&lt;"
			| c when c = quote
				-> Buffer.add_string buf
					(if quote = '"' then "&quote;" else "&apos;")
			| c
				-> Buffer.add_char buf c);
			perform (i + 1)
		end in
	perform 0


(** Output an attribute.
	@param out	Output channel.
	@param name	Name of the attribute.
	@param text Value of the attribute.
*)
let output_attr out (name, text) =
	output_char out ' ';
	output_string out name;
	output_string out "=\"";
	output_string out (escape_attr text '"');
	output_char out '"'


(** Output a node on the given channel.
	@param out		Channel to output to.
	@param node		Node to output.
	@param indent	Indentation.
*)
let rec output_node out indent node =
	let output str = output_string out str in
	let rec only_text children =
		match children with
		  [] -> true
		| (TEXT _)::tl -> only_text tl
		| _ -> false in
	match node with
	
	  TEXT text
		-> output text
		
	| COM text
		-> begin
			output indent;
			output "<!--";
			output text;
			output "-->"
		end
		
	| PI (id, data)
		-> begin
			output indent;
			output "<!";
			output id;
			output " ";
			output data;
			output ">"
		end
		
	| ELT(name, attrs, children)
		-> begin
			output indent;
			output "<";
			output name;
			List.iter (output_attr out) attrs;
			if children = []
				then output "/>"
				else begin
					output ">";
					List.iter (output_node out (indent ^ "\t")) children;
					if not (only_text children)  then output indent;
					output "</";
					output name;
					output ">"
				end;
		end	


(** Output an XML document to the given output channel.
	@param out	Output channel.
	@param doc	Document to output.
 *)
let output_doc out doc =
	output_string out "<?xml version=\"";
	output_string out doc.version;
	output_string out "\" encoding=\"";
	output_string out doc.encoding;
	output_string out "\" standalone=\"";
	output_string out (if doc.standalone then "yes" else "no");
	output_string out "\"?>";
	output_node out "\n" doc.element;
	output_char out '\n'


(** Output the given XML document on the standard output.
	@param doc	XML document to output.
*)
let output doc = output_doc stdout doc


(** Output the given XML document on the named file.
	@param filename		Path to the file to write to.
	@param doc			XML document to output.
	@raise Sys_error	In case of error during opening of the file.
*)
let output_file filename doc = 
	let out = open_out filename in
	output_doc out doc;
	close_out out