Class: Lich::DragonRealms::DRParser::InventoryItemSax

Inherits:
Object
  • Object
show all
Defined in:
documented/dragonrealms/drinfomon/drparser.rb

Overview

Ox::Sax handler that extracts the cmd attribute and item name from the first element of an inventory line, e.g. a small pouch. Streaming means the element is captured before any trailing prose, which is not well-formed XML; the resulting parse error is swallowed by #error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ InventoryItemSax

Initializes a SAX handler for parsing a single inventory item line.

Sets up internal state to capture the cmd attribute and item name from the first element encountered; later elements are ignored.



242
243
244
245
246
247
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 242

def initialize
  @in_d = false
  @done = false
  @cmd = nil
  @name = nil
end

Instance Attribute Details

#cmd ⇒ Object (readonly)

Returns the value of attribute cmd.



234
235
236
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 234

def cmd
  @cmd
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



234
235
236
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 234

def name
  @name
end

Instance Method Details

#attr(name, value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Called by the SAX parser when an attribute is encountered.

Captures the cmd attribute value from the active element. The cmd attribute contains the in-game command to retrieve the item (e.g., 'get #12345' or 'get #1 in #2').

Parameters:

  • name (String) —

    the attribute name (e.g., 'cmd', 'id')

  • value (String) —

    the attribute value



270
271
272
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 270

def attr(name, value)
  @cmd = value if @in_d && name == "cmd"
end

#end_element(name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Called by the SAX parser when a closing tag is encountered.

Marks the element as closed and sets a flag to ignore any subsequent elements, since only the first on an inventory line carries the item data.

Parameters:

  • name (String) —

    the tag name (e.g., 'd', 'component')



294
295
296
297
298
299
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 294

def end_element(name)
  return unless @in_d && name == "d"

  @in_d = false
  @done = true # ignore any later <d> elements; we only want the leading item
end

#error(_message, _line, _column) ⇒ Object

Trailing prose after is not valid XML. We have already captured the element by the time Ox reports it, so the error is intentionally ignored.



303
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 303

def error(_message, _line, _column); end

#start_element(name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Called by the SAX parser when an opening tag is encountered.

Marks the element as active so that its cmd attribute and text content can be captured. Only the first element is processed; later ones are skipped.

Parameters:

  • name (String) —

    the tag name (e.g., 'd', 'component')



257
258
259
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 257

def start_element(name)
  @in_d = true if name == "d" && !@done
end

#text(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Called by the SAX parser when text content is encountered.

Captures the item name from inside the element. Only the first text node is stored; subsequent calls are ignored.

Parameters:

  • value (String) —

    the text content of the element



282
283
284
# File 'documented/dragonrealms/drinfomon/drparser.rb', line 282

def text(value)
  @name = value if @in_d && @name.nil?
end