# Copyright 2013 David Axmark
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
# This tool converts a doxygen XML index to a similar index that the CDT dynamic
# help system can understand. The XML index used for input is generated by
# doxygen when setting the "XML_OUTPUT = yes" option in the Doxyfile.
#
# The reason for not using XSLT is that some of the attributes requires
# simple text transformations. For example the href attribute in <topic>.
#

require "./cmd_arguments"
require "./doxy_converter"
require "rexml/document"
include REXML

# Title of the index
ECLIPSE_INDEX_TITLE = "MoSync API Reference"

# Type of index
ECLIPSE_INDEX_TYPE = "HELP_TYPE_CPP"

# The indentation of the XML files
XML_INDENT = 4

##
# Reads a doxygen index.xml file and converts it to a
# documentation index file for CDT.
#
def main
  parsed_args = CmdArguments.new( ARGV )

  # Read the doxygen index file
  doxy_index_xml_file = File.new( parsed_args.doxygen_input_xml, "r" )
  doxy_index_xml_doc = Document.new( doxy_index_xml_file )

  # Create CDT index XML
  result_cdt_doc = Document.new( )
  result_cdt_doc << XMLDecl.new( "1.0", "UTF-8" )
  doxy_converter = DoxyConverter.new( parsed_args.eclipse_base_path )

  doxy_converter.doxy_to_cdt_xml( doxy_index_xml_doc,
                                  result_cdt_doc,
                                  ECLIPSE_INDEX_TITLE,
                                  ECLIPSE_INDEX_TYPE )

  # Save and close
  save_document( result_cdt_doc, parsed_args.cdt_output_xml )
  doxy_index_xml_file.close( )
end

##
# Saves the given document to the given path.
#
# @param xml_doc An XML tree to save.
# @param path The location of where to save the tree.
#
def save_document(xml_doc, path)
  output_xml = File.new( path, "w" )
  formatter = REXML::Formatters::Pretty.new( XML_INDENT )
  formatter.write( xml_doc, output_xml )
  output_xml.close( )
end


# Start main only if the script is executed from command line
if __FILE__ == $0
  main( );
end
