lib/rtm/ontopia/io/to_cxtm.rb in rtm-ontopia-0.2.0 vs lib/rtm/ontopia/io/to_cxtm.rb in rtm-ontopia-0.2.1

- old
+ new

@@ -8,16 +8,31 @@ # Consequently the (de-) serializer provided here # are only Wrapper module Ontopia module TopicMap - def to_cxtm(filename, base_iri = nil) + def to_cxtm(file=nil) raise("Only supported for Ontopia.") unless self.kind_of?(net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl) - out_file = java.io.FileOutputStream.new(filename) - writer = net.ontopia.topicmaps.xml.CanonicalXTMWriter.new(out_file) + if file + if file.is_a?(java.io.OutputStream) + out_stream = file + else + out_stream = java.io.BufferedOutputStream.new(java.io.FileOutputStream.new(file)) + end + else + # if no file was given we want to return a string which will be written into a stream by the writer + out_stream = java.io.ByteArrayOutputStream.new + end + + writer = net.ontopia.topicmaps.xml.CanonicalXTMWriter.new(out_stream) writer.write(self.wrapped) - out_file.flush - out_file.close + out_stream.flush + + # close the file unless we were provided with a stream + out_stream.close unless file && file.is_a?(java.io.OutputStream) + + # if there was no file, we have a ByteArrayOutputStream. Get the String out of it (and return it) + out_stream.to_s unless file end end RTM.register_extension(self) end