Mercurial > emacs
annotate lisp/xml.el @ 50090:6d355ce2d494
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Wed, 12 Mar 2003 07:38:52 +0000 |
parents | 9431fa452e95 |
children | ff1b1d15e1f2 |
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 |
37958
d1fdbba91c71
(xml-parse-tag): The document may contain invalid characters.
Gerd Moellmann <gerd@gnu.org>
parents:
34825
diff
changeset
|
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
30329 | 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: | |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
42 ;; <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
|
43 ;; <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
|
44 ;; <node3 attr5="name5" attr6="name6">value3</node3> |
30329 | 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 | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
76 (defsubst xml-node-name (node) |
30329 | 77 "Return the tag associated with NODE. |
78 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
|
79 (car node)) |
30329 | 80 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
81 (defsubst xml-node-attributes (node) |
30329 | 82 "Return the list of attributes of NODE. |
83 The list can be nil." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
84 (nth 1 node)) |
30329 | 85 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
86 (defsubst xml-node-children (node) |
30329 | 87 "Return the list of children of NODE. |
88 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
|
89 (cddr node)) |
30329 | 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." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
94 (let ((match ())) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
95 (dolist (child (xml-node-children node)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
96 (if child |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
97 (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
|
98 (push child match)))) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
99 (nreverse match))) |
30329 | 100 |
101 (defun xml-get-attribute (node attribute) | |
102 "Get from NODE the value of ATTRIBUTE. | |
103 An empty string is returned if the attribute was not found." | |
104 (if (xml-node-attributes node) | |
105 (let ((value (assoc attribute (xml-node-attributes node)))) | |
106 (if value | |
107 (cdr value) | |
108 "")) | |
109 "")) | |
110 | |
111 ;;******************************************************************* | |
112 ;;** | |
113 ;;** Creating the list | |
114 ;;** | |
115 ;;******************************************************************* | |
116 | |
117 (defun xml-parse-file (file &optional parse-dtd) | |
118 "Parse the well-formed XML FILE. | |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
119 If FILE is already edited, this will keep the buffer alive. |
30329 | 120 Returns the top node with all its children. |
121 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
|
122 (let ((keep)) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
123 (if (get-file-buffer file) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
124 (progn |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
125 (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
|
126 (setq keep (point))) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
127 (find-file file)) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
128 |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
129 (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
|
130 (point-max) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
131 (current-buffer) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
132 parse-dtd))) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
133 (if keep |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
134 (goto-char keep) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
135 (kill-buffer (current-buffer))) |
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
136 xml))) |
30329 | 137 |
138 (defun xml-parse-region (beg end &optional buffer parse-dtd) | |
139 "Parse the region from BEG to END in BUFFER. | |
140 If BUFFER is nil, it defaults to the current buffer. | |
141 Returns the XML list for the region, or raises an error if the region | |
142 is not a well-formed XML file. | |
143 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped, | |
144 and returned as the first element of the list" | |
145 (let (xml result dtd) | |
146 (save-excursion | |
147 (if buffer | |
148 (set-buffer buffer)) | |
149 (goto-char beg) | |
150 (while (< (point) end) | |
151 (if (search-forward "<" end t) | |
152 (progn | |
153 (forward-char -1) | |
154 (if (null xml) | |
155 (progn | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
156 (setq result (xml-parse-tag end parse-dtd)) |
30329 | 157 (cond |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
158 ((null result)) |
30329 | 159 ((listp (car result)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
160 (setq dtd (car result)) |
30329 | 161 (add-to-list 'xml (cdr result))) |
162 (t | |
163 (add-to-list 'xml result)))) | |
164 | |
165 ;; translation of rule [1] of XML specifications | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
166 (error "XML files can have only one toplevel tag"))) |
30329 | 167 (goto-char end))) |
168 (if parse-dtd | |
169 (cons dtd (reverse xml)) | |
170 (reverse xml))))) | |
171 | |
172 | |
173 (defun xml-parse-tag (end &optional parse-dtd) | |
174 "Parse the tag that is just in front of point. | |
175 The end tag must be found before the position END in the current buffer. | |
176 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and | |
177 returned as the first element in the list. | |
178 Returns one of: | |
179 - a list : the matching node | |
180 - nil : the point is not looking at a tag. | |
181 - a cons cell: the first element is the DTD, the second is the node" | |
182 (cond | |
183 ;; Processing instructions (like the <?xml version="1.0"?> tag at the | |
184 ;; beginning of a document) | |
185 ((looking-at "<\\?") | |
186 (search-forward "?>" end) | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
187 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
30329 | 188 (xml-parse-tag end)) |
189 ;; Character data (CDATA) sections, in which no tag should be interpreted | |
190 ((looking-at "<!\\[CDATA\\[") | |
191 (let ((pos (match-end 0))) | |
192 (unless (search-forward "]]>" end t) | |
193 (error "CDATA section does not end anywhere in the document")) | |
194 (buffer-substring-no-properties pos (match-beginning 0)))) | |
195 ;; DTD for the document | |
196 ((looking-at "<!DOCTYPE") | |
197 (let (dtd) | |
198 (if parse-dtd | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
199 (setq dtd (xml-parse-dtd end)) |
30329 | 200 (xml-skip-dtd end)) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
201 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
30329 | 202 (if dtd |
203 (cons dtd (xml-parse-tag end)) | |
204 (xml-parse-tag end)))) | |
205 ;; skip comments | |
206 ((looking-at "<!--") | |
207 (search-forward "-->" end) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
208 nil) |
30329 | 209 ;; end tag |
210 ((looking-at "</") | |
211 '()) | |
212 ;; opening tag | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
213 ((looking-at "<\\([^/>[:space:]]+\\)") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
214 (goto-char (match-end 1)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
215 (let* ((case-fold-search nil) ;; XML is case-sensitive. |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
216 (node-name (match-string 1)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
217 ;; Parse the attribute list. |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
218 (children (list (xml-parse-attlist end) (intern node-name))) |
30329 | 219 pos) |
220 | |
221 ;; is this an empty element ? | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
222 (if (looking-at "/[[:space:]]*>") |
30329 | 223 (progn |
224 (forward-char 2) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
225 (nreverse (cons '("") children))) |
30329 | 226 |
227 ;; 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
|
228 (if (eq (char-after) ?>) |
30329 | 229 (progn |
230 (forward-char 1) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
231 ;; 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
|
232 ;; one might contain spaces after the tag name |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
233 (while (not (looking-at (concat "</" node-name "[[:space:]]*>"))) |
30329 | 234 (cond |
235 ((looking-at "</") | |
236 (error (concat | |
237 "XML: invalid syntax -- invalid end tag (expecting " | |
238 node-name | |
34825
2cad4cde52bd
(top level comment): Updated to reflect the fact that
Gerd Moellmann <gerd@gnu.org>
parents:
33977
diff
changeset
|
239 ") at pos " (number-to-string (point))))) |
30329 | 240 ((= (char-after) ?<) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
241 (let ((tag (xml-parse-tag end))) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
242 (when tag |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
243 (push tag children)))) |
30329 | 244 (t |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
245 (setq pos (point)) |
30329 | 246 (search-forward "<" end) |
247 (forward-char -1) | |
248 (let ((string (buffer-substring-no-properties pos (point))) | |
249 (pos 0)) | |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
250 |
30329 | 251 ;; Clean up the string (no newline characters) |
252 ;; Not done, since as per XML specifications, the XML processor | |
253 ;; should always pass the whole string to the application. | |
254 ;; (while (string-match "\\s +" string pos) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
255 ;; (setq string (replace-match " " t t string)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
256 ;; (setq pos (1+ (match-beginning 0)))) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
257 |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
258 (setq string (xml-substitute-special string)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
259 (setq children |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
260 (if (stringp (car children)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
261 ;; The two strings were separated by a comment. |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
262 (cons (concat (car children) string) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
263 (cdr children)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
264 (cons string children))))))) |
30329 | 265 (goto-char (match-end 0)) |
266 (if (> (point) end) | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
267 (error "XML: End tag for %s not found before end of region" |
30329 | 268 node-name)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
269 (nreverse children)) |
30329 | 270 |
271 ;; This was an invalid start tag | |
272 (error "XML: Invalid attribute list") | |
273 )))) | |
37958
d1fdbba91c71
(xml-parse-tag): The document may contain invalid characters.
Gerd Moellmann <gerd@gnu.org>
parents:
34825
diff
changeset
|
274 (t ;; This is not a tag. |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
275 (error "XML: Invalid character")) |
30329 | 276 )) |
277 | |
278 (defun xml-parse-attlist (end) | |
279 "Return the attribute-list that point is looking at. | |
280 The search for attributes end at the position END in the current buffer. | |
281 Leaves the point on 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
|
282 (let ((attlist ()) |
30329 | 283 name) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
284 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
285 (while (looking-at "\\([a-zA-Z_:][-a-zA-Z0-9._:]*\\)[[:space:]]*=[[:space:]]*") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
286 (setq name (intern (match-string 1))) |
30329 | 287 (goto-char (match-end 0)) |
288 | |
289 ;; Do we have a string between quotes (or double-quotes), | |
290 ;; or a simple word ? | |
43739
373960858ccc
* xml.el (xml-parse-attlist): Accept empty strings.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
42031
diff
changeset
|
291 (unless (looking-at "\"\\([^\"]*\\)\"") |
373960858ccc
* xml.el (xml-parse-attlist): Accept empty strings.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
42031
diff
changeset
|
292 (unless (looking-at "'\\([^']*\\)'") |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
293 (error "XML: Attribute values must be given between quotes"))) |
30329 | 294 |
295 ;; Each attribute must be unique within a given element | |
296 (if (assoc name attlist) | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
297 (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
|
298 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
299 (push (cons name (match-string-no-properties 1)) attlist) |
30329 | 300 (goto-char (match-end 0)) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
301 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
30329 | 302 (if (> (point) end) |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
303 (error "XML: end of attribute list not found before end of region")) |
30329 | 304 ) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
305 (nreverse attlist))) |
30329 | 306 |
307 ;;******************************************************************* | |
308 ;;** | |
309 ;;** The DTD (document type declaration) | |
310 ;;** The following functions know how to skip or parse the DTD of | |
311 ;;** a document | |
312 ;;** | |
313 ;;******************************************************************* | |
314 | |
315 (defun xml-skip-dtd (end) | |
316 "Skip the DTD that point is looking at. | |
317 The DTD must end before the position END in the current buffer. | |
318 The point must be just before the starting tag of the DTD. | |
319 This follows the rule [28] in the XML specifications." | |
320 (forward-char (length "<!DOCTYPE")) | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
321 (if (looking-at "[[:space:]]*>") |
30329 | 322 (error "XML: invalid DTD (excepting name of the document)")) |
323 (condition-case nil | |
324 (progn | |
325 (forward-word 1) ;; name of the document | |
50081
9431fa452e95
(xml-skip-dtd): Fix parsing of DOCTYPE.
Juanma Barranquero <lekktu@gmail.com>
parents:
49787
diff
changeset
|
326 (goto-char (- (re-search-forward "[[:space:]]") 1)) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
327 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
30329 | 328 (if (looking-at "\\[") |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
329 (re-search-forward "\\][[:space:]]*>" end) |
30329 | 330 (search-forward ">" end))) |
331 (error (error "XML: No end to the DTD")))) | |
332 | |
333 (defun xml-parse-dtd (end) | |
334 "Parse the DTD that point is looking at. | |
335 The DTD must end before the position END in the current buffer." | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
336 (forward-char (length "<!DOCTYPE")) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
337 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
338 (if (looking-at ">") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
339 (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
|
340 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
341 ;; Get the name of the document |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
342 (looking-at "\\sw+") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
343 (let ((dtd (list (match-string-no-properties 0) 'dtd)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
344 type element end-pos) |
30329 | 345 (goto-char (match-end 0)) |
346 | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
347 (goto-char (- (re-search-forward "[^[:space:]]") 1)) |
30329 | 348 |
349 ;; External DTDs => don't know how to handle them yet | |
350 (if (looking-at "SYSTEM") | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
351 (error "XML: Don't know how to handle external DTDs")) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
352 |
30329 | 353 (if (not (= (char-after) ?\[)) |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
354 (error "XML: Unknown declaration in the DTD")) |
30329 | 355 |
356 ;; Parse the rest of the DTD | |
357 (forward-char 1) | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
358 (while (and (not (looking-at "[[:space:]]*\\]")) |
30329 | 359 (<= (point) end)) |
360 (cond | |
361 | |
362 ;; Translation of rule [45] of XML specifications | |
363 ((looking-at | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
364 "[[:space:]]*<!ELEMENT[[:space:]]+\\([a-zA-Z0-9.%;]+\\)[[:space:]]+\\([^>]+\\)>") |
30329 | 365 |
30779
aa097d8d4f1a
(xml-parse-tag, xml-parse-attlist): Do not downcase
Gerd Moellmann <gerd@gnu.org>
parents:
30329
diff
changeset
|
366 (setq element (intern (match-string-no-properties 1)) |
30329 | 367 type (match-string-no-properties 2)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
368 (setq end-pos (match-end 0)) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
369 |
30329 | 370 ;; Translation of rule [46] of XML specifications |
371 (cond | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
372 ((string-match "^EMPTY[[:space:]]*$" type) ;; empty declaration |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
373 (setq type 'empty)) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
374 ((string-match "^ANY[[:space:]]*$" type) ;; any type of contents |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
375 (setq type 'any)) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
376 ((string-match "^(\\(.*\\))[[:space:]]*$" type) ;; children ([47]) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
377 (setq type (xml-parse-elem-type (match-string-no-properties 1 type)))) |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
378 ((string-match "^%[^;]+;[[:space:]]*$" type) ;; substitution |
30329 | 379 nil) |
380 (t | |
381 (error "XML: Invalid element type in the DTD"))) | |
382 | |
383 ;; rule [45]: the element declaration must be unique | |
384 (if (assoc element dtd) | |
38409
153f1b1f2efd
Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents:
37958
diff
changeset
|
385 (error "XML: elements declaration must be unique in a DTD (<%s>)" |
30329 | 386 (symbol-name element))) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
387 |
30329 | 388 ;; Store the element in the DTD |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
389 (push (list element type) dtd) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
390 (goto-char end-pos)) |
30329 | 391 |
392 | |
393 (t | |
394 (error "XML: Invalid DTD item")) | |
395 ) | |
396 ) | |
397 | |
398 ;; Skip the end of the DTD | |
399 (search-forward ">" end) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
400 (nreverse dtd))) |
30329 | 401 |
402 | |
403 (defun xml-parse-elem-type (string) | |
404 "Convert a STRING for an element type into an elisp structure." | |
405 | |
406 (let (elem modifier) | |
407 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string) | |
408 (progn | |
409 (setq elem (match-string 1 string) | |
410 modifier (match-string 2 string)) | |
411 (if (string-match "|" elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
412 (setq elem (cons 'choice |
30329 | 413 (mapcar 'xml-parse-elem-type |
414 (split-string elem "|")))) | |
415 (if (string-match "," elem) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
416 (setq elem (cons 'seq |
30329 | 417 (mapcar 'xml-parse-elem-type |
418 (split-string elem ",")))) | |
419 ))) | |
49133
4d76986458e8
(xml-parse-tag, xml-parse-attlist, xml-skip-dtd, xml-parse-dtd,
Juanma Barranquero <lekktu@gmail.com>
parents:
49065
diff
changeset
|
420 (if (string-match "[[:space:]]*\\([^+*?]+\\)\\([+*?]?\\)" string) |
30329 | 421 (setq elem (match-string 1 string) |
422 modifier (match-string 2 string)))) | |
423 | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
424 (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
|
425 (setq elem 'pcdata)) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
426 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
427 (cond |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
428 ((string= modifier "+") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
429 (list '+ elem)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
430 ((string= modifier "*") |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
431 (list '* elem)) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
432 ((string= modifier "?") |
49787
6269b5c10aec
(xml-parse-elem-type): Fix use of character constant.
Juanma Barranquero <lekktu@gmail.com>
parents:
49133
diff
changeset
|
433 (list '\? elem)) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
434 (t |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
435 elem)))) |
30329 | 436 |
437 | |
438 ;;******************************************************************* | |
439 ;;** | |
440 ;;** Substituting special XML sequences | |
441 ;;** | |
442 ;;******************************************************************* | |
443 | |
444 (defun xml-substitute-special (string) | |
445 "Return STRING, after subsituting special XML sequences." | |
446 (while (string-match "<" string) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
447 (setq string (replace-match "<" t nil string))) |
30329 | 448 (while (string-match ">" string) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
449 (setq string (replace-match ">" t nil string))) |
30329 | 450 (while (string-match "'" string) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
451 (setq string (replace-match "'" t nil string))) |
30329 | 452 (while (string-match """ string) |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
453 (setq string (replace-match "\"" t nil string))) |
49065 | 454 ;; This goes last so it doesn't confuse the matches above. |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
455 (while (string-match "&" string) |
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
456 (setq string (replace-match "&" t nil string))) |
30329 | 457 string) |
458 | |
459 ;;******************************************************************* | |
460 ;;** | |
461 ;;** Printing a tree. | |
462 ;;** This function is intended mainly for debugging purposes. | |
463 ;;** | |
464 ;;******************************************************************* | |
465 | |
466 (defun xml-debug-print (xml) | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
467 (dolist (node xml) |
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
468 (xml-debug-print-internal node ""))) |
30329 | 469 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
470 (defun xml-debug-print-internal (xml indent-string) |
30329 | 471 "Outputs the XML tree in the current buffer. |
472 The first line indented with INDENT-STRING." | |
473 (let ((tree xml) | |
474 attlist) | |
475 (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
|
476 |
30329 | 477 ;; output the attribute list |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
478 (setq attlist (xml-node-attributes tree)) |
30329 | 479 (while attlist |
480 (insert " ") | |
481 (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
|
482 (setq attlist (cdr attlist))) |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
483 |
30329 | 484 (insert ">") |
49036
466922eb2b8d
(xml-substitute-special): Move "&" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
48869
diff
changeset
|
485 |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
486 (setq tree (xml-node-children tree)) |
30329 | 487 |
488 ;; output the children | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
489 (dolist (node tree) |
30329 | 490 (cond |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
491 ((listp node) |
30329 | 492 (insert "\n") |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
493 (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
|
494 ((stringp node) (insert node)) |
30329 | 495 (t |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
496 (error "Invalid XML tree")))) |
30329 | 497 |
498 (insert "\n" indent-string | |
42031
54db4085a7df
Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40030
diff
changeset
|
499 "</" (symbol-name (xml-node-name xml)) ">"))) |
30329 | 500 |
501 (provide 'xml) | |
502 | |
503 ;;; xml.el ends here |