Class: Sawtooth::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/sawtooth/document.rb

Overview

Provides the current parser stack, delegates basically all calles to the supplied parser.

Also the document exposes methods which can be used to directly interact with the stack.

Defined Under Namespace

Classes: Node, Stack

Constant Summary collapse

DOCUMENT_NODE =

Special freaky node for the Document and Comments

[Node.new(nil, '@document')]
COMMENT_NAME =
'@comment'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate = nil) ⇒ Document

Creates a new Document instance with an empty stack and the supplied delegate. The delegate is required to apply the rules.



41
42
43
44
# File 'lib/sawtooth/document.rb', line 41

def initialize(delegate = nil)
  @delegate = delegate
  reset!
end

Instance Attribute Details

#delegateObject

Returns the value of attribute delegate.



36
37
38
# File 'lib/sawtooth/document.rb', line 36

def delegate
  @delegate
end

#stackObject (readonly)

Both the stack and the delegate can be accessed.



35
36
37
# File 'lib/sawtooth/document.rb', line 35

def stack
  @stack
end

#stacksObject (readonly)

Both the stack and the delegate can be accessed.



35
36
37
# File 'lib/sawtooth/document.rb', line 35

def stacks
  @stacks
end

Instance Method Details

#<<(obj) ⇒ Object Also known as: push

Allow an element to be pushed onto the stack



47
48
49
50
# File 'lib/sawtooth/document.rb', line 47

def <<(obj)
  stack << obj
  self
end

#[](key) ⇒ Object

Direct access to customizeable stacks



86
87
88
# File 'lib/sawtooth/document.rb', line 86

def [](key)
  stacks[key]
end

#characters(str) ⇒ Object Also known as: cdata_block

Characters and CDATA will be appended the current text block, if any



99
100
101
102
# File 'lib/sawtooth/document.rb', line 99

def characters(str)
  @text ||= ""
  @text << str
end

#comment(str) ⇒ Object

Called when comments are encountered, empty implementation,



106
107
108
109
# File 'lib/sawtooth/document.rb', line 106

def comment(str)
  cnode = Node.new(nil, COMMENT_NAME, {}, str)
  delegate.comment((DOCUMENT_NODE + path + [cnode]).compact, self, cnode) if delegate.respond_to?(:comment)
end

#currentObject Also known as: top

Shortcut method for current, i.e. an alias of peek without an argument.



70
# File 'lib/sawtooth/document.rb', line 70

def current; peek(0) end

#end_documentObject

Callend when document ends parsing, does call with special @document path.



120
121
122
# File 'lib/sawtooth/document.rb', line 120

def end_document
  delegate.end_document(DOCUMENT_NODE, self) if delegate.respond_to?(:end_document)
end

#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object

Called at the end of an element.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sawtooth/document.rb', line 135

def end_element_namespace(name, prefix = nil, uri = nil)
  # fill text
  node.text = @text.to_s.strip if @text

  # call delegate
  delegate.end_element(path, self, node) if delegate.respond_to?(:end_element)

  # clear stack
  @path_stack.pop
  @text = nil
end

#error(string) ⇒ Object

Pass an error along to the parser, parser should handle whether to continue or abort parsing.



154
155
156
# File 'lib/sawtooth/document.rb', line 154

def error(string)
  delegate.error(path, self, string) if delegate.respond_to?(:error)
end

#nodeObject

Get current node.



83
# File 'lib/sawtooth/document.rb', line 83

def node; @path_stack.last end

#parentObject

Alias for ‘peek(1)`.



74
# File 'lib/sawtooth/document.rb', line 74

def parent; peek(1); end

#pathObject

Get current path stack.



80
# File 'lib/sawtooth/document.rb', line 80

def path; @path_stack end

#peek(n = 0) ⇒ Object

Peek at an element in the stack, i.e. element 0 is the last element.

doc.peek     # => returns last element
doc.peek(1)  # => returns second last element


64
65
66
# File 'lib/sawtooth/document.rb', line 64

def peek(n = 0)
  stack[(n + 1) * -1]
end

#popObject

Pop an element of the stack



54
55
56
# File 'lib/sawtooth/document.rb', line 54

def pop
  stack.pop
end

#reset!Object

Resets path, stack and the current text.



91
92
93
94
95
96
# File 'lib/sawtooth/document.rb', line 91

def reset!
  @path_stack = []
  @stack = []
  @stacks = Hash.new { |hsh, k| hsh[k] = Stack.new }
  @text = nil
end

#rootObject

Alias for ‘stack.first`



77
# File 'lib/sawtooth/document.rb', line 77

def root; stack.first end

#start_documentObject

Called when document starts parsing, clears path and stack and calls with special @document path.



113
114
115
116
# File 'lib/sawtooth/document.rb', line 113

def start_document
  reset!
  delegate.start_document(DOCUMENT_NODE, self) if delegate.respond_to?(:start_document)
end

#start_element_namespace(name, attrs_ary = [], prefix = nil, uri = nil, ns = []) ⇒ Object

Called at the beginning of an element.



125
126
127
128
129
130
131
132
# File 'lib/sawtooth/document.rb', line 125

def start_element_namespace(name, attrs_ary = [], prefix = nil, uri = nil, ns = [])
  @text = nil
  node = Node.new(uri, name, attrs_ary.inject({}) { |hsh, a| hsh[a.localname] = a.value; hsh }, '')
  path << node

  # call delegate
  delegate.start_element(path, self, node) if delegate.respond_to?(:start_element)
end

#warning(string) ⇒ Object

Pass a warning along to the parser



148
149
150
# File 'lib/sawtooth/document.rb', line 148

def warning(string)
  delegate.warning(path, self, string) if delegate.respond_to?(:warning)
end