1 ;; @(#) xml.el --- XML parser
3 ;; Copyright (C) 2000 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")
277 (defun xml-parse-attlist (end)
278 "Return the attribute-list that point is looking at.
279 The search for attributes end at the position END in the current buffer.
280 Leaves the point on the first non-blank character after the tag."
283 (skip-chars-forward " \t\n")
284 (while (looking-at "\\([a-zA-Z_:][-a-zA-Z0-9._:]*\\)[ \t\n]*=[ \t\n]*")
285 (set 'name
(intern (match-string 1)))
286 (goto-char (match-end 0))
288 ;; Do we have a string between quotes (or double-quotes),
289 ;; or a simple word ?
290 (unless (looking-at "\"\\([^\"]+\\)\"")
291 (unless (looking-at "'\\([^\"]+\\)'")
292 (error "XML: Attribute values must be given between quotes.")))
294 ;; Each attribute must be unique within a given element
295 (if (assoc name attlist
)
296 (error "XML: each attribute must be unique within an element."))
298 (set 'attlist
(append attlist
299 (list (cons name
(match-string-no-properties 1)))))
300 (goto-char (match-end 0))
301 (skip-chars-forward " \t\n")
303 (error "XML: end of attribute list not found before end of region."))
308 ;;*******************************************************************
310 ;;** The DTD (document type declaration)
311 ;;** The following functions know how to skip or parse the DTD of
314 ;;*******************************************************************
316 (defun xml-skip-dtd (end)
317 "Skip the DTD that point is looking at.
318 The DTD must end before the position END in the current buffer.
319 The point must be just before the starting tag of the DTD.
320 This follows the rule [28] in the XML specifications."
321 (forward-char (length "<!DOCTYPE"))
322 (if (looking-at "[ \t\n]*>")
323 (error "XML: invalid DTD (excepting name of the document)"))
326 (forward-word 1) ;; name of the document
327 (skip-chars-forward " \t\n")
328 (if (looking-at "\\[")
329 (re-search-forward "\\][ \t\n]*>" end
)
330 (search-forward ">" end
)))
331 (error (error "XML: No end to the DTD"))))
333 (defun xml-parse-dtd (end)
334 "Parse the DTD that point is looking at.
335 The DTD must end before the position END in the current buffer."
336 (let (dtd type element end-pos
)
337 (forward-char (length "<!DOCTYPE"))
338 (skip-chars-forward " \t\n")
340 (error "XML: invalid DTD (excepting name of the document)"))
342 ;; Get the name of the document
344 (set 'dtd
(list 'dtd
(match-string-no-properties 0)))
345 (goto-char (match-end 0))
347 (skip-chars-forward " \t\n")
349 ;; External DTDs => don't know how to handle them yet
350 (if (looking-at "SYSTEM")
351 (error "XML: Don't know how to handle external DTDs."))
353 (if (not (= (char-after) ?\
[))
354 (error "XML: Unknown declaration in the DTD."))
356 ;; Parse the rest of the DTD
358 (while (and (not (looking-at "[ \t\n]*\\]"))
362 ;; Translation of rule [45] of XML specifications
364 "[\t \n]*<!ELEMENT[ \t\n]+\\([a-zA-Z0-9.%;]+\\)[ \t\n]+\\([^>]+\\)>")
366 (setq element
(intern (match-string-no-properties 1))
367 type
(match-string-no-properties 2))
368 (set 'end-pos
(match-end 0))
370 ;; Translation of rule [46] of XML specifications
372 ((string-match "^EMPTY[ \t\n]*$" type
) ;; empty declaration
374 ((string-match "^ANY[ \t\n]*$" type
) ;; any type of contents
376 ((string-match "^(\\(.*\\))[ \t\n]*$" type
) ;; children ([47])
377 (set 'type
(xml-parse-elem-type (match-string-no-properties 1 type
))))
378 ((string-match "^%[^;]+;[ \t\n]*$" type
) ;; substitution
381 (error "XML: Invalid element type in the DTD")))
383 ;; rule [45]: the element declaration must be unique
384 (if (assoc element dtd
)
385 (error "XML: elements declaration must be unique in a DTD (<%s>)."
386 (symbol-name element
)))
388 ;; Store the element in the DTD
389 (set 'dtd
(append dtd
(list (list element type
))))
395 (error "XML: Invalid DTD item"))
399 ;; Skip the end of the DTD
400 (search-forward ">" end
)
405 (defun xml-parse-elem-type (string)
406 "Convert a STRING for an element type into an elisp structure."
409 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string
)
411 (setq elem
(match-string 1 string
)
412 modifier
(match-string 2 string
))
413 (if (string-match "|" elem
)
414 (set 'elem
(append '(choice)
415 (mapcar 'xml-parse-elem-type
416 (split-string elem
"|"))))
417 (if (string-match "," elem
)
418 (set 'elem
(append '(seq)
419 (mapcar 'xml-parse-elem-type
420 (split-string elem
","))))
422 (if (string-match "[ \t\n]*\\([^+*?]+\\)\\([+*?]?\\)" string
)
423 (setq elem
(match-string 1 string
)
424 modifier
(match-string 2 string
))))
426 (if (and (stringp elem
)
427 (string= elem
"#PCDATA"))
431 ((string= modifier
"+")
433 ((string= modifier
"*")
435 ((string= modifier
"?")
441 ;;*******************************************************************
443 ;;** Substituting special XML sequences
445 ;;*******************************************************************
447 (defun xml-substitute-special (string)
448 "Return STRING, after subsituting special XML sequences."
449 (while (string-match "&" string
)
450 (set 'string
(replace-match "&" t nil string
)))
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
)))
461 ;;*******************************************************************
463 ;;** Printing a tree.
464 ;;** This function is intended mainly for debugging purposes.
466 ;;*******************************************************************
468 (defun xml-debug-print (xml)
470 (xml-debug-print-internal (car xml
) "")
471 (set 'xml
(cdr xml
)))
474 (defun xml-debug-print-internal (xml &optional indent-string
)
475 "Outputs the XML tree in the current buffer.
476 The first line indented with INDENT-STRING."
479 (unless indent-string
480 (set 'indent-string
""))
482 (insert indent-string
"<" (symbol-name (xml-node-name tree
)))
484 ;; output the attribute list
485 (set 'attlist
(xml-node-attributes tree
))
488 (insert (symbol-name (caar attlist
)) "=\"" (cdar attlist
) "\"")
489 (set 'attlist
(cdr attlist
)))
493 (set 'tree
(xml-node-children tree
))
495 ;; output the children
500 (xml-debug-print-internal (car tree
) (concat indent-string
" "))
502 ((stringp (car tree
))
506 (error "Invalid XML tree")))
507 (set 'tree
(cdr tree
))
510 (insert "\n" indent-string
511 "</" (symbol-name (xml-node-name xml
)) ">")