rng-parse.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; rng-parse.el --- parse an XML file and validate it against a schema
  2. ;; Copyright (C) 2003, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: XML, RelaxNG
  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 combines the validation machinery in rng-match.el with the
  18. ;; parser in nxml-parse.el by using the `nxml-validate-function' hook.
  19. ;;; Code:
  20. (require 'nxml-parse)
  21. (require 'rng-match)
  22. (require 'rng-dt)
  23. (defvar rng-parse-prev-was-start-tag nil)
  24. (defun rng-parse-validate-file (schema file)
  25. "Parse and validate the XML document in FILE and return it as a list.
  26. The returned list has the same form as that returned by
  27. `nxml-parse-file'. SCHEMA is a list representing the schema to use
  28. for validation, such as returned by the function `rng-c-load-schema'.
  29. If the XML document is invalid with respect to schema, an error will
  30. be signaled in the same way as when it is not well-formed."
  31. (with-current-buffer (nxml-parse-find-file file)
  32. (unwind-protect
  33. (let ((nxml-parse-file-name file)
  34. (nxml-validate-function 'rng-parse-do-validate)
  35. (rng-dt-namespace-context-getter '(nxml-ns-get-context))
  36. rng-parse-prev-was-start-tag)
  37. ;; We don't simply call nxml-parse-file, because
  38. ;; we want to do rng-match-with-schema in the same
  39. ;; buffer in which we will call the other rng-match-* functions.
  40. (rng-match-with-schema schema
  41. (nxml-parse-instance)))
  42. (kill-buffer nil))))
  43. (defun rng-parse-do-validate (text start-tag)
  44. (cond ((and (let ((tem rng-parse-prev-was-start-tag))
  45. (setq rng-parse-prev-was-start-tag (and start-tag t))
  46. tem)
  47. (not start-tag)
  48. (rng-match-text-typed-p))
  49. (unless (rng-match-element-value (or text ""))
  50. (cons "Invalid data" (and text 'text))))
  51. ((and text
  52. (not (rng-blank-p text))
  53. (not (rng-match-mixed-text)))
  54. (cons "Text not allowed" 'text))
  55. ((not start-tag)
  56. (unless (rng-match-end-tag)
  57. (cons "Missing elements" nil)))
  58. ((not (rng-match-start-tag-open
  59. (rng-parse-to-match-name (car start-tag))))
  60. (cons "Element not allowed" nil))
  61. (t
  62. (let ((atts (cadr start-tag))
  63. (i 0)
  64. att err)
  65. (while (and atts (not err))
  66. (setq att (car atts))
  67. (when (not (and (consp (car att))
  68. (eq (caar att) nxml-xmlns-namespace-uri)))
  69. (setq err
  70. (cond ((not (rng-match-attribute-name
  71. (rng-parse-to-match-name (car att))))
  72. (cons "Attribute not allowed"
  73. (cons 'attribute-name i)))
  74. ((not (rng-match-attribute-value (cdr att)))
  75. (cons "Invalid attribute value"
  76. (cons 'attribute-value i))))))
  77. (setq atts (cdr atts))
  78. (setq i (1+ i)))
  79. (or err
  80. (unless (rng-match-start-tag-close)
  81. (cons "Missing attributes" 'tag-close)))))))
  82. (defun rng-parse-to-match-name (name)
  83. (if (consp name)
  84. name
  85. (cons nil name)))
  86. (provide 'rng-parse)
  87. ;;; rng-parse.el ends here