Module: RTM::IO::Ontopia::TopicMap

Defined in:
lib/rtm/ontopia/io/to_cxtm.rb

Instance Method Summary collapse

Instance Method Details

#to_cxtm(file = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rtm/ontopia/io/to_cxtm.rb', line 13

def to_cxtm(file=nil)
  raise("Only supported for Ontopia.") unless self.kind_of?(Java::NetOntopiaTopicmapsImplTmapi2::TopicMapImpl)
  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 = Java::NetOntopiaTopicmapsXml::CanonicalXTMWriter.new(out_stream)
  writer.write(self.wrapped)
  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