Class: Archimate::FileFormats::ArchiFileWriter

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

Constant Summary collapse

TEXT_SUBSTITUTIONS =
[
  ['
', '
'],
  ['"', '"'],
  ['>', '>']
].freeze

Instance Attribute Summary

Attributes inherited from Writer

#model

Instance Method Summary collapse

Methods inherited from Writer

#remove_nil_values, #serialize, write

Constructor Details

#initialize(model) ⇒ ArchiFileWriter

Returns a new instance of ArchiFileWriter.



16
17
18
19
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 16

def initialize(model)
  super
  @version = "3.1.1"
end

Instance Method Details

#archi_style_hash(style) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 140

def archi_style_hash(style)
  {
    "fillColor" => style&.fill_color&.to_rgba,
    "font" => style&.font&.to_archi_font,
    "fontColor" => style&.font_color&.to_rgba,
    "lineColor" => style&.line_color&.to_rgba,
    "lineWidth" => style&.line_width&.to_s,
    "textAlignment" => style&.text_alignment&.to_s,
    "textPosition" => style&.text_position
  }
end

#process_text(doc_str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 21

def process_text(doc_str)
  %w(documentation content).each do |tag|
    TEXT_SUBSTITUTIONS.each do |from, to|
      doc_str.gsub!(%r{<#{tag}>([^<]*#{from}[^<]*)</#{tag}>}) do |str|
        str.gsub(from, to)
      end
    end
  end
  doc_str.gsub(
    %r{<(/)?archimate:}, "<\\1"
  ).gsub(
    %r{<(/)?model}, "<\\1archimate:model"
  )
end

#serialize_access_type(val) ⇒ Object



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

def serialize_access_type(val)
  case val
  when nil
    nil
  else
    DataModel::AccessType.index(val)
  end
end

#serialize_bounds(xml, bounds) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 181

def serialize_bounds(xml, bounds)
  xml.bounds(
    remove_nil_values(
      x: bounds.x&.to_i,
      y: bounds.y&.to_i,
      width: bounds.width&.to_i,
      height: bounds.height&.to_i
    )
  )
end

#serialize_connection(xml, connection) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 192

def serialize_connection(xml, connection)
  xml.sourceConnection(
    remove_nil_values(
      {
        "xsi:type" => connection.type,
        "id" => connection.id,
        "name" => connection.name
      }.merge(
        archi_style_hash(connection.style).merge(
          "source" => connection.source,
          "target" => connection.target,
          "relationship" => connection.relationship
        )
      )
    )
  ) do
    serialize(xml, connection.bendpoints)
    serialize(xml, connection.documentation)
    serialize(xml, connection.properties)
  end
end

#serialize_diagram(xml, diagram) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 123

def serialize_diagram(xml, diagram)
  xml.element(
    remove_nil_values(
      "xsi:type" => diagram.type || "archimate:ArchimateDiagramModel",
      "id" => diagram.id,
      "name" => diagram.name,
      "connectionRouterType" => diagram.connection_router_type,
      "viewpoint" => ArchiFileFormat::VIEWPOINTS.index(diagram.viewpoint_type)&.to_s,
      "background" => diagram.background
    )
  ) do
    serialize(xml, diagram.nodes)
    serialize(xml, diagram.documentation)
    serialize(xml, diagram.properties)
  end
end

#serialize_documentation(xml, documentation, element_name = "documentation") ⇒ Object



75
76
77
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 75

def serialize_documentation(xml, documentation, element_name = "documentation")
  xml.send(element_name) { xml.text(documentation.text) }
end

#serialize_element(xml, element) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 85

def serialize_element(xml, element)
  xml.element(
    remove_nil_values(
      "xsi:type" => "archimate:#{element.type}",
      "id" => element.id,
      "name" => element.name
    )
  ) do
    serialize(xml, element.documentation)
    serialize(xml, element.properties)
  end
end

#serialize_item(xml, item) ⇒ Object



79
80
81
82
83
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 79

def serialize_item(xml, item)
  item_instance = model.lookup(item)
  $stderr.puts "serialize_item item `#{item.inspect}` could not found." if item_instance.nil?
  serialize(xml, item_instance)
end

#serialize_location(xml, bendpoint) ⇒ Object

startX = location.x - source_attachment.x startY = location.y - source_attachment.y endX = location.x - target_attachment.x endY = location.y - source_attachment.y



218
219
220
221
222
223
224
225
226
227
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 218

def serialize_location(xml, bendpoint)
  xml.bendpoint(
    remove_nil_values(
      startX: bendpoint.x == 0 ? nil : bendpoint.x&.to_i,
      startY: bendpoint.y == 0 ? nil : bendpoint.y&.to_i,
      endX: bendpoint.end_x&.to_i,
      endY: bendpoint.end_y&.to_i
    )
  )
end

#serialize_model(xml) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 47

def serialize_model(xml)
  xml["archimate"].model(
    "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
    "xmlns:archimate" => "http://www.archimatetool.com/archimate",
    "name" => model.name,
    "id" => model.id,
    "version" => @version
  ) do
    serialize(xml, model.organizations)
    serialize(xml, model.properties)
    model.documentation.each { |d| serialize_documentation(xml, d, "purpose") }
  end
end

#serialize_organization(xml, organization) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 61

def serialize_organization(xml, organization)
  xml.folder(
    remove_nil_values(name: organization.name, id: organization.id, type: organization.type)
  ) do
    serialize(xml, organization.organizations)
    serialize(xml, organization.documentation)
    organization.items.each { |i| serialize_item(xml, i) }
  end
end

#serialize_property(xml, property) ⇒ Object



71
72
73
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 71

def serialize_property(xml, property)
  xml.property(remove_nil_values(key: property.key, value: property.value))
end

#serialize_relationship(xml, rel) ⇒ Object



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

def serialize_relationship(xml, rel)
  xml.element(
    remove_nil_values(
      "xsi:type" => "archimate:#{rel.type}",
      "id" => rel.id,
      "name" => rel.name,
      "source" => rel.source,
      "target" => rel.target,
      "accessType" => serialize_access_type(rel.access_type)
    )
  ) do
    serialize(xml, rel.documentation)
    serialize(xml, rel.properties)
  end
end

#serialize_view_node(xml, child) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 152

def serialize_view_node(xml, child)
  style_hash = archi_style_hash(child.style)
  fill_color = style_hash.delete("fillColor")
  xml.child(
    remove_nil_values(
      {
        "xsi:type" => child.type,
        "id" => child.id,
        "name" => child.name
      }.merge(
        style_hash.merge(
          "targetConnections" => child.target_connections.empty? ? nil : child.target_connections.join(" "),
          "fillColor" => fill_color,
          "model" => child.model,
          "archimateElement" => child.archimate_element,
          "type" => child.child_type
        )
      )
    )
  ) do
    serialize(xml, child.bounds) unless child.bounds.nil?
    serialize(xml, child.connections)
    xml.content { xml.text child.content } unless child.content.nil?
    serialize(xml, child.nodes)
    serialize(xml, child.documentation)
    serialize(xml, child.properties)
  end
end

#write(archifile_io) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/archimate/file_formats/archi_file_writer.rb', line 36

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