Skip to content
Browse files

keyword options for source-seq, parse, and parse-str

Signed-off-by: Ryan Senior <senior.ryan@gmail.com>
  • Loading branch information...
1 parent 2f6e6f0 commit 1cf6cf853255e7a6cc65f8d6522b38c8f3e6ed8b @jkk jkk committed with senior Feb 10, 2012
Showing with 31 additions and 8 deletions.
  1. +24 −7 src/main/clojure/clojure/data/xml.clj
  2. +7 −1 src/test/clojure/clojure/data/xml/test_parse.clj
View
31 src/main/clojure/clojure/data/xml.clj
@@ -219,27 +219,44 @@
(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)))
(defn parse
"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
View
8 src/test/clojure/clojure/data/xml/test_parse.clj
@@ -72,4 +72,10 @@
<html><h1>Heading Stuff</h1></html>"
expected (element :html {}
(element :h1 {} "Heading Stuff"))]
- (is (= expected (parse-str input)))))
+ (is (= expected (parse-str input)))))
+
+(deftest test-coalescing
+ (let [input "<a><![CDATA[\nfoo bar\n]]><![CDATA[\nbaz\n]]></a>"]
+ (is (= ["\nfoo bar\n\nbaz\n"] (:content (parse-str input))))
+ (is (= ["\nfoo bar\n" "\nbaz\n"] (:content
+ (parse-str input :coalescing false))))))

0 comments on commit 1cf6cf8

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