Sha256: 98f48e5b2453912a21cb76682591b64612fe921360a687c6c8a4b888a42a1fd3

Contents?: true

Size: 1.63 KB

Versions: 18

Compression:

Stored size: 1.63 KB

Contents

require "nokogiri"
require "relaton/cli/base_convertor"

module Relaton
  module Cli
    class XMLConvertorNew < Relaton::Cli::BaseConvertor
      def to_yaml
        convert_and_write(file_content, :to_yaml)
      end

      # Convert to YAML
      #
      # This interface allow us to convert any XML file to YAML.
      # It only require us to provide a valid XML file and it can
      # do converstion using default attributes, but it also allow
      # us to provide custom options to customize this converstion
      # process.
      #
      # @param file [File] The complete path to a XML file
      # @param options [Hash] Options as hash key, value pairs.
      #
      def self.to_yaml(file, options = {})
        new(file, options).to_yaml
      end

      private

      def default_ext
        "yaml"
      end

      # @param content [Nokogiri::XML::Document]
      # @return [Hash]
      def convert_content(content)
        if content.root.name == "bibdata"
          # Bibdata.from_xml(content.to_s)
          Relaton::Cli.parse_xml(content).to_hash
        else
          # Bibcollection.from_xml(content)
          title = content.at("relaton-collection/title").text
          author = content.at("relaton-collection/contributor/organization/name").text
          collection = { "root" => { "title" => title, "author" => author } }

          collection["root"]["items"] = content.xpath("//bibdata").map do |bib|
            Relaton::Cli.parse_xml(bib).to_hash
          end

          collection
        end
      end

      def file_content
        Nokogiri::XML(File.read(file, encoding: "utf-8")).remove_namespaces!
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
relaton-cli-0.5.0 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.4.3 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.4.2 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.4.1 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.4.0 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.19 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.18 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.17 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.16 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.15 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.14 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.13 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.12 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.11 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.10 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.9 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.8 lib/relaton/cli/xml_convertor_new.rb
relaton-cli-0.3.7 lib/relaton/cli/xml_convertor_new.rb