rng-loc.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. ;;; rng-loc.el --- locate the schema to use for validation
  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. ;;; Code:
  18. (require 'nxml-util)
  19. (require 'nxml-parse)
  20. (require 'rng-parse)
  21. (require 'rng-uri)
  22. (require 'rng-util)
  23. (require 'xmltok)
  24. (defvar rng-current-schema-file-name nil
  25. "Filename of schema being used for current buffer.
  26. It is nil if using a vacuous schema.")
  27. (make-variable-buffer-local 'rng-current-schema-file-name)
  28. (defvar rng-schema-locating-files-default
  29. (list "schemas.xml" (expand-file-name "schema/schemas.xml" data-directory))
  30. "Default value for variable `rng-schema-locating-files'.")
  31. (defvar rng-schema-locating-file-schema-file
  32. (expand-file-name "schema/locate.rnc" data-directory)
  33. "File containing schema for schema locating files.")
  34. (defvar rng-schema-locating-file-schema nil
  35. "Schema for schema locating files or nil if not yet loaded.")
  36. (defcustom rng-schema-locating-files rng-schema-locating-files-default
  37. "List of schema locating files."
  38. :type '(repeat file)
  39. :group 'relax-ng)
  40. (defvar rng-schema-loader-alist '(("rnc" . rng-c-load-schema))
  41. "Alist of schema extensions vs schema loader functions.")
  42. (defvar rng-cached-document-element nil)
  43. (defvar rng-document-type-history nil)
  44. (defun rng-set-document-type (type-id)
  45. (interactive (list (rng-read-type-id)))
  46. (condition-case err
  47. (when (not (string= type-id ""))
  48. (let ((schema-file (rng-locate-schema-file type-id)))
  49. (unless schema-file
  50. (error "Could not locate schema for type id `%s'" type-id))
  51. (rng-set-schema-file-1 schema-file))
  52. (rng-save-schema-location-1 t type-id)
  53. (rng-what-schema))
  54. (nxml-file-parse-error
  55. (nxml-display-file-parse-error err))))
  56. (defun rng-read-type-id ()
  57. (condition-case err
  58. (let ((type-ids (rng-possible-type-ids))
  59. (completion-ignore-case nil))
  60. (completing-read "Document type id: "
  61. (mapcar (lambda (x) (cons x nil))
  62. type-ids)
  63. nil
  64. t
  65. nil
  66. 'rng-document-type-history))
  67. (nxml-file-parse-error
  68. (nxml-display-file-parse-error err))))
  69. (defun rng-set-schema-file (filename)
  70. "Set the schema for the current buffer to the schema in FILENAME.
  71. FILENAME must be the name of a file containing a schema.
  72. The extension of FILENAME is used to determine what kind of schema it
  73. is. The variable `rng-schema-loader-alist' maps from schema
  74. extensions to schema loader functions. The function
  75. `rng-c-load-schema' is the loader for RELAX NG compact syntax. The
  76. association is between the buffer and the schema: the association is
  77. lost when the buffer is killed."
  78. (interactive "fSchema file: ")
  79. (condition-case err
  80. (progn
  81. (rng-set-schema-file-1 filename)
  82. (rng-save-schema-location-1 t))
  83. (nxml-file-parse-error
  84. (nxml-display-file-parse-error err))))
  85. (defun rng-set-vacuous-schema ()
  86. "Set the schema for the current buffer to allow any well-formed XML."
  87. (interactive)
  88. (rng-set-schema-file-1 nil)
  89. (rng-what-schema))
  90. (defun rng-set-schema-file-1 (filename)
  91. (setq filename (and filename (expand-file-name filename)))
  92. (setq rng-current-schema
  93. (if filename
  94. (rng-load-schema filename)
  95. rng-any-element))
  96. (setq rng-current-schema-file-name filename)
  97. (run-hooks 'rng-schema-change-hook))
  98. (defun rng-load-schema (filename)
  99. (let* ((extension (file-name-extension filename))
  100. (loader (cdr (assoc extension rng-schema-loader-alist))))
  101. (or loader
  102. (if extension
  103. (error "No schema loader available for file extension `%s'"
  104. extension)
  105. (error "No schema loader available for null file extension")))
  106. (funcall loader filename)))
  107. (defun rng-what-schema ()
  108. "Display a message saying what schema `rng-validate-mode' is using."
  109. (interactive)
  110. (if rng-current-schema-file-name
  111. (message "Using schema %s"
  112. (abbreviate-file-name rng-current-schema-file-name))
  113. (message "Using vacuous schema")))
  114. (defun rng-auto-set-schema (&optional no-display-error)
  115. "Set the schema for this buffer based on the buffer's contents and file-name."
  116. (interactive)
  117. (condition-case err
  118. (progn
  119. (rng-set-schema-file-1 (rng-locate-schema-file))
  120. (rng-what-schema))
  121. (nxml-file-parse-error
  122. (if no-display-error
  123. (error "%s at position %s in %s"
  124. (nth 3 err)
  125. (nth 2 err)
  126. (abbreviate-file-name (nth 1 err)))
  127. (nxml-display-file-parse-error err)))))
  128. (defun rng-locate-schema-file (&optional type-id)
  129. "Return the file-name of the schema to use for the current buffer.
  130. Return nil if no schema could be located.
  131. If TYPE-ID is non-nil, then locate the schema for this TYPE-ID."
  132. (let* ((rng-cached-document-element nil)
  133. (schema
  134. (if type-id
  135. (cons type-id nil)
  136. (rng-locate-schema-file-using rng-schema-locating-files)))
  137. files type-ids)
  138. (while (consp schema)
  139. (setq files rng-schema-locating-files)
  140. (setq type-id (car schema))
  141. (setq schema nil)
  142. (when (member type-id type-ids)
  143. (error "Type-id loop for type-id `%s'" type-id))
  144. (setq type-ids (cons type-id type-ids))
  145. (while (and files (not schema))
  146. (setq schema
  147. (rng-locate-schema-file-from-type-id type-id
  148. (car files)))
  149. (setq files (cdr files))))
  150. (and schema
  151. (rng-uri-file-name schema))))
  152. (defun rng-possible-type-ids ()
  153. "Return a list of the known type IDs."
  154. (let ((files rng-schema-locating-files)
  155. type-ids)
  156. (while files
  157. (setq type-ids (rng-possible-type-ids-using (car files) type-ids))
  158. (setq files (cdr files)))
  159. (rng-uniquify-equal (sort type-ids 'string<))))
  160. (defun rng-locate-schema-file-using (files)
  161. "Locate a schema using the schema locating files FILES.
  162. FILES is a list of file-names.
  163. Return either a URI, a list (TYPE-ID) where TYPE-ID is a string,
  164. or nil."
  165. (let (rules
  166. ;; List of types that override normal order-based
  167. ;; priority, most important first
  168. preferred-types
  169. ;; Best result found so far; same form as return value.
  170. best-so-far)
  171. (while (and (progn
  172. (while (and (not rules) files)
  173. (setq rules (rng-get-parsed-schema-locating-file
  174. (car files)))
  175. (setq files (cdr files)))
  176. rules)
  177. (or (not best-so-far) preferred-types))
  178. (let* ((rule (car rules))
  179. (rule-type (car rule))
  180. (rule-matcher (get rule-type 'rng-rule-matcher)))
  181. (setq rules (cdr rules))
  182. (cond (rule-matcher
  183. (when (and (or (not best-so-far)
  184. (memq rule-type preferred-types)))
  185. (setq best-so-far
  186. (funcall rule-matcher (cdr rule)))
  187. preferred-types)
  188. (setq preferred-types
  189. (nbutlast preferred-types
  190. (length (memq rule-type preferred-types)))))
  191. ((eq rule-type 'applyFollowingRules)
  192. (when (not best-so-far)
  193. (let ((prefer (cdr (assq 'ruleType (cdr rule)))))
  194. (when (and prefer
  195. (not (memq (setq prefer (intern prefer))
  196. preferred-types)))
  197. (setq preferred-types
  198. (nconc preferred-types (list prefer)))))))
  199. ((eq rule-type 'include)
  200. (let ((uri (cdr (assq 'rules (cdr rule)))))
  201. (when uri
  202. (setq rules
  203. (append (rng-get-parsed-schema-locating-file
  204. (rng-uri-file-name uri))
  205. rules))))))))
  206. best-so-far))
  207. (put 'documentElement 'rng-rule-matcher 'rng-match-document-element-rule)
  208. (put 'namespace 'rng-rule-matcher 'rng-match-namespace-rule)
  209. (put 'uri 'rng-rule-matcher 'rng-match-uri-rule)
  210. (put 'transformURI 'rng-rule-matcher 'rng-match-transform-uri-rule)
  211. (put 'default 'rng-rule-matcher 'rng-match-default-rule)
  212. (defun rng-match-document-element-rule (props)
  213. (let ((document-element (rng-document-element))
  214. (prefix (cdr (assq 'prefix props)))
  215. (local-name (cdr (assq 'localName props))))
  216. (and (or (not prefix)
  217. (if (= (length prefix) 0)
  218. (not (nth 1 document-element))
  219. (string= prefix (nth 1 document-element))))
  220. (or (not local-name)
  221. (string= local-name
  222. (nth 2 document-element)))
  223. (rng-match-default-rule props))))
  224. (defun rng-match-namespace-rule (props)
  225. (let ((document-element (rng-document-element))
  226. (ns (cdr (assq 'ns props))))
  227. (and document-element
  228. ns
  229. (eq (nth 0 document-element)
  230. (if (string= ns "")
  231. nil
  232. (nxml-make-namespace ns)))
  233. (rng-match-default-rule props))))
  234. (defun rng-document-element ()
  235. "Return a list (NS PREFIX LOCAL-NAME).
  236. NS is t if the document has a non-nil, but not otherwise known namespace."
  237. (or rng-cached-document-element
  238. (setq rng-cached-document-element
  239. (save-excursion
  240. (save-restriction
  241. (widen)
  242. (goto-char (point-min))
  243. (let (xmltok-dtd)
  244. (xmltok-save
  245. (xmltok-forward-prolog)
  246. (xmltok-forward)
  247. (when (memq xmltok-type '(start-tag
  248. partial-start-tag
  249. empty-element
  250. partial-empty-element))
  251. (list (rng-get-start-tag-namespace)
  252. (xmltok-start-tag-prefix)
  253. (xmltok-start-tag-local-name))))))))))
  254. (defun rng-get-start-tag-namespace ()
  255. (let ((prefix (xmltok-start-tag-prefix))
  256. namespace att value)
  257. (while xmltok-namespace-attributes
  258. (setq att (car xmltok-namespace-attributes))
  259. (setq xmltok-namespace-attributes (cdr xmltok-namespace-attributes))
  260. (when (if prefix
  261. (and (xmltok-attribute-prefix att)
  262. (string= (xmltok-attribute-local-name att)
  263. prefix))
  264. (not (xmltok-attribute-prefix att)))
  265. (setq value (xmltok-attribute-value att))
  266. (setq namespace (if value (nxml-make-namespace value) t))))
  267. (if (and prefix (not namespace))
  268. t
  269. namespace)))
  270. (defun rng-match-transform-uri-rule (props)
  271. (let ((from-pattern (cdr (assq 'fromPattern props)))
  272. (to-pattern (cdr (assq 'toPattern props)))
  273. (file-name (buffer-file-name)))
  274. (and file-name
  275. (setq file-name (expand-file-name file-name))
  276. (rng-file-name-matches-uri-pattern-p file-name from-pattern)
  277. (condition-case ()
  278. (let ((new-file-name
  279. (replace-match
  280. (save-match-data
  281. (rng-uri-pattern-file-name-replace-match to-pattern))
  282. t
  283. nil
  284. file-name)))
  285. (and (file-name-absolute-p new-file-name)
  286. (file-exists-p new-file-name)
  287. (rng-file-name-uri new-file-name)))
  288. (rng-uri-error nil)))))
  289. (defun rng-match-uri-rule (props)
  290. (let ((resource (cdr (assq 'resource props)))
  291. (pattern (cdr (assq 'pattern props)))
  292. (file-name (buffer-file-name)))
  293. (and file-name
  294. (setq file-name (expand-file-name file-name))
  295. (cond (resource
  296. (condition-case ()
  297. (eq (compare-strings (rng-uri-file-name resource)
  298. 0
  299. nil
  300. (expand-file-name file-name)
  301. 0
  302. nil
  303. nxml-file-name-ignore-case)
  304. t)
  305. (rng-uri-error nil)))
  306. (pattern
  307. (rng-file-name-matches-uri-pattern-p file-name
  308. pattern)))
  309. (rng-match-default-rule props))))
  310. (defun rng-file-name-matches-uri-pattern-p (file-name pattern)
  311. (condition-case ()
  312. (and (let ((case-fold-search nxml-file-name-ignore-case))
  313. (string-match (rng-uri-pattern-file-name-regexp pattern)
  314. file-name))
  315. t)
  316. (rng-uri-error nil)))
  317. (defun rng-match-default-rule (props)
  318. (or (cdr (assq 'uri props))
  319. (let ((type-id (cdr (assq 'typeId props))))
  320. (and type-id
  321. (cons (rng-collapse-space type-id) nil)))))
  322. (defun rng-possible-type-ids-using (file type-ids)
  323. (let ((rules (rng-get-parsed-schema-locating-file file))
  324. rule)
  325. (while rules
  326. (setq rule (car rules))
  327. (setq rules (cdr rules))
  328. (cond ((eq (car rule) 'typeId)
  329. (let ((id (cdr (assq 'id (cdr rule)))))
  330. (when id
  331. (setq type-ids
  332. (cons (rng-collapse-space id)
  333. type-ids)))))
  334. ((eq (car rule) 'include)
  335. (let ((uri (cdr (assq 'rules (cdr rule)))))
  336. (when uri
  337. (setq type-ids
  338. (rng-possible-type-ids-using
  339. (rng-get-parsed-schema-locating-file
  340. (rng-uri-file-name uri))
  341. type-ids)))))))
  342. type-ids))
  343. (defun rng-locate-schema-file-from-type-id (type-id file)
  344. "Locate the schema for type id TYPE-ID using schema locating file FILE.
  345. Return either a URI, a list (TYPE-ID) where TYPE-ID is a string,
  346. or nil."
  347. (let ((rules (rng-get-parsed-schema-locating-file file))
  348. schema rule)
  349. (while (and rules (not schema))
  350. (setq rule (car rules))
  351. (setq rules (cdr rules))
  352. (cond ((and (eq (car rule) 'typeId)
  353. (let ((id (assq 'id (cdr rule))))
  354. (and id
  355. (string= (rng-collapse-space (cdr id)) type-id))))
  356. (setq schema (rng-match-default-rule (cdr rule))))
  357. ((eq (car rule) 'include)
  358. (let ((uri (cdr (assq 'rules (cdr rule)))))
  359. (when uri
  360. (setq schema
  361. (rng-locate-schema-file-from-type-id
  362. type-id
  363. (rng-uri-file-name uri))))))))
  364. schema))
  365. (defvar rng-schema-locating-file-alist nil)
  366. (defun rng-get-parsed-schema-locating-file (file)
  367. "Return a list of rules for the schema locating file FILE."
  368. (setq file (expand-file-name file))
  369. (let ((cached (assoc file rng-schema-locating-file-alist))
  370. (mtime (nth 5 (file-attributes file)))
  371. parsed)
  372. (cond ((not mtime)
  373. (when cached
  374. (setq rng-schema-locating-file-alist
  375. (delq cached rng-schema-locating-file-alist)))
  376. nil)
  377. ((and cached (equal (nth 1 cached) mtime))
  378. (nth 2 cached))
  379. (t
  380. (setq parsed (rng-parse-schema-locating-file file))
  381. (if cached
  382. (setcdr cached (list mtime parsed))
  383. (setq rng-schema-locating-file-alist
  384. (cons (list file mtime parsed)
  385. rng-schema-locating-file-alist)))
  386. parsed))))
  387. (defconst rng-locate-namespace-uri
  388. (nxml-make-namespace "http://thaiopensource.com/ns/locating-rules/1.0"))
  389. (defun rng-parse-schema-locating-file (file)
  390. "Return list of rules.
  391. Each rule has the form (TYPE (ATTR . VAL) ...), where
  392. TYPE is a symbol for the element name, ATTR is a symbol for the attribute
  393. and VAL is a string for the value.
  394. Attribute values representing URIs are made absolute and xml:base
  395. attributes are removed."
  396. (when (and (not rng-schema-locating-file-schema)
  397. rng-schema-locating-file-schema-file)
  398. (setq rng-schema-locating-file-schema
  399. (rng-load-schema rng-schema-locating-file-schema-file)))
  400. (let* ((element
  401. (if rng-schema-locating-file-schema
  402. (rng-parse-validate-file rng-schema-locating-file-schema
  403. file)
  404. (nxml-parse-file file)))
  405. (children (cddr element))
  406. (base-uri (rng-file-name-uri file))
  407. child name rules atts att props prop-name prop-value)
  408. (when (equal (car element)
  409. (cons rng-locate-namespace-uri "locatingRules"))
  410. (while children
  411. (setq child (car children))
  412. (setq children (cdr children))
  413. (when (consp child)
  414. (setq name (car child))
  415. (when (eq (car name) rng-locate-namespace-uri)
  416. (setq atts (cadr child))
  417. (setq props nil)
  418. (while atts
  419. (setq att (car atts))
  420. (when (stringp (car att))
  421. (setq prop-name (intern (car att)))
  422. (setq prop-value (cdr att))
  423. (when (memq prop-name '(uri rules resource))
  424. (setq prop-value
  425. (rng-uri-resolve prop-value base-uri)))
  426. (setq props (cons (cons prop-name prop-value)
  427. props)))
  428. (setq atts (cdr atts)))
  429. (setq rules
  430. (cons (cons (intern (cdr name)) (nreverse props))
  431. rules))))))
  432. (nreverse rules)))
  433. (defun rng-save-schema-location ()
  434. "Save the association between the buffer's file and the current schema.
  435. This ensures that the schema that is currently being used will be used
  436. if the file is edited in a future session. The association will be
  437. saved to the first writable file in `rng-schema-locating-files'."
  438. (interactive)
  439. (rng-save-schema-location-1 nil))
  440. (defun rng-save-schema-location-1 (prompt &optional type-id)
  441. (unless (or rng-current-schema-file-name type-id)
  442. (error "Buffer is using a vacuous schema"))
  443. (let ((files rng-schema-locating-files)
  444. (document-file-name (buffer-file-name))
  445. (schema-file-name rng-current-schema-file-name)
  446. file)
  447. (while (and files (not file))
  448. (if (file-writable-p (car files))
  449. (setq file (expand-file-name (car files)))
  450. (setq files (cdr files))))
  451. (cond ((not file)
  452. (if prompt
  453. nil
  454. (error "No writable schema locating file configured")))
  455. ((not document-file-name)
  456. (if prompt
  457. nil
  458. (error "Buffer does not have a filename")))
  459. ((and prompt
  460. (not (y-or-n-p (format "Save %s to %s "
  461. (if type-id
  462. "type identifier"
  463. "schema location")
  464. file)))))
  465. (t
  466. (with-current-buffer (find-file-noselect file)
  467. (let ((modified (buffer-modified-p)))
  468. (if (> (buffer-size) 0)
  469. (let (xmltok-dtd)
  470. (goto-char (point-min))
  471. (xmltok-save
  472. (xmltok-forward-prolog)
  473. (xmltok-forward)
  474. (unless (eq xmltok-type 'start-tag)
  475. (error "Locating file `%s' invalid" file))))
  476. (insert "<?xml version=\"1.0\"?>\n"
  477. "<locatingRules xmlns=\""
  478. (nxml-namespace-name rng-locate-namespace-uri)
  479. "\">")
  480. (let ((pos (point)))
  481. (insert "\n</locatingRules>\n")
  482. (goto-char pos)))
  483. (insert "\n")
  484. (insert (let ((locating-file-uri (rng-file-name-uri file)))
  485. (format "<uri resource=\"%s\" %s=\"%s\"/>"
  486. (rng-escape-string
  487. (rng-relative-uri
  488. (rng-file-name-uri document-file-name)
  489. locating-file-uri))
  490. (if type-id "typeId" "uri")
  491. (rng-escape-string
  492. (or type-id
  493. (rng-relative-uri
  494. (rng-file-name-uri schema-file-name)
  495. locating-file-uri))))))
  496. (indent-according-to-mode)
  497. (when (or (not modified)
  498. (y-or-n-p (format "Save file %s "
  499. (buffer-file-name))))
  500. (save-buffer))))))))
  501. (provide 'rng-loc)
  502. ;;; rng-loc.el ends here