Sassone.Partial (Sassone v1.0.1)

View Source

Supports parsing an XML document partially. This module is useful when the XML document cannot be turned into a Stream e.g over sockets.

Example

iex> {:ok, partial} = Sassone.Partial.new(StackHandler, [])
iex> {:cont, partial} = Sassone.Partial.parse(partial, "<foo>")
iex> {:cont, partial} = Sassone.Partial.parse(partial, "</foo>")
iex> Sassone.Partial.terminate(partial)
{:ok,
 [
   end_document: {},
   end_element: {nil, "foo"},
   start_element: {nil, "foo", []},
   start_document: []
 ]}

Summary

Functions

Obtain the state set by the user.

Builds up a Sassone.Partial, which can be used for later parsing.

Continue parsing next chunk of the document with a partial.

Same as partial/2, but continue previous parsing with a new, provided state as the third argument instead of the previous accumulated state.

Terminates the XML document parsing.

Types

t()

@opaque t()

Functions

get_state(partial)

@spec get_state(partial :: t()) :: state :: term()

Obtain the state set by the user.

new(handler, initial_state, options \\ [])

@spec new(Sassone.Handler.t(), Sassone.Handler.state(), options :: Keyword.t()) ::
  {:ok, partial :: t()} | {:error, exception :: Sassone.ParseError.t()}

Builds up a Sassone.Partial, which can be used for later parsing.

parse(partial, data)

@spec parse(
  partial :: t(),
  data :: binary()
) ::
  {:cont, partial :: t()}
  | {:halt, state :: term()}
  | {:halt, state :: term(), rest :: binary()}
  | {:error, exception :: Sassone.ParseError.t()}

Continue parsing next chunk of the document with a partial.

This function can return in 3 ways:

  • {:cont, partial} - The parsing process has not been terminated.
  • {:halt, user_state} - The parsing process has been terminated, usually because of parser stopping.
  • {:halt, user_state, rest} - The parsing process has been terminated, usually because of parser halting.
  • {:error, exception} - The parsing process has erred.

parse(partial, data, user_state)

@spec parse(
  partial :: t(),
  data :: binary(),
  user_state :: term()
) ::
  {:cont, partial :: t()}
  | {:halt, state :: term()}
  | {:halt, state :: term(), rest :: binary()}
  | {:error, exception :: Sassone.ParseError.t()}

Same as partial/2, but continue previous parsing with a new, provided state as the third argument instead of the previous accumulated state.

i.e. Sassone.Partial.parse(partial, binary, new_state) # coninue previous partial with a new state

This function can return in 3 ways:

  • {:cont, partial} - The parsing process has not been terminated.
  • {:halt, user_state} - The parsing process has been terminated, usually because of parser stopping.
  • {:halt, user_state, rest} - The parsing process has been terminated, usually because of parser halting.
  • {:error, exception} - The parsing process has erred.

terminate(partial)

@spec terminate(partial :: t()) ::
  {:ok, state :: term()} | {:error, exception :: Sassone.ParseError.t()}

Terminates the XML document parsing.