Skip to content
Browse files

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

  • Loading branch information...
1 parent bc00307 commit 680dc2ba7a5dab8d95eae42f6e9fd8df49902855 @senior senior committed Jun 26, 2012
View
63 src/main/clojure/clojure/data/xml.clj
@@ -65,7 +65,8 @@
(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)
@@ -167,66 +168,6 @@
(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,3 +98,118 @@
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
@@ -1,27 +0,0 @@
-; 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)))))

8 comments on commit 680dc2b

@amalloy

Why was this deleted? It's not used internally, but it's a very valuable external feature, which is why it was added.

@senior
Clojure member
senior commented on 680dc2b Jun 29, 2012

I thought it was dead code, sorry about that. I'll get it put back in. Maybe I should add some examples/docs to the readme so users know it's there?

@amalloy

Sure. There was one in the tests, but I suppose that makes it look like an internal feature. It exists to let prxml/hiccup users use the same "interface" (data structures) in data.xml without any hassle. So maybe just an example showing that (emit (sexp-as-element [:tr [:td 'whatever]])) emits valid HTML?

@senior
Clojure member
senior commented on 680dc2b Jun 29, 2012

Just pushed up a commit to roll the change back. A basic example like that in the readme would be fine. I'll add it tomorrow unless you beat me to. Once it's in I'll cut another release.

@abedra
Clojure member
abedra commented on 680dc2b Jun 29, 2012

Do you need a release cut or have you already done it?

@amalloy
@abedra
Clojure member
abedra commented on 680dc2b Jun 29, 2012

Cool. I just want to make sure it gets out if folks need it.

@senior
Clojure member
senior commented on 680dc2b Jun 29, 2012

I've added an example to the README and cut an 0.0.6 release.

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