Skip to content

Commit e8eee2d

Browse files
committed
Clean remnants of 0.1.0 qname handling
- qname function now returns canonical (keyword) names - Remove QName defrecord from Clojurescript - Rename canonical-name to as-qname - Remove to-qname
1 parent c30911e commit e8eee2d

12 files changed

Lines changed: 86 additions & 78 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
From 0.2.0-alpha1 to 0.2.0-alpha2
2+
- qname function now returns canonical (keyword) names
3+
- Remove QName defrecord from Clojurescript
4+
- Rename canonical-name to as-qname
5+
- Remove to-qname
6+
17
From 0.1.0-beta3 to 0.2.0-alpha1
28
- Define uniform mapping of xml namespaces to clojure namespaces via percent-encoding
39
- Remove declare-ns and alias-ns

src/main/clojure/clojure/data/xml.clj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@
3434
(export-api node/element* node/element node/cdata node/xml-comment
3535
prxml/sexp-as-element prxml/sexps-as-fragment event/element-nss
3636
name/alias-uri name/parse-qname name/qname-uri
37-
name/qname-local name/qname name/to-qname name/uri-symbol name/symbol-uri
37+
name/qname-local name/qname name/as-qname name/uri-symbol name/symbol-uri
38+
name/uri-file name/print-uri-file-command!
3839
process/find-xmlns process/aggregate-xmlns)
3940

40-
(defn canonical-name
41-
"Put (q)name into canonical form as per ns-env"
42-
[n]
43-
(name/canonical-name (qname-uri n) (qname-local n) ""))
44-
4541
(defn event-seq
4642
"Parses the XML InputSource source using a pull-parser. Returns
4743
a lazy sequence of Event records. Accepts key pairs

src/main/clojure/clojure/data/xml.cljs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
[clojure.data.xml.protocols :refer [AsQName]]))
99

1010
(export-api
11-
name/parse-qname name/qname-uri name/qname-local name/qname name/to-qname name/uri-symbol name/symbol-uri
11+
name/parse-qname name/qname-uri name/qname-local name/qname name/as-qname name/uri-symbol name/symbol-uri
1212
node/element* node/element node/cdata node/xml-comment
1313
dom/extend-dom-as-data! dom/element-node dom/element-data)
1414

15-
(defn canonical-name
16-
"Put (q)name into canonical form as per ns-env"
17-
[n]
18-
(name/canonical-name (qname-uri n) (qname-local n) ""))
19-
2015
;;;; ## TODO event-seq
2116
;; This probably won't happen due to js' non-blocking semantics
2217
;; Instead, for clojurescript, the machinery around event-seq could be implemented

src/main/clojure/clojure/data/xml/js/dom.cljs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns clojure.data.xml.js.dom
22
(:require
3-
[clojure.data.xml.name :refer [qname-uri qname-local canonical-name xmlns-uri]]
3+
[clojure.data.xml.name :refer [qname-uri qname-local qname xmlns-uri]]
44
[clojure.data.xml.node :as node]))
55

66
(def doc
@@ -103,9 +103,8 @@
103103
;; ## -> DATA
104104

105105
(defn- dom-element-tag [el]
106-
(canonical-name (.-namespaceURI el)
107-
(.-localName el)
108-
""))
106+
(qname (.-namespaceURI el)
107+
(.-localName el)))
109108

110109
(defn- xmlns-attr? [a]
111110
(identical? xmlns-uri (.-namespaceURI a)))

src/main/clojure/clojure/data/xml/js/name.cljs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,20 @@
22
(:require [clojure.data.xml.protocols :refer [AsQName qname-uri qname-local]]
33
[clojure.string :as str]))
44

