nxml-ns.el 4.5 KB

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