Mercurial > emacs
annotate lisp/xml.el @ 51888:b5a29d6f2851
(c-declare-lang-variables): Don't use mapcan.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 13 Jul 2003 00:20:27 +0000 |
parents | 79747466c43b |
children | 608552c82ffc |
rev | line source |
---|---|
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
1 ;;; xml.el --- XML parser |
30329 | 2 |
51102 | 3 ;; Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc. |
30329 | 4 |
5 ;; Author: Emmanuel Briot <briot@gnat.com> | |
51697 | 6 ;; Maintainer: Mark A. Hershberger <mah@everybody.org> |
51102 | 7 ;; Keywords: xml, data |
30329 | 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 | |
51102 | 28 ;; This file contains a somewhat incomplete non-validating XML parser. It |
29 ;; parses a file, and returns a list that can be used internally by | |
30 ;; any other lisp libraries. | |
30329 | 31 |
32 ;;; FILE FORMAT | |
33 | |
51102 | 34 ;; The document type declaration may either be ignored or (optionally) |
35 ;; parsed, but currently the parsing will only accept element | |
36 ;; declarations. The XML file is assumed to be well-formed. In case | |
37 ;; of error, the parsing stops and the XML file is shown where the | |
38 ;; parsing stopped. | |
30329 | 39 ;; |
51102 | 40 ;; It also knows how to ignore comments and processing instructions. |
30329 | 41 ;; |
42 ;; The XML file should have the following format: | |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
43 ;; <node1 attr1="name1" attr2="name2" ...>value |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
44 ;; <node2 attr3="name3" attr4="name4">value2</node2> |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
45 ;; <node3 attr5="name5" attr6="name6">value3</node3> |
30329 | 46 ;; </node1> |
47 ;; Of course, the name of the nodes and attributes can be anything. There can | |
48 ;; be any number of attributes (or none), as well as any number of children | |
49 ;; below the nodes. | |
50 ;; | |
51 ;; There can be only top level node, but with any number of children below. | |
52 | |
53 ;;; LIST FORMAT | |
54 | |
55 ;; The functions `xml-parse-file' and `xml-parse-tag' return a list with | |
56 ;; the following format: | |
57 ;; | |
58 ;; xml-list ::= (node node ...) | |
59 ;; node ::= (tag_name attribute-list . child_node_list) | |
60 ;; child_node_list ::= child_node child_node ... | |
61 ;; child_node ::= node | string | |
62 ;; tag_name ::= string | |
63 ;; attribute_list ::= (("attribute" . "value") ("attribute" . "value") ...) | |
64 ;; | nil | |
65 ;; string ::= "..." | |
66 ;; | |
51102 | 67 ;; Some macros are provided to ease the parsing of this list. |
68 ;; Whitespace is preserved. Fixme: There should be a tree-walker that | |
69 ;; can remove it. | |
30329 | 70 |
71 ;;; Code: | |
72 | |
51102 | 73 ;; Note that {buffer-substring,match-string}-no-properties were |
74 ;; formerly used in several places, but that removes composition info. | |
75 | |
30329 | 76 ;;******************************************************************* |
77 ;;** | |
78 ;;** Macros to parse the list | |
79 ;;** | |
80 ;;******************************************************************* | |
81 | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
82 (defsubst xml-node-name (node) |
30329 | 83 "Return the tag associated with NODE. |
84 The tag is a lower-case symbol." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
85 (car node)) |
30329 | 86 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
87 (defsubst xml-node-attributes (node) |
30329 | 88 "Return the list of attributes of NODE. |
89 The list can be nil." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
90 (nth 1 node)) |
30329 | 91 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
92 (defsubst xml-node-children (node) |
30329 | 93 "Return the list of children of NODE. |
94 This is a list of nodes, and it can be nil." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
95 (cddr node)) |
30329 | 96 |
97 (defun xml-get-children (node child-name) | |
98 "Return the children of NODE whose tag is CHILD-NAME. | |
99 CHILD-NAME should be a lower case symbol." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
100 (let ((match ())) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
101 (dolist (child (xml-node-children node)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
102 (if child |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
103 (if (equal (xml-node-name child) child-name) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
104 (push child match)))) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
105 (nreverse match))) |
30329 | 106 |
107 (defun xml-get-attribute (node attribute) | |
108 "Get from NODE the value of ATTRIBUTE. | |
109 An empty string is returned if the attribute was not found." | |
110 (if (xml-node-attributes node) | |
111 (let ((value (assoc attribute (xml-node-attributes node)))) | |
112 (if value | |
113 (cdr value) | |
114 "")) | |
115 "")) | |
116 | |
117 ;;******************************************************************* | |
118 ;;** | |
119 ;;** Creating the list | |
120 ;;** | |
121 ;;******************************************************************* | |
122 | |
51102 | 123 ;;;###autoload |
30329 | 124 (defun xml-parse-file (file &optional parse-dtd) |
51102 | 125 "Parse the well-formed XML file FILE. |
126 If FILE is already visited, use its buffer and don't kill it. | |
30329 | 127 Returns the top node with all its children. |
128 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped." | |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
129 (let ((keep)) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
130 (if (get-file-buffer file) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
131 (progn |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
132 (set-buffer (get-file-buffer file)) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
133 (setq keep (point))) |
51102 | 134 (let (auto-mode-alist) ; no need for xml-mode |
135 (find-file file))) | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
136 |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
137 (let ((xml (xml-parse-region (point-min) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
138 (point-max) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
139 (current-buffer) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
140 parse-dtd))) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
141 (if keep |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
142 (goto-char keep) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
143 (kill-buffer (current-buffer))) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
144 xml))) |
30329 | 145 |
51102 | 146 ;; Note that this is setup so that we can do whitespace-skipping with |
147 ;; `(skip-syntax-forward " ")', inter alia. Previously this was slow | |
148 ;; compared with `re-search-forward', but that has been fixed. Also | |
149 ;; note that the standard syntax table contains other characters with | |
150 ;; whitespace syntax, like NBSP, but they are invalid in contexts in | |
151 ;; which we might skip whitespace -- specifically, they're not | |
152 ;; NameChars [XML 4]. | |
153 | |
154 (defvar xml-syntax-table | |
155 (let ((table (make-syntax-table))) | |
156 ;; Get space syntax correct per XML [3]. | |
157 (dotimes (c 31) | |
158 (modify-syntax-entry c "." table)) ; all are space in standard table | |
159 (dolist (c '(?\t ?\n ?\r)) ; these should be space | |
160 (modify-syntax-entry c " " table)) | |
161 ;; For skipping attributes. | |
162 (modify-syntax-entry ?\" "\"" table) | |
163 (modify-syntax-entry ?' "\"" table) | |
164 ;; Non-alnum name chars should be symbol constituents (`-' and `_' | |
165 ;; are OK by default). | |
166 (modify-syntax-entry ?. "_" table) | |
167 (modify-syntax-entry ?: "_" table) | |
168 ;; XML [89] | |
169 (dolist (c '(#x00B7 #x02D0 #x02D1 #x0387 #x0640 #x0E46 #x0EC6 #x3005 | |
170 #x3031 #x3032 #x3033 #x3034 #x3035 #x309D #x309E #x30FC | |
171 #x30FD #x30FE)) | |
172 (modify-syntax-entry (decode-char 'ucs c) "w" table)) | |
173 ;; Fixme: rest of [4] | |
174 table) | |
175 "Syntax table used by `xml-parse-region'.") | |
176 | |
177 ;; XML [5] | |
178 ;; Note that [:alpha:] matches all multibyte chars with word syntax. | |
51105
aac5eaf1454e
(xml-name-regexp): Wrap in `eval-and-compile'.
John Paul Wallington <jpw@pobox.com>
parents:
51102
diff
changeset
|
179 (eval-and-compile |
aac5eaf1454e
(xml-name-regexp): Wrap in `eval-and-compile'.
John Paul Wallington <jpw@pobox.com>
parents:
51102
diff
changeset
|
180 (defconst xml-name-regexp "[[:alpha:]_:][[:alnum:]._:-]*")) |
51102 | 181 |
182 ;; Fixme: This needs re-writing to deal with the XML grammar properly, i.e. | |
183 ;; document ::= prolog element Misc* | |
184 ;; prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? | |
185 | |
186 ;;;###autoload | |
30329 | 187 (defun xml-parse-region (beg end &optional buffer parse-dtd) |
188 "Parse the region from BEG to END in BUFFER. | |
189 If BUFFER is nil, it defaults to the current buffer. | |
190 Returns the XML list for the region, or raises an error if the region | |
191 is not a well-formed XML file. | |
192 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped, | |
51102 | 193 and returned as the first element of the list." |
194 (save-restriction | |
195 (narrow-to-region beg end) | |
196 ;; Use fixed syntax table to ensure regexp char classes and syntax | |
197 ;; specs DTRT. | |
198 (with-syntax-table (standard-syntax-table) | |
199 (let ((case-fold-search nil) ; XML is case-sensitive. | |
200 xml result dtd) | |
201 (save-excursion | |
202 (if buffer | |
203 (set-buffer buffer)) | |
204 (goto-char (point-min)) | |
205 (while (not (eobp)) | |
206 (if (search-forward "<" nil t) | |
207 (progn | |
208 (forward-char -1) | |
209 (if xml | |
210 ;; translation of rule [1] of XML specifications | |
211 (error "XML files can have only one toplevel tag") | |
212 (setq result (xml-parse-tag parse-dtd)) | |
30329 | 213 (cond |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
214 ((null result)) |
30329 | 215 ((listp (car result)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
216 (setq dtd (car result)) |
51102 | 217 (if (cdr result) ; possible leading comment |
218 (add-to-list 'xml (cdr result)))) | |
30329 | 219 (t |
51102 | 220 (add-to-list 'xml result))))) |
221 (goto-char (point-max)))) | |
222 (if parse-dtd | |
223 (cons dtd (nreverse xml)) | |
224 (nreverse xml))))))) | |
30329 | 225 |
226 | |
51102 | 227 (defun xml-parse-tag (&optional parse-dtd) |
228 "Parse the tag at point. | |
30329 | 229 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and |
230 returned as the first element in the list. | |
231 Returns one of: | |
51102 | 232 - a list : the matching node |
233 - nil : the point is not looking at a tag. | |
234 - a pair : the first element is the DTD, the second is the node." | |
30329 | 235 (cond |
236 ;; Processing instructions (like the <?xml version="1.0"?> tag at the | |
51102 | 237 ;; beginning of a document). |
30329 | 238 ((looking-at "<\\?") |
51102 | 239 (search-forward "?>") |
240 (skip-syntax-forward " ") | |
241 (xml-parse-tag parse-dtd)) | |
30329 | 242 ;; Character data (CDATA) sections, in which no tag should be interpreted |
243 ((looking-at "<!\\[CDATA\\[") | |
244 (let ((pos (match-end 0))) | |
51102 | 245 (unless (search-forward "]]>" nil t) |
30329 | 246 (error "CDATA section does not end anywhere in the document")) |
51102 | 247 (buffer-substring pos (match-beginning 0)))) |
30329 | 248 ;; DTD for the document |
249 ((looking-at "<!DOCTYPE") | |
250 (let (dtd) | |
251 (if parse-dtd | |
51102 | 252 (setq dtd (xml-parse-dtd)) |
253 (xml-skip-dtd)) | |
254 (skip-syntax-forward " ") | |
30329 | 255 (if dtd |
51102 | 256 (cons dtd (xml-parse-tag)) |
257 (xml-parse-tag)))) | |
30329 | 258 ;; skip comments |
259 ((looking-at "<!--") | |
51102 | 260 (search-forward "-->") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
261 nil) |
30329 | 262 ;; end tag |
263 ((looking-at "</") | |
264 '()) | |
265 ;; opening tag | |
51102 | 266 ((looking-at "<\\([^/>[:space:]]+\\)") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
267 (goto-char (match-end 1)) |
51102 | 268 (let* ((node-name (match-string 1)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
269 ;; Parse the attribute list. |
51102 | 270 (children (list (xml-parse-attlist) (intern node-name))) |
30329 | 271 pos) |
272 | |
273 ;; is this an empty element ? | |
51102 | 274 (if (looking-at "/>") |
30329 | 275 (progn |
276 (forward-char 2) | |
51334
ed3269a70a9f
(xml-parse-tag): Return (foo nil) rather than (foo nil "")
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51105
diff
changeset
|
277 (nreverse children)) |
30329 | 278 |
279 ;; is this a valid start tag ? | |
40030
7507bd185307
(xml-parse-tag): Use eq on char-after's return value.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
39407
diff
changeset
|
280 (if (eq (char-after) ?>) |
30329 | 281 (progn |
282 (forward-char 1) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
283 ;; Now check that we have the right end-tag. Note that this |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
284 ;; one might contain spaces after the tag name |
51102 | 285 (let ((end (concat "</" node-name "\\s-*>"))) |
286 (while (not (looking-at end)) | |
287 (cond | |
288 ((looking-at "</") | |
289 (error "XML: Invalid end tag (expecting %s) at pos %d" | |
290 node-name (point))) | |
291 ((= (char-after) ?<) | |
292 (let ((tag (xml-parse-tag))) | |
293 (when tag | |
294 (push tag children)))) | |
295 (t | |
296 (setq pos (point)) | |
297 (search-forward "<") | |
298 (forward-char -1) | |
299 (let ((string (buffer-substring pos (point))) | |
300 (pos 0)) | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
301 |
51102 | 302 ;; Clean up the string. As per XML |
303 ;; specifications, the XML processor should | |
304 ;; always pass the whole string to the | |
305 ;; application. But \r's should be replaced: | |
306 ;; http://www.w3.org/TR/2000/REC-xml-20001006#sec-line-ends | |
307 (while (string-match "\r\n?" string pos) | |
308 (setq string (replace-match "\n" t t string)) | |
309 (setq pos (1+ (match-beginning 0)))) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
310 |
51102 | 311 (setq string (xml-substitute-special string)) |
312 (setq children | |
313 (if (stringp (car children)) | |
314 ;; The two strings were separated by a comment. | |
315 (cons (concat (car children) string) | |
316 (cdr children)) | |
317 (cons string children)))))))) | |
318 | |
30329 | 319 (goto-char (match-end 0)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
320 (nreverse children)) |
30329 | 321 ;; This was an invalid start tag |
51102 | 322 (error "XML: Invalid attribute list"))))) |
37958
d1fdbba91c71
(xml-parse-tag): The document may contain invalid characters.
Gerd Moellmann <gerd@gnu.org>
parents:
34825
diff
changeset
|
323 (t ;; This is not a tag. |
51102 | 324 (error "XML: Invalid character")))) |
30329 | 325 |
51102 | 326 (defun xml-parse-attlist () |
327 "Return the attribute-list after point. | |
328 Leave point at the first non-blank character after the tag." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
329 (let ((attlist ()) |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
330 start-pos name) |
51102 | 331 (skip-syntax-forward " ") |
332 (while (looking-at (eval-when-compile | |
333 (concat "\\(" xml-name-regexp "\\)\\s-*=\\s-*"))) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
334 (setq name (intern (match-string 1))) |
30329 | 335 (goto-char (match-end 0)) |
336 | |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
337 ;; See also: http://www.w3.org/TR/2000/REC-xml-20001006#AVNormalize |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
338 |
30329 | 339 ;; Do we have a string between quotes (or double-quotes), |
340 ;; or a simple word ? | |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
341 (if (looking-at "\"\\([^\"]*\\)\"") |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
342 (setq start-pos (match-beginning 0)) |
50210
575aa6820adc
(xml-parse-attlist): typo in attribute parsing.
Juanma Barranquero <lekktu@gmail.com>
parents:
50144
diff
changeset
|
343 (if (looking-at "'\\([^']*\\)'") |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
344 (setq start-pos (match-beginning 0)) |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
345 (error "XML: Attribute values must be given between quotes"))) |
30329 | 346 |
347 ;; Each attribute must be unique within a given element | |
348 (if (assoc name attlist) | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
349 (error "XML: each attribute must be unique within an element")) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
350 |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
351 ;; Multiple whitespace characters should be replaced with a single one |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
352 ;; in the attributes |
51102 | 353 (let ((string (match-string 1)) |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
354 (pos 0)) |
51102 | 355 (replace-regexp-in-string "\\s-\\{2,\\}" " " string) |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
356 (push (cons name (xml-substitute-special string)) attlist)) |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
357 |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
358 (goto-char start-pos) |
51102 | 359 (forward-sexp) ; we have string syntax |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
360 |
51102 | 361 (skip-syntax-forward " ")) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
362 (nreverse attlist))) |
30329 | 363 |
364 ;;******************************************************************* | |
365 ;;** | |
366 ;;** The DTD (document type declaration) | |
367 ;;** The following functions know how to skip or parse the DTD of | |
368 ;;** a document | |
369 ;;** | |
370 ;;******************************************************************* | |
371 | |
51102 | 372 ;; Fixme: This fails at least if the DTD contains conditional sections. |
373 | |
374 (defun xml-skip-dtd () | |
375 "Skip the DTD at point. | |
30329 | 376 This follows the rule [28] in the XML specifications." |
377 (forward-char (length "<!DOCTYPE")) | |
51102 | 378 (if (looking-at "\\s-*>") |
30329 | 379 (error "XML: invalid DTD (excepting name of the document)")) |
380 (condition-case nil | |
381 (progn | |
51102 | 382 (forward-sexp) |
383 (skip-syntax-forward " ") | |
30329 | 384 (if (looking-at "\\[") |
51102 | 385 (re-search-forward "]\\s-*>") |
386 (search-forward ">"))) | |
30329 | 387 (error (error "XML: No end to the DTD")))) |
388 | |
51102 | 389 (defun xml-parse-dtd () |
390 "Parse the DTD at point." | |
391 (forward-char (eval-when-compile (length "<!DOCTYPE"))) | |
392 (skip-syntax-forward " ") | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
393 (if (looking-at ">") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
394 (error "XML: invalid DTD (excepting name of the document)")) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
395 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
396 ;; Get the name of the document |
51102 | 397 (looking-at xml-name-regexp) |
398 (let ((dtd (list (match-string 0) 'dtd)) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
399 type element end-pos) |
30329 | 400 (goto-char (match-end 0)) |
401 | |
51102 | 402 (skip-syntax-forward " ") |
403 ;; XML [75] | |
404 (cond ((looking-at "PUBLIC\\s-+") | |
405 (goto-char (match-end 0)) | |
406 (unless (or (re-search-forward | |
407 "\\=\"\\([[:space:][:alnum:]-'()+,./:=?;!*#@$_%]*\\)\"" | |
408 nil t) | |
409 (re-search-forward | |
410 "\\='\\([[:space:][:alnum:]-()+,./:=?;!*#@$_%]*\\)'" | |
411 nil t)) | |
412 (error "XML: missing public id")) | |
413 (let ((pubid (match-string 1))) | |
414 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t) | |
415 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t)) | |
416 (error "XML: missing system id")) | |
417 (push (list pubid (match-string 1) 'public) dtd))) | |
418 ((looking-at "SYSTEM\\s-+") | |
419 (goto-char (match-end 0)) | |
420 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t) | |
421 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t)) | |
422 (error "XML: missing system id")) | |
423 (push (list (match-string 1) 'system) dtd))) | |
424 (skip-syntax-forward " ") | |
425 (if (eq ?> (char-after)) | |
426 (forward-char) | |
427 (skip-syntax-forward " ") | |
428 (if (not (eq (char-after) ?\[)) | |
429 (error "XML: bad DTD") | |
430 (forward-char) | |
431 ;; Parse the rest of the DTD | |
432 ;; Fixme: Deal with ENTITY, ATTLIST, NOTATION, PIs. | |
433 (while (not (looking-at "\\s-*\\]")) | |
434 (skip-syntax-forward " ") | |
435 (cond | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
436 |
51102 | 437 ;; Translation of rule [45] of XML specifications |
438 ((looking-at | |
439 "<!ELEMENT\\s-+\\([[:alnum:].%;]+\\)\\s-+\\([^>]+\\)>") | |
440 | |
441 (setq element (intern (match-string 1)) | |
442 type (match-string-no-properties 2)) | |
443 (setq end-pos (match-end 0)) | |
30329 | 444 |
51102 | 445 ;; Translation of rule [46] of XML specifications |
446 (cond | |
447 ((string-match "^EMPTY[ \t\n\r]*$" type) ;; empty declaration | |
448 (setq type 'empty)) | |
449 ((string-match "^ANY[ \t\n\r]*$" type) ;; any type of contents | |
450 (setq type 'any)) | |
451 ((string-match "^(\\(.*\\))[ \t\n\r]*$" type) ;; children ([47]) | |
452 (setq type (xml-parse-elem-type (match-string 1 type)))) | |
453 ((string-match "^%[^;]+;[ \t\n\r]*$" type) ;; substitution | |
454 nil) | |
455 (t | |
456 (error "XML: Invalid element type in the DTD"))) | |
30329 | 457 |
51102 | 458 ;; rule [45]: the element declaration must be unique |
459 (if (assoc element dtd) | |
460 (error "XML: element declarations must be unique in a DTD (<%s>)" | |
461 (symbol-name element))) | |
30329 | 462 |
51102 | 463 ;; Store the element in the DTD |
464 (push (list element type) dtd) | |
465 (goto-char end-pos)) | |
466 ((looking-at "<!--") | |
467 (search-forward "-->")) | |
30329 | 468 |
51102 | 469 (t |
470 (error "XML: Invalid DTD item"))) | |
471 | |
472 ;; Skip the end of the DTD | |
473 (search-forward ">")))) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
474 (nreverse dtd))) |
30329 | 475 |
476 | |
477 (defun xml-parse-elem-type (string) | |
51102 | 478 "Convert element type STRING into a Lisp structure." |
30329 | 479 |
480 (let (elem modifier) | |
481 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string) | |
482 (progn | |
483 (setq elem (match-string 1 string) | |
484 modifier (match-string 2 string)) | |
485 (if (string-match "|" elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
486 (setq elem (cons 'choice |
30329 | 487 (mapcar 'xml-parse-elem-type |
488 (split-string elem "|")))) | |
489 (if (string-match "," elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
490 (setq elem (cons 'seq |
30329 | 491 (mapcar 'xml-parse-elem-type |
51102 | 492 (split-string elem ","))))))) |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
493 (if (string-match "[ \t\n\r]*\\([^+*?]+\\)\\([+*?]?\\)" string) |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
494 (setq elem (match-string 1 string) |
30329 | 495 modifier (match-string 2 string)))) |
496 | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
497 (if (and (stringp elem) (string= elem "#PCDATA")) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
498 (setq elem 'pcdata)) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
499 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
500 (cond |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
501 ((string= modifier "+") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
502 (list '+ elem)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
503 ((string= modifier "*") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
504 (list '* elem)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
505 ((string= modifier "?") |
49787
6269b5c10aec
(xml-parse-elem-type): Fix use of character constant.
Juanma Barranquero <lekktu@gmail.com>
parents:
49133
diff
changeset
|
506 (list '\? elem)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
507 (t |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
508 elem)))) |
30329 | 509 |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
510 ;;******************************************************************* |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
511 ;;** |
30329 | 512 ;;** Substituting special XML sequences |
513 ;;** | |
514 ;;******************************************************************* | |
515 | |
51102 | 516 (eval-when-compile |
517 (defvar str)) ; dynamic from replace-regexp-in-string | |
518 | |
519 ;; Fixme: Take declared entities from the DTD when they're available. | |
520 (defun xml-substitute-entity (match) | |
521 "Subroutine of xml-substitute-special." | |
522 (save-match-data | |
523 (let ((match1 (match-string 1 str))) | |
524 (cond ((string= match1 "lt") "<") | |
525 ((string= match1 "gt") ">") | |
526 ((string= match1 "apos") "'") | |
527 ((string= match1 "quot") "\"") | |
528 ((string= match1 "amp") "&") | |
529 ((and (string-match "#\\([0-9]+\\)" match1) | |
530 (let ((c (decode-char | |
531 'ucs | |
532 (string-to-number (match-string 1 match1))))) | |
533 (if c (string c))))) ; else unrepresentable | |
534 ((and (string-match "#x\\([[:xdigit:]]+\\)" match1) | |
535 (let ((c (decode-char | |
536 'ucs | |
537 (string-to-number (match-string 1 match1) 16)))) | |
538 (if c (string c))))) | |
539 ;; Default to asis. Arguably, unrepresentable code points | |
540 ;; might be best replaced with U+FFFD. | |
541 (t match))))) | |
542 | |
30329 | 543 (defun xml-substitute-special (string) |
51102 | 544 "Return STRING, after subsituting entity references." |
545 ;; This originally made repeated passes through the string from the | |
546 ;; beginning, which isn't correct, since then either "&amp;" or | |
547 ;; "&amp;" won't DTRT. | |
548 (replace-regexp-in-string "&\\([^;]+\\);" | |
549 #'xml-substitute-entity string t t)) | |
30329 | 550 |
551 ;;******************************************************************* | |
552 ;;** | |
553 ;;** Printing a tree. | |
554 ;;** This function is intended mainly for debugging purposes. | |
555 ;;** | |
556 ;;******************************************************************* | |
557 | |
558 (defun xml-debug-print (xml) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
559 (dolist (node xml) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
560 (xml-debug-print-internal node ""))) |
30329 | 561 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
562 (defun xml-debug-print-internal (xml indent-string) |
30329 | 563 "Outputs the XML tree in the current buffer. |
51102 | 564 The first line is indented with INDENT-STRING." |
30329 | 565 (let ((tree xml) |
566 attlist) | |
51102 | 567 (insert indent-string ?< (symbol-name (xml-node-name tree))) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
568 |
30329 | 569 ;; output the attribute list |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
570 (setq attlist (xml-node-attributes tree)) |
30329 | 571 (while attlist |
51102 | 572 (insert ?\ (symbol-name (caar attlist)) "=\"" (cdar attlist) ?\") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
573 (setq attlist (cdr attlist))) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
574 |
51102 | 575 (insert ?>) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
576 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
577 (setq tree (xml-node-children tree)) |
30329 | 578 |
579 ;; output the children | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
580 (dolist (node tree) |
30329 | 581 (cond |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
582 ((listp node) |
51102 | 583 (insert ?\n) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
584 (xml-debug-print-internal node (concat indent-string " "))) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
585 ((stringp node) (insert node)) |
30329 | 586 (t |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
587 (error "Invalid XML tree")))) |
30329 | 588 |
51102 | 589 (insert ?\n indent-string |
590 ?< ?/ (symbol-name (xml-node-name xml)) ?>))) | |
30329 | 591 |
592 (provide 'xml) | |
593 | |
594 ;;; xml.el ends here |