Commit | Line | Data |
---|---|---|
8cd39fb3 MH |
1 | ;;; nxml-ns.el --- XML namespace processing |
2 | ||
dcb8ac09 | 3 | ;; Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc. |
8cd39fb3 MH |
4 | |
5 | ;; Author: James Clark | |
6 | ;; Keywords: XML | |
7 | ||
341543da | 8 | ;; This file is part of GNU Emacs. |
8cd39fb3 | 9 | |
341543da GM |
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 3, or (at your option) | |
13 | ;; any later version. | |
8cd39fb3 | 14 | |
341543da GM |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
23 | ;; Boston, MA 02110-1301, USA. | |
8cd39fb3 MH |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; This file uses a prefix of `nxml-ns'. | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (require 'nxml-util) | |
32 | ||
33 | (defvar nxml-ns-state nil | |
34 | "Contains the state of namespace processing. The state | |
35 | is never modified destructively and so can be saved and restored | |
36 | without copying. | |
37 | ||
38 | The value is a stack represented by a list. The list has length N + 1 | |
39 | where N is the number of open elements. Each member of the list | |
40 | represents the bindings in effect for a particular element. Each | |
41 | member is itself a list whose car is the default namespace | |
42 | \(a symbol or nil) and whose cdr is an alist of (PREFIX . NS) pairs | |
43 | where PREFIX is a string (never nil) and NS is the namespace URI | |
44 | symbol.") | |
45 | ||
46 | (defconst nxml-ns-initial-state | |
47 | (list (list nil (cons "xml" nxml-xml-namespace-uri))) | |
48 | "A list to be used as the initial value of nxml-ns-state. This | |
49 | represents the state with no open elements and with the default | |
50 | namespace bindings (no default namespace and only the xml prefix bound).") | |
51 | ||
52 | (defsubst nxml-ns-state () nxml-ns-state) | |
53 | ||
54 | (defsubst nxml-ns-set-state (state) | |
55 | (setq nxml-ns-state state)) | |
56 | ||
57 | (defsubst nxml-ns-state-equal (state) | |
58 | (equal nxml-ns-state state)) | |
59 | ||
60 | (defmacro nxml-ns-save (&rest body) | |
61 | `(let ((nxml-ns-state nxml-ns-initial-state)) | |
62 | ,@body)) | |
63 | ||
64 | (put 'nxml-ns-save 'lisp-indent-function 0) | |
65 | (def-edebug-spec nxml-ns-save t) | |
66 | ||
67 | (defun nxml-ns-init () | |
68 | (setq nxml-ns-state nxml-ns-initial-state)) | |
69 | ||
70 | (defun nxml-ns-push-state () | |
71 | "Change the state by starting a new element. Namespace declarations | |
72 | are inherited from the parent state." | |
73 | (setq nxml-ns-state (cons (car nxml-ns-state) nxml-ns-state))) | |
74 | ||
75 | (defun nxml-ns-pop-state () | |
76 | "Change the state by ending an element. The behaviour is undefined | |
77 | if there is no open element." | |
78 | (setq nxml-ns-state (cdr nxml-ns-state))) | |
79 | ||
80 | (defun nxml-ns-get-prefix (prefix) | |
81 | "Return the symbol for namespace bound to PREFIX, or nil if PREFIX | |
82 | is unbound. PREFIX is a string, never nil." | |
83 | (let ((binding (assoc prefix (cdar nxml-ns-state)))) | |
84 | (and binding (cdr binding)))) | |
85 | ||
86 | (defun nxml-ns-set-prefix (prefix ns) | |
87 | "Change the binding of PREFIX. PREFIX is a string (never nil). NS | |
88 | is a symbol (never nil). The change will be in effect until the end of | |
89 | the current element." | |
90 | (setq nxml-ns-state | |
91 | (let ((bindings (car nxml-ns-state))) | |
92 | (cons (cons (car bindings) | |
93 | (cons (cons prefix ns) (cdr bindings))) | |
94 | (cdr nxml-ns-state))))) | |
95 | ||
96 | (defun nxml-ns-get-default () | |
97 | "Return the current default namespace as a symbol, or nil | |
98 | if there is no default namespace." | |
99 | (caar nxml-ns-state)) | |
100 | ||
101 | (defun nxml-ns-set-default (ns) | |
102 | "Changes the current default namespace. The change | |
103 | will be in effect until the end of the current element. | |
104 | NS is a symbol or nil." | |
105 | (setq nxml-ns-state | |
106 | (cons (cons ns (cdar nxml-ns-state)) | |
107 | (cdr nxml-ns-state)))) | |
108 | ||
109 | (defun nxml-ns-get-context () | |
110 | (car nxml-ns-state)) | |
111 | ||
112 | (defun nxml-ns-prefixes-for (ns &optional attributep) | |
113 | (let ((current (car nxml-ns-state)) | |
114 | prefixes) | |
115 | (when (if attributep | |
116 | (not ns) | |
117 | (eq (car current) ns)) | |
118 | (setq prefixes '(nil))) | |
119 | (setq current (cdr current)) | |
120 | (while (let ((binding (rassq ns current))) | |
121 | (when binding | |
122 | (when (eq (nxml-ns-get-prefix (car binding)) ns) | |
123 | (add-to-list 'prefixes | |
124 | (car binding))) | |
125 | (setq current | |
126 | (cdr (member binding current)))))) | |
127 | prefixes)) | |
128 | ||
129 | (defun nxml-ns-prefix-for (ns) | |
130 | (car (rassq ns (cdar nxml-ns-state)))) | |
131 | ||
132 | (defun nxml-ns-changed-prefixes () | |
133 | (let ((old (cadr nxml-ns-state)) | |
134 | (new (car nxml-ns-state)) | |
135 | changed) | |
136 | (if (eq old new) | |
137 | nil | |
138 | (unless (eq (car new) (car old)) | |
139 | (setq changed '(nil))) | |
140 | (setq new (cdr new)) | |
141 | (setq old (cdr old)) | |
142 | (while (not (eq new old)) | |
143 | (setq changed | |
144 | (cons (caar new) changed)) | |
145 | (setq new (cdr new)))) | |
146 | changed)) | |
147 | ||
148 | (provide 'nxml-ns) | |
149 | ||
ab4c34c6 | 150 | ;; arch-tag: 5968e4b7-fb37-46ce-8621-c65db9793028 |
8cd39fb3 | 151 | ;;; nxml-ns.el ends here |