Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for Strings as tag and attribute names (DXML-8)

  • Loading branch information...
commit 620b21f023d09d2150253be8a17977d2a406525c 1 parent bc03865
@senior senior authored
View
18 src/main/clojure/clojure/data/xml.clj
@@ -24,11 +24,20 @@
(defn event [type name & [attrs str]]
(Event. type name attrs str))
+(defn qualified-name [event-name]
+ (if (instance? clojure.lang.Named event-name)
+ [(namespace event-name) (name event-name)]
+ (let [name-parts (str/split event-name #"/" 2)]
+ (if (= 2 (count name-parts))
+ name-parts
+ [nil (first name-parts)]))))
+
(defn write-attributes [attrs ^javax.xml.stream.XMLStreamWriter writer]
(doseq [[k v] attrs]
- (if (namespace k)
- (.writeAttribute writer (str (namespace k)) (name k) (str v))
- (.writeAttribute writer (name k) (str v)))))
+ (let [[attr-ns attr-name] (qualified-name k)]
+ (if attr-ns
+ (.writeAttribute writer attr-ns attr-name (str v))
+ (.writeAttribute writer attr-name (str v))))))
; Represents a node of an XML tree
(defrecord Element [tag attrs content])
@@ -36,8 +45,7 @@
(defrecord Comment [content])
(defn emit-start-tag [event ^javax.xml.stream.XMLStreamWriter writer]
- (let [nspace (namespace (:name event))
- qname (name (:name event))]
+ (let [[nspace qname] (qualified-name (:name event))]
(.writeStartElement writer "" qname (or nspace ""))
(write-attributes (:attrs event) writer)))
View
6 src/test/clojure/clojure/data/xml/test_emit.clj
@@ -35,6 +35,12 @@
"</a>")]
(is (= expect (emit-str deep-tree)))))
+(deftest defaults
+ ;;XML below should be updated when namespace support is in
+ (let [expect (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bar item=\"1\"><baz item=\"2\">done</baz></bar>")]
+ (is (= expect (emit-str (element "foo/bar" {"foo/item" 1} [(element "foo/baz" {"foo/item" 2} "done")]))))))
+
+
(deftest mixed-quotes
(is (= (lazy-parse*
(str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
Please sign in to comment.
Something went wrong with that request. Please try again.