Permalink
Browse files

Renamed emit-stream to emit, added new top level emit-str function

  • Loading branch information...
1 parent 08c493f commit 8c487a0661fa241187a865833b66e791dab420a2 @senior senior committed Jan 30, 2012
Showing with 23 additions and 27 deletions.
  1. +11 −12 src/main/clojure/clojure/data/xml.clj
  2. +12 −15 src/test/clojure/clojure/data/xml/test_emit.clj
@@ -248,10 +248,9 @@
") doesn't match declaration ("
(.getEncoding stream) ")")))))
-(defn emit-stream
+(defn emit
"Prints the given Element tree as XML text to stream.
Options:
- :indent <num> Amount to increase indent depth each time
:encoding <str> Character encoding to use"
[e ^java.io.Writer stream & {:as opts}]
(let [^javax.xml.stream.XMLStreamWriter writer (-> (javax.xml.stream.XMLOutputFactory/newInstance)
@@ -265,13 +264,12 @@
(.writeEndDocument writer)
stream))
-(defn emit
- "Prints the given Element tree as XML text to *out*.
- Options:
- :indent <num> Amount to increase indent depth each time
- :encoding <str> Character encoding to use"
- [e & {:as opts}]
- (apply emit-stream e *out* opts))
+(defn emit-str
+ "Emits the Element to String and returns it"
+ [e]
+ (let [^java.io.StringWriter sw (java.io.StringWriter.)]
+ (emit e sw)
+ (.toString sw)))
(defn indenting-transformer []
(doto (-> (javax.xml.transform.TransformerFactory/newInstance) .newTransformer)
@@ -280,12 +278,13 @@
(.setOutputProperty "{http://xml.apache.org/xslt}indent-amount" "2")))
(defn indent
- "Uses emit-stream to emit XML and indents the result. WARNING: this is slow
+ "Emits the XML and indents the result. WARNING: this is slow
it will emit the XML and read it in again to indent it. Intended for
debugging/testing only."
[e ^java.io.Writer stream & {:as opts}]
(let [sw (java.io.StringWriter.)
- _ (apply emit-stream e sw opts)
+ _ (apply emit e sw opts)
source (-> sw .toString java.io.StringReader. javax.xml.transform.stream.StreamSource.)
result (javax.xml.transform.stream.StreamResult. stream)]
- (.transform (indenting-transformer) source result)))
+ (.transform (indenting-transformer) source result)))
+
@@ -33,21 +33,20 @@
" t8<f>t10</f>t11</e>"
" t12<g>t13</g>t14"
"</a>")]
- (is (= expect (with-out-str (xml/emit deep-tree))))))
+ (is (= expect (xml/emit-str deep-tree)))))
(deftest mixed-quotes
(is (= (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<mixed double=\"&quot;double&quot;quotes&quot;here&quot;\""
" single=\"'single'quotes'here\"></mixed>")
- (with-out-str
- (xml/emit (element :mixed
- {:single "'single'quotes'here"
- :double "\"double\"quotes\"here\""}))))))
+ (xml/emit-str (element :mixed
+ {:single "'single'quotes'here"
+ :double "\"double\"quotes\"here\""})))))
(defn emit-char-seq [xml-tree encoding]
(with-open [bos (java.io.ByteArrayOutputStream.)
stream (java.io.OutputStreamWriter. bos encoding)]
- (xml/emit-stream xml-tree stream :encoding encoding)
+ (xml/emit xml-tree stream :encoding encoding)
(.flush stream)
(map #(if (pos? %) (char %) %) (.toByteArray bos))))
@@ -65,23 +64,21 @@
(is (thrown? Exception
(let [stream (java.io.ByteArrayOutputStream.)]
(binding [*out* (java.io.OutputStreamWriter. stream "UTF-8")]
- (xml/emit (element :foo) :encoding "ISO-8859-1"))))))
+ (xml/emit (element :foo) *out* :encoding "ISO-8859-1"))))))
(deftest emitting-cdata
(is (= (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<cdata-stuff><![CDATA[<goes><here>]]></cdata-stuff>")
- (with-out-str
- (xml/emit (element :cdata-stuff {}
- (cdata "<goes><here>")))))) )
+ (xml/emit-str (element :cdata-stuff {}
+ (cdata "<goes><here>"))))) )
(deftest emitting-comment
(is (= (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<comment-stuff>comment <!-- goes here --> not here</comment-stuff>")
- (with-out-str
- (xml/emit (element :comment-stuff {}
- "comment "
- (xml-comment " goes here ")
- " not here"))))) )
+ (xml/emit-str (element :comment-stuff {}
+ "comment "
+ (xml-comment " goes here ")
+ " not here")))) )
(deftest test-indent
(let [nested-xml (lazy-parse* (str "<a><b><c><d>foo</d></c></b></a>"))

0 comments on commit 8c487a0

Please sign in to comment.