Skip to content
Browse files

Revert "Removed the sexp-as-fragment code, it's no longer used"

This reverts commit 680dc2b.
  • Loading branch information...
1 parent e407e7f commit 205a9df7ea77679b4d4a253a68775859fb13a1a1 @senior senior committed Jun 28, 2012
View
63 src/main/clojure/clojure/data/xml.clj
@@ -65,8 +65,7 @@
(cons (:content element)
(cons (Event. :end-element (:tag element) nil nil) next-items)))
Event
- (gen-event [event]
- event)
+ (gen-event [event] event)
(next-events [_ next-items]
next-items)
@@ -168,6 +167,66 @@
(fn [^Event event] (.str event))
events)))
+(defprotocol AsElements
+ (as-elements [expr] "Return a seq of elements represented by an expression."))
+
+(extend-protocol AsElements
+ clojure.lang.IPersistentVector
+ (as-elements [v]
+ (let [[tag & [attrs & after-attrs :as content]] v
+ [attrs content] (if (map? attrs)
+ [(into {} (for [[k v] attrs]
+ [k (str v)]))
+ after-attrs]
+ [{} content])]
+ [(Element. tag attrs (mapcat as-elements content))]))
+
+ clojure.lang.ISeq
+ (as-elements [s]
+ (mapcat as-elements s))
+
+ clojure.lang.Keyword
+ (as-elements [k]
+ [(Element. k {} ())])
+
+ java.lang.String
+ (as-elements [s]
+ [s])
+
+ nil
+ (as-elements [_] nil)
+
+ java.lang.Object
+ (as-elements [o]
+ [(str o)]))
+
+(defn sexps-as-fragment
+ "Convert a compact prxml/hiccup-style data structure into the more formal
+ tag/attrs/content format. A seq of elements will be returned, which may
+ not be suitable for immediate use as there is no root element. See also
+ sexp-as-element.
+
+ The format is [:tag-name attr-map? content*]. Each vector opens a new tag;
+ seqs do not open new tags, and are just used for inserting groups of elements
+ into the parent tag. A bare keyword not in a vector creates an empty element.
+
+ To provide XML conversion for your own data types, extend the AsElements
+ protocol to them."
+ ([] nil)
+ ([sexp] (as-elements sexp))
+ ([sexp & sexps] (mapcat as-elements (cons sexp sexps))))
+
+(defn sexp-as-element
+ "Convert a single sexp into an Element"
+ [sexp]
+ (let [[root & more] (sexps-as-fragment sexp)]
+ (when more
+ (throw
+ (IllegalArgumentException.
+ "Cannot have multiple root elements; try creating a fragment instead")))
+ root))
+
+
(defn- attr-prefix [^XMLStreamReader sreader index]
(let [p (.getAttributePrefix sreader index)]
(when-not (str/blank? p)
View
115 src/test/clojure/clojure/data/xml/test_emit.clj
@@ -98,118 +98,3 @@
expect (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\n "
"<b>\n <c>\n <d>foo</d>\n </c>\n </b>\n</a>\n")]
(is (= expect (indent-str nested-xml)))))
-
-(comment
-
-
-
-
-(import '[clojure.data.xml Element])
-
- ;;
- ;; Doesn't work
- ;;
- (defn just-elements2 [total]
- (Element. :sparql {:xmlns "http://www.w3.org/2005/sparql-results#"}
- (list
- (Element. :foo {}
- (for [x (range 1 total)]
- (Element. :result {}
- (list
- (Element. :binding {:name "a"} (list (Element. :literal {} (list (str x)))))
- (Element. :binding {:name "b"} (list (Element. :literal {} (list (str "b" x)))))
- (Element. :binding {:name "c"} (list (Element. :literal {} (list (str "c" x))))))))))))
-
-
-
-
-
-
-
-
- ;;
- ;; Works
- ;;
- (defn just-elements [total]
- (Element. :sparql {:xmlns "http://www.w3.org/2005/sparql-results#"}
- (for [x (range 1 total)]
- (Element. :result {}
- (list
- (Element. :binding {:name "a"} (list (Element. :literal {} (list (str x)))))
- (Element. :binding {:name "b"} (list (Element. :literal {} (list (str "b" x)))))
- (Element. :binding {:name "c"} (list (Element. :literal {} (list (str "c" x))))))))))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- (defn just-elements4 [total]
- (Element. :sparql {:xmlns "http://www.w3.org/2005/sparql-results#"}
- (list
- (Element. :foo {}
- (for [x (range 1 total)]
- (Element. :result {}
- (list
- (Element. :binding {:name "a"} (list ))
- (Element. :binding {:name "b"} (list ))
- (Element. :binding {:name "c"} (list ))))))))
-
- )
-
- (defn just-elements-nate [total]
- (Element. :sparql {:xmlns "http://www.w3.org/2005/sparql-results#"}
- (lazy-seq
- (list
- (Element. :foo {}
- (for [x (range 1 total)]
- (Element. :result {}
- (list
- (Element. :binding {:name "a"} (list (Element. :literal {} (list (str x)))))
- (Element. :binding {:name "b"} (list (Element. :literal {} (list (str "b" x)))))
- (Element. :binding {:name "c"} (list (Element. :literal {} (list (str "c" x)))))))))))))
-
- (defn just-elements3 [total]
- [:sparql {:xmlns "http://www.w3.org/2005/sparql-results#"}
- [[:foo {}
- (for [x (range 1 total)]
- [:result {}
- [[:binding {:name "a"} [[:literal {} (str x)]]]
- [:binding {:name "b"} [[:literal {} (str "b" x)]]]
- [:binding {:name "c"} [[:literal {} (str "c" x)]]]]])]]])
-
-
- (defn output-big-xml [path doc]
- (with-open [oo (java.io.FileWriter. path)]
- (emit doc oo)))
-
- (defn output-big-xml-event [path doc]
- (with-open [oo (java.io.FileWriter. path)]
- (emit-events doc oo)))
-
- #_(defn output-big-xml2 [path doc]
- (with-open [oo (java.io.FileWriter. path)]
- (emit2 doc oo))))
View
27 src/test/clojure/clojure/data/xml/test_sexp.clj
@@ -0,0 +1,27 @@
+; Copyright (c) Rich Hickey. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns ^{:doc "Tests for reading [:tag {:attr 'value} body*] as XML."
+ :author "Alan Malloy"}
+ clojure.data.xml.test-sexp
+ (:use clojure.test
+ clojure.data.xml
+ [clojure.data.xml.test-utils :only (test-stream lazy-parse*)]))
+
+(deftest as-element
+ (let [xml-input "<tag attr=\"value\"><body /></tag>"
+ sexp-input [:tag {:attr "value"} :body]]
+ (is (= (lazy-parse* xml-input)
+ (sexp-as-element sexp-input)))))
+
+(deftest as-fragment
+ (let [input (list [:tag1 "stuff"]
+ [:tag2 "other"])]
+ (is (= (sexps-as-fragment input)
+ (map sexp-as-element input)))
+ (is (thrown? Exception (sexp-as-element input)))))

0 comments on commit 205a9df

Please sign in to comment.
Something went wrong with that request. Please try again.