Mercurial > emacs
annotate lisp/xml.el @ 51123:605e818cbe30
(gud-gdba-command-name): Use -noasync option for Gdb
with MS windows.
(gdb-display-end): Only make buffer writeable temporarily.
Move "View" submenu up one level.
author | Nick Roberts <nickrob@snap.net.nz> |
---|---|
date | Wed, 21 May 2003 20:12:20 +0000 |
parents | aac5eaf1454e |
children | ed3269a70a9f |
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> | |
51102 | 6 ;; Maintainer: FSF |
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) | |
51102 | 277 ;; Fixme: Inconsistent with the nil content returned from |
278 ;; `<tag></tag>'. | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
279 (nreverse (cons '("") children))) |
30329 | 280 |
281 ;; 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
|
282 (if (eq (char-after) ?>) |
30329 | 283 (progn |
284 (forward-char 1) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
285 ;; 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
|
286 ;; one might contain spaces after the tag name |
51102 | 287 (let ((end (concat "</" node-name "\\s-*>"))) |
288 (while (not (looking-at end)) | |
289 (cond | |
290 ((looking-at "</") | |
291 (error "XML: Invalid end tag (expecting %s) at pos %d" | |
292 node-name (point))) | |
293 ((= (char-after) ?<) | |
294 (let ((tag (xml-parse-tag))) | |
295 (when tag | |
296 (push tag children)))) | |
297 (t | |
298 (setq pos (point)) | |
299 (search-forward "<") | |
300 (forward-char -1) | |
301 (let ((string (buffer-substring pos (point))) | |
302 (pos 0)) | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
303 |
51102 | 304 ;; Clean up the string. As per XML |
305 ;; specifications, the XML processor should | |
306 ;; always pass the whole string to the | |
307 ;; application. But \r's should be replaced: | |
308 ;; http://www.w3.org/TR/2000/REC-xml-20001006#sec-line-ends | |
309 (while (string-match "\r\n?" string pos) | |
310 (setq string (replace-match "\n" t t string)) | |
311 (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
|
312 |
51102 | 313 (setq string (xml-substitute-special string)) |
314 (setq children | |
315 (if (stringp (car children)) | |
316 ;; The two strings were separated by a comment. | |
317 (cons (concat (car children) string) | |
318 (cdr children)) | |
319 (cons string children)))))))) | |
320 | |
30329 | 321 (goto-char (match-end 0)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
322 (nreverse children)) |
30329 | 323 ;; This was an invalid start tag |
51102 | 324 (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
|
325 (t ;; This is not a tag. |
51102 | 326 (error "XML: Invalid character")))) |
30329 | 327 |
51102 | 328 (defun xml-parse-attlist () |
329 "Return the attribute-list after point. | |
330 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
|
331 (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
|
332 start-pos name) |
51102 | 333 (skip-syntax-forward " ") |
334 (while (looking-at (eval-when-compile | |
335 (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
|
336 (setq name (intern (match-string 1))) |
30329 | 337 (goto-char (match-end 0)) |
338 | |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
339 ;; 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
|
340 |
30329 | 341 ;; Do we have a string between quotes (or double-quotes), |
342 ;; 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
|
343 (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
|
344 (setq start-pos (match-beginning 0)) |
50210
575aa6820adc
(xml-parse-attlist): typo in attribute parsing.
Juanma Barranquero <lekktu@gmail.com>
parents:
50144
diff
changeset
|
345 (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
|
346 (setq start-pos (match-beginning 0)) |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
347 (error "XML: Attribute values must be given between quotes"))) |
30329 | 348 |
349 ;; Each attribute must be unique within a given element | |
350 (if (assoc name attlist) | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
351 (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
|
352 |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
353 ;; 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
|
354 ;; in the attributes |
51102 | 355 (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
|
356 (pos 0)) |
51102 | 357 (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
|
358 (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
|
359 |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
360 (goto-char start-pos) |
51102 | 361 (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
|
362 |
51102 | 363 (skip-syntax-forward " ")) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
364 (nreverse attlist))) |
30329 | 365 |
366 ;;******************************************************************* | |
367 ;;** | |
368 ;;** The DTD (document type declaration) | |
369 ;;** The following functions know how to skip or parse the DTD of | |
370 ;;** a document | |
371 ;;** | |
372 ;;******************************************************************* | |
373 | |
51102 | 374 ;; Fixme: This fails at least if the DTD contains conditional sections. |
375 | |
376 (defun xml-skip-dtd () | |
377 "Skip the DTD at point. | |
30329 | 378 This follows the rule [28] in the XML specifications." |
379 (forward-char (length "<!DOCTYPE")) | |
51102 | 380 (if (looking-at "\\s-*>") |
30329 | 381 (error "XML: invalid DTD (excepting name of the document)")) |
382 (condition-case nil | |
383 (progn | |
51102 | 384 (forward-sexp) |
385 (skip-syntax-forward " ") | |
30329 | 386 (if (looking-at "\\[") |
51102 | 387 (re-search-forward "]\\s-*>") |
388 (search-forward ">"))) | |
30329 | 389 (error (error "XML: No end to the DTD")))) |
390 | |
51102 | 391 (defun xml-parse-dtd () |
392 "Parse the DTD at point." | |
393 (forward-char (eval-when-compile (length "<!DOCTYPE"))) | |
394 (skip-syntax-forward " ") | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
395 (if (looking-at ">") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
396 (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
|
397 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
398 ;; Get the name of the document |
51102 | 399 (looking-at xml-name-regexp) |
400 (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
|
401 type element end-pos) |
30329 | 402 (goto-char (match-end 0)) |
403 | |
51102 | 404 (skip-syntax-forward " ") |
405 ;; XML [75] | |
406 (cond ((looking-at "PUBLIC\\s-+") | |
407 (goto-char (match-end 0)) | |
408 (unless (or (re-search-forward | |
409 "\\=\"\\([[:space:][:alnum:]-'()+,./:=?;!*#@$_%]*\\)\"" | |
410 nil t) | |
411 (re-search-forward | |
412 "\\='\\([[:space:][:alnum:]-()+,./:=?;!*#@$_%]*\\)'" | |
413 nil t)) | |
414 (error "XML: missing public id")) | |
415 (let ((pubid (match-string 1))) | |
416 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t) | |
417 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t)) | |
418 (error "XML: missing system id")) | |
419 (push (list pubid (match-string 1) 'public) dtd))) | |
420 ((looking-at "SYSTEM\\s-+") | |
421 (goto-char (match-end 0)) | |
422 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t) | |
423 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t)) | |
424 (error "XML: missing system id")) | |
425 (push (list (match-string 1) 'system) dtd))) | |
426 (skip-syntax-forward " ") | |
427 (if (eq ?> (char-after)) | |
428 (forward-char) | |
429 (skip-syntax-forward " ") | |
430 (if (not (eq (char-after) ?\[)) | |
431 (error "XML: bad DTD") | |
432 (forward-char) | |
433 ;; Parse the rest of the DTD | |
434 ;; Fixme: Deal with ENTITY, ATTLIST, NOTATION, PIs. | |
435 (while (not (looking-at "\\s-*\\]")) | |
436 (skip-syntax-forward " ") | |
437 (cond | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
438 |
51102 | 439 ;; Translation of rule [45] of XML specifications |
440 ((looking-at | |
441 "<!ELEMENT\\s-+\\([[:alnum:].%;]+\\)\\s-+\\([^>]+\\)>") | |
442 | |
443 (setq element (intern (match-string 1)) | |
444 type (match-string-no-properties 2)) | |
445 (setq end-pos (match-end 0)) | |
30329 | 446 |
51102 | 447 ;; Translation of rule [46] of XML specifications |
448 (cond | |
449 ((string-match "^EMPTY[ \t\n\r]*$" type) ;; empty declaration | |
450 (setq type 'empty)) | |
451 ((string-match "^ANY[ \t\n\r]*$" type) ;; any type of contents | |
452 (setq type 'any)) | |
453 ((string-match "^(\\(.*\\))[ \t\n\r]*$" type) ;; children ([47]) | |
454 (setq type (xml-parse-elem-type (match-string 1 type)))) | |
455 ((string-match "^%[^;]+;[ \t\n\r]*$" type) ;; substitution | |
456 nil) | |
457 (t | |
458 (error "XML: Invalid element type in the DTD"))) | |
30329 | 459 |
51102 | 460 ;; rule [45]: the element declaration must be unique |
461 (if (assoc element dtd) | |
462 (error "XML: element declarations must be unique in a DTD (<%s>)" | |
463 (symbol-name element))) | |
30329 | 464 |
51102 | 465 ;; Store the element in the DTD |
466 (push (list element type) dtd) | |
467 (goto-char end-pos)) | |
468 ((looking-at "<!--") | |
469 (search-forward "-->")) | |
30329 | 470 |
51102 | 471 (t |
472 (error "XML: Invalid DTD item"))) | |
473 | |
474 ;; Skip the end of the DTD | |
475 (search-forward ">")))) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
476 (nreverse dtd))) |
30329 | 477 |
478 | |
479 (defun xml-parse-elem-type (string) | |
51102 | 480 "Convert element type STRING into a Lisp structure." |
30329 | 481 |
482 (let (elem modifier) | |
483 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string) | |
484 (progn | |
485 (setq elem (match-string 1 string) | |
486 modifier (match-string 2 string)) | |
487 (if (string-match "|" elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
488 (setq elem (cons 'choice |
30329 | 489 (mapcar 'xml-parse-elem-type |
490 (split-string elem "|")))) | |
491 (if (string-match "," elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
492 (setq elem (cons 'seq |
30329 | 493 (mapcar 'xml-parse-elem-type |
51102 | 494 (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
|
495 (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
|
496 (setq elem (match-string 1 string) |
30329 | 497 modifier (match-string 2 string)))) |
498 | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
499 (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
|
500 (setq elem 'pcdata)) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
501 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
502 (cond |
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 "*") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
506 (list '* elem)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
507 ((string= modifier "?") |
49787
6269b5c10aec
(xml-parse-elem-type): Fix use of character constant.
Juanma Barranquero <lekktu@gmail.com>
parents:
49133
diff
changeset
|
508 (list '\? elem)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
509 (t |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
510 elem)))) |
30329 | 511 |
50144
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
512 ;;******************************************************************* |
ff1b1d15e1f2
(xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents:
50081
diff
changeset
|
513 ;;** |
30329 | 514 ;;** Substituting special XML sequences |
515 ;;** | |
516 ;;******************************************************************* | |
517 | |
51102 | 518 (eval-when-compile |
519 (defvar str)) ; dynamic from replace-regexp-in-string | |
520 | |
521 ;; Fixme: Take declared entities from the DTD when they're available. | |
522 (defun xml-substitute-entity (match) | |
523 "Subroutine of xml-substitute-special." | |
524 (save-match-data | |
525 (let ((match1 (match-string 1 str))) | |
526 (cond ((string= match1 "lt") "<") | |
527 ((string= match1 "gt") ">") | |
528 ((string= match1 "apos") "'") | |
529 ((string= match1 "quot") "\"") | |
530 ((string= match1 "amp") "&") | |
531 ((and (string-match "#\\([0-9]+\\)" match1) | |
532 (let ((c (decode-char | |
533 'ucs | |
534 (string-to-number (match-string 1 match1))))) | |
535 (if c (string c))))) ; else unrepresentable | |
536 ((and (string-match "#x\\([[:xdigit:]]+\\)" match1) | |
537 (let ((c (decode-char | |
538 'ucs | |
539 (string-to-number (match-string 1 match1) 16)))) | |
540 (if c (string c))))) | |
541 ;; Default to asis. Arguably, unrepresentable code points | |
542 ;; might be best replaced with U+FFFD. | |
543 (t match))))) | |
544 | |
30329 | 545 (defun xml-substitute-special (string) |
51102 | 546 "Return STRING, after subsituting entity references." |
547 ;; This originally made repeated passes through the string from the | |
548 ;; beginning, which isn't correct, since then either "&amp;" or | |
549 ;; "&amp;" won't DTRT. | |
550 (replace-regexp-in-string "&\\([^;]+\\);" | |
551 #'xml-substitute-entity string t t)) | |
30329 | 552 |
553 ;;******************************************************************* | |
554 ;;** | |
555 ;;** Printing a tree. | |
556 ;;** This function is intended mainly for debugging purposes. | |
557 ;;** | |
558 ;;******************************************************************* | |
559 | |
560 (defun xml-debug-print (xml) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
561 (dolist (node xml) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
562 (xml-debug-print-internal node ""))) |
30329 | 563 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
564 (defun xml-debug-print-internal (xml indent-string) |
30329 | 565 "Outputs the XML tree in the current buffer. |
51102 | 566 The first line is indented with INDENT-STRING." |
30329 | 567 (let ((tree xml) |
568 attlist) | |
51102 | 569 (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
|
570 |
30329 | 571 ;; output the attribute list |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
572 (setq attlist (xml-node-attributes tree)) |
30329 | 573 (while attlist |
51102 | 574 (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
|
575 (setq attlist (cdr attlist))) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
576 |
51102 | 577 (insert ?>) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
578 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
579 (setq tree (xml-node-children tree)) |
30329 | 580 |
581 ;; output the children | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
582 (dolist (node tree) |
30329 | 583 (cond |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
584 ((listp node) |
51102 | 585 (insert ?\n) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
586 (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
|
587 ((stringp node) (insert node)) |
30329 | 588 (t |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
589 (error "Invalid XML tree")))) |
30329 | 590 |
51102 | 591 (insert ?\n indent-string |
592 ?< ?/ (symbol-name (xml-node-name xml)) ?>))) | |
30329 | 593 |
594 (provide 'xml) | |
595 | |
596 ;;; xml.el ends here |