Comment made by: bendlas
Hi Alex,
thanks for bringing this up! The current, horrible implementation of indent has plagued my mind as well.
Thanks, also, for your implementation of an indenting java.xml.stream.XMLStreamWriter. Before going into detail on how your code could be integrated, or what options would suffice to introduce it in configuration, let me frame some solvable problems, that this ticket touches on:
- Being able to efficiently indent xml, ideally platform-independently
- Being able to swap out the XMLStreamWriter, and possibly other nitty-gritties of parse/emit, by configuration
I'll focus on the "efficiently indenting xml" part, for the purposes of this comment (and ticket, hopefully ;-) )
For a jvm-only solution, we should have a report on the possibility of (e.g.) hooking a StAXSource (link: 1) into the indenting-transformer (link: 2), before rolling our own. On the other hand, if cljs could profit as well, rolling our own streaming transformer makes sense, even if this could be achieved by other means in the jvm backend.
If you squint at your c.d.xml.jvm.indent namespace a little, you might already see the transducer, it contains, popping out at you. The XMLStreamWriter interface seems like incidental complexity in a simple text-node transformer, for the want of a streamable data model. Luckily data.xml, from the very beginning, was built on a streaming event model for xml (link: 3). I have been planning to support tree-transformations, in the form of transducers over the event stream, and indentation would be an awesome first use-case for this.
What do you think? If you're interested in taking this further, here is a commit, that defines an :event-xform config-option for emit* (link: 4).
`
(emit-str (parse-str "bar lala <br/> gag")
:event-xform (fn [xf]
(fn
([s] (xf s))
([s {:as e :keys [str]}]
(-> s
(cond-> str (xf (clojure.data.xml.event/->CharsEvent "^.^")))
(xf e))))))
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>^.^bar lala <br/>^.^ gag"
`
(link: 1) https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stax/StAXSource.html
(link: 2) https://github.com/clojure/data.xml/blob/master/src/main/clojure/clojure/data/xml/jvm/pprint.clj#L15
(link: 3) https://github.com/clojure/data.xml/blob/master/src/main/clojure/clojure/data/xml/event.clj
(link: 4) https://github.com/bendlas/data.xml/commit/0c2baa690154bfa731fa1f98a539542a0205e6b1