Class: Archimate::FileFormats::ModelExchangeFileWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/archimate/file_formats/model_exchange_file_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

#remove_nil_values, #serialize, write

Constructor Details

#initialize(model) ⇒ ModelExchangeFileWriter

Returns a new instance of ModelExchangeFileWriter.



9
10
11
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 9

def initialize(model)
  super
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 7

def model
  @model
end

Instance Method Details

#elementbase(xml, element) ⇒ Object



32
33
34
35
36
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 32

def elementbase(xml, element)
  serialize_label(xml, element.name)
  serialize(xml, element.documentation)
  serialize_properties(xml, element)
end

#identifier(str) ⇒ Object

TODO: Archi uses hex numbers for ids which may not be valid for identifer. If we are converting from Archi, decorate the IDs here.



151
152
153
154
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 151

def identifier(str)
  return "id-#{str}" if str =~ /^\d/
  str
end

#model_namespacesObject

TODO: this should replace namespaces as appropriate for the desired export version



145
146
147
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 145

def model_namespaces
  model.namespaces
end

#relationship_attributes(relationship) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 49

def relationship_attributes(relationship)
  {
    identifier: identifier(relationship.id),
    source: identifier(relationship.source),
    target: identifier(relationship.target),
    "xsi:type" => meff_type(relationship.type)
  }
end

#serialize_color(xml, color, sym) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 123

def serialize_color(xml, color, sym)
  return if color.nil?
  h = {
    r: color.r,
    g: color.g,
    b: color.b,
    a: color.a
  }
  h.delete(:a) if color.a.nil? || color.a == 100
  xml.send(sym, h)
end

#serialize_element(xml, element) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 24

def serialize_element(xml, element)
  return if element.type == "SketchModel" # TODO: print a warning that data is lost
  xml.element(identifier: identifier(element['id']),
              "xsi:type" => meff_type(element.type)) do
    elementbase(xml, element)
  end
end

#serialize_elements(xml) ⇒ Object



20
21
22
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 20

def serialize_elements(xml)
  xml.elements { serialize(xml, model.elements) } unless model.elements.empty?
end

#serialize_font(xml, style) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 112

def serialize_font(xml, style)
  return unless style && (style.font || style.font_color)
  xml.font(
    remove_nil_values(
      name: style.font&.name,
      size: style.font&.size&.round,
      style: font_style_string(style.font)
    )
  ) { serialize_color(xml, style&.font_color, :color) }
end

#serialize_label(xml, str) ⇒ Object



38
39
40
41
42
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 38

def serialize_label(xml, str)
  return if str.nil? || str.strip.empty?
  name_attrs = str.lang && !str.lang.empty? ? {"xml:lang" => str.lang} : {}
  xml.label(name_attrs) { xml.text text_proc(str) }
end

#serialize_location(xml, location) ⇒ Object



135
136
137
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 135

def serialize_location(xml, location)
  xml.bendpoint(x: location.x.round, y: location.y.round)
end

#serialize_organization(xml, organization) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 73

def serialize_organization(xml, organization)
  return if organization.items.empty? && organization.documentation.empty? && organization.organizations.empty?
  item_attrs = organization.id.nil? || organization.id.empty? ? {} : {identifier: organization.id}
  xml.item(item_attrs) do
    serialize_organization_body(xml, organization)
  end
end

#serialize_organization_body(xml, organization) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 81

def serialize_organization_body(xml, organization)
  return if organization.items.empty? && organization.documentation.empty? && organization.organizations.empty?
  str = organization.name
  label_attrs = organization.name&.lang && !organization.name.lang.empty? ? {"xml:lang" => organization.name.lang} : {}
  xml.label(label_attrs) { xml.text text_proc(str) } unless str.nil? || str.strip.empty?
  serialize(xml, organization.documentation)
  serialize(xml, organization.organizations)
  organization.items.each { |i| serialize_item(xml, i) }
end

#serialize_organization_root(xml, organizations) ⇒ Object



66
67
68
69
70
71
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 66

def serialize_organization_root(xml, organizations)
  return unless organizations && organizations.size > 0
  xml.organization do
    serialize(xml, organizations)
  end
end

#serialize_properties(xml, element) ⇒ Object



91
92
93
94
95
96
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 91

def serialize_properties(xml, element)
  return if element.properties.empty?
  xml.properties do
    serialize(xml, element.properties)
  end
end

#serialize_relationship(xml, relationship) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 58

def serialize_relationship(xml, relationship)
  xml.relationship(
    relationship_attributes(relationship)
  ) do
    elementbase(xml, relationship)
  end
end

#serialize_relationships(xml) ⇒ Object



44
45
46
47
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 44

def serialize_relationships(xml)
  return if model.relationships.empty?
  xml.relationships { serialize(xml, model.relationships) }
end

#serialize_style(xml, style) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 98

def serialize_style(xml, style)
  return unless style
  xml.style(
    remove_nil_values(
      lineWidth: style.line_width
    )
  ) do
    serialize_color(xml, style.fill_color, :fillColor)
    serialize_color(xml, style.line_color, :lineColor)
    serialize_font(xml, style)
    # TODO: complete this
  end
end

#text_proc(str) ⇒ Object

# Processes text for text elements



140
141
142
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 140

def text_proc(str)
  str.strip.tr("\r", "\n")
end

#write(output_io) ⇒ Object



13
14
15
16
17
18
# File 'lib/archimate/file_formats/model_exchange_file_writer.rb', line 13

def write(output_io)
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    serialize_model(xml)
  end
  output_io.write(builder.to_xml)
end