Class: OpenC3::XmlAccessor

Inherits:
Accessor show all
Defined in:
lib/openc3/accessors/xml_accessor.rb

Direct Known Subclasses

HtmlAccessor

Instance Attribute Summary

Attributes inherited from Accessor

#packet

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Accessor

#args, convert_to_type, #initialize, #read_item, #read_items, #write_item, #write_items

Constructor Details

This class inherits a constructor from OpenC3::Accessor

Class Method Details

.buffer_to_doc(buffer) ⇒ Object



75
76
77
# File 'lib/openc3/accessors/xml_accessor.rb', line 75

def self.buffer_to_doc(buffer)
  Nokogiri.XML(buffer)
end

.doc_to_buffer(doc) ⇒ Object



79
80
81
# File 'lib/openc3/accessors/xml_accessor.rb', line 79

def self.doc_to_buffer(doc)
  doc.to_xml
end

.read_item(item, buffer) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openc3/accessors/xml_accessor.rb', line 24

def self.read_item(item, buffer)
  return nil if item.data_type == :DERIVED
  doc = buffer_to_doc(buffer)
  doc_value = doc.xpath(item.key)
  # Nokogiri returns a Nokogiri::XML::Text which responds to first
  # unless they've applied some XPath functions to the result like normalize-space
  if doc_value.respond_to?(:first)
    doc_value = doc_value.first
  end
  return convert_to_type(doc_value.to_s, item)
end

.read_items(items, buffer) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openc3/accessors/xml_accessor.rb', line 45

def self.read_items(items, buffer)
  doc = buffer_to_doc(buffer)
  result = {}
  items.each do |item|
    if item.data_type == :DERIVED
      result[item.name] = nil
    else
      doc_value = doc.xpath(item.key)
      # Nokogiri returns a Nokogiri::XML::Text which responds to first
      # unless they've applied some XPath functions to the result like normalize-space
      if doc_value.respond_to?(:first)
        doc_value = doc_value.first
      end
      result[item.name] = convert_to_type(doc_value.to_s, item)
    end
  end
  return result
end

.write_item(item, value, buffer) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/openc3/accessors/xml_accessor.rb', line 36

def self.write_item(item, value, buffer)
  return nil if item.data_type == :DERIVED
  doc = buffer_to_doc(buffer)
  node = doc.xpath(item.key).first
  node.content = value.to_s
  buffer.replace(doc_to_buffer(doc))
  return value
end

.write_items(items, values, buffer) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/openc3/accessors/xml_accessor.rb', line 64

def self.write_items(items, values, buffer)
  doc = buffer_to_doc(buffer)
  items.each_with_index do |item, index|
    next if item.data_type == :DERIVED
    node = doc.xpath(item.key).first
    node.content = values[index].to_s
  end
  buffer.replace(doc_to_buffer(doc))
  return values
end

Instance Method Details

#enforce_derived_write_conversion(_item) ⇒ Object

If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written



105
106
107
# File 'lib/openc3/accessors/xml_accessor.rb', line 105

def enforce_derived_write_conversion(_item)
  return true
end

#enforce_encodingObject

If this is set it will enforce that buffer data is encoded in a specific encoding



85
86
87
# File 'lib/openc3/accessors/xml_accessor.rb', line 85

def enforce_encoding
  return nil
end

#enforce_lengthObject

This affects whether the Packet class enforces the buffer length at all. Set to false to remove any correlation between buffer length and defined sizes of items in COSMOS



92
93
94
# File 'lib/openc3/accessors/xml_accessor.rb', line 92

def enforce_length
  return false
end

#enforce_short_buffer_allowedObject

This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Note that the buffer is still resized to the defined length



99
100
101
# File 'lib/openc3/accessors/xml_accessor.rb', line 99

def enforce_short_buffer_allowed
  return true
end