1 ;; jabber-xml.el - XML functions
3 ;; Copyright (C) 2003, 2004, 2007, 2008 - Magnus Henoch - mange@freemail.hu
4 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
6 ;; This file is a part of jabber.el.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 (require 'jabber-util
)
25 (defun jabber-escape-xml (str)
26 "escape strings for xml"
28 (let ((newstr (concat str
)))
29 ;; Form feeds might appear in code you copy, etc. Nevertheless,
31 (setq newstr
(jabber-replace-in-string newstr
"\f" "\n"))
32 ;; Other control characters are also illegal, except for
34 (setq newstr
(jabber-replace-in-string newstr
"[\000-\010\013\014\016-\037]" " "))
35 (setq newstr
(jabber-replace-in-string newstr
"&" "&"))
36 (setq newstr
(jabber-replace-in-string newstr
"<" "<"))
37 (setq newstr
(jabber-replace-in-string newstr
">" ">"))
38 (setq newstr
(jabber-replace-in-string newstr
"'" "'"))
39 (setq newstr
(jabber-replace-in-string newstr
"\"" """))
43 (defun jabber-unescape-xml (str)
44 "unescape xml strings"
45 ;; Eventually this can be done with `xml-substitute-special', but the
46 ;; version in xml.el of GNU Emacs 21.3 is buggy.
49 (setq newstr
(jabber-replace-in-string newstr
""" "\""))
50 (setq newstr
(jabber-replace-in-string newstr
"'" "'"))
51 (setq newstr
(jabber-replace-in-string newstr
">" ">"))
52 (setq newstr
(jabber-replace-in-string newstr
"<" "<"))
53 (setq newstr
(jabber-replace-in-string newstr
"&" "&"))
57 (defun jabber-sexp2xml (sexp)
58 "converts an SEXP in the format (tagname ((attribute-name . attribute-value)...) children...) and converts it to well-formatted xml."
61 (jabber-escape-xml sexp
))
65 (setq xml
(concat xml
(jabber-sexp2xml tag
))))
67 ;; work around bug in old versions of xml.el, where ("") can appear
68 ;; as children of a node
71 (zerop (length (car sexp
))))
76 (symbol-name (car sexp
))))
77 (dolist (attr (cadr sexp
))
81 (symbol-name (car attr
))
82 (jabber-escape-xml (cdr attr
)))))))
85 (setq xml
(concat xml
">"))
86 (dolist (child (cddr sexp
))
88 (jabber-sexp2xml child
))))
91 (symbol-name (car sexp
))
97 (defun jabber-xml-skip-tag-forward (&optional dont-recurse-into-stream
)
98 "Skip to end of tag or matching closing tag if present.
99 Return t iff after a closing tag, otherwise throws an 'unfinished
101 If DONT-RECURSE-INTO-STREAM is true, stop after an opening
104 The version of `sgml-skip-tag-forward' in Emacs 21 isn't good
106 (skip-chars-forward "^<")
108 ((looking-at "<!\\[CDATA\\[")
109 (if (search-forward "]]>" nil t
)
110 (goto-char (match-end 0))
111 (throw 'unfinished nil
)))
112 ((looking-at "<\\([^ \t\n/>]+\\)\\([ \t\n]+[^=]+='[^']*'\\|[ \t\n]+[^=]+=\"[^\"]*\"\\)*")
113 (let ((node-name (match-string 1)))
114 (goto-char (match-end 0))
117 (goto-char (match-end 0))
121 (unless (and dont-recurse-into-stream
(equal node-name
"stream:stream"))
123 do
(skip-chars-forward "^<")
124 until
(looking-at (regexp-quote (concat "</" node-name
">")))
125 do
(jabber-xml-skip-tag-forward))
126 (goto-char (match-end 0)))
129 (throw 'unfinished nil
)))))
131 (throw 'unfinished nil
))))
133 (defsubst jabber-xml-node-name
(node)
134 "Return the tag associated with NODE.
135 The tag is a lower-case symbol."
136 (if (listp node
) (car node
)))
138 (defsubst jabber-xml-node-attributes
(node)
139 "Return the list of attributes of NODE.
140 The list can be nil."
141 (if (listp node
) (nth 1 node
)))
143 (defsubst jabber-xml-node-children
(node)
144 "Return the list of children of NODE.
145 This is a list of nodes, and it can be nil."
146 (let ((children (cddr node
)))
147 ;; Work around a bug in early versions of xml.el
148 (if (equal children
'(("")))
152 (defun jabber-xml-get-children (node child-name
)
153 "Return the children of NODE whose tag is CHILD-NAME.
154 CHILD-NAME should be a lower case symbol."
156 (dolist (child (jabber-xml-node-children node
))
158 (if (equal (jabber-xml-node-name child
) child-name
)
159 (push child match
))))
162 ;; `xml-get-attribute' returns "" if the attribute is not found, which
163 ;; is not very useful. Therefore, we use `xml-get-attribute-or-nil'
164 ;; if present, or emulate its behavior.
166 (if (fboundp 'xml-get-attribute-or-nil
)
167 (defsubst jabber-xml-get-attribute
(node attribute
)
168 "Get from NODE the value of ATTRIBUTE.
169 Return nil if the attribute was not found."
171 (xml-get-attribute-or-nil node attribute
)))
172 (defsubst jabber-xml-get-attribute
(node attribute
)
173 "Get from NODE the value of ATTRIBUTE.
174 Return nil if the attribute was not found."
176 (let ((result (xml-get-attribute node attribute
)))
177 (and (> (length result
) 0) result
))))))
179 (defsubst jabber-xml-get-xmlns
(node)
180 "Get \"xmlns\" attribute of NODE, or nil if not present."
181 (jabber-xml-get-attribute node
'xmlns
))
183 (defun jabber-xml-path (xml-data path
)
184 "Find sub-node of XML-DATA according to PATH.
185 PATH is a vaguely XPath-inspired list. Each element can be:
187 a symbol go to first child node with this node name
188 cons cell car is string containing namespace URI,
189 cdr is string containing node name. Find
190 first matching child node.
191 any string character data of this node"
192 (let ((node xml-data
))
193 (while (and path node
)
194 (let ((step (car path
)))
197 (setq node
(car (jabber-xml-get-children node step
))))
199 ;; This will be easier with namespace-aware use
200 ;; of xml.el. It will also be more correct.
201 ;; Now, it only matches explicit namespace declarations.
203 (dolist (x (jabber-xml-get-children node
(intern (cdr step
))))
204 (when (string= (jabber-xml-get-attribute x
'xmlns
)
208 (setq node
(car (jabber-xml-node-children node
)))
209 (unless (stringp node
)
212 (error "Unknown path step: %s" step
))))
213 (setq path
(cdr path
)))
216 (defmacro jabber-xml-let-attributes
(attributes xml-data
&rest body
)
217 "Bind variables to the same-name attribute values in XML-DATA."
218 `(let ,(mapcar #'(lambda (attr)
219 (list attr
`(jabber-xml-get-attribute ,xml-data
',attr
)))
222 (put 'jabber-xml-let-attributes
'lisp-indent-function
2)
224 (provide 'jabber-xml
)
226 ;;; arch-tag: ca206e65-7026-4ee8-9af2-ff6a9c5af98a