Wolodja Wentland <[email protected]> writes:
> On Tue, Jul 10, 2012 at 17:16 +0200, Tassilo Horn wrote:
>> I think, I'd simply copy the clojure.xml code and adapt it to my needs.
>
>> Not too pretty, but should be adequate for getting the job done.
>
> I look into that, but it really wouldn't be pretty. I still think that
> it must be possible to add two lines (one line each at beginning/end)
> to a file that I am reading. I would know how to do that if I weren't
> working with inputstreams (which the parse functions expect me to use)
> but sequences of lines.
Hm, I think, you can use proxy to create a concatenation of input
streams. Something like
--8<---------------cut here---------------start------------->8---
(defn concat-input-stream
"Gets one or many input streams and returns a new input stream that
concatenates the given streams."
^java.io.InputStream [^java.io.InputStream is & more]
(proxy [java.io.InputStream] []
(read []
(let [input (.read is)]
(if (and (== -1 input) (seq more))
(.read (apply concat-input-stream (first more) (rest more)))
input)))))
(def mystream (concat-input-stream (java.io.ByteArrayInputStream. (.getBytes
"ab"))
(java.io.ByteArrayInputStream. (.getBytes
"cd"))
(java.io.ByteArrayInputStream. (.getBytes
"ef"))))
(repeatedly 7 #(.read mystream))
;=> (97 98 99 100 101 102 -1)
--8<---------------cut here---------------end--------------->8---
Not tested extensively, but it should work in theory. Basically, you
would use your usual xml-parse function like
--8<---------------cut here---------------start------------->8---
(xml-parse (concat-input-stream (java.io.ByteArrayInputStream. (.getBytes
"<root>"))
your-real-file-input-stream
(java.io.ByteArrayInputStream. (.getBytes
"</root>"))))
--8<---------------cut here---------------end--------------->8---
to slap the missing root element around the real contents.
HTH,
Tassilo
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en