lib/adiwg/mdtranslator/readers/mdJson/mdJson_reader.rb in adiwg-mdtranslator-0.10.2 vs lib/adiwg/mdtranslator/readers/mdJson/mdJson_reader.rb in adiwg-mdtranslator-0.12.0
- old
+ new
@@ -9,81 +9,160 @@
# Stan Smith 2014-07-03 resolve require statements using Mdtranslator.reader_module
# Stan Smith 2014-07-08 moved json schema version testing to 'adiwg_1_get_version'
# Stan Smith 2014-08-18 add json name/version to internal object
# Stan Smith 2014-12-01 add data dictionary
# Stan Smith 2014-12-03 changed class name to MdJsonReader from AdiwgJsonReader
+# Stan Smith 2014-12-11 refactored to handle namespacing readers and writers
require 'json'
-require ADIWG::Mdtranslator.reader_module('module_contacts', $response[:readerVersionUsed])
-require ADIWG::Mdtranslator.reader_module('module_metadata', $response[:readerVersionUsed])
-require ADIWG::Mdtranslator.reader_module('module_dataDictionary', $response[:readerVersionUsed])
-class MdJsonReader
+module ADIWG
+ module Mdtranslator
+ module Readers
+ module MdJson
- def initialize
- end
+ # set reader namespace
+ $ReaderNS = ADIWG::Mdtranslator::Readers::MdJson
- def unpack(jsonObj)
+ def self.readFile(file)
+ # set anticipated format of file in $response
+ $response[:readerFormat] = 'json'
- # instance classes needed in script
- intMetadataClass = InternalMetadata.new
+ # receive json file into ruby hash
+ parseJson(file)
+ if !$response[:readerStructurePass]
+ return false
+ end
- # create new internal metadata container for the reader
- intBase = intMetadataClass.newBase
+ # check mdJson version name
+ checkVersionName
+ if !$response[:readerStructurePass]
+ return false
+ end
- # convert the received JSON to a Ruby hash
- hashObj = JSON.parse(jsonObj)
+ # check mdJson version number
+ checkVersionNumber
+ if !$response[:readerStructurePass]
+ return false
+ end
- # get json schema name and version
- if hashObj.has_key?('version')
- hVersion = hashObj['version']
- unless hVersion.empty?
- if hVersion.has_key?('name')
- s = hVersion['name']
- if !s.nil?
- intBase[:jsonVersion][:name] = s
- end
- end
- if hVersion.has_key?('version')
- s = hVersion['version']
- if !s.nil?
- intBase[:jsonVersion][:version] = s
- end
- end
- end
- end
+ # validate file against mdJson schema definition
+ require 'adiwg/mdtranslator/readers/mdJson/mdJson_validator'
+ $ReaderNS.validate(file)
+ if !$response[:readerValidationPass]
+ return false
+ end
- # contact
- # load the array of contacts from the json object
- # ... the contacts array uses a local id to reference the
- # ... contact in the array from elsewhere in the json metadata
- if hashObj.has_key?('contact')
- aContacts = hashObj['contact']
- aContacts.each do |hContact|
- unless hContact.empty?
- intBase[:contacts] << Md_Contact.unpack(hContact)
- end
- end
- end
+ # unpack mdJson file
+ require readerModule('module_mdJson')
+ intObj = $ReaderNS.unpack(@hMdJson)
+ return intObj
- # add default contacts
- intBase[:contacts].concat(Md_Contact.setDefaultContacts)
+ end
- # metadata
- # load metadata from the hash object
- if hashObj.has_key?('metadata')
- hMetadata = hashObj['metadata']
- intBase[:metadata] = Md_Metadata.unpack(hMetadata)
- end
+ def self.parseJson(file)
+ # validate the input file structure
+ # test for valid json syntax by attempting to parse the file
+ begin
+ @hMdJson = JSON.parse(file)
+ $response[:readerStructurePass] = true
+ rescue JSON::JSONError => err
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << err
+ return
+ end
+ end
- # data dictionary
- if hashObj.has_key?('dataDictionary')
- hDictionary = hashObj['dataDictionary']
- intBase[:dataDictionary] = Md_DataDictionary.unpack(hDictionary)
- end
+ def self.checkVersionName
+ # find version name on the input json file
+ if @hMdJson.has_key?('version')
+ hVersion = @hMdJson['version']
+ else
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << 'The input file is missing the version:{} block.'
+ return
+ end
- # return ADIwg internal container
- return intBase
+ # check the version name
+ if hVersion.has_key?('name')
+ s = hVersion['name']
+ if !s.nil?
+ $response[:readerFound] = s
+ else
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << 'The input file version name is missing.'
+ return
+ end
+ else
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << "The input file version:{} block is missing the 'name' attribute."
+ return
+ end
- end
+ # check the version name is 'mdJson'
+ if s != 'mdJson'
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << "The mdTranslator reader expected the input file version name to be 'mdJson'."
+ $response[:readerStructureMessages] << "Version name found was: '#{s}'."
+ return
+ end
+ end
-end
\ No newline at end of file
+ def self.checkVersionNumber
+ # find version number on the input json file
+ hVersion = @hMdJson['version']
+ if hVersion.has_key?('version')
+ s = hVersion['version']
+ if !s.nil?
+ $response[:readerVersionFound] = s
+ end
+ else
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << "The input file version:{} block is missing the 'version' number attribute."
+ return
+ end
+
+ # test the reader version requested is supported
+ # remove maintenance release number first
+ # ... then look for a module folder name ending with the requested version
+ # ... example: 'modules_1.2'
+ aVersionParts = s.split('.')
+ if aVersionParts.length >= 2
+ readerVersion = aVersionParts[0] +'.' + aVersionParts[1]
+ dir = File.join(File.dirname(__FILE__), 'modules_' + readerVersion)
+ if !File.directory?(dir)
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << 'The input file version is not supported.'
+ $response[:readerStructureMessages] << "mdJson version requested was '#{s}'"
+ return
+ end
+ $response[:readerVersionUsed] = readerVersion
+ else
+ $response[:readerStructurePass] = false
+ $response[:readerStructureMessages] << 'The input file version number must be in the form MAJOR.MINOR.PATCH, e.g. 1.2.3'
+ $response[:readerStructureMessages] << 'Note the PATCH number is optional.'
+ return
+ end
+ end
+
+ # require modules for the requested version
+ def self.readerModule(moduleName)
+ dir = File.join(File.dirname(__FILE__), 'modules_' + $response[:readerVersionUsed])
+ file = File.join(dir, moduleName)
+
+ # test for the existance of the module in the current mdJson version directory
+ if !File.exist?(File.join(dir, moduleName + '.rb'))
+ # file not found
+ # ... look for module in previous version directory
+ # ... note: no previous version directories exist yet
+
+ # no prior version directory found
+ # ... file not found
+ return nil
+ end
+ return file
+ end
+
+ end
+ end
+ end
+end