Class: Arboretum::XML::IO::ArboretumSax

Inherits:
Ox::Sax
  • Object
show all
Includes:
DocTree::Elements
Defined in:
lib/arboretum/xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(style) ⇒ ArboretumSax

Returns a new instance of ArboretumSax.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/arboretum/xml.rb', line 49

def initialize(style)
  # Must initalize line to have access
  @line = nil # Integer

  # Root document element
  root = DocRootElement.new # DocRootElement

  # Intialize a tree for the parser to build on
  @tree = Tree.new(root)    # Tree

  # Contextual information for parsing process
  @open_elements = [root]    # Array of Elements

  # Read style
  @style = style
end

Instance Attribute Details

#treeObject

Returns the value of attribute tree.



47
48
49
# File 'lib/arboretum/xml.rb', line 47

def tree
  @tree
end

Instance Method Details

#abort(name) ⇒ Object



161
162
163
164
165
# File 'lib/arboretum/xml.rb', line 161

def abort(name)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "Unimplemented method"
  self.print_read_failure(__method__, args, reason)
end

#attr(name, str) ⇒ Object



94
95
96
97
98
99
# File 'lib/arboretum/xml.rb', line 94

def attr(name, str)
  # Get the most recently opened element
  most_recent_open = @open_elements.last
  # Append found values to attribute
  most_recent_open.add_attr_value(name, str)
end

#cdata(str) ⇒ Object



151
152
153
154
155
# File 'lib/arboretum/xml.rb', line 151

def cdata(str)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "Unimplemented method"
  self.print_read_failure(__method__, args, reason)
end

#comment(str) ⇒ Object



100
101
102
103
104
105
# File 'lib/arboretum/xml.rb', line 100

def comment(str)
  # Add CommentElement to tree
  comment_element = CommentElement.new(str)
  comment_element.graft_last_onto(@open_elements.last)
  # Do not open the element (no children)
end

#doctype(str) ⇒ Object



146
147
148
149
150
# File 'lib/arboretum/xml.rb', line 146

def doctype(str)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "Unimplemented method"
  self.print_read_failure(__method__, args, reason)
end

#end_element(name) ⇒ Object



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

def end_element(name)
  # Close the most recent element/Get the closed element
  closed_element = @open_elements.pop

  # Partially ensure the correct element has been closed
  if not "#{closed_element.namespaced_tag}".eql?(name.to_s)
    args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
    reason = "Non-matching element open/close tags: #{closed_element.tag} and #{name}"
    self.print_read_failure(__method__, args, reason)
  end
end

#end_instruct(target) ⇒ Object



141
142
143
144
145
# File 'lib/arboretum/xml.rb', line 141

def end_instruct(target)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "Unimplemented method"
  self.print_read_failure(__method__, args, reason)
end

#error(msg, line, column) ⇒ Object



156
157
158
159
160
# File 'lib/arboretum/xml.rb', line 156

def error(msg, line, column)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "A read error occured"
  self.print_read_failure(__method__, args, reason)
end

#instruct(target) ⇒ Object

Unimplemented calls w/debug output



136
137
138
139
140
# File 'lib/arboretum/xml.rb', line 136

def instruct(target)
  args = method(__method__).parameters.map {|_,arg_name| binding.local_variable_get(arg_name)}
  reason = "Unimplemented method"
  self.print_read_failure(__method__, args, reason)
end


66
67
68
69
70
71
72
# File 'lib/arboretum/xml.rb', line 66

def print_read_failure(method_name, args, reason="None given...")
  puts "Reading of line #{@line} failed..."
  puts "Reason: #{reason}"
  puts "Method: #{method_name.to_s}"
  puts "Args: #{args}"
  puts "--------"
end

#start_element(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arboretum/xml.rb', line 74

def start_element(name)
  # Extract namespace if one exists
  element_ns = nil
  element_tag = name
  tag_str = name.to_s
  if tag_str.include?(':')
    tag_split = tag_str.split(':')
    raise XMLParseException if tag_split.length != 2
    # Final information
    element_ns = tag_split[0].to_sym
    element_tag = tag_split[1].to_sym
  end

  # Add TaggedElement to tree
  opened_element = TaggedElement.new(element_ns, element_tag)
  opened_element.graft_last_onto(@open_elements.last)

  # Open the element if paired
  @open_elements.push(opened_element)
end

#text(str) ⇒ Object

Do not open the element (no children)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/arboretum/xml.rb', line 106

def text(str)
  if @style == :clean
    # Compress, but preserve, whitespace
    str.gsub!(/\s+/, ' ')

    # Add TextElement to tree
    text_element = TextElement.new(str)
    text_element.graft_last_onto(@open_elements.last)
    # Do not open the element (no children)

  elsif @style == :preserve
    # Add TextElement to tree
    text_element = TextElement.new(str)
    text_element.graft_last_onto(@open_elements.last)
    # Do not open the element (no children)
  end
end