|
21 | 21 | (:import (goog.string StringBuffer))])) |
22 | 22 |
|
23 | 23 | (export-api |
24 | | - #?@(:clj [jvm/parse-qname jvm/make-qname jvm/encode-uri jvm/decode-uri] |
25 | | - :cljs [jsn/parse-qname jsn/make-qname jsn/encode-uri jsn/decode-uri])) |
| 24 | + #?@(:clj [jvm/parse-qname jvm/encode-uri jvm/decode-uri] |
| 25 | + :cljs [jsn/parse-qname jsn/encode-uri jsn/decode-uri])) |
26 | 26 |
|
27 | 27 | ;; protocol functions can be redefined by extend-*, so we wrap |
28 | 28 | ;; protocols/qname-uri protocols/qname-local within regular fns |
|
47 | 47 | (protocols/qname-local v)) |
48 | 48 |
|
49 | 49 | (defn qname |
50 | | - ([name] (make-qname "" name "")) |
51 | | - ([uri name] (make-qname (or uri "") name "")) |
52 | | - ([uri name prefix] (make-qname (or uri "") name (or prefix "")))) |
| 50 | + ([local] (qname "" local)) |
| 51 | + ([uri local] (keyword (when-not (str/blank? uri) |
| 52 | + (encode-uri (str "xmlns." uri))) |
| 53 | + local)) |
| 54 | + ([uri local prefix] (qname uri local))) |
53 | 55 |
|
54 | 56 | ;; The empty string shall be equal to nil for xml names |
55 | 57 | (defn namespaced? [qn] |
|
76 | 78 | xmlns-uri |
77 | 79 | (throw (ex-info "Keyword ns is not an xmlns. Needs to be in the form :xmlns.<encoded-uri>/<local>" |
78 | 80 | {:kw kw})))) |
79 | | - "")) |
80 | | - #?(:clj String :cljs string) |
81 | | - (qname-local [s] (qname-local (parse-qname s))) |
82 | | - (qname-uri [s] (qname-uri (parse-qname s)))) |
83 | | - |
84 | | -(defn canonical-name |
85 | | - ([local] (canonical-name "" local "")) |
86 | | - ([uri local] (canonical-name uri local "")) |
87 | | - ([uri local prefix] |
88 | | - (keyword (when-not (str/blank? uri) |
89 | | - (encode-uri (str "xmlns." uri))) |
90 | | - local))) |
91 | | - |
92 | | -(defn to-qname [n] |
93 | | - (make-qname (or (qname-uri n) "") (qname-local n) "")) |
| 81 | + ""))) |
| 82 | + |
| 83 | +(defn as-qname [n] |
| 84 | + (qname (qname-uri n) (qname-local n))) |
| 85 | + |
| 86 | +(defn uri-file |
| 87 | + "Dummy file name for :require'ing xmlns uri" |
| 88 | + [uri] |
| 89 | + (str (str/replace (name (uri-symbol uri)) |
| 90 | + "." "/") |
| 91 | + ".cljc")) |
| 92 | + |
| 93 | +(defn print-uri-file-command! |
| 94 | + "Shell command to create a dummy file for xmlns. Execute from a source root." |
| 95 | + [uri] |
| 96 | + (println "echo \"(ns" (str (uri-symbol uri) ")\" >") (uri-file uri))) |
94 | 97 |
|
95 | 98 | #?(:clj |
96 | 99 | (defn alias-uri |
97 | | - "Define a clojure namespace alias for xmlns uri. |
| 100 | + "Define a Clojure namespace aliases for xmlns uris. |
| 101 | +
|
| 102 | + This sets up the current namespace for reading qnames denoted with |
| 103 | + Clojure's ::alias/keywords reader feature. |
| 104 | + |
| 105 | +
|
98 | 106 | ## Example |
99 | 107 | (alias-uri :D \"DAV:\") |
100 | | - {:tag ::D/propfind}" |
| 108 | + ; similar in effect to |
| 109 | + ;; (require '[xmlns.DAV%3A :as D]) |
| 110 | + ; but required namespace is auto-created |
| 111 | + ; henceforth, shorthand keywords can be used |
| 112 | + {:tag ::D/propfind} |
| 113 | + ; ::D/propfind will be expanded to :xmlns.DAV%3A/propfind |
| 114 | + ; in the current namespace by the reader |
| 115 | +
|
| 116 | + ## Clojurescript support |
| 117 | + Currently, namespaces can't be auto-created in Clojurescript. |
| 118 | + Dummy files for aliased uris have to exist. Have a look at `uri-file` and `print-uri-file-command!` to create those." |
101 | 119 | {:arglists '([& {:as alias-nss}])} |
102 | 120 | [& ans] |
103 | 121 | (loop [[a n & rst :as ans] ans] |
|
0 commit comments