Testing cvslog 4.
[gnus] / contrib / xml.el
1 ;;; xml.el --- XML parser
2
3 ;; Copyright (C) 2000, 2001 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 ;;
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 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."
124   (let ((keep))
125     (if (get-file-buffer file)
126         (progn
127           (set-buffer (get-file-buffer file))
128           (setq keep (point)))
129       (find-file file))
130     
131     (let ((xml (xml-parse-region (point-min)
132                                  (point-max)
133                                  (current-buffer)
134                                  parse-dtd)))
135       (if keep
136           (goto-char keep)
137         (kill-buffer (current-buffer)))
138       xml)))
139
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)
148     (save-excursion
149       (if buffer
150           (set-buffer buffer))
151       (goto-char beg)
152       (while (< (point) end)
153         (if (search-forward "<" end t)
154             (progn
155               (forward-char -1)
156               (if (null xml)
157                   (progn
158                     (set 'result (xml-parse-tag end parse-dtd))
159                     (cond
160                      ((listp (car result))
161                       (set 'dtd (car result))
162                       (add-to-list 'xml (cdr result)))
163                      (t
164                       (add-to-list 'xml result))))
165
166                 ;;  translation of rule [1] of XML specifications
167                 (error "XML files can have only one toplevel tag.")))
168           (goto-char end)))
169       (if parse-dtd
170           (cons dtd (reverse xml))
171         (reverse xml)))))
172
173
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.
179 Returns one of:
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"
183   (cond
184    ;; Processing instructions (like the <?xml version="1.0"?> tag at the
185    ;; beginning of a document)
186    ((looking-at "<\\?")
187     (search-forward "?>" end)
188     (skip-chars-forward " \t\n")
189     (xml-parse-tag end))
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")
198     (let (dtd)
199       (if parse-dtd
200           (set 'dtd (xml-parse-dtd end))
201         (xml-skip-dtd end))
202       (skip-chars-forward " \t\n")
203       (if dtd
204           (cons dtd (xml-parse-tag end))
205         (xml-parse-tag end))))
206    ;;  skip comments
207    ((looking-at "<!--")
208     (search-forward "-->" end)
209     (skip-chars-forward " \t\n")
210     (xml-parse-tag end))
211    ;;  end tag
212    ((looking-at "</")
213     '())
214    ;;  opening tag
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
219            pos)
220       (goto-char (match-end 1))
221
222       ;; parses the attribute list
223       (set 'children (append children (list (xml-parse-attlist end))))
224
225       ;; is this an empty element ?
226       (if (looking-at "/>")
227           (progn
228             (forward-char 2)
229             (skip-chars-forward " \t\n")
230             (append children '("")))
231
232         ;; is this a valid start tag ?
233         (if (= (char-after) ?>)
234             (progn
235               (forward-char 1)
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]*>")))
240                 (cond
241                  ((looking-at "</")
242                   (error (concat
243                           "XML: invalid syntax -- invalid end tag (expecting "
244                           node-name
245                           ") at pos " (number-to-string (point)))))
246                  ((= (char-after) ?<)
247                   (set 'children (append children (list (xml-parse-tag end)))))
248                  (t
249                   (set 'pos (point))
250                   (search-forward "<" end)
251                   (forward-char -1)
252                   (let ((string (buffer-substring-no-properties pos (point)))
253                         (pos 0))
254                     
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))))
261                     
262                     (set 'children (append children
263                                            (list (xml-substitute-special string))))))))
264               (goto-char (match-end 0))
265               (skip-chars-forward " \t\n")
266               (if (> (point) end)
267                   (error "XML: End tag for %s not found before end of region."
268                          node-name))
269               children
270               )
271
272           ;;  This was an invalid start tag
273           (error "XML: Invalid attribute list")
274           ))))
275    (t ;; This is not a tag.
276     (error "XML: Invalid character."))
277    ))
278
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."
283   (let ((attlist '())
284         name)
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))
289
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.")))
295
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."))
299       
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")
304       (if (> (point) end)
305           (error "XML: end of attribute list not found before end of region."))
306       )
307     attlist
308     ))
309
310 ;;*******************************************************************
311 ;;**
312 ;;**  The DTD (document type declaration)
313 ;;**  The following functions know how to skip or parse the DTD of
314 ;;**  a document
315 ;;**
316 ;;*******************************************************************
317
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)"))
326   (condition-case nil
327       (progn
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"))))
334
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")
341     (if (looking-at ">")
342         (error "XML: invalid DTD (excepting name of the document)"))
343
344     ;;  Get the name of the document
345     (looking-at "\\sw+")
346     (set 'dtd (list 'dtd (match-string-no-properties 0)))
347     (goto-char (match-end 0))
348
349     (skip-chars-forward " \t\n")
350
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."))
354     
355     (if (not (= (char-after) ?\[))
356         (error "XML: Unknown declaration in the DTD."))
357
358     ;;  Parse the rest of the DTD
359     (forward-char 1)
360     (while (and (not (looking-at "[ \t\n]*\\]"))
361                 (<= (point) end))
362       (cond
363
364        ;;  Translation of rule [45] of XML specifications
365        ((looking-at
366          "[\t \n]*<!ELEMENT[ \t\n]+\\([a-zA-Z0-9.%;]+\\)[ \t\n]+\\([^>]+\\)>")
367
368         (setq element (intern (match-string-no-properties 1))
369               type    (match-string-no-properties 2))
370         (set 'end-pos (match-end 0))
371         
372         ;;  Translation of rule [46] of XML specifications
373         (cond
374          ((string-match "^EMPTY[ \t\n]*$" type)     ;; empty declaration
375           (set 'type 'empty))
376          ((string-match "^ANY[ \t\n]*$" type)       ;; any type of contents
377           (set 'type 'any))
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
381           nil)
382          (t
383           (error "XML: Invalid element type in the DTD")))
384
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)))
389         
390         ;;  Store the element in the DTD
391         (set 'dtd (append dtd (list (list element type))))
392         (goto-char end-pos)
393         )
394
395
396        (t
397         (error "XML: Invalid DTD item"))
398        )
399       )
400
401     ;;  Skip the end of the DTD
402     (search-forward ">" end)
403   dtd
404   ))
405
406
407 (defun xml-parse-elem-type (string)
408   "Convert a STRING for an element type into an elisp structure."
409
410   (let (elem modifier)
411     (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string)
412         (progn
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 ","))))
423               )))
424       (if (string-match "[ \t\n]*\\([^+*?]+\\)\\([+*?]?\\)" string)
425           (setq elem     (match-string 1 string)
426                 modifier (match-string 2 string))))
427
428       (if (and (stringp elem)
429                (string= elem "#PCDATA"))
430           (set 'elem 'pcdata))
431     
432       (cond
433        ((string= modifier "+")
434         (list '+ elem))
435        ((string= modifier "*")
436         (list '* elem))
437        ((string= modifier "?")
438         (list '? elem))
439        (t
440         elem))))
441
442
443 ;;*******************************************************************
444 ;;**
445 ;;**  Substituting special XML sequences
446 ;;**
447 ;;*******************************************************************
448
449 (defun xml-substitute-special (string)
450   "Return STRING, after subsituting special XML sequences."
451   (while (string-match "&amp;" string)
452     (set 'string (replace-match "&"  t nil string)))
453   (while (string-match "&lt;" string)
454     (set 'string (replace-match "<"  t nil string)))
455   (while (string-match "&gt;" string)
456     (set 'string (replace-match ">"  t nil string)))
457   (while (string-match "&apos;" string)
458     (set 'string (replace-match "'"  t nil string)))
459   (while (string-match "&quot;" string)
460     (set 'string (replace-match "\"" t nil string)))
461   string)
462
463 ;;*******************************************************************
464 ;;**
465 ;;**  Printing a tree.
466 ;;**  This function is intended mainly for debugging purposes.
467 ;;**
468 ;;*******************************************************************
469
470 (defun xml-debug-print (xml)
471   (while xml
472     (xml-debug-print-internal (car xml) "")
473     (set 'xml (cdr xml)))
474   )
475
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."
479   (let ((tree xml)
480         attlist)
481     (unless indent-string
482       (set 'indent-string ""))
483     
484     (insert indent-string "<" (symbol-name (xml-node-name tree)))
485     
486     ;;  output the attribute list
487     (set 'attlist (xml-node-attributes tree))
488     (while attlist
489       (insert " ")
490       (insert (symbol-name (caar attlist)) "=\"" (cdar attlist) "\"")
491       (set 'attlist (cdr attlist)))
492     
493     (insert ">")
494     
495     (set 'tree (xml-node-children tree))
496
497     ;;  output the children
498     (while tree
499       (cond
500        ((listp (car tree))
501         (insert "\n")
502         (xml-debug-print-internal (car tree) (concat indent-string "  "))
503         )
504        ((stringp (car tree))
505         (insert (car tree))
506         )
507        (t
508         (error "Invalid XML tree")))
509       (set 'tree (cdr tree))
510      )
511
512     (insert "\n" indent-string
513             "</" (symbol-name (xml-node-name xml)) ">")
514     ))
515
516 (provide 'xml)
517
518 ;;; xml.el ends here