lib/adiwg/mdtranslator.rb in adiwg-mdtranslator-1.2.1 vs lib/adiwg/mdtranslator.rb in adiwg-mdtranslator-1.3.0

- old
+ new

@@ -1,8 +1,6 @@ # MdTranslator - ADIwg MdTranslator RubyGem entry point -# ... pass input file to appropriate reader and writer; -# ... assign module path names for require statements # History: # Stan Smith 2014-07-02 original script # Stan Smith 2014-07-21 added ADIWG namespace # Stan Smith 2014-07-21 added validation of json structure @@ -16,44 +14,44 @@ # Stan Smith 2014-12-01 added translator version to $response # Stan Smith 2014-12-02 organized shared class/code/units folders for 19115-2, 19110 # Stan Smith 2014-12-11 refactored to handle namespacing readers and writers # Stan Smith 2015-01-15 changed translate() to keyword parameter list # Stan Smith 2015-03-04 moved addFinalMessages into this module from rails app +# Stan Smith 2015-06-22 replace global ($response) with passed in object (responseObj) +# ... created as an instance of class ResponseHash +# Stan Smith 2015-07-14 deleted readerFound +# Stan Smith 2015-07-14 renamed readerVersionFound to readerVersionRequested +# Stan Smith 2015-07-16 moved module_coordinates from mdJson reader to internal +# Stan Smith 2015-07-17 added support for user supplied CSS for html writer -# add main directories to load_path -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'mdtranslator/internal')) - +# required by readers and writers require 'adiwg/mdtranslator/version' - -# additional require statements to support rails -# ... these modules contain methods called by rails endpoints -# ... by default rails does not resolve namespaces beyond this point require 'adiwg/mdtranslator/readers/mdReaders' require 'adiwg/mdtranslator/writers/mdWriters' +require 'adiwg/mdtranslator/internal/internal_metadata_obj' +require 'adiwg/mdtranslator/internal/module_dateTimeFun' +require 'adiwg/mdtranslator/internal/module_geoFormat' +require 'adiwg/mdtranslator/internal/module_coordinates' module ADIWG module Mdtranslator - def self.translate(file:, reader:, validate: 'normal', writer: nil, showAllTags: false) + def self.translate(file:, reader:, validate: 'normal', writer: nil, showAllTags: false, css: nil, cssLink: nil) - $showAllTags = showAllTags - # the reader and writer specified in the translate parameter string should load and # return this hash ... - # --------------------------- + # ------------------------------------------------------------------------------------ # readerFormat: the anticipated format of the input file, parsing by the reader will # proceed assuming this format, set by reader # readerStructurePass: 'true' if input file structure is determined to be valid. # Set by the reader. # readerStructureMessages: an array of quoted string messages. If readerStructurePass # is false, set one or more messages to assist the user in fixing file structure # problems. Set by reader. # readerRequested: name of the reader requested by the user, set from the translate # parameter list - # readerFound: name of the reader used by the reader, set by reader. Will be same as - # readerRequested if readerRequested name is valid - # readerVersionFound: version of the reader requested by the input file, set by reader + # readerVersionRequested: version of the reader requested by the input file, set by reader # readerVersionUsed: version of the reader the reader method decided to use in processing # the input file. Set by the reader. Default 'normal'. # readerValidationLevel: validation level requested to be applied to the input file, set # from the parameter list # readerValidationPass: true if the input file passes the level of validation requested, @@ -74,57 +72,84 @@ # writerPass: true if the writer completes the creation of the output file without errors, # set by writer # writerMessages: an array of quoted string messages. If writerPass is 'false', set one # or more messages to assist user in fixing file data problems. Set by writer. # writerOutput: output file returned from the writer, set by writer + # writerShowTags: include tags in XML output for empty elements + # writerMissingIdCount: counter for creating unique elements IDs for ISO elements that did not + # provide IDs in the input metadata file + # translatorVersion: current version of the mdTranslator - $response = { + responseObj = { readerFormat: nil, readerStructurePass: nil, readerStructureMessages: [], - readerRequested: reader, - readerFound: nil, - readerVersionFound: nil, + readerRequested: nil, + readerVersionRequested: nil, readerVersionUsed: nil, - readerValidationLevel: validate, + readerValidationLevel: nil, readerValidationPass: nil, readerValidationMessages: [], readerExecutionPass: nil, readerExecutionMessages: [], - writerName: writer, + writerName: nil, writerVersion: nil, writerFormat: nil, writerPass: nil, writerMessages: [], - writerOutput: nil + writerOutput: nil, + writerShowTags: false, + writerMissingIdCount: '_000', + translatorVersion: nil } + paramsObj = { + cssLink: cssLink, + css: css + } + + # add the passed in parameters to the response hash + responseObj[:readerRequested] = reader + responseObj[:readerValidationLevel] = validate + responseObj[:writerName] = writer + responseObj[:writerShowTags] = showAllTags + + # add mdTranslator version to response hash + responseObj[:translatorVersion] = ADIWG::Mdtranslator::VERSION + # handle readers if reader.nil? || reader == '' - $response[:readerExecutionPass] = false - $response[:readerExecutionMessages] << 'Reader name was not provided' - return $response + responseObj[:readerExecutionPass] = false + responseObj[:readerExecutionMessages] << 'Reader name was not provided' + return responseObj + else - require File.join(File.dirname(__FILE__), 'mdtranslator/readers/mdReaders') - intObj = ADIWG::Mdtranslator::Readers.handleReader(file) + intObj = ADIWG::Mdtranslator::Readers.handleReader(file, responseObj) - # if readerExecutionPass is nil no error messages were set during exection - # and the execution is assumed to have been successful - if $response[:readerExecutionPass].nil? - $response[:readerExecutionPass] = true - elsif !$response[:readerExecutionPass] - return $response + # if readerValidation failed - exit + if !responseObj[:readerValidationPass] + return responseObj end + + # if readerExecutionPass is nil no error messages were set while + # reading the input file into the internal object. + # the read operation is assumed to have been successful and pass is set to true. + if responseObj[:readerExecutionPass].nil? + responseObj[:readerExecutionPass] = true + elsif !responseObj[:readerExecutionPass] + return responseObj + end end # handle writers if writer.nil? || writer == '' - $response[:writerMessages] << 'Writer name was not provided.' + responseObj[:writerMessages] << 'Writer name was not provided.' else require File.join(File.dirname(__FILE__), 'mdtranslator/writers/mdWriters') - ADIWG::Mdtranslator::Writers.handleWriter(intObj) + ADIWG::Mdtranslator::Writers.handleWriter(intObj, responseObj, paramsObj) end - return $response + + return responseObj end end