Sassone.Handler behaviour (Sassone v1.0.0)

View Source

This module provides callbacks to implement SAX events handler.

The initial user_state is the third argument in Sassone.parse_string/3 and Sassone.parse_stream/3. It can be accumulated and passed around during the parsing time by returning it as the result of the callback implementation, which can be used to keep track of data when parsing is happening.

Returning {:ok, new_state} continues the parsing process with the new state.

Returning {:cont, handler, new_state} continues the parsing process with the new handler module and new state.

Returning {:stop, anything} stops the prosing process immediately, and anything will be returned. This is usually handy when we want to get the desired return without parsing the whole file.

Returning {:halt, anything} stops the prosing process immediately, anything will be returned, together with the rest of buffer being parsed. This is usually handy when we want to get the desired return without parsing the whole file.

SAX Events

There are a couple of events that need to be handled in the handler.

  • :start_document.
  • :start_element.
  • :characters – the binary that matches CharData* and Reference. Note that it is not trimmed and includes ALL whitespace characters that match CharData.
  • :cdata – the binary that matches CData*.
  • :end_document.
  • :end_element.

Check out data/0 type for more information of what are emitted for each event type.

Examples

defmodule MyEventHandler do
  @behaviour Sassone.Handler

  def handle_event(:start_document, prolog, state) do
    {:ok, [{:start_document, prolog} | state]}
  end

  def handle_event(:end_document, _data, state) do
    {:ok, [{:end_document} | state]}
  end

  def handle_event(:start_element, {namespace, name, attributes}, state) do
    {:ok, [{:start_element, namespace, name, attributes} | state]}
  end

  def handle_event(:end_element, {namespace, name}, state) do
    {:ok, [{:end_element, namespace, name} | state]}
  end

  def handle_event(:characters, chars, state) do
    {:ok, [{:chacters, chars} | state]}
  end
end

Summary

Types

cdata()

@type cdata() :: String.t()

characters()

@type characters() :: String.t()

data()

@type data() ::
  cdata()
  | characters()
  | end_document()
  | end_element()
  | start_document()
  | start_element()

end_document()

@type end_document() :: state()

end_element()

@type end_element() :: {Sassone.XML.namespace(), Sassone.XML.name()}

event()

@type event() ::
  :cdata
  | :characters
  | :end_document
  | :end_element
  | :start_document
  | :start_element

start_document()

@type start_document() :: Keyword.t()

start_element()

@type start_element() ::
  {Sassone.XML.namespace(), Sassone.XML.name(), [Sassone.XML.attribute()]}

state()

@type state() :: any()

t()

@type t() :: module()

Callbacks

handle_event(event, data, state)

@callback handle_event(event(), data(), state()) ::
  {:ok, state()} | {:cont, t(), state()} | {:stop, state()} | {:halt, state()}