Module: DcmDict::XML::RexmlTool

Defined in:
lib/dcm_dict/xml/rexml_tool.rb

Class Method Summary collapse

Class Method Details

.create_xml_doc(xml_string) ⇒ Object

Create XML root object from xml source string xml_string



56
57
58
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 56

def self.create_xml_doc(xml_string)
  REXML::Document.new(xml_string)
end

.each_tr_set(doc, xpath) ⇒ Object

Calls the given block from doc once for each ‘table row’ identified by xpath



61
62
63
64
65
66
67
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 61

def self.each_tr_set(doc, xpath)
  alltr = REXML::XPath.match(doc, xpath)
  alltr.each do |tr|
    trset = tr.get_elements('xmlns:td')
    yield trset if block_given?
  end
end

.extract_data_element_field_from_tr_set(trset) ⇒ Object

Extract data element data from xml table row using trset (NodeSet) as source



38
39
40
41
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 38

def self.extract_data_element_field_from_tr_set(trset)
  proc = tag_field_extract_proc(trset)
  TagFieldData.new(proc).data_element_data
end

.extract_data_element_field_from_xml_tr(xml_tr_string) ⇒ Object

Extract data element data from a table row using xml_tr_string as source string



32
33
34
35
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 32

def self.extract_data_element_field_from_xml_tr(xml_tr_string)
  nodeset = extract_rexml_nodeset(xml_tr_string)
  extract_data_element_field_from_tr_set(nodeset)
end

.extract_rexml_nodeset(xml_tr_string) ⇒ Object



95
96
97
98
99
100
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 95

def self.extract_rexml_nodeset(xml_tr_string)
  doc = create_xml_doc(xml_tr_string)
  each_tr_set(doc, "//xmlns:tr") do |tdset|
    return tdset
  end
end

.extract_uid_field_from_tr_set(trset) ⇒ Object

Extract uid data from a table row using trset (XML Element) as source



50
51
52
53
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 50

def self.extract_uid_field_from_tr_set(trset)
  proc = uid_field_extract_proc(trset)
  UidFieldData.new(proc).uid_data
end

.extract_uid_field_from_xml_tr(xml_tr_string) ⇒ Object

Extract uid data from a table row using xml_tr_string as source string



44
45
46
47
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 44

def self.extract_uid_field_from_xml_tr(xml_tr_string)
  nodeset = extract_rexml_nodeset(xml_tr_string)
  extract_uid_field_from_tr_set(nodeset)
end

.make_rexml_proc(node_set, node_set_idx) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 78

def self.make_rexml_proc(node_set, node_set_idx)
  Proc.new do |key|
    element = node_set[node_set_idx[key]]
    field = +'' # fix for ruby 3.4 >> make string 'not chilled'
    if element
      element.each_element_with_text do |txt1|
        field << "\n"  unless field.nil_or_empty?
        txt1.each_element_with_text do |txt2|
          field << txt2.texts.map(&:value).join('')
        end
        field << txt1.texts.map(&:value).join('')
      end
    end
    field
  end
end

.tag_field_extract_proc(node_set) ⇒ Object



70
71
72
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 70

def self.tag_field_extract_proc(node_set)
  make_rexml_proc(node_set, DataElementNodeSetIdx)
end

.uid_field_extract_proc(node_set) ⇒ Object



74
75
76
# File 'lib/dcm_dict/xml/rexml_tool.rb', line 74

def self.uid_field_extract_proc(node_set)
  make_rexml_proc(node_set, UidNodeSetIdx)
end