Sha256: 5e3d81eda4e821989c21f9c274789f9a4910a33a72f045ed5ebfc2837226c7ce

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

# Main module
module ChemScanner
  require "nokogiri"
  require "chem_scanner/chem_draw/parser"

  # Read and Parse CDXML
  class Cdxml < ChemDraw::Parser
    attr_accessor :version, :reader

    CDXML_DOCTYPE = "http://www.cambridgesoft.com/xml/cdxml.dtd"

    def initialize
      super

      @type = "cdxml"
    end

    def read(file, is_path = true)
      fs = is_path ? File.open(file) : file
      @cdxml = Nokogiri::XML(fs)
      return false if @cdxml.internal_subset.system_id != CDXML_DOCTYPE

      read_global

      @cdxml.xpath("//page").each do |page|
        @reader = page
        read_objects
      end

      rebuild_objects_map

      @scheme = Interpreter::Scheme.new(self)
      @scheme.interpret

      @molecules = @scheme.molecules
      @reactions = @scheme.reactions

      true
    end

    def raw_data
      @cdxml.to_xml
    end

    def read_global
      @version = @cdxml.xpath("//CDXML/@CreationProgram").text.split(" ").last

      ct = @cdxml.xpath("//CDXML/colortable").first
      @color_table = read_colortable(ct, "cdxml")

      ft = @cdxml.xpath("//CDXML/fonttable").first
      @font_table = read_fonttable(ft, "cdxml")
    end

    def read_objects
      nodes = @reader.element_children

      nodes.each do |node|
        @reader = node
        nid = (node.attr("id") || 0).to_i

        if ChemDraw::CDXML_OBJ[node.name] == "Group"
          read_objects
        else
          parse_object(node.name, nid)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chem_scanner-0.1.3 lib/chem_scanner/cdxml.rb