Skip to content

Commit

Permalink
Removed the sexp-as-fragment code, it's no longer used
Browse files Browse the repository at this point in the history
  • Loading branch information
senior committed Jun 26, 2012
1 parent bc00307 commit 680dc2b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 88 deletions.
63 changes: 2 additions & 61 deletions src/main/clojure/clojure/data/xml.clj
Expand Up @@ -65,7 +65,8 @@
(cons (:content element) (cons (:content element)
(cons (Event. :end-element (:tag element) nil nil) next-items))) (cons (Event. :end-element (:tag element) nil nil) next-items)))
Event Event
(gen-event [event] event) (gen-event [event]
event)
(next-events [_ next-items] (next-events [_ next-items]
next-items) next-items)


Expand Down Expand Up @@ -167,66 +168,6 @@
(fn [^Event event] (.str event)) (fn [^Event event] (.str event))
events))) 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] (defn- attr-prefix [^XMLStreamReader sreader index]
(let [p (.getAttributePrefix sreader index)] (let [p (.getAttributePrefix sreader index)]
(when-not (str/blank? p) (when-not (str/blank? p)
Expand Down
115 changes: 115 additions & 0 deletions src/test/clojure/clojure/data/xml/test_emit.clj
Expand Up @@ -98,3 +98,118 @@
expect (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\n " 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")] "<b>\n <c>\n <d>foo</d>\n </c>\n </b>\n</a>\n")]
(is (= expect (indent-str nested-xml))))) (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))))
27 changes: 0 additions & 27 deletions src/test/clojure/clojure/data/xml/test_sexp.clj

This file was deleted.

8 comments on commit 680dc2b

@amalloy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@senior
Copy link
Member Author

@senior senior commented on 680dc2b Jun 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@senior senior commented on 680dc2b Jun 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@abedra abedra commented on 680dc2b Jun 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@amalloy
Copy link
Contributor

@amalloy amalloy commented on 680dc2b Jun 29, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abedra
Copy link
Member

@abedra abedra commented on 680dc2b Jun 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@senior
Copy link
Member Author

@senior senior commented on 680dc2b Jun 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.