annotate lisp/xml.el @ 50289:03c508c0bdce

(emerge-show-file-name): Read event irrespective of whether minibuffer window is resized. Clear minibuffer when done.
author John Paul Wallington <jpw@pobox.com>
date Mon, 24 Mar 2003 08:52:20 +0000
parents 575aa6820adc
children d604b76d3bbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
4
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
5 ;; Author: Emmanuel Briot <briot@gnat.com>
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
6 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
7 ;; Keywords: xml
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
8
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
10
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
14 ;; any later version.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
15
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
20
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
24 ;; Boston, MA 02111-1307, USA.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
25
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
26 ;;; Commentary:
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
27
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
28 ;; This file contains a full XML parser. It parses a file, and returns a list
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
29 ;; that can be used internally by any other lisp file.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
30 ;; See some example in todo.el
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
31
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
32 ;;; FILE FORMAT
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
33
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
34 ;; It does not parse the DTD, if present in the XML file, but knows how to
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
35 ;; ignore it. The XML file is assumed to be well-formed. In case of error, the
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
36 ;; parsing stops and the XML file is shown where the parsing stopped.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
37 ;;
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
38 ;; It also knows how to ignore comments, as well as the special ?xml? tag
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
39 ;; in the XML file.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
40 ;;
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
45 ;; </node1>
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
46 ;; Of course, the name of the nodes and attributes can be anything. There can
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
47 ;; be any number of attributes (or none), as well as any number of children
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
48 ;; below the nodes.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
49 ;;
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
50 ;; There can be only top level node, but with any number of children below.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
51
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
52 ;;; LIST FORMAT
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
53
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
54 ;; The functions `xml-parse-file' and `xml-parse-tag' return a list with
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
55 ;; the following format:
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
56 ;;
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
57 ;; xml-list ::= (node node ...)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
58 ;; node ::= (tag_name attribute-list . child_node_list)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
59 ;; child_node_list ::= child_node child_node ...
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
60 ;; child_node ::= node | string
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
61 ;; tag_name ::= string
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
62 ;; attribute_list ::= (("attribute" . "value") ("attribute" . "value") ...)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
63 ;; | nil
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
64 ;; string ::= "..."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
65 ;;
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
66 ;; Some macros are provided to ease the parsing of this list
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
67
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
68 ;;; Code:
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
69
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
70 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
71 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
72 ;;** Macros to parse the list
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
73 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
74 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
77 "Return the tag associated with NODE.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
82 "Return the list of attributes of NODE.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
87 "Return the list of children of NODE.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
90
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
91 (defun xml-get-children (node child-name)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
92 "Return the children of NODE whose tag is CHILD-NAME.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
100
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
101 (defun xml-get-attribute (node attribute)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
102 "Get from NODE the value of ATTRIBUTE.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
103 An empty string is returned if the attribute was not found."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
104 (if (xml-node-attributes node)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
105 (let ((value (assoc attribute (xml-node-attributes node))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
106 (if value
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
107 (cdr value)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
108 ""))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
109 ""))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
110
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
111 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
112 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
113 ;;** Creating the list
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
114 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
115 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
116
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
117 (defun xml-parse-file (file &optional parse-dtd)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
120 Returns the top node with all its children.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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 "&amp;" -> "&" 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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
137
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
138 (defun xml-parse-region (beg end &optional buffer parse-dtd)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
139 "Parse the region from BEG to END in BUFFER.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
140 If BUFFER is nil, it defaults to the current buffer.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
141 Returns the XML list for the region, or raises an error if the region
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
142 is not a well-formed XML file.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
143 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped,
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
144 and returned as the first element of the list"
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
145 (let (xml result dtd)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
146 (save-excursion
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
147 (if buffer
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
148 (set-buffer buffer))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
149 (goto-char beg)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
150 (while (< (point) end)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
151 (if (search-forward "<" end t)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
152 (progn
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
153 (forward-char -1)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
154 (if (null xml)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
161 (add-to-list 'xml (cdr result)))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
162 (t
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
163 (add-to-list 'xml result))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
164
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
167 (goto-char end)))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
168 (if parse-dtd
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
169 (cons dtd (reverse xml))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
170 (reverse xml)))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
171
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
172
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
173 (defun xml-parse-tag (end &optional parse-dtd)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
174 "Parse the tag that is just in front of point.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
175 The end tag must be found before the position END in the current buffer.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
176 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
177 returned as the first element in the list.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
178 Returns one of:
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
179 - a list : the matching node
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
180 - nil : the point is not looking at a tag.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
181 - a cons cell: the first element is the DTD, the second is the node"
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
182 (cond
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
183 ;; Processing instructions (like the <?xml version="1.0"?> tag at the
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
184 ;; beginning of a document)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
185 ((looking-at "<\\?")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
186 (search-forward "?>" end)
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
187 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
188 (xml-parse-tag end))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
189 ;; Character data (CDATA) sections, in which no tag should be interpreted
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
190 ((looking-at "<!\\[CDATA\\[")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
191 (let ((pos (match-end 0)))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
192 (unless (search-forward "]]>" end t)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
193 (error "CDATA section does not end anywhere in the document"))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
194 (buffer-substring-no-properties pos (match-beginning 0))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
195 ;; DTD for the document
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
196 ((looking-at "<!DOCTYPE")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
197 (let (dtd)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
200 (xml-skip-dtd end))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
201 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
202 (if dtd
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
203 (cons dtd (xml-parse-tag end))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
204 (xml-parse-tag end))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
205 ;; skip comments
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
206 ((looking-at "<!--")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
209 ;; end tag
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
210 ((looking-at "</")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
211 '())
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
212 ;; opening tag
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
213 ((looking-at "<\\([^/> \t\n\r]+\\)")
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
219 pos)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
220
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
221 ;; is this an empty element ?
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
222 (if (looking-at "/[ \t\n\r]*>")
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
223 (progn
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
226
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
229 (progn
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
233 (while (not (looking-at (concat "</" node-name "[ \t\n\r]*>")))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
234 (cond
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
235 ((looking-at "</")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
236 (error (concat
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
237 "XML: invalid syntax -- invalid end tag (expecting "
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
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
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
246 (search-forward "<" end)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
247 (forward-char -1)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
248 (let ((string (buffer-substring-no-properties pos (point)))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
249 (pos 0))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
250
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
251 ;; Clean up the string. As per XML
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
252 ;; specifications, the XML processor should
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
253 ;; always pass the whole string to the
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
254 ;; application. But \r's should be replaced:
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
255 ;; http://www.w3.org/TR/2000/REC-xml-20001006#sec-line-ends
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
256 (while (string-match "\r\n?" string pos)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
257 (setq string (replace-match "\n" t t string))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
258 (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
259
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
260 (setq string (xml-substitute-special string))
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
261 (setq children
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
262 (if (stringp (car children))
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
263 ;; 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
264 (cons (concat (car children) string)
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
265 (cdr children))
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
266 (cons string children)))))))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
267 (goto-char (match-end 0))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
268 (if (> (point) end)
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
269 (error "XML: End tag for %s not found before end of region"
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
270 node-name))
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
271 (nreverse children))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
272
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
273 ;; This was an invalid start tag
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
274 (error "XML: Invalid attribute list")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
275 ))))
37958
d1fdbba91c71 (xml-parse-tag): The document may contain invalid characters.
Gerd Moellmann <gerd@gnu.org>
parents: 34825
diff changeset
276 (t ;; This is not a tag.
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
277 (error "XML: Invalid character"))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
278 ))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
279
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
280 (defun xml-parse-attlist (end)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
281 "Return the attribute-list that point is looking at.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
282 The search for attributes end at the position END in the current buffer.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
283 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
284 (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
285 start-pos name)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
286 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
287 (while (looking-at "\\([a-zA-Z_:][-a-zA-Z0-9._:]*\\)[ \t\n\r]*=[ \t\n\r]*")
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
288 (setq name (intern (match-string 1)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
289 (goto-char (match-end 0))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
290
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
291 ;; 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
292
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
293 ;; Do we have a string between quotes (or double-quotes),
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
294 ;; 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
295 (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
296 (setq start-pos (match-beginning 0))
50210
575aa6820adc (xml-parse-attlist): typo in attribute parsing.
Juanma Barranquero <lekktu@gmail.com>
parents: 50144
diff changeset
297 (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
298 (setq start-pos (match-beginning 0))
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
299 (error "XML: Attribute values must be given between quotes")))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
300
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
301 ;; Each attribute must be unique within a given element
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
302 (if (assoc name attlist)
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
303 (error "XML: each attribute must be unique within an element"))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
304
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
305 ;; 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
306 ;; in the attributes
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
307 (let ((string (match-string-no-properties 1))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
308 (pos 0))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
309 (while (string-match "[ \t\n\r]+" string pos)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
310 (setq string (replace-match " " t nil string))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
311 (setq pos (1+ (match-beginning 0))))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
312 (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
313
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
314 (goto-char start-pos)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
315 (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
316 (goto-char (match-end 0))
50210
575aa6820adc (xml-parse-attlist): typo in attribute parsing.
Juanma Barranquero <lekktu@gmail.com>
parents: 50144
diff changeset
317 (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
318 (goto-char (match-end 0))))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
319
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
320 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
321 (if (> (point) end)
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
322 (error "XML: end of attribute list not found before end of region")))
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
323 (nreverse attlist)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
324
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
325 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
326 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
327 ;;** The DTD (document type declaration)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
328 ;;** The following functions know how to skip or parse the DTD of
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
329 ;;** a document
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
330 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
331 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
332
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
333 (defun xml-skip-dtd (end)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
334 "Skip the DTD that point is looking at.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
335 The DTD must end before the position END in the current buffer.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
336 The point must be just before the starting tag of the DTD.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
337 This follows the rule [28] in the XML specifications."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
338 (forward-char (length "<!DOCTYPE"))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
339 (if (looking-at "[ \t\n\r]*>")
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
340 (error "XML: invalid DTD (excepting name of the document)"))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
341 (condition-case nil
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
342 (progn
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
343 (forward-word 1)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
344 (goto-char (- (re-search-forward "[ \t\n\r]") 1))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
345 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
346 (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
347 (re-search-forward "\\][ \t\n\r]*>" end)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
348 (search-forward ">" end)))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
349 (error (error "XML: No end to the DTD"))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
350
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
351 (defun xml-parse-dtd (end)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
352 "Parse the DTD that point is looking at.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
353 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
354 (forward-char (length "<!DOCTYPE"))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
355 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
356 (if (looking-at ">")
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
357 (error "XML: invalid DTD (excepting name of the document)"))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
358
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
359 ;; Get the name of the document
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
360 (looking-at "\\sw+")
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
361 (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
362 type element end-pos)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
363 (goto-char (match-end 0))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
364
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
365 (goto-char (- (re-search-forward "[^ \t\n\r]") 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
366
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
367 ;; External DTDs => don't know how to handle them yet
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
368 (if (looking-at "SYSTEM")
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
369 (error "XML: Don't know how to handle external DTDs"))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
370
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
371 (if (not (= (char-after) ?\[))
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
372 (error "XML: Unknown declaration in the DTD"))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
373
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
374 ;; Parse the rest of the DTD
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
375 (forward-char 1)
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
376 (while (and (not (looking-at "[ \t\n\r]*\\]"))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
377 (<= (point) end))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
378 (cond
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
379
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
380 ;; Translation of rule [45] of XML specifications
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
381 ((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
382 "[ \t\n\r]*<!ELEMENT[ \t\n\r]+\\([a-zA-Z0-9.%;]+\\)[ \t\n\r]+\\([^>]+\\)>")
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
383
30779
aa097d8d4f1a (xml-parse-tag, xml-parse-attlist): Do not downcase
Gerd Moellmann <gerd@gnu.org>
parents: 30329
diff changeset
384 (setq element (intern (match-string-no-properties 1))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
385 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
386 (setq end-pos (match-end 0))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
387
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
388 ;; Translation of rule [46] of XML specifications
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
389 (cond
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
390 ((string-match "^EMPTY[ \t\n\r]*$" type) ;; empty declaration
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
391 (setq type 'empty))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
392 ((string-match "^ANY[ \t\n\r]*$" type) ;; any type of contents
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
393 (setq type 'any))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
394 ((string-match "^(\\(.*\\))[ \t\n\r]*$" type) ;; children ([47])
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
395 (setq type (xml-parse-elem-type (match-string-no-properties 1 type))))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
396 ((string-match "^%[^;]+;[ \t\n\r]*$" type) ;; substitution
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
397 nil)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
398 (t
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
399 (error "XML: Invalid element type in the DTD")))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
400
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
401 ;; rule [45]: the element declaration must be unique
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
402 (if (assoc element dtd)
38409
153f1b1f2efd Emacs lisp coding convention fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 37958
diff changeset
403 (error "XML: elements declaration must be unique in a DTD (<%s>)"
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
404 (symbol-name element)))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
405
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
406 ;; 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
407 (push (list element type) dtd)
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
408 (goto-char end-pos))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
409
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
410
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
411 (t
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
412 (error "XML: Invalid DTD item"))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
413 )
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
414 )
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
415
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
416 ;; Skip the end of the DTD
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
417 (search-forward ">" end)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
418 (nreverse dtd)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
419
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
420
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
421 (defun xml-parse-elem-type (string)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
422 "Convert a STRING for an element type into an elisp structure."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
423
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
424 (let (elem modifier)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
425 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
426 (progn
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
427 (setq elem (match-string 1 string)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
428 modifier (match-string 2 string))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
429 (if (string-match "|" elem)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
430 (setq elem (cons 'choice
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
431 (mapcar 'xml-parse-elem-type
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
432 (split-string elem "|"))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
433 (if (string-match "," elem)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
434 (setq elem (cons 'seq
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
435 (mapcar 'xml-parse-elem-type
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
436 (split-string elem ","))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
437 )))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
438 (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
439 (setq elem (match-string 1 string)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
440 modifier (match-string 2 string))))
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
441
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
442 (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
443 (setq elem 'pcdata))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
444
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
445 (cond
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
446 ((string= modifier "+")
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
447 (list '+ elem))
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
448 ((string= modifier "*")
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
449 (list '* elem))
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
450 ((string= modifier "?")
49787
6269b5c10aec (xml-parse-elem-type): Fix use of character constant.
Juanma Barranquero <lekktu@gmail.com>
parents: 49133
diff changeset
451 (list '\? elem))
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
452 (t
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
453 elem))))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
454
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
455 ;;*******************************************************************
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
456 ;;**
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
457 ;;** Converting code points to strings
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
458 ;;**
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
459 ;;*******************************************************************
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
460
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
461 (defun xml-ucs-to-string (codepoint)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
462 "Return a string representation of CODEPOINT. If it can't be
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
463 converted, return '?'."
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
464 (cond ((boundp 'decode-char)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
465 (char-to-string (decode-char 'ucs codepoint)))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
466 ((and (< codepoint 128)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
467 (> codepoint 31))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
468 (char-to-string codepoint))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
469 (t "?"))) ; FIXME: There's gotta be a better way to
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
470 ; designate an unknown character.
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
471
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
472 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
473 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
474 ;;** Substituting special XML sequences
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
475 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
476 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
477
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
478 (defun xml-substitute-special (string)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
479 "Return STRING, after subsituting special XML sequences."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
480 (while (string-match "&lt;" string)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
481 (setq string (replace-match "<" t nil string)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
482 (while (string-match "&gt;" string)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
483 (setq string (replace-match ">" t nil string)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
484 (while (string-match "&apos;" string)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
485 (setq string (replace-match "'" t nil string)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
486 (while (string-match "&quot;" string)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
487 (setq string (replace-match "\"" t nil string)))
50144
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
488 (while (string-match "&#\\([0-9]+\\);" string)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
489 (setq string (replace-match (xml-ucs-to-string
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
490 (string-to-number
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
491 (match-string-no-properties 1 string)))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
492 t nil string)))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
493 (while (string-match "&#x\\([0-9a-fA-F]+\\);" 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 string (replace-match (xml-ucs-to-string
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
495 (string-to-number
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
496 (match-string-no-properties 1 string)
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
497 16))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
498 t nil string)))
ff1b1d15e1f2 (xml-ucs-to-string): New function to convert Unicode codepoints to strings.
Juanma Barranquero <lekktu@gmail.com>
parents: 50081
diff changeset
499
49065
265dc22fb2c0 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 49036
diff changeset
500 ;; This goes last so it doesn't confuse the matches above.
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
501 (while (string-match "&amp;" string)
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
502 (setq string (replace-match "&" t nil string)))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
503 string)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
504
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
505 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
506 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
507 ;;** Printing a tree.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
508 ;;** This function is intended mainly for debugging purposes.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
509 ;;**
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
510 ;;*******************************************************************
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
511
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
512 (defun xml-debug-print (xml)
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
513 (dolist (node xml)
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
514 (xml-debug-print-internal node "")))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
515
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
516 (defun xml-debug-print-internal (xml indent-string)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
517 "Outputs the XML tree in the current buffer.
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
518 The first line indented with INDENT-STRING."
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
519 (let ((tree xml)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
520 attlist)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
521 (insert indent-string "<" (symbol-name (xml-node-name tree)))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
522
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
523 ;; output the attribute list
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
524 (setq attlist (xml-node-attributes tree))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
525 (while attlist
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
526 (insert " ")
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
527 (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
528 (setq attlist (cdr attlist)))
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
529
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
530 (insert ">")
49036
466922eb2b8d (xml-substitute-special): Move "&amp;" -> "&" last.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 48869
diff changeset
531
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
532 (setq tree (xml-node-children tree))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
533
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
534 ;; output the children
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
535 (dolist (node tree)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
536 (cond
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
537 ((listp node)
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
538 (insert "\n")
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
539 (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
540 ((stringp node) (insert node))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
541 (t
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
542 (error "Invalid XML tree"))))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
543
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
544 (insert "\n" indent-string
42031
54db4085a7df Use setq rather than (set 'foo bar).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 40030
diff changeset
545 "</" (symbol-name (xml-node-name xml)) ">")))
30329
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
546
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
547 (provide 'xml)
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
548
1ea701655cf8 *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
549 ;;; xml.el ends here