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 Returns the top node with all its children.
122 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped."
124 (let ((xml (xml-parse-region (point-min)
128 (kill-buffer (current-buffer))
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
)
143 (while (< (point) end
)
144 (if (search-forward "<" end t
)
149 (set 'result
(xml-parse-tag end parse-dtd
))
151 ((listp (car result
))
152 (set 'dtd
(car result
))
153 (add-to-list 'xml
(cdr result
)))
155 (add-to-list 'xml result
))))
157 ;; translation of rule [1] of XML specifications
158 (error "XML files can have only one toplevel tag.")))
161 (cons dtd
(reverse xml
))
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.
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"
175 ;; Processing instructions (like the <?xml version="1.0"?> tag at the
176 ;; beginning of a document)
178 (search-forward "?>" end
)
179 (skip-chars-forward " \t\n")
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")
191 (set 'dtd
(xml-parse-dtd end
))
193 (skip-chars-forward " \t\n")
195 (cons dtd
(xml-parse-tag end
))
196 (xml-parse-tag end
))))
199 (search-forward "-->" end
)
200 (skip-chars-forward " \t\n")
206 ((looking-at "<\\([^/> \t]+\\)")
207 (let* ((node-name (match-string 1))
208 (children (list (intern node-name
)))
210 (goto-char (match-end 1))
212 ;; parses the attribute list
213 (set 'children
(append children
(list (xml-parse-attlist end
))))
215 ;; is this an empty element ?
216 (if (looking-at "/>")
219 (skip-chars-forward " \t\n")
220 (append children
'("")))
222 ;; is this a valid start tag ?
223 (if (= (char-after) ?
>)
226 (skip-chars-forward " \t\n")
227 (while (not (looking-at (concat "</" node-name
">")))
231 "XML: invalid syntax -- invalid end tag (expecting "
235 (set 'children
(append children
(list (xml-parse-tag end
)))))
238 (search-forward "<" end
)
240 (let ((string (buffer-substring-no-properties pos
(point)))
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))))
250 (set 'children
(append children
251 (list (xml-substitute-special string
))))))))
252 (goto-char (match-end 0))
253 (skip-chars-forward " \t\n")
255 (error "XML: End tag for %s not found before end of region."
260 ;; This was an invalid start tag
261 (error "XML: Invalid attribute list")
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."
271 (skip-chars-forward " \t\n")
272 (while (looking-at "\\([a-zA-Z_:][a-zA-Z0-9.-_:]*\\)[ \t\n]*=[ \t\n]*")
273 (set 'name
(intern (match-string 1)))
274 (goto-char (match-end 0))
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.")))
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."))
286 (set 'attlist
(append attlist
287 (list (cons name
(match-string 1)))))
288 (goto-char (match-end 0))
289 (skip-chars-forward " \t\n")
291 (error "XML: end of attribute list not found before end of region."))
296 ;;*******************************************************************
298 ;;** The DTD (document type declaration)
299 ;;** The following functions know how to skip or parse the DTD of
302 ;;*******************************************************************
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)"))
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"))))
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")
328 (error "XML: invalid DTD (excepting name of the document)"))
330 ;; Get the name of the document
332 (set 'dtd
(list 'dtd
(match-string-no-properties 0)))
333 (goto-char (match-end 0))
335 (skip-chars-forward " \t\n")
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."))
341 (if (not (= (char-after) ?\
[))
342 (error "XML: Unknown declaration in the DTD."))
344 ;; Parse the rest of the DTD
346 (while (and (not (looking-at "[ \t\n]*\\]"))
350 ;; Translation of rule [45] of XML specifications
352 "[\t \n]*<!ELEMENT[ \t\n]+\\([a-zA-Z0-9.%;]+\\)[ \t\n]+\\([^>]+\\)>")
354 (setq element
(intern (match-string-no-properties 1))
355 type
(match-string-no-properties 2))
356 (set 'end-pos
(match-end 0))
358 ;; Translation of rule [46] of XML specifications
360 ((string-match "^EMPTY[ \t\n]*$" type
) ;; empty declaration
362 ((string-match "^ANY[ \t\n]*$" type
) ;; any type of contents
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
369 (error "XML: Invalid element type in the DTD")))
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
)))
376 ;; Store the element in the DTD
377 (set 'dtd
(append dtd
(list (list element type
))))
383 (error "XML: Invalid DTD item"))
387 ;; Skip the end of the DTD
388 (search-forward ">" end
)
393 (defun xml-parse-elem-type (string)
394 "Convert a STRING for an element type into an elisp structure."
397 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string
)
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
","))))
410 (if (string-match "[ \t\n]*\\([^+*?]+\\)\\([+*?]?\\)" string
)
411 (setq elem
(match-string 1 string
)
412 modifier
(match-string 2 string
))))
414 (if (and (stringp elem
)
415 (string= elem
"#PCDATA"))
419 ((string= modifier
"+")
421 ((string= modifier
"*")
423 ((string= modifier
"?")
429 ;;*******************************************************************
431 ;;** Substituting special XML sequences
433 ;;*******************************************************************
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
)))
449 ;;*******************************************************************
451 ;;** Printing a tree.
452 ;;** This function is intended mainly for debugging purposes.
454 ;;*******************************************************************
456 (defun xml-debug-print (xml)
458 (xml-debug-print-internal (car xml
) "")
459 (set 'xml
(cdr xml
)))
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."
467 (unless indent-string
468 (set 'indent-string
""))
470 (insert indent-string
"<" (symbol-name (xml-node-name tree
)))
472 ;; output the attribute list
473 (set 'attlist
(xml-node-attributes tree
))
476 (insert (symbol-name (caar attlist
)) "=\"" (cdar attlist
) "\"")
477 (set 'attlist
(cdr attlist
)))
481 (set 'tree
(xml-node-children tree
))
483 ;; output the children
488 (xml-debug-print-internal (car tree
) (concat indent-string
" "))
490 ((stringp (car tree
))
494 (error "Invalid XML tree")))
495 (set 'tree
(cdr tree
))
498 (insert "\n" indent-string
499 "</" (symbol-name (xml-node-name xml
)) ">")