Class: Rsxml::Visitor::BuildRsxmlVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/rsxml/visitor.rb

Constant Summary collapse

OPTS =

The :style option specifies how the Rsxml is to be produced

:xml style is with compact <tt>"prefix:local_part"</tt> Strings for QNames, and namespace declaration attributes
:exploded style is with <tt>[local_part, prefix, uri]</tt> triples for QNames, and no namespace declaration attributes
{:style=>[:xml, :exploded]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil, &element_transformer) ⇒ BuildRsxmlVisitor

Returns a new instance of BuildRsxmlVisitor.



53
54
55
56
57
# File 'lib/rsxml/visitor.rb', line 53

def initialize(opts=nil, &element_transformer)
  @opts = Util.check_opts(OPTS, opts) 
  @cursor_stack = []
  @element_transformer = element_transformer
end

Instance Attribute Details

#cursor_stackObject (readonly)

Returns the value of attribute cursor_stack.



44
45
46
# File 'lib/rsxml/visitor.rb', line 44

def cursor_stack
  @cursor_stack
end

#element_transformerObject (readonly)

Returns the value of attribute element_transformer.



46
47
48
# File 'lib/rsxml/visitor.rb', line 46

def element_transformer
  @element_transformer
end

#optsObject (readonly)

Returns the value of attribute opts.



45
46
47
# File 'lib/rsxml/visitor.rb', line 45

def opts
  @opts
end

#sexpObject (readonly)

Returns the value of attribute sexp.



43
44
45
# File 'lib/rsxml/visitor.rb', line 43

def sexp
  @sexp
end

Instance Method Details

#compact_attr_names(attrs) ⇒ Object



65
66
67
# File 'lib/rsxml/visitor.rb', line 65

def compact_attr_names(attrs)
  Hash[attrs.map{|qname,value| [compact_qname(qname), value]}]
end

#compact_qname(qname) ⇒ Object



59
60
61
62
63
# File 'lib/rsxml/visitor.rb', line 59

def compact_qname(qname)
  local_part, prefix, uri = qname

  [prefix, local_part].map{|s| (!s || s.empty?) ? nil : s}.compact.join(":")
end

#element(context, element_name, attrs, ns_decls) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rsxml/visitor.rb', line 69

def element(context, element_name, attrs, ns_decls)

  element_name, attrs = element_transformer.call(context, element_name, attrs) if element_transformer

  opts[:style] ||= :exploded
  if opts[:style] == :xml
    element_name = compact_qname(element_name)
    attrs = compact_attr_names(attrs)
    ns_attrs = Namespace.namespace_attributes(ns_decls)
    attrs = attrs.merge(ns_attrs)
  end
  
  el = [element_name, (attrs if attrs.size>0)].compact

  if !cursor_stack.last
    @sexp = el
  else
    cursor_stack.last << el
  end
  cursor_stack.push(el)

  begin
    yield
  ensure
    cursor_stack.pop
  end
end

#text(context, text) ⇒ Object



97
98
99
# File 'lib/rsxml/visitor.rb', line 97

def text(context, text)
  cursor_stack.last << text
end