# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 module RTM::IO # Export of TMAPI Topic Maps # Utilizing the TMAPIX libary http://code.google.com/p/tmapix/ # Consequently the serializers provided here are only wrappers module TmapiXTo module TopicMap private # Serializes a Topic Map using the given writer. # # If the optional file is given (using the positional parameter or the :file keys in the params hash), # it is written to that file, otherwise a String will be returned. # The file can be provided as a String (which is then interpreted as a filename) or as a java.io.OutputStream. # E.g. to_anything(my_writer, "/some/file") # writes to the file # to_anything(my_writer, some_output_stream) # writes to the outputstream # E.g. to_anything(my_writer, :file => "/some/file") # writes to the file # to_anything(my_writer) # returns a string # # If the optional base_iri is given (using the positional parameter or the :base_iri key in the params hash), # the given base_iri will be used during export. Otherwise the topic maps's base_iri will be used. # E.g. to_anything(my_writer, "/some/file", "http://example.org/something") # uses the base_iri given # to_anything(my_writer) # uses the base_iri of the topic map # # Please be aware that you cannot leave out the filename positional parameter and just provide the base_iri. # If you want to provide a base_iri but no filename provide +nil+ as filename or use the params hash. # E.g. to_anything(my_writer, nil, my_base_iri) # to_anything(my_writer, :base_iri => my_base_iri) # # :call-seq: # to_anything(writer_class, params = {}) # to_anything(writer_class, file, params = {}) # to_anything(writer_class, file, base_iri, params={}) # def to_anything(writer_class, *args) raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap) params = {} # See if we got a params hash. It must be at the end, if it is given. # Take it off, so it won't be unshifted later. params = args.pop if args.last.is_a?(Hash) # Take the file as next (i.e. second after the writer) positional parameter or from hash. # Leave nil if neither found. file = args.shift || params[:file] # Take the base_iri as next (i.e. third after the file) positional parameter or from the hash. # Use the topic maps's base_iri if neither found. base_iri = args.shift || params[:base_iri] || self.base_iri # If a file was provided somehow, we wrap it with output streams 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 # create the java writer writer = writer_class.new(out_stream, base_iri) # set params params.each do |k,v| # skip our params, as they're not of interest for the writer next if k == :file || k == :base_iri writer.setProperty(k.to_s,v.to_s) end if writer.respond_to?(:setProperty) # do the actual work writer.write(self) 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) # otherwise, we return true return file ? true : out_stream.to_s end public # Serializes an RTM::TopicMap to XTM v. 2.0. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # :call-seq: # to_xtm20 # to_xtm20(file) # to_xtm20(file, base_iri) # def to_xtm20(*args) to_anything(Java::OrgTmapixIo::XTM20TopicMapWriter, *args) end alias :to_xtm :to_xtm20 alias :to_xtm2 :to_xtm20 # Serializes an RTM::TopicMap to XTM v. 1.0. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # :call-seq: # to_xtm10 # to_xtm10(file) # to_xtm10(file, base_iri) # def to_xtm10(*args) to_anything(Java::OrgTmapixIo::XTM10TopicMapWriter, *args) end alias :to_xtm1 :to_xtm10 # Serializes an RTM::TopicMap to JTM. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # :call-seq: # to_jtm # to_jtm(file) # to_jtm(file, base_iri) # def to_jtm(*args) to_anything(Java::OrgTmapixIo::JTMTopicMapWriter, *args) end alias :to_json :to_jtm # Serializes an RTM::TopicMap to LTM. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # :call-seq: # to_ltm # to_ltm(file) # to_ltm(file, base_iri) # def to_ltm(*args) to_anything(Java::OrgTmapixIo::LTMTopicMapWriter, *args) end # Serializes an RTM::TopicMap to TM/XML. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # :call-seq: # to_tmxml # to_tmxml(file) # to_tmxml(file, base_iri) # def to_tmxml(*args) to_anything(Java::OrgTmapixIo::TMXMLTopicMapWriter, *args) end # Serializes an RTM::TopicMap to CTM. # # If no argument is given, a String is returned. # # The argument file can be provided as a String # (which is then interpreted as a filename) or as a java.io.OutputStream. # # The argument base_iri may be used to define a new base_iri, if not, the current # one is used. Base_iri may be a String. # # A property-Hash may be given to set properties of the writer. # # :call-seq: # to_ctm(properties={}) # to_ctm(file, properties={}) # to_ctm(file, base_iri, properties={}) # def to_ctm(*args) # set default params default_params = { "writer.features.export.itemidentifier" => false, "writer.features.prefixDetection.enabled" => false, "writer.features.templateDetection.enabled" => false, "writer.features.templateDetection.topicTemplates" => false, "writer.features.templateDetection.associationTemplates" => false, "writer.features.templateMerger.enabled" => false } # enhance the args with the default params if args.last.is_a?(Hash) args.push(default_params.merge(args.pop)) else args.push(default_params) end to_anything(Java::DeTopicmapslabCtmWriterCore::CTMTopicMapWriter, *args) end end RTM.register_extension(self) end end