From e6445ebe2b7f5ee3e96f9a5a36cfce81500939b5 Mon Sep 17 00:00:00 2001 From: Justin Kramer Date: Fri, 10 Feb 2012 10:07:27 -0500 Subject: [PATCH] keyword options for source-seq, parse, and parse-str --- src/main/clojure/clojure/data/xml.clj | 31 +++++++++++++++++----- src/test/clojure/clojure/data/xml/test_parse.clj | 8 +++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/clojure/clojure/data/xml.clj b/src/main/clojure/clojure/data/xml.clj index eba575d..d5209be 100644 --- a/src/main/clojure/clojure/data/xml.clj +++ b/src/main/clojure/clojure/data/xml.clj @@ -219,12 +219,29 @@ (recur);; Consume and ignore comments, spaces, processing instructions etc )))) +(def ^{:private true} xml-input-factory-props + {:allocator javax.xml.stream.XMLInputFactory/ALLOCATOR + :coalescing javax.xml.stream.XMLInputFactory/IS_COALESCING + :namespace-aware javax.xml.stream.XMLInputFactory/IS_NAMESPACE_AWARE + :replacing-entity-references javax.xml.stream.XMLInputFactory/IS_REPLACING_ENTITY_REFERENCES + :supporting-external-entities javax.xml.stream.XMLInputFactory/IS_SUPPORTING_EXTERNAL_ENTITIES + :validating javax.xml.stream.XMLInputFactory/IS_VALIDATING + :reporter javax.xml.stream.XMLInputFactory/REPORTER + :resolver javax.xml.stream.XMLInputFactory/RESOLVER + :support-dtd javax.xml.stream.XMLInputFactory/SUPPORT_DTD}) + +(defn- new-xml-input-factory [props] + (let [fac (javax.xml.stream.XMLInputFactory/newInstance)] + (doseq [[k v] props + :let [prop (xml-input-factory-props k)]] + (.setProperty fac prop v)) + fac)) + (defn source-seq "Parses the XML InputSource source using a pull-parser. Returns a lazy sequence of Event records." - [s] - (let [fac (doto (javax.xml.stream.XMLInputFactory/newInstance) - (.setProperty javax.xml.stream.XMLInputFactory/IS_COALESCING true)) + [s & {:as props}] + (let [fac (new-xml-input-factory (merge {:coalescing true} props)) sreader (.createXMLStreamReader fac s)] (pull-seq sreader))) @@ -232,14 +249,14 @@ "Convenience function. Parses the source, which can be an InputStream or Reader, and returns a lazy tree of Element records. See lazy-source-seq for finer-grained control." - [source] - (event-tree (source-seq source))) + [source & props] + (event-tree (apply source-seq source props))) (defn parse-str "Parses the passed in string to Clojure data structures" - [s] + [s & props] (let [sr (java.io.StringReader. s)] - (parse sr))) + (apply parse sr props))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; XML Emitting diff --git a/src/test/clojure/clojure/data/xml/test_parse.clj b/src/test/clojure/clojure/data/xml/test_parse.clj index 74d41f2..9a6fca3 100644 --- a/src/test/clojure/clojure/data/xml/test_parse.clj +++ b/src/test/clojure/clojure/data/xml/test_parse.clj @@ -72,4 +72,10 @@

Heading Stuff

" expected (element :html {} (element :h1 {} "Heading Stuff"))] - (is (= expected (parse-str input))))) \ No newline at end of file + (is (= expected (parse-str input))))) + +(deftest test-coalescing + (let [input ""] + (is (= ["\nfoo bar\n\nbaz\n"] (:content (parse-str input)))) + (is (= ["\nfoo bar\n" "\nbaz\n"] (:content + (parse-str input :coalescing false)))))) \ No newline at end of file -- 1.7.5.4