Class: Glimmer::XML::XmlVisitor

Inherits:
NodeVisitor show all
Defined in:
lib/glimmer/xml/xml_visitor.rb

Direct Known Subclasses

HtmlVisitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXmlVisitor

Returns a new instance of XmlVisitor.



31
32
33
# File 'lib/glimmer/xml/xml_visitor.rb', line 31

def initialize
  @document = ""
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



29
30
31
# File 'lib/glimmer/xml/xml_visitor.rb', line 29

def document
  @document
end

Instance Method Details

#append_attributes(node) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/glimmer/xml/xml_visitor.rb', line 76

def append_attributes(node)
  return if node.name == 'xml' || node.name_space_context?
#         Glimmer::Config.logger&.debug "Append Attributes"
#         Glimmer::Config.logger&.debug(node.attributes)
  node.attributes.each do |attribute, value|
    attribute_name = attribute
    attribute_name = "#{attribute.name_space.name}:#{attribute.name}" if attribute.is_a?(Node)
    attribute_name = attribute_name.to_s.gsub('_', Glimmer::Config.xml_attribute_underscore) if attribute_name.is_a?(Symbol)
    @document += " #{attribute_name}"
    @document += "=\"#{value}\"" unless value.nil?
  end
end

#append_close_tag(node) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/glimmer/xml/xml_visitor.rb', line 67

def append_close_tag(node)
  return if node.name == 'xml' || node.name_space_context?
  if (node.contents)
    @document += "</"
    @document += "#{node.name_space.name}:" if node.name_space
    @document += "#{node.name}>"
  end
end

#begin_open_tag(node) ⇒ Object



50
51
52
53
54
55
# File 'lib/glimmer/xml/xml_visitor.rb', line 50

def begin_open_tag(node)
  return if node.name == 'xml' || node.name_space_context?
  @document += "<"
  @document += "#{node.name_space.name}:" if node.name_space
  @document += node.name
end

#end_open_tag(node) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/glimmer/xml/xml_visitor.rb', line 57

def end_open_tag(node)
  return if node.name == 'xml' || node.name_space_context?
  if (node.contents)
    @document += ">"
  else
    @document += " " if node.attributes.keys.size > 0
    @document += " />"
  end
end

#process_after_children(node) ⇒ Object



45
46
47
48
# File 'lib/glimmer/xml/xml_visitor.rb', line 45

def process_after_children(node)
  return if (!node.is_a?(Glimmer::XML::Node))
  append_close_tag(node)
end

#process_before_children(node) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/glimmer/xml/xml_visitor.rb', line 35

def process_before_children(node)
  if (!node.is_a?(Glimmer::XML::Node))
    @document += node.to_s
    return
  end
  begin_open_tag(node)
  append_attributes(node) if node.attributes
  end_open_tag(node)
end