5-
(defrecord QName [uri local prefix]
6-
AsQName
7-
(qname-local [_] local)
8-
(qname-uri [_] uri))
9-
10-
(specify! (.-prototype QName)
11-
IEquiv
12-
(-equiv [this other]
13-
(and (instance? QName other)
14-
(= (.-local this) (.-local other))
15-
(= (.-uri this) (.-uri other)))))
16-
17-
(defn make-qname [uri name prefix]
18-
(->QName uri name prefix))
19-
205
(def parse-qname
21-
(memoize
22-
(fn [s]
23-
(let [[_ ns-uri local] (re-matches #"(?:\{([^}]+)\})?([^{]*)" s)]
24-
(make-qname (or ns-uri "") local "")))))
6+
(memoize (partial re-matches #"(?:\{([^}]+)\})?([^{]*)")))
257

268
(defn decode-uri [ns]
279
(js/decodeURIComponent ns))
2810

2911
(defn encode-uri [uri]
3012
(js/encodeURIComponent uri))
13+
14+
(extend-protocol AsQName
15+
string
16+
(qname-local [s]
17+
(let [[_ _ local] (parse-qname s)]
18+
local))
19+
(qname-uri [s]
20+
(let [[_ uri _] (parse-qname s)]
21+
uri)))

src/main/clojure/clojure/data/xml/jvm/name.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
(qname-local [qname] (.getLocalPart qname))
2020
(qname-uri [qname] (.getNamespaceURI qname)))
2121

22-
(def parse-qname
22+
(def ^QName parse-qname
2323
(memoize
2424
(fn [s]
2525
;; TODO weakly memoize this?
2626
(QName/valueOf s))))
2727

28-
(definline make-qname [uri name prefix]
29-
`(QName. ~uri ~name ~prefix))
28+
(extend-protocol AsQName
29+
String
30+
(qname-local [s]
31+
(.getLocalPart (parse-qname s)))
32+
(qname-uri [s]
33+
(.getNamespaceURI (parse-qname s))))
3034

3135
(definline decode-uri [ns]
3236
`(URLDecoder/decode ~ns "UTF-8"))

src/main/clojure/clojure/data/xml/jvm/parse.clj

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[clojure.data.xml.impl :refer
1515
[static-case]]
1616
[clojure.data.xml.name :refer
17-
[canonical-name]])
17+
[qname]])
1818
(:import
1919
(javax.xml.stream
2020
XMLInputFactory XMLStreamReader XMLStreamConstants)))
@@ -38,9 +38,9 @@
3838
(defn- attr-hash [^XMLStreamReader sreader]
3939
(persistent!
4040
(reduce (fn [tr i]
41-
(assoc! tr (canonical-name (.getAttributeNamespace sreader i)
42-
(.getAttributeLocalName sreader i)
43-
(.getAttributePrefix sreader i))
41+
(assoc! tr (qname (.getAttributeNamespace sreader i)
42+
(.getAttributeLocalName sreader i)
43+
(.getAttributePrefix sreader i))
4444
(.getAttributeValue sreader i)))
4545
(transient {})
4646
(range (.getAttributeCount sreader)))))
@@ -74,13 +74,12 @@
7474
(location-hash sreader))]
7575
(static-case
7676
(.next sreader)
77-
; condp == (.next sreader)
7877
XMLStreamConstants/START_ELEMENT
7978
(if (include-node? :element)
8079
(let [ns-env (nss-hash sreader (or (first ns-envs) {}))]
81-
(cons (->StartElementEvent (canonical-name (.getNamespaceURI sreader)
82-
(.getLocalName sreader)
83-
(.getPrefix sreader))
80+
(cons (->StartElementEvent (qname (.getNamespaceURI sreader)
81+
(.getLocalName sreader)
82+
(.getPrefix sreader))
8483
(attr-hash sreader)
8584
ns-env
8685
location)

src/main/clojure/clojure/data/xml/name.cljc

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
(:import (goog.string StringBuffer))]))
2222

2323
(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]))
2626

2727
;; protocol functions can be redefined by extend-*, so we wrap
2828
;; protocols/qname-uri protocols/qname-local within regular fns
@@ -47,9 +47,11 @@
4747
(protocols/qname-local v))
4848

4949
(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)))
5355

5456
;; The empty string shall be equal to nil for xml names
5557
(defn namespaced? [qn]
@@ -76,28 +78,44 @@
7678
xmlns-uri
7779
(throw (ex-info "Keyword ns is not an xmlns. Needs to be in the form :xmlns.<encoded-uri>/<local>"
7880
{: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)))
9497

9598
#?(:clj
9699
(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+
98106
## Example
99107
(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."
101119
{:arglists '([& {:as alias-nss}])}
102120
[& ans]
103121
(loop [[a n & rst :as ans] ans]

src/main/clojure/clojure/data/xml/node.cljc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
(ns clojure.data.xml.node
1010
"Data types for xml nodes: Element, CData and Comment"
1111
{:author "Herwig Hochleitner"}
12-
(:require [clojure.data.xml.name :refer [to-qname]]))
12+
(:require [clojure.data.xml.name :refer [as-qname]]))
1313

1414
;; Parsed data format
1515
;; Represents a node of an XML tree
1616
(defrecord Element [tag attrs content]
1717
Object
1818
(toString [_]
19-
(let [qname (to-qname tag)]
19+
(let [qname (as-qname tag)]
2020
(apply str (concat ["<" qname]
2121
(mapcat (fn [[n a]]
22-
[" " (to-qname n) "=" (pr-str a)])
22+
[" " (as-qname n) "=" (pr-str a)])
2323
attrs)
2424
(if (seq content)
2525
(concat [">"] content ["</" qname ">"])

src/main/resources/clojure/data/xml/spec.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
::s/invalid)))
1919
(fn [{:keys [uri local] :as arg}]
2020
(.log js/console arg)
21-
(name/canonical-name uri local "")))
21+
(name/qname uri local)))
2222
#(not (str/blank? (:local %)))))
2323

2424
(s/def ::name/qname
@@ -30,7 +30,7 @@
3030
:str (s/and string? ::qname-conformer)))
3131
(s/with-gen
3232
#(gen/fmap (fn [[uri local]]
33-
(name/canonical-name uri local))
33+
(name/qname uri local))
3434
(gen/tuple (gen/fmap (fn [s] (when-not (str/blank? s)
3535
(str "urn:" s)))
3636
(gen/string-alphanumeric))

0 commit comments

Comments
 (0)