Class ParserSet

java.lang.Object
org.apache.juneau.parser.ParserSet

public final class ParserSet extends Object
Represents a group of Parsers that can be looked up by media type.
Description
Provides the following features:
  • Finds parsers based on HTTP Content-Type header values.
  • Sets common properties on all parsers in a single method call.
  • Locks all parsers in a single method call.
  • Clones existing groups and all parsers within the group in a single method call.
Match ordering
Parsers are matched against Content-Type strings in the order they exist in this group.

Adding new entries will cause the entries to be prepended to the group. This allows for previous parsers to be overridden through subsequent calls.

For example, calling g.append(P1.class,P2.class).append(P3.class,P4.class) will result in the order P3, P4, P1, P2.

Example:

// Construct a new parser group builder ParserSet parsers = ParserSet.create(); .add(JsonParser.class, XmlParser.class); // Add some parsers to it .forEach(x -> x.swaps(CalendarSwap.IsoLocalDateTime.class)) .forEach(x -> x.beansRequireSerializable()) .build(); // Find the appropriate parser by Content-Type ReaderParser parser = (ReaderParser)parsers.getParser("text/json"); // Parse a bean from JSON String json = "{...}"; AddressBook addressBook = parser.parse(json, AddressBook.class);

Notes:
  • This class is thread safe and reusable.
See Also:
  • Constructor Details

  • Method Details

    • create

      public static ParserSet.Builder create(BeanStore beanStore)
      Static creator.
      Parameters:
      beanStore - The bean store to use for creating beans.
      Returns:
      A new builder for this object.
    • create

      public static ParserSet.Builder create()
      Static creator.
      Returns:
      A new builder for this object.
    • copy

      Creates a copy of this parser group.
      Returns:
      A new copy of this parser group.
    • getParserMatch

      public ParserMatch getParserMatch(String contentTypeHeader)
      Searches the group for a parser that can handle the specified Content-Type header value.

      The returned object includes both the parser and media type that matched.

      Parameters:
      contentTypeHeader - The HTTP Content-Type header value.
      Returns:
      The parser and media type that matched the content type header, or null if no match was made.
    • getParserMatch

      public ParserMatch getParserMatch(MediaType mediaType)
      Same as getParserMatch(String) but matches using a MediaType instance.
      Parameters:
      mediaType - The HTTP Content-Type header value as a media type.
      Returns:
      The parser and media type that matched the media type, or null if no match was made.
    • getParser

      public Parser getParser(String contentTypeHeader)
      Same as getParserMatch(String) but returns just the matched parser.
      Parameters:
      contentTypeHeader - The HTTP Content-Type header string.
      Returns:
      The parser that matched the content type header, or null if no match was made.
    • getParser

      public Parser getParser(MediaType mediaType)
      Same as getParserMatch(MediaType) but returns just the matched parser.
      Parameters:
      mediaType - The HTTP media type.
      Returns:
      The parser that matched the media type, or null if no match was made.
    • getSupportedMediaTypes

      Returns the media types that all parsers in this group can handle

      Entries are ordered in the same order as the parsers in the group.

      Returns:
      An unmodifiable list of media types.
    • getParsers

      public List<Parser> getParsers()
      Returns the parsers in this group.
      Returns:
      An unmodifiable list of parsers in this group.
    • isEmpty

      public boolean isEmpty()
      Returns true if this group contains no parsers.
      Returns:
      true if this group contains no parsers.