Function: rng-uri-file-name-1

rng-uri-file-name-1 is a byte-compiled function defined in rng-uri.el.gz.

Signature

(rng-uri-file-name-1 URI PATTERN)

Source Code

;; Defined in /usr/src/emacs/lisp/nxml/rng-uri.el.gz
;; pattern is either nil or match or replace
(defun rng-uri-file-name-1 (uri pattern)
  (unless (string-match "\\`\\(?:[^%]\\|%[[:xdigit:]]\\{2\\}\\)*\\'" uri)
    (rng-uri-error "Bad escapes in URI `%s'" uri))
  (setq uri (rng-uri-unescape-multibyte uri))
  (let* ((components
	  (or (rng-uri-split uri)
	      (rng-uri-error "Cannot split URI `%s' into its components" uri)))
	 (scheme (nth 0 components))
	 (authority (nth 1 components))
	 (path (nth 2 components))
	 (absolutep (string-match "\\`/" path))
	 (query (nth 3 components))
	 (fragment-id (nth 4 components)))
    (cond ((not scheme)
	   (unless pattern
	     (rng-uri-error "URI `%s' does not have a scheme" uri)))
	  ((not (string= (downcase scheme) "file"))
	   (rng-uri-error "URI `%s' does not use the `file:' scheme" uri)))
    (when (not (member authority
		       (cons (system-name) '(nil "" "localhost"))))
      (rng-uri-error "URI `%s' does not start with `file:///' or `file://localhost/'"
	     uri))
    (when query
      (rng-uri-error "`?' not escaped in file URI `%s'" uri))
    (when fragment-id
      (rng-uri-error "URI `%s' has a fragment identifier" uri))
    (when (string-search ";" path)
      (rng-uri-error "`;' not escaped in URI `%s'" uri))
    (when (string-match "%2[fF]" path) ;; 2f is hex code of slash
      (rng-uri-error "Escaped slash in URI `%s'" uri))
    (when (and (eq system-type 'windows-nt)
	       absolutep
	       (file-name-absolute-p (substring path 1)))
      (setq path (substring path 1)))
    (when (and pattern (string-match "\\`\\./" path))
      (setq path (substring path 2)))
    (setq path
	  (cond ((eq pattern 'match)
		 (rng-uri-unescape-unibyte-match path))
		((eq pattern 'replace)
		 (rng-uri-unescape-unibyte-replace path 2))
		(t
		 (rng-uri-unescape-unibyte path))))
    (when (string-search "\000" path)
      (rng-uri-error "URI `%s' has NUL character in path" uri))
    (when (eq pattern 'match)
      (setq path
	    (concat (if absolutep
			"\\(\\)"
		      "\\(\\(?:[^/]*/\\)*\\)")
		    path)))
    (cond ((eq pattern 'match)
	   (concat "\\`" path "\\'"))
	  ((and (eq pattern 'replace)
		(not absolutep))
	   (concat "\\1" path))
	  (t path))))