Class MarkupParseContext

java.lang.Object
org.javagi.base.ProxyInstance
org.gnome.glib.MarkupParseContext
All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class MarkupParseContext extends ProxyInstance

A parse context is used to parse a stream of bytes that you expect to contain marked-up text.

See g_markup_parse_context_new(), GMarkupParser, and so on for more details.

  • Constructor Details

    • MarkupParseContext

      public MarkupParseContext(MemorySegment address)
      Create a MarkupParseContext proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • MarkupParseContext

      public MarkupParseContext(MarkupParser parser, Set<MarkupParseFlags> flags, @Nullable MemorySegment userData)
      Creates a new parse context. A parse context is used to parse marked-up documents. You can feed any number of documents into a context, as long as no errors occur; once an error occurs, the parse context can't continue to parse text (you have to free it and create a new parse context).
      Parameters:
      parser - a GMarkupParser
      flags - one or more GMarkupParseFlags
      userData - user data to pass to GMarkupParser functions
    • MarkupParseContext

      public MarkupParseContext(MarkupParser parser, MarkupParseFlags flags, @Nullable MemorySegment userData)
      Creates a new parse context. A parse context is used to parse marked-up documents. You can feed any number of documents into a context, as long as no errors occur; once an error occurs, the parse context can't continue to parse text (you have to free it and create a new parse context).
      Parameters:
      parser - a GMarkupParser
      flags - one or more GMarkupParseFlags
      userData - user data to pass to GMarkupParser functions
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the MarkupParseContext class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • endParse

      public boolean endParse() throws GErrorException

      Signals to the GMarkupParseContext that all data has been fed into the parse context with g_markup_parse_context_parse().

      This function reports an error if the document isn't complete, for example if elements are still open.

      Returns:
      true on success, false if an error was set
      Throws:
      GErrorException - see GError
    • free

      public void free()

      Frees a GMarkupParseContext.

      This function can't be called from inside one of the GMarkupParser functions or while a subparser is pushed.

    • getElement

      public String getElement()

      Retrieves the name of the currently open element.

      If called from the start_element or end_element handlers this will give the element_name as passed to those functions. For the parent elements, see g_markup_parse_context_get_element_stack().

      Returns:
      the name of the currently open element, or null
      Since:
      2.2
    • getElementStack

      public SList<String> getElementStack()

      Retrieves the element stack from the internal state of the parser.

      The returned GSList is a list of strings where the first item is the currently open tag (as would be returned by g_markup_parse_context_get_element()) and the next item is its immediate parent.

      This function is intended to be used in the start_element and end_element handlers where g_markup_parse_context_get_element() would merely return the name of the element that is being processed.

      Returns:
      the element stack, which must not be modified
      Since:
      2.16
    • getOffset

      public long getOffset()

      Retrieves the current offset from the beginning of the document, in bytes.

      The information is meant to accompany the values returned by getPosition(Out, Out), and comes with the same accuracy guarantees.

      Returns:
      the offset
      Since:
      2.88
    • getPosition

      public void getPosition(@Nullable Out<Integer> lineNumber, @Nullable Out<Integer> charNumber)
      Retrieves the current line number and the number of the character on that line. Intended for use in error messages; there are no strict semantics for what constitutes the "current" line number other than "the best number we could come up with for error messages."
      Parameters:
      lineNumber - return location for a line number, or null
      charNumber - return location for a char-on-line number, or null
    • getTagStart

      public void getTagStart(Out<Long> lineNumber, Out<Long> charNumber, Out<Long> offset)

      Retrieves the start position of the current start or end tag.

      This function can be used in the start_element or end_element callbacks to obtain location information for error reporting.

      Note that lineNumber and charNumber are intended for human readable error messages and are therefore 1-based and in Unicode characters. offset on the other hand is meant for programmatic use, and thus is 0-based and in bytes.

      The information is meant to accompany the values returned by getPosition(Out, Out), and comes with the same accuracy guarantees.

      Parameters:
      lineNumber - return location for the line number
      charNumber - return location for the character number
      offset - return location for offset from the beginning of the document
      Since:
      2.88
    • getUserData

      public @Nullable MemorySegment getUserData()

      Returns the user_data associated with context.

      This will either be the user_data that was provided to g_markup_parse_context_new() or to the most recent call of g_markup_parse_context_push().

      Returns:
      the provided user_data. The returned data belongs to the markup context and will be freed when g_markup_parse_context_free() is called.
      Since:
      2.18
    • parse

      public boolean parse(String text, long textLen) throws GErrorException

      Feed some data to the GMarkupParseContext.

      The data need not be valid UTF-8; an error will be signaled if it's invalid. The data need not be an entire document; you can feed a document into the parser incrementally, via multiple calls to this function. Typically, as you receive data from a network connection or file, you feed each received chunk of data into this function, aborting the process if an error occurs. Once an error is reported, no further data may be fed to the GMarkupParseContext; all errors are fatal.

      Parameters:
      text - chunk of text to parse
      textLen - length of text in bytes
      Returns:
      false if an error occurred, true on success
      Throws:
      GErrorException - see GError
    • pop

      public @Nullable MemorySegment pop()

      Completes the process of a temporary sub-parser redirection.

      This function exists to collect the user_data allocated by a matching call to g_markup_parse_context_push(). It must be called in the end_element handler corresponding to the start_element handler during which g_markup_parse_context_push() was called. You must not call this function from the error callback -- the userData is provided directly to the callback in that case.

      This function is not intended to be directly called by users interested in invoking subparsers. Instead, it is intended to be used by the subparsers themselves to implement a higher-level interface.

      Returns:
      the user data passed to g_markup_parse_context_push()
      Since:
      2.18
    • push

      public void push(MarkupParser parser, @Nullable MemorySegment userData)

      Temporarily redirects markup data to a sub-parser.

      This function may only be called from the start_element handler of a GMarkupParser. It must be matched with a corresponding call to g_markup_parse_context_pop() in the matching end_element handler (except in the case that the parser aborts due to an error).

      All tags, text and other data between the matching tags is redirected to the subparser given by parser. userData is used as the user_data for that parser. userData is also passed to the error callback in the event that an error occurs. This includes errors that occur in subparsers of the subparser.

      The end tag matching the start tag for which this call was made is handled by the previous parser (which is given its own user_data) which is why g_markup_parse_context_pop() is provided to allow "one last access" to the userData provided to this function. In the case of error, the userData provided here is passed directly to the error callback of the subparser and g_markup_parse_context_pop() should not be called. In either case, if userData was allocated then it ought to be freed from both of these locations.

      This function is not intended to be directly called by users interested in invoking subparsers. Instead, it is intended to be used by the subparsers themselves to implement a higher-level interface.

      As an example, see the following implementation of a simple parser that counts the number of tags encountered.

      typedef struct
      {
        gint tag_count;
      } CounterData;
      
      static void
      counter_start_element (GMarkupParseContext  *context,
                             const gchar          *element_name,
                             const gchar         **attribute_names,
                             const gchar         **attribute_values,
                             gpointer              user_data,
                             GError              **error)
      {
        CounterData *data = user_data;
      
        data->tag_count++;
      }
      
      static void
      counter_error (GMarkupParseContext *context,
                     GError              *error,
                     gpointer             user_data)
      {
        CounterData *data = user_data;
      
        g_slice_free (CounterData, data);
      }
      
      static GMarkupParser counter_subparser =
      {
        counter_start_element,
        NULL,
        NULL,
        NULL,
        counter_error
      };
      

      In order to allow this parser to be easily used as a subparser, the following interface is provided:

      void
      start_counting (GMarkupParseContext *context)
      {
        CounterData *data = g_slice_new (CounterData);
      
        data->tag_count = 0;
        g_markup_parse_context_push (context, &counter_subparser, data);
      }
      
      gint
      end_counting (GMarkupParseContext *context)
      {
        CounterData *data = g_markup_parse_context_pop (context);
        int result;
      
        result = data->tag_count;
        g_slice_free (CounterData, data);
      
        return result;
      }
      

      The subparser would then be used as follows:

      static void start_element (context, element_name, ...)
      {
        if (strcmp (element_name, "count-these") == 0)
          start_counting (context);
      
        // else, handle other tags...
      }
      
      static void end_element (context, element_name, ...)
      {
        if (strcmp (element_name, "count-these") == 0)
          g_print ("Counted %d tags\\n", end_counting (context));
      
        // else, handle other tags...
      }
      
      Parameters:
      parser - a GMarkupParser
      userData - user data to pass to GMarkupParser functions
      Since:
      2.18
    • ref

      public MarkupParseContext ref()
      Increases the reference count of context.
      Returns:
      the same this MarkupParseContext
      Since:
      2.36
    • unref

      public void unref()
      Decreases the reference count of context. When its reference count drops to 0, it is freed.
      Since:
      2.36