1 ;; @(#) xml.el --- XML parser
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Emmanuel Briot <briot@gnat.com>
6 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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
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.
38 ;; It also knows how to ignore comments, as well as the special ?xml? tag
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>
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
50 ;; There can be only top level node, but with any number of children below.
54 ;; The functions `xml-parse-file' and `xml-parse-tag' return a list with
55 ;; the following format:
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") ...)
66 ;; Some macros are provided to ease the parsing of this list
70 ;;*******************************************************************
72 ;;** Macros to parse the list
74 ;;*******************************************************************
76 (defmacro xml-node-name
(node)
77 "Return the tag associated with NODE.
78 The tag is a lower-case symbol."
81 (defmacro xml-node-attributes
(node)
82 "Return the list of attributes of NODE.
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."
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
))
98 (if (equal (xml-node-name (car children
)) child-name
)
99 (set 'match
(append match
(list (car children
))))))
100 (set 'children
(cdr children
)))
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
))))
113 ;;*******************************************************************
115 ;;** Creating the list
117 ;;*******************************************************************
119 (defun xml-parse-file (file &optional parse-dtd
)
120 "Parse the well-formed XML FILE.
121 If FILE is already edited, this will keep the buffer alive.
122 Returns the top node with all its children.
123 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped."
125 (if (get-file-buffer file
)
127 (set-buffer (get-file-buffer file
))
131 (let ((xml (xml-parse-region (point-min)
137 (kill-buffer (current-buffer)))
140 (defun xml-parse-region (beg end
&optional buffer parse-dtd
)
141 "Parse the region from BEG to END in BUFFER.
142 If BUFFER is nil, it defaults to the current buffer.
143 Returns the XML list for the region, or raises an error if the region
144 is not a well-formed XML file.
145 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped,
146 and returned as the first element of the list"
147 (let (xml result dtd
)
152 (while (< (point) end
)
153 (if (search-forward "<" end t
)
158 (set 'result
(xml-parse-tag end parse-dtd
))
160 ((listp (car result
))
161 (set 'dtd
(car result
))
162 (add-to-list 'xml
(cdr result
)))
164 (add-to-list 'xml result
))))
166 ;; translation of rule [1] of XML specifications
167 (error "XML files can have only one toplevel tag.")))
170 (cons dtd
(reverse xml
))
174 (defun xml-parse-tag (end &optional parse-dtd
)
175 "Parse the tag that is just in front of point.
176 The end tag must be found before the position END in the current buffer.
177 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
178 returned as the first element in the list.
180 - a list : the matching node
181 - nil : the point is not looking at a tag.
182 - a cons cell: the first element is the DTD, the second is the node"
184 ;; Processing instructions (like the <?xml version="1.0"?> tag at the
185 ;; beginning of a document)
187 (search-forward "?>" end
)
188 (skip-chars-forward " \t\n")
190 ;; Character data (CDATA) sections, in which no tag should be interpreted
191 ((looking-at "<!\\[CDATA\\[")
192 (let ((pos (match-end 0)))
193 (unless (search-forward "]]>" end t
)
194 (error "CDATA section does not end anywhere in the document"))
195 (buffer-substring-no-properties pos
(match-beginning 0))))
196 ;; DTD for the document
197 ((looking-at "<!DOCTYPE")
200 (set 'dtd
(xml-parse-dtd end
))
202 (skip-chars-forward " \t\n")
204 (cons dtd
(xml-parse-tag end
))
205 (xml-parse-tag end
))))
208 (search-forward "-->" end
)
209 (skip-chars-forward " \t\n")
215 ((looking-at "<\\([^/> \t\n]+\\)")
216 (let* ((node-name (match-string 1))
217 (children (list (intern node-name
)))
218 (case-fold-search nil
) ;; XML is case-sensitive
220 (goto-char (match-end 1))
222 ;; parses the attribute list
223 (set 'children
(append children
(list (xml-parse-attlist end
))))
225 ;; is this an empty element ?
226 (if (looking-at "/>")
229 (skip-chars-forward " \t\n")
230 (append children
'("")))
232 ;; is this a valid start tag ?
233 (if (= (char-after) ?
>)
236 (skip-chars-forward " \t\n")
237 ;; Now check that we have the right end-tag. Note that this one might
238 ;; contain spaces after the tag name
239 (while (not (looking-at (concat "</" node-name
"[ \t\n]*>")))
243 "XML: invalid syntax -- invalid end tag (expecting "
245 ") at pos " (number-to-string (point)))))
247 (set 'children
(append children
(list (xml-parse-tag end
)))))
250 (search-forward "<" end
)
252 (let ((string (buffer-substring-no-properties pos
(point)))
255 ;; Clean up the string (no newline characters)
256 ;; Not done, since as per XML specifications, the XML processor
257 ;; should always pass the whole string to the application.
258 ;; (while (string-match "\\s +" string pos)
259 ;; (set 'string (replace-match " " t t string))
260 ;; (set 'pos (1+ (match-beginning 0))))
262 (set 'children
(append children
263 (list (xml-substitute-special string
))))))))
264 (goto-char (match-end 0))
265 (skip-chars-forward " \t\n")
267 (error "XML: End tag for %s not found before end of region."
272 ;; This was an invalid start tag
273 (error "XML: Invalid attribute list")
275 (t ;; This is not a tag.
276 (error "XML: Invalid character."))
279 (defun xml-parse-attlist (end)
280 "Return the attribute-list that point is looking at.
281 The search for attributes end at the position END in the current buffer.
282 Leaves the point on the first non-blank character after the tag."
285 (skip-chars-forward " \t\n")
286 (while (looking-at "\\([a-zA-Z_:][-a-zA-Z0-9._:]*\\)[ \t\n]*=[ \t\n]*")
287 (set 'name
(intern (match-string 1)))
288 (goto-char (match-end 0))
290 ;; Do we have a string between quotes (or double-quotes),
291 ;; or a simple word ?
292 (unless (looking-at "\"\\([^\"]+\\)\"")
293 (unless (looking-at "'\\([^\"]+\\)'")
294 (error "XML: Attribute values must be given between quotes.")))
296 ;; Each attribute must be unique within a given element
297 (if (assoc name attlist
)
298 (error "XML: each attribute must be unique within an element."))
300 (set 'attlist
(append attlist
301 (list (cons name
(match-string-no-properties 1)))))
302 (goto-char (match-end 0))
303 (skip-chars-forward " \t\n")
305 (error "XML: end of attribute list not found before end of region."))
310 ;;*******************************************************************
312 ;;** The DTD (document type declaration)
313 ;;** The following functions know how to skip or parse the DTD of
316 ;;*******************************************************************
318 (defun xml-skip-dtd (end)
319 "Skip the DTD that point is looking at.
320 The DTD must end before the position END in the current buffer.
321 The point must be just before the starting tag of the DTD.
322 This follows the rule [28] in the XML specifications."
323 (forward-char (length "<!DOCTYPE"))
324 (if (looking-at "[ \t\n]*>")
325 (error "XML: invalid DTD (excepting name of the document)"))
328 (forward-word 1) ;; name of the document
329 (skip-chars-forward " \t\n")
330 (if (looking-at "\\[")
331 (re-search-forward "\\][ \t\n]*>" end
)
332 (search-forward ">" end
)))
333 (error (error "XML: No end to the DTD"))))
335 (defun xml-parse-dtd (end)
336 "Parse the DTD that point is looking at.
337 The DTD must end before the position END in the current buffer."
338 (let (dtd type element end-pos
)
339 (forward-char (length "<!DOCTYPE"))
340 (skip-chars-forward " \t\n")
342 (error "XML: invalid DTD (excepting name of the document)"))
344 ;; Get the name of the document
346 (set 'dtd
(list 'dtd
(match-string-no-properties 0)))
347 (goto-char (match-end 0))
349 (skip-chars-forward " \t\n")
351 ;; External DTDs => don't know how to handle them yet
352 (if (looking-at "SYSTEM")
353 (error "XML: Don't know how to handle external DTDs."))
355 (if (not (= (char-after) ?\
[))
356 (error "XML: Unknown declaration in the DTD."))
358 ;; Parse the rest of the DTD
360 (while (and (not (looking-at "[ \t\n]*\\]"))
364 ;; Translation of rule [45] of XML specifications
366 "[\t \n]*<!ELEMENT[ \t\n]+\\([a-zA-Z0-9.%;]+\\)[ \t\n]+\\([^>]+\\)>")
368 (setq element
(intern (match-string-no-properties 1))
369 type
(match-string-no-properties 2))
370 (set 'end-pos
(match-end 0))
372 ;; Translation of rule [46] of XML specifications
374 ((string-match "^EMPTY[ \t\n]*$" type
) ;; empty declaration
376 ((string-match "^ANY[ \t\n]*$" type
) ;; any type of contents
378 ((string-match "^(\\(.*\\))[ \t\n]*$" type
) ;; children ([47])
379 (set 'type
(xml-parse-elem-type (match-string-no-properties 1 type
))))
380 ((string-match "^%[^;]+;[ \t\n]*$" type
) ;; substitution
383 (error "XML: Invalid element type in the DTD")))
385 ;; rule [45]: the element declaration must be unique
386 (if (assoc element dtd
)
387 (error "XML: elements declaration must be unique in a DTD (<%s>)."
388 (symbol-name element
)))
390 ;; Store the element in the DTD
391 (set 'dtd
(append dtd
(list (list element type
))))
397 (error "XML: Invalid DTD item"))
401 ;; Skip the end of the DTD
402 (search-forward ">" end
)
407 (defun xml-parse-elem-type (string)
408 "Convert a STRING for an element type into an elisp structure."
411 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string
)
413 (setq elem
(match-string 1 string
)
414 modifier
(match-string 2 string
))
415 (if (string-match "|" elem
)
416 (set 'elem
(append '(choice)
417 (mapcar 'xml-parse-elem-type
418 (split-string elem
"|"))))
419 (if (string-match "," elem
)
420 (set 'elem
(append '(seq)
421 (mapcar 'xml-parse-elem-type
422 (split-string elem
","))))
424 (if (string-match "[ \t\n]*\\([^+*?]+\\)\\([+*?]?\\)" string
)
425 (setq elem
(match-string 1 string
)
426 modifier
(match-string 2 string
))))
428 (if (and (stringp elem
)
429 (string= elem
"#PCDATA"))
433 ((string= modifier
"+")
435 ((string= modifier
"*")
437 ((string= modifier
"?")
443 ;;*******************************************************************
445 ;;** Substituting special XML sequences
447 ;;*******************************************************************
449 (defun xml-substitute-special (string)
450 "Return STRING, after subsituting special XML sequences."
451 (while (string-match "&" string
)
452 (set 'string
(replace-match "&" t nil string
)))
453 (while (string-match "<" string
)
454 (set 'string
(replace-match "<" t nil string
)))
455 (while (string-match ">" string
)
456 (set 'string
(replace-match ">" t nil string
)))
457 (while (string-match "'" string
)
458 (set 'string
(replace-match "'" t nil string
)))
459 (while (string-match """ string
)
460 (set 'string
(replace-match "\"" t nil string
)))
463 ;;*******************************************************************
465 ;;** Printing a tree.
466 ;;** This function is intended mainly for debugging purposes.
468 ;;*******************************************************************
470 (defun xml-debug-print (xml)
472 (xml-debug-print-internal (car xml
) "")
473 (set 'xml
(cdr xml
)))
476 (defun xml-debug-print-internal (xml &optional indent-string
)
477 "Outputs the XML tree in the current buffer.
478 The first line indented with INDENT-STRING."
481 (unless indent-string
482 (set 'indent-string
""))
484 (insert indent-string
"<" (symbol-name (xml-node-name tree
)))
486 ;; output the attribute list
487 (set 'attlist
(xml-node-attributes tree
))
490 (insert (symbol-name (caar attlist
)) "=\"" (cdar attlist
) "\"")
491 (set 'attlist
(cdr attlist
)))
495 (set 'tree
(xml-node-children tree
))
497 ;; output the children
502 (xml-debug-print-internal (car tree
) (concat indent-string
" "))
504 ((stringp (car tree
))
508 (error "Invalid XML tree")))
509 (set 'tree
(cdr tree
))
512 (insert "\n" indent-string
513 "</" (symbol-name (xml-node-name xml
)) ">")