Commit | Line | Data |
---|---|---|
47db06aa GM |
1 | ;; @(#) xml.el --- XML parser |
2 | ||
3 | ;; Copyright (C) 2000 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Emmanuel Briot <briot@gnat.com> | |
6 | ;; Maintainer: Emmanuel Briot <briot@gnat.com> | |
7 | ;; Keywords: xml | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; This file contains a full XML parser. It parses a file, and returns a list | |
29 | ;; that can be used internally by any other lisp file. | |
30 | ;; See some example in todo.el | |
31 | ||
32 | ;;; FILE FORMAT | |
33 | ||
34 | ;; It does not parse the DTD, if present in the XML file, but knows how to | |
35 | ;; ignore it. The XML file is assumed to be well-formed. In case of error, the | |
36 | ;; parsing stops and the XML file is shown where the parsing stopped. | |
37 | ;; | |
38 | ;; It also knows how to ignore comments, as well as the special ?xml? tag | |
39 | ;; in the XML file. | |
40 | ;; | |
41 | ;; The XML file should have the following format: | |
42 | ;; <node1 attr1="name1" attr2="name2" ...> value | |
43 | ;; <node2 attr3="name3" attr4="name4"> value2 </node2> | |
44 | ;; <node3 attr5="name5" attr6="name6"> value3 </node3> | |
45 | ;; </node1> | |
46 | ;; Of course, the name of the nodes and attributes can be anything. There can | |
47 | ;; be any number of attributes (or none), as well as any number of children | |
48 | ;; below the nodes. | |
49 | ;; | |
50 | ;; There can be only top level node, but with any number of children below. | |
51 | ||
52 | ;;; LIST FORMAT | |
53 | ||
54 | ;; The functions `xml-parse-file' and `xml-parse-tag' return a list with | |
55 | ;; the following format: | |
56 | ;; | |
57 | ;; xml-list ::= (node node ...) | |
58 | ;; node ::= (tag_name attribute-list . child_node_list) | |
59 | ;; child_node_list ::= child_node child_node ... | |
60 | ;; child_node ::= node | string | |
61 | ;; tag_name ::= string | |
62 | ;; attribute_list ::= (("attribute" . "value") ("attribute" . "value") ...) | |
63 | ;; | nil | |
64 | ;; string ::= "..." | |
65 | ;; | |
47db06aa GM |
66 | ;; Some macros are provided to ease the parsing of this list |
67 | ||
68 | ;;; Code: | |
69 | ||
70 | ;;******************************************************************* | |
71 | ;;** | |
72 | ;;** Macros to parse the list | |
73 | ;;** | |
74 | ;;******************************************************************* | |
75 | ||
76 | (defmacro xml-node-name (node) | |
77 | "Return the tag associated with NODE. | |
78 | The tag is a lower-case symbol." | |
79 | (list 'car node)) | |
80 | ||
81 | (defmacro xml-node-attributes (node) | |
82 | "Return the list of attributes of NODE. | |
83 | The list can be nil." | |
84 | (list 'nth 1 node)) | |
85 | ||
86 | (defmacro xml-node-children (node) | |
87 | "Return the list of children of NODE. | |
88 | This is a list of nodes, and it can be nil." | |
89 | (list 'cddr node)) | |
90 | ||
91 | (defun xml-get-children (node child-name) | |
92 | "Return the children of NODE whose tag is CHILD-NAME. | |
93 | CHILD-NAME should be a lower case symbol." | |
94 | (let ((children (xml-node-children node)) | |
95 | match) | |
96 | (while children | |
97 | (if (car children) | |
98 | (if (equal (xml-node-name (car children)) child-name) | |
99 | (set 'match (append match (list (car children)))))) | |
100 | (set 'children (cdr children))) | |
101 | match)) | |
102 | ||
103 | (defun xml-get-attribute (node attribute) | |
104 | "Get from NODE the value of ATTRIBUTE. | |
105 | An empty string is returned if the attribute was not found." | |
106 | (if (xml-node-attributes node) | |
107 | (let ((value (assoc attribute (xml-node-attributes node)))) | |
108 | (if value | |
109 | (cdr value) | |
110 | "")) | |
111 | "")) | |
112 | ||
113 | ;;******************************************************************* | |
114 | ;;** | |
115 | ;;** Creating the list | |
116 | ;;** | |
117 | ;;******************************************************************* | |
118 | ||
119 | (defun xml-parse-file (file &optional parse-dtd) | |
120 | "Parse the well-formed XML FILE. | |
121 | Returns the top node with all its children. | |
122 | If PARSE-DTD is non-nil, the DTD is parsed rather than skipped." | |
123 | (find-file file) | |
124 | (let ((xml (xml-parse-region (point-min) | |
125 | (point-max) | |
126 | (current-buffer) | |
127 | parse-dtd))) | |
128 | (kill-buffer (current-buffer)) | |
129 | xml)) | |
130 | ||
131 | (defun xml-parse-region (beg end &optional buffer parse-dtd) | |
132 | "Parse the region from BEG to END in BUFFER. | |
133 | If BUFFER is nil, it defaults to the current buffer. | |
134 | Returns the XML list for the region, or raises an error if the region | |
135 | is not a well-formed XML file. | |
136 | If PARSE-DTD is non-nil, the DTD is parsed rather than skipped, | |
137 | and returned as the first element of the list" | |
138 | (let (xml result dtd) | |
139 | (save-excursion | |
140 | (if buffer | |
141 | (set-buffer buffer)) | |
142 | (goto-char beg) | |
143 | (while (< (point) end) | |
144 | (if (search-forward "<" end t) | |
145 | (progn | |
146 | (forward-char -1) | |
147 | (if (null xml) | |
148 | (progn | |
149 | (set 'result (xml-parse-tag end parse-dtd)) | |
150 | (cond | |
151 | ((listp (car result)) | |
152 | (set 'dtd (car result)) | |
153 | (add-to-list 'xml (cdr result))) | |
154 | (t | |
155 | (add-to-list 'xml result)))) | |
156 | ||
157 | ;; translation of rule [1] of XML specifications | |
158 | (error "XML files can have only one toplevel tag."))) | |
159 | (goto-char end))) | |
160 | (if parse-dtd | |
161 | (cons dtd (reverse xml)) | |
162 | (reverse xml))))) | |
163 | ||
164 | ||
165 | (defun xml-parse-tag (end &optional parse-dtd) | |
166 | "Parse the tag that is just in front of point. | |
167 | The end tag must be found before the position END in the current buffer. | |
168 | If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and | |
169 | returned as the first element in the list. | |
170 | Returns one of: | |
171 | - a list : the matching node | |
172 | - nil : the point is not looking at a tag. | |
173 | - a cons cell: the first element is the DTD, the second is the node" | |
174 | (cond | |
175 | ;; Processing instructions (like the <?xml version="1.0"?> tag at the | |
176 | ;; beginning of a document) | |
177 | ((looking-at "<\\?") | |
178 | (search-forward "?>" end) | |
179 | (skip-chars-forward " \t\n") | |
180 | (xml-parse-tag end)) | |
181 | ;; Character data (CDATA) sections, in which no tag should be interpreted | |
182 | ((looking-at "<!\\[CDATA\\[") | |
183 | (let ((pos (match-end 0))) | |
184 | (unless (search-forward "]]>" end t) | |
185 | (error "CDATA section does not end anywhere in the document")) | |
186 | (buffer-substring-no-properties pos (match-beginning 0)))) | |
187 | ;; DTD for the document | |
188 | ((looking-at "<!DOCTYPE") | |
189 | (let (dtd) | |
190 | (if parse-dtd | |
191 | (set 'dtd (xml-parse-dtd end)) | |
192 | (xml-skip-dtd end)) | |
193 | (skip-chars-forward " \t\n") | |
194 | (if dtd | |
195 | (cons dtd (xml-parse-tag end)) | |
196 | (xml-parse-tag end)))) | |
197 | ;; skip comments | |
198 | ((looking-at "<!--") | |
199 | (search-forward "-->" end) | |
200 | (skip-chars-forward " \t\n") | |
201 | (xml-parse-tag end)) | |
202 | ;; end tag | |
203 | ((looking-at "</") | |
204 | '()) | |
205 | ;; opening tag | |
af9bd539 | 206 | ((looking-at "<\\([^/> \t\n]+\\)") |
47db06aa | 207 | (let* ((node-name (match-string 1)) |
6eb6c4c1 | 208 | (children (list (intern node-name))) |
47db06aa GM |
209 | pos) |
210 | (goto-char (match-end 1)) | |
211 | ||
212 | ;; parses the attribute list | |
213 | (set 'children (append children (list (xml-parse-attlist end)))) | |
214 | ||
215 | ;; is this an empty element ? | |
216 | (if (looking-at "/>") | |
217 | (progn | |
218 | (forward-char 2) | |
219 | (skip-chars-forward " \t\n") | |
220 | (append children '(""))) | |
221 | ||
222 | ;; is this a valid start tag ? | |
223 | (if (= (char-after) ?>) | |
224 | (progn | |
225 | (forward-char 1) | |
226 | (skip-chars-forward " \t\n") | |
227 | (while (not (looking-at (concat "</" node-name ">"))) | |
228 | (cond | |
229 | ((looking-at "</") | |
230 | (error (concat | |
231 | "XML: invalid syntax -- invalid end tag (expecting " | |
232 | node-name | |
233 | ")"))) | |
234 | ((= (char-after) ?<) | |
235 | (set 'children (append children (list (xml-parse-tag end))))) | |
236 | (t | |
237 | (set 'pos (point)) | |
238 | (search-forward "<" end) | |
239 | (forward-char -1) | |
240 | (let ((string (buffer-substring-no-properties pos (point))) | |
241 | (pos 0)) | |
242 | ||
243 | ;; Clean up the string (no newline characters) | |
244 | ;; Not done, since as per XML specifications, the XML processor | |
245 | ;; should always pass the whole string to the application. | |
246 | ;; (while (string-match "\\s +" string pos) | |
247 | ;; (set 'string (replace-match " " t t string)) | |
248 | ;; (set 'pos (1+ (match-beginning 0)))) | |
249 | ||
250 | (set 'children (append children | |
251 | (list (xml-substitute-special string)))))))) | |
252 | (goto-char (match-end 0)) | |
253 | (skip-chars-forward " \t\n") | |
254 | (if (> (point) end) | |
255 | (error "XML: End tag for %s not found before end of region." | |
256 | node-name)) | |
257 | children | |
258 | ) | |
259 | ||
260 | ;; This was an invalid start tag | |
261 | (error "XML: Invalid attribute list") | |
262 | )))) | |
263 | )) | |
264 | ||
265 | (defun xml-parse-attlist (end) | |
266 | "Return the attribute-list that point is looking at. | |
267 | The search for attributes end at the position END in the current buffer. | |
268 | Leaves the point on the first non-blank character after the tag." | |
269 | (let ((attlist '()) | |
270 | name) | |
271 | (skip-chars-forward " \t\n") | |
272 | (while (looking-at "\\([a-zA-Z_:][a-zA-Z0-9.-_:]*\\)[ \t\n]*=[ \t\n]*") | |
6eb6c4c1 | 273 | (set 'name (intern (match-string 1))) |
47db06aa GM |
274 | (goto-char (match-end 0)) |
275 | ||
276 | ;; Do we have a string between quotes (or double-quotes), | |
277 | ;; or a simple word ? | |
278 | (unless (looking-at "\"\\([^\"]+\\)\"") | |
279 | (unless (looking-at "'\\([^\"]+\\)'") | |
280 | (error "XML: Attribute values must be given between quotes."))) | |
281 | ||
282 | ;; Each attribute must be unique within a given element | |
283 | (if (assoc name attlist) | |
284 | (error "XML: each attribute must be unique within an element.")) | |
285 | ||
286 | (set 'attlist (append attlist | |
287 | (list (cons name (match-string 1))))) | |
288 | (goto-char (match-end 0)) | |
289 | (skip-chars-forward " \t\n") | |
290 | (if (> (point) end) | |
291 | (error "XML: end of attribute list not found before end of region.")) | |
292 | ) | |
293 | attlist | |
294 | )) | |
295 | ||
296 | ;;******************************************************************* | |
297 | ;;** | |
298 | ;;** The DTD (document type declaration) | |
299 | ;;** The following functions know how to skip or parse the DTD of | |
300 | ;;** a document | |
301 | ;;** | |
302 | ;;******************************************************************* | |
303 | ||
304 | (defun xml-skip-dtd (end) | |
305 | "Skip the DTD that point is looking at. | |
306 | The DTD must end before the position END in the current buffer. | |
307 | The point must be just before the starting tag of the DTD. | |
308 | This follows the rule [28] in the XML specifications." | |
309 | (forward-char (length "<!DOCTYPE")) | |
310 | (if (looking-at "[ \t\n]*>") | |
311 | (error "XML: invalid DTD (excepting name of the document)")) | |
312 | (condition-case nil | |
313 | (progn | |
314 | (forward-word 1) ;; name of the document | |
315 | (skip-chars-forward " \t\n") | |
316 | (if (looking-at "\\[") | |
317 | (re-search-forward "\\][ \t\n]*>" end) | |
318 | (search-forward ">" end))) | |
319 | (error (error "XML: No end to the DTD")))) | |
320 | ||
321 | (defun xml-parse-dtd (end) | |
322 | "Parse the DTD that point is looking at. | |
323 | The DTD must end before the position END in the current buffer." | |
324 | (let (dtd type element end-pos) | |
325 | (forward-char (length "<!DOCTYPE")) | |
326 | (skip-chars-forward " \t\n") | |
327 | (if (looking-at ">") | |
328 | (error "XML: invalid DTD (excepting name of the document)")) | |
329 | ||
330 | ;; Get the name of the document | |
331 | (looking-at "\\sw+") | |
332 | (set 'dtd (list 'dtd (match-string-no-properties 0))) | |
333 | (goto-char (match-end 0)) | |
334 | ||
335 | (skip-chars-forward " \t\n") | |
336 | ||
337 | ;; External DTDs => don't know how to handle them yet | |
338 | (if (looking-at "SYSTEM") | |
339 | (error "XML: Don't know how to handle external DTDs.")) | |
340 | ||
341 | (if (not (= (char-after) ?\[)) | |
342 | (error "XML: Unknown declaration in the DTD.")) | |
343 | ||
344 | ;; Parse the rest of the DTD | |
345 | (forward-char 1) | |
346 | (while (and (not (looking-at "[ \t\n]*\\]")) | |
347 | (<= (point) end)) | |
348 | (cond | |
349 | ||
350 | ;; Translation of rule [45] of XML specifications | |
351 | ((looking-at | |
352 | "[\t \n]*<!ELEMENT[ \t\n]+\\([a-zA-Z0-9.%;]+\\)[ \t\n]+\\([^>]+\\)>") | |
353 | ||
6eb6c4c1 | 354 | (setq element (intern (match-string-no-properties 1)) |
47db06aa GM |
355 | type (match-string-no-properties 2)) |
356 | (set 'end-pos (match-end 0)) | |
357 | ||
358 | ;; Translation of rule [46] of XML specifications | |
359 | (cond | |
360 | ((string-match "^EMPTY[ \t\n]*$" type) ;; empty declaration | |
361 | (set 'type 'empty)) | |
362 | ((string-match "^ANY[ \t\n]*$" type) ;; any type of contents | |
363 | (set 'type 'any)) | |
364 | ((string-match "^(\\(.*\\))[ \t\n]*$" type) ;; children ([47]) | |
365 | (set 'type (xml-parse-elem-type (match-string-no-properties 1 type)))) | |
366 | ((string-match "^%[^;]+;[ \t\n]*$" type) ;; substitution | |
367 | nil) | |
368 | (t | |
369 | (error "XML: Invalid element type in the DTD"))) | |
370 | ||
371 | ;; rule [45]: the element declaration must be unique | |
372 | (if (assoc element dtd) | |
373 | (error "XML: elements declaration must be unique in a DTD (<%s>)." | |
374 | (symbol-name element))) | |
375 | ||
376 | ;; Store the element in the DTD | |
377 | (set 'dtd (append dtd (list (list element type)))) | |
378 | (goto-char end-pos) | |
379 | ) | |
380 | ||
381 | ||
382 | (t | |
383 | (error "XML: Invalid DTD item")) | |
384 | ) | |
385 | ) | |
386 | ||
387 | ;; Skip the end of the DTD | |
388 | (search-forward ">" end) | |
389 | dtd | |
390 | )) | |
391 | ||
392 | ||
393 | (defun xml-parse-elem-type (string) | |
394 | "Convert a STRING for an element type into an elisp structure." | |
395 | ||
396 | (let (elem modifier) | |
397 | (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string) | |
398 | (progn | |
399 | (setq elem (match-string 1 string) | |
400 | modifier (match-string 2 string)) | |
401 | (if (string-match "|" elem) | |
402 | (set 'elem (append '(choice) | |
403 | (mapcar 'xml-parse-elem-type | |
404 | (split-string elem "|")))) | |
405 | (if (string-match "," elem) | |
406 | (set 'elem (append '(seq) | |
407 | (mapcar 'xml-parse-elem-type | |
408 | (split-string elem ",")))) | |
409 | ))) | |
410 | (if (string-match "[ \t\n]*\\([^+*?]+\\)\\([+*?]?\\)" string) | |
411 | (setq elem (match-string 1 string) | |
412 | modifier (match-string 2 string)))) | |
413 | ||
414 | (if (and (stringp elem) | |
415 | (string= elem "#PCDATA")) | |
416 | (set 'elem 'pcdata)) | |
417 | ||
418 | (cond | |
419 | ((string= modifier "+") | |
420 | (list '+ elem)) | |
421 | ((string= modifier "*") | |
422 | (list '* elem)) | |
423 | ((string= modifier "?") | |
424 | (list '? elem)) | |
425 | (t | |
426 | elem)))) | |
427 | ||
428 | ||
429 | ;;******************************************************************* | |
430 | ;;** | |
431 | ;;** Substituting special XML sequences | |
432 | ;;** | |
433 | ;;******************************************************************* | |
434 | ||
435 | (defun xml-substitute-special (string) | |
436 | "Return STRING, after subsituting special XML sequences." | |
437 | (while (string-match "&" string) | |
438 | (set 'string (replace-match "&" t nil string))) | |
439 | (while (string-match "<" string) | |
440 | (set 'string (replace-match "<" t nil string))) | |
441 | (while (string-match ">" string) | |
442 | (set 'string (replace-match ">" t nil string))) | |
443 | (while (string-match "'" string) | |
444 | (set 'string (replace-match "'" t nil string))) | |
445 | (while (string-match """ string) | |
446 | (set 'string (replace-match "\"" t nil string))) | |
447 | string) | |
448 | ||
449 | ;;******************************************************************* | |
450 | ;;** | |
451 | ;;** Printing a tree. | |
452 | ;;** This function is intended mainly for debugging purposes. | |
453 | ;;** | |
454 | ;;******************************************************************* | |
455 | ||
456 | (defun xml-debug-print (xml) | |
457 | (while xml | |
458 | (xml-debug-print-internal (car xml) "") | |
459 | (set 'xml (cdr xml))) | |
460 | ) | |
461 | ||
462 | (defun xml-debug-print-internal (xml &optional indent-string) | |
463 | "Outputs the XML tree in the current buffer. | |
464 | The first line indented with INDENT-STRING." | |
465 | (let ((tree xml) | |
466 | attlist) | |
467 | (unless indent-string | |
468 | (set 'indent-string "")) | |
469 | ||
470 | (insert indent-string "<" (symbol-name (xml-node-name tree))) | |
471 | ||
472 | ;; output the attribute list | |
473 | (set 'attlist (xml-node-attributes tree)) | |
474 | (while attlist | |
475 | (insert " ") | |
476 | (insert (symbol-name (caar attlist)) "=\"" (cdar attlist) "\"") | |
477 | (set 'attlist (cdr attlist))) | |
478 | ||
479 | (insert ">") | |
480 | ||
481 | (set 'tree (xml-node-children tree)) | |
482 | ||
483 | ;; output the children | |
484 | (while tree | |
485 | (cond | |
486 | ((listp (car tree)) | |
487 | (insert "\n") | |
488 | (xml-debug-print-internal (car tree) (concat indent-string " ")) | |
489 | ) | |
490 | ((stringp (car tree)) | |
491 | (insert (car tree)) | |
492 | ) | |
493 | (t | |
494 | (error "Invalid XML tree"))) | |
495 | (set 'tree (cdr tree)) | |
496 | ) | |
497 | ||
498 | (insert "\n" indent-string | |
499 | "</" (symbol-name (xml-node-name xml)) ">") | |
500 | )) | |
501 | ||
502 | (provide 'xml) | |
503 | ||
504 | ;;; xml.el ends here |