Module: ASpaceImport::XML::DOM

Defined in:
backend/app/converters/lib/xml_dom.rb

Defined Under Namespace

Modules: ClassMethods Classes: Config

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



47
48
49
# File 'backend/app/converters/lib/xml_dom.rb', line 47

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#configObject



52
53
54
# File 'backend/app/converters/lib/xml_dom.rb', line 52

def config
  self.class.config
end

#object(path, defn) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'backend/app/converters/lib/xml_dom.rb', line 85

def object(path, defn)
  @context ||= [@doc]
  @context.last.xpath(path).each do |node|
    @context << node
    obj = ASpaceImport::JSONModel(defn[:obj]).new
    @batch << obj
    defn[:map].each do |key, defn|
      process_field(obj, key, defn)
    end
    if defn[:defaults]
      defn[:defaults].each do |key, val|
        if obj[key].nil?
          obj[key] = val
        end
      end
    end
    yield obj if block_given?
    @context.pop
  end
  if @context.length == 1
    @batch.flush
  end
end

#process_field(obj, key, value) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'backend/app/converters/lib/xml_dom.rb', line 110

def process_field(obj, key, value)
  raise "Received a non-string mapping: #{key}" unless key.is_a?(String)

  # xpath => Array
  if value.is_a?(Array)
    value.each do |i|
      process_field(obj, key, i)
    end
  # xpath => :field_name
  elsif value.is_a?(Symbol)
    @context.last.xpath(key).each do |node|
      if obj[value].is_a?(Array)
        obj[value] << node.inner_text
      else
        obj[value] = node.inner_text
      end
    end
  # xpath => Proc
  elsif value.is_a?(Proc)
    @context.last.xpath(key).each do |node|
      value.call(obj, node)
    end
  # xpath => sub record definition
  elsif value.is_a?(Hash)
    object(key, value) do |sub_obj|
      if value[:rel].is_a?(Proc)
        value[:rel].call(obj, sub_obj)
      else
        property_def = obj.class.schema['properties'][value[:rel].to_s]
        if property_def.nil?
          raise Converter::ConverterMappingError.new("The converter maps '#{key}' to a bad target (property '#{value[:rel]}' on record_type '#{obj.jsonmodel_type}').")
        end

        property_type = ASpaceImport::Utils.get_property_type(property_def)
        filtered_value = ASpaceImport::Utils.value_filter(property_type[0]).call(sub_obj)
        obj[value[:rel]] << filtered_value
      end
    end

  else
    raise Converter::ConverterMappingError.new("Bad types in mapping: (#{key.class.name}) => (#{value.class.name})")
  end
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'backend/app/converters/lib/xml_dom.rb', line 57

def run
  if config.doc_frag_nodes.empty?
    @doc = Nokogiri::XML::Document.parse(IO.read(@input_file))
    @doc.remove_namespaces!

    config.mappings.each do |path, defn|
      object(path, defn)
    end
  else
    parser = Saxerator.parser(IO.read(@input_file)) do |config|
      config.output_type = :xml
      config.ignore_namespaces!
      config.strip_namespaces!
    end

    config.doc_frag_nodes.each do |break_node|
      parser.for_tag(break_node).each do |xml|
        @doc = xml
        @context = [@doc]
        config.mappings.each do |path, defn|
          object(path, defn)
        end
      end
    end
  end
end