Class DesignFormatter

  • All Implemented Interfaces:
    java.io.Serializable

    public class DesignFormatter
    extends java.lang.Object
    implements java.io.Serializable
    Class focused on flexible and consistent formatting and parsing of different values throughout reading and writing Design. An instance of this class is used by DesignAttributeHandler.
    Since:
    7.4
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      DesignFormatter()
      Creates the formatter with default types already mapped.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected <T> void addConverter​(java.lang.Class<?> type, Converter<java.lang.String,​?> converter)
      Adds a converter for a given type.
      boolean canConvert​(java.lang.Class<?> type)
      Checks whether or not a value of a given type can be converted.
      static java.lang.String decodeFromTextNode​(java.lang.String input)
      Decodes HTML entities in a text from text node and replaces them with actual characters.
      static java.lang.String encodeForTextNode​(java.lang.String input)
      Encodes some special characters in a given input String to make it ready to be written as contents of a text node.
      protected <T> Converter<java.lang.String,​T> findConverterFor​(java.lang.Class<? extends T> sourceType)
      Finds a converter for a given type.
      protected <T> Converter<java.lang.String,​T> findConverterFor​(java.lang.Class<? extends T> sourceType, boolean strict)
      Finds a converter for a given type.
      java.lang.String format​(java.lang.Object object)
      Finds a formatter for a given object and attempts to format it.
      <T> java.lang.String format​(T object, java.lang.Class<? extends T> type)
      Formats an object according to a converter suitable for a given type.
      protected java.util.Set<java.lang.Class<?>> getRegisteredClasses()
      Returns a set of classes that have a converter registered.
      protected void mapDefaultTypes()
      Maps default types to their converters.
      <T> T parse​(java.lang.String value, java.lang.Class<? extends T> type)
      Parses a given string as a value of given type.
      protected void removeConverter​(java.lang.Class<?> type)
      Removes the converter for given type, if it was present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DesignFormatter

        public DesignFormatter()
        Creates the formatter with default types already mapped.
    • Method Detail

      • mapDefaultTypes

        protected void mapDefaultTypes()
        Maps default types to their converters.
      • addConverter

        protected <T> void addConverter​(java.lang.Class<?> type,
                                        Converter<java.lang.String,​?> converter)
        Adds a converter for a given type.
        Parameters:
        type - Type to convert to/from.
        converter - Converter.
        Since:
        8.0
      • removeConverter

        protected void removeConverter​(java.lang.Class<?> type)
        Removes the converter for given type, if it was present.
        Parameters:
        type - Type to remove converter for.
      • getRegisteredClasses

        protected java.util.Set<java.lang.Class<?>> getRegisteredClasses()
        Returns a set of classes that have a converter registered. This is not the same as the list of supported classes - subclasses of classes in this set are also supported.
        Returns:
        An unmodifiable set of classes that have a converter registered.
      • parse

        public <T> T parse​(java.lang.String value,
                           java.lang.Class<? extends T> type)
        Parses a given string as a value of given type.
        Parameters:
        value - String value to convert.
        type - Expected result type.
        Returns:
        String converted to the expected result type using a registered converter for that type.
      • format

        public java.lang.String format​(java.lang.Object object)
        Finds a formatter for a given object and attempts to format it.
        Parameters:
        object - Object to format.
        Returns:
        String representation of the object, as returned by the registered converter.
      • format

        public <T> java.lang.String format​(T object,
                                           java.lang.Class<? extends T> type)
        Formats an object according to a converter suitable for a given type.
        Parameters:
        object - Object to format.
        type - Type of the object.
        Returns:
        String representation of the object, as returned by the registered converter.
      • canConvert

        public boolean canConvert​(java.lang.Class<?> type)
        Checks whether or not a value of a given type can be converted. If a converter for a superclass is found, this will return true.
        Parameters:
        type - Type to check.
        Returns:
        true when either a given type or its supertype has a converter, false otherwise.
      • findConverterFor

        protected <T> Converter<java.lang.String,​T> findConverterFor​(java.lang.Class<? extends T> sourceType,
                                                                           boolean strict)
        Finds a converter for a given type. May return a converter for a superclass instead, if one is found and strict is false.
        Parameters:
        sourceType - Type to find a converter for.
        strict - Whether or not search should be strict. When this is false, a converter for a superclass of given type may be returned.
        Returns:
        A valid converter for a given type or its supertype, null if it was not found.
        Since:
        8.0
      • findConverterFor

        protected <T> Converter<java.lang.String,​T> findConverterFor​(java.lang.Class<? extends T> sourceType)
        Finds a converter for a given type. May return a converter for a superclass instead, if one is found.
        Parameters:
        sourceType - Type to find a converter for.
        Returns:
        A valid converter for a given type or its subtype, null if it was not found.
        Since:
        8.0
      • encodeForTextNode

        public static java.lang.String encodeForTextNode​(java.lang.String input)

        Encodes some special characters in a given input String to make it ready to be written as contents of a text node. WARNING: this will e.g. encode "<someTag>" to "&lt;someTag&gt;" as this method doesn't do any parsing and assumes that there are no intended HTML elements in the input. Only some entities are actually encoded: &,<, > It's assumed that other entities are taken care of by Jsoup.

        Typically, this method will be used by components to encode data (like option items in AbstractSelect) when dumping to HTML format

        Parameters:
        input - String to be encoded
        Returns:
        String with &,< and > replaced with their HTML entities
        Since:
        7.5.7
      • decodeFromTextNode

        public static java.lang.String decodeFromTextNode​(java.lang.String input)

        Decodes HTML entities in a text from text node and replaces them with actual characters.

        Typically this method will be used by components to read back data (like option items in AbstractSelect) from HTML. Note that this method unencodes more characters than encodeForTextNode(String) encodes

        Parameters:
        input -
        Returns:
        Since:
        7.6