# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
# License:   Apache License, Version 2.0

module RTM::IO
  # Import for TMAPI Topic Maps
  # Utilizing the TMAPIX libary http://code.google.com/p/tmapix/
  # Consequently the deserializers provided here are only wrappers

  module TmapiXFrom
    module TopicMap

      def from_anything(reader_class, *args)
        raise "Only supported for TMAPI backends!!" unless self.kind_of?(Java::OrgTmapiCore::TopicMap) || self.kind_of?(Java::OrgTmapiCore::TopicMapSystem)

        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 source as next (i.e. second after the writer) positional parameter or from hash.
        # Leave nil if neither found.
        source = args.shift || params[:source]

        # 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]
        unless base_iri
          if self.kind_of?(RTM::TopicMap)
            base_iri = self.base_iri
          else
            base_iri = "imported:FIXME"
          end
        end

        if self.kind_of?(RTM::TopicMap)
          topic_map = self
        else
          topic_map = create(base_iri)
        end

        # TODO catch java.io.FileNotFoundException
        reader = reader_class.new(topic_map, java.io.File.new(source), base_iri)
        if reader_class == Java::OrgTmapixIo::LTMTopicMapReader
          reader.setLegacyMode(true)
        end
        reader.read
        topic_map
      end

      # Reads a Topic Map from an LTM File
      # whose location is given as String parameter.
      #
      # :call-seq:
      #   from_ltm(filename)
      #
      def from_ltm(*args)
        from_anything(Java::OrgTmapixIo::LTMTopicMapReader, *args)
      end

      # Reads a Topic Map from an CTM File
      # whose location is given as String parameter.
      #
      # :call-seq:
      #   from_ctm(filename)
      #
      def from_ctm(*args)
        from_anything(Java::OrgTmapixIo::CTMTopicMapReader, *args)
      end

      # Reads a JSON Topic Maps (JTM) File
      # whose location is given as String parameter.
      #
      # :call-seq:
      #   from_jtm(filename)
      #
      def from_jtm(*args)
        from_anything(Java::OrgTmapixIo::JTMTopicMapReader, *args)
      end

      # Reads a Friendly Topic Maps XML (TM/XML) File
      # independent of the version
      # whose location is given as String parameter.
      #
      # :call-seq:
      #   from_tmxml(filename)
      #
      def from_tmxml(*args)
        from_anything(Java::OrgTmapixIo::TMXMLTopicMapReader, *args)
      end

      # Reads a XML Topic Maps (XTM) File
      # independent of the version
      # whose location is given as String parameter.
      #
      # :call-seq:
      #   from_xtm(filename)
      #
      def from_xtm(*args)
        from_anything(Java::OrgTmapixIo::XTMTopicMapReader, *args)
      end
      alias :from_xtm21 :from_xtm
      alias :from_xtm20 :from_xtm
      alias :from_xtm2 :from_xtm
      alias :from_xtm10 :from_xtm
      alias :from_xtm1 :from_xtm

      # Reads a Notation3 (N3) File
      # whose location is given as String .
      # The source vocabulary must
      # provide the mapping vocabulary.
      # The syntax of the vocabulary is guessed.
      #
      # :call-seq:
      #   from_n3(filename, source vocabulary)
      #
      def from_n3(filename, vocab)
        raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
        reader = Java::OrgTmapixIo::N3TopicMapReader.new self,  java.io.File.new(filename)
        reader.setMappingSource(java.io.File.new(vocab))
        reader.read
      end

      # Reads a RDF/XML File
      # whose location is given as String
      # The source vocabulary must
      # provide the mapping vocabulary.
      # The syntax of the vocabulary is guessed.
      #
      # :call-seq:
      #   from_rdfxml(filename, source vocabulary)
      #
      def from_rdfxml(filename, vocab)
        raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
        reader = Java::OrgTmapixIo::RDFXMLTopicMapReader.new self,  java.io.File.new(filename)
        reader.setMappingSource(java.io.File.new(vocab))
        reader.read
      end
    end
    TopicMapSystem = TopicMap # We want importers for both, TopicMap and TopicMapSystem
    RTM.register_extension(self)
  end
end