# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 module RTM def self.from_xtm2lx(*args) RTM::IO::FROMXTM2LX.from_xtm2(*args) end end module RTM::IO # XTM2 Export. # Each Topic Maps Construct gets a to_xtm2 method. # The result is a REXML::Element except for TopicMap where it is a REXML::Document. module FROMXTM2LX require 'libxml' XTM2DEBUG = false # Reads XTM2 from source (io object). # Example: RTM::IO::FROMXTM2.from_xtm2(File.open(file_name),"http://rtm.rubyforge.org/topicmaps/tm1/") # supported options: # :strip_whitespace (defaults to false, may be set to true), # :deprefix (defaults to nil, may be set to a string (or regex) which will be removed from the beginning of an (unresolved) item_identifier if it is there. def self.from_xtm2(source, base_locator, target=nil,options={}) tm = RTM.create(base_locator) unless target parser = XML::SaxParser.new parser.callbacks = XML::LibXMLSax2wrapper.new(FROMXTM2::XTM2Listener.new(base_locator, target || tm, options)) parser.filename = source parser.parse #true end end end module XML # Acts as Callback structure for the LibXML-Ruby SAX Parser and calls # a REXML SAX2Listener API. class LibXMLSax2wrapper include LibXML::XML::SaxParser::Callbacks def initialize(rexml_sax2listener) @dest = rexml_sax2listener @ns = Hash.new("http://www.topicmaps.org/xtm/") end def on_start_document @dest.start_document end def on_end_document @dest.end_document end def on_start_element(qname, attr) prefix, localname = qname.split(":") unless localname localname = prefix uri = @ns.default else uri = @ns[prefix] end @dest.start_element(uri, localname, qname, attr) end def on_end_element(qname) prefix, localname = qname.split(":") unless localname localname = prefix uri = @ns.default else uri = @ns[prefix] end @dest.end_element(uri, localname, qname) end def on_characters(text) @dest.characters(text) end def on_cdata_block(content) @dest.cdata(content) end def on_parser_error(msg) warn("SAX Parser Error: #{msg}") end def on_parser_warning(*msg) warn("SAX Parser Warning: #{msg}") end def on_parser_fatal_error(msg) warn("SAX Parser Fatal Error: #{msg}") end end end