Class: Moxml::SAX::BlockHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/moxml/sax/block_handler.rb

Overview

Block-based SAX handler with DSL

Provides a convenient block-based API for simple SAX parsing cases without requiring a full class definition.

Examples:

Block-based parsing

context.sax_parse(xml) do
  start_element { |name, attrs| puts "Element: #{name}" }
  characters { |text| puts "Text: #{text}" }
  end_element { |name| puts "End: #{name}" }
end

With instance variables

books = []
context.sax_parse(xml) do
  start_element do |name, attrs|
    books << { id: attrs["id"] } if name == "book"
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(&block) { ... } ⇒ BlockHandler

Create a new block handler

Parameters:

  • block (Proc)

    Block containing DSL calls

Yields:

  • DSL context for defining handlers



32
33
34
35
36
# File 'lib/moxml/sax/block_handler.rb', line 32

def initialize(&block)
  super()
  @handlers = {}
  instance_eval(&block) if block
end

Instance Method Details

#cdata {|text| ... } ⇒ void

This method returns an undefined value.

Define handler for CDATA section event

Yields:

  • Block to execute when CDATA section is encountered

Yield Parameters:

  • text (String)

    CDATA content

Yield Returns:

  • (void)


94
95
96
# File 'lib/moxml/sax/block_handler.rb', line 94

def cdata(&block)
  @handlers[:cdata] = block
end

#characters {|text| ... } ⇒ void

This method returns an undefined value.

Define handler for character data event

Yields:

  • Block to execute when character data is encountered

Yield Parameters:

  • text (String)

    Character data

Yield Returns:

  • (void)


84
85
86
# File 'lib/moxml/sax/block_handler.rb', line 84

def characters(&block)
  @handlers[:characters] = block
end

#comment {|text| ... } ⇒ void

This method returns an undefined value.

Define handler for comment event

Yields:

  • Block to execute when comment is encountered

Yield Parameters:

  • text (String)

    Comment content

Yield Returns:

  • (void)


104
105
106
# File 'lib/moxml/sax/block_handler.rb', line 104

def comment(&block)
  @handlers[:comment] = block
end

#end_document { ... } ⇒ void

This method returns an undefined value.

Define handler for document end event

Yields:

  • Block to execute when document parsing completes

Yield Returns:

  • (void)


52
53
54
# File 'lib/moxml/sax/block_handler.rb', line 52

def end_document(&block)
  @handlers[:end_document] = block
end

#end_element {|name| ... } ⇒ void

This method returns an undefined value.

Define handler for element end event

Yields:

  • Block to execute when closing tag is encountered

Yield Parameters:

  • name (String)

    Element name

Yield Returns:

  • (void)


74
75
76
# File 'lib/moxml/sax/block_handler.rb', line 74

def end_element(&block)
  @handlers[:end_element] = block
end

#error {|error| ... } ⇒ void

This method returns an undefined value.

Define handler for error event

Yields:

  • Block to execute when error occurs

Yield Parameters:

Yield Returns:

  • (void)


125
126
127
# File 'lib/moxml/sax/block_handler.rb', line 125

def error(&block)
  @handlers[:error] = block
end

#on_cdata(text) ⇒ Object



165
166
167
# File 'lib/moxml/sax/block_handler.rb', line 165

def on_cdata(text)
  @handlers[:cdata]&.call(text)
end

#on_characters(text) ⇒ Object



160
161
162
# File 'lib/moxml/sax/block_handler.rb', line 160

def on_characters(text)
  @handlers[:characters]&.call(text)
end

#on_comment(text) ⇒ Object



170
171
172
# File 'lib/moxml/sax/block_handler.rb', line 170

def on_comment(text)
  @handlers[:comment]&.call(text)
end

#on_end_documentObject



145
146
147
# File 'lib/moxml/sax/block_handler.rb', line 145

def on_end_document
  @handlers[:end_document]&.call
end

#on_end_element(name) ⇒ Object



155
156
157
# File 'lib/moxml/sax/block_handler.rb', line 155

def on_end_element(name)
  @handlers[:end_element]&.call(name)
end

#on_error(error) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/moxml/sax/block_handler.rb', line 180

def on_error(error)
  if @handlers[:error]
    @handlers[:error].call(error)
  else
    super
  end
end

#on_processing_instruction(target, data) ⇒ Object



175
176
177
# File 'lib/moxml/sax/block_handler.rb', line 175

def on_processing_instruction(target, data)
  @handlers[:processing_instruction]&.call(target, data)
end

#on_start_documentObject



140
141
142
# File 'lib/moxml/sax/block_handler.rb', line 140

def on_start_document
  @handlers[:start_document]&.call
end

#on_start_element(name, attributes = {}, namespaces = {}) ⇒ Object



150
151
152
# File 'lib/moxml/sax/block_handler.rb', line 150

def on_start_element(name, attributes = {}, namespaces = {})
  @handlers[:start_element]&.call(name, attributes, namespaces)
end

#on_warning(message) ⇒ Object



189
190
191
# File 'lib/moxml/sax/block_handler.rb', line 189

def on_warning(message)
  @handlers[:warning]&.call(message)
end

#processing_instruction {|target, data| ... } ⇒ void

This method returns an undefined value.

Define handler for processing instruction event

Yields:

  • Block to execute when PI is encountered

Yield Parameters:

  • target (String)

    PI target

  • data (String)

    PI data

Yield Returns:

  • (void)


115
116
117
# File 'lib/moxml/sax/block_handler.rb', line 115

def processing_instruction(&block)
  @handlers[:processing_instruction] = block
end

#start_document { ... } ⇒ void

This method returns an undefined value.

Define handler for document start event

Yields:

  • Block to execute when document parsing begins

Yield Returns:

  • (void)


43
44
45
# File 'lib/moxml/sax/block_handler.rb', line 43

def start_document(&block)
  @handlers[:start_document] = block
end

#start_element {|name, attributes, namespaces| ... } ⇒ void

This method returns an undefined value.

Define handler for element start event

Yields:

  • Block to execute when opening tag is encountered

Yield Parameters:

  • name (String)

    Element name

  • attributes (Hash<String, String>)

    Element attributes

  • namespaces (Hash<String, String>)

    Namespace declarations

Yield Returns:

  • (void)


64
65
66
# File 'lib/moxml/sax/block_handler.rb', line 64

def start_element(&block)
  @handlers[:start_element] = block
end

#warning {|message| ... } ⇒ void

This method returns an undefined value.

Define handler for warning event

Yields:

  • Block to execute when warning occurs

Yield Parameters:

  • message (String)

    Warning message

Yield Returns:

  • (void)


135
136
137
# File 'lib/moxml/sax/block_handler.rb', line 135

def warning(&block)
  @handlers[:warning] = block
end