Class SvgDataUriGenerator


  • @ApplicationScoped
    public class SvgDataUriGenerator
    extends Object
    An util class that consumes and generates an SVG data-uri, but it performs additional operations.
    • Constructor Detail

      • SvgDataUriGenerator

        public SvgDataUriGenerator()
    • Method Detail

      • encodeUtf8

        public static String encodeUtf8​(String dataUriDecoded)
        Encodes the specified data-uri string in XML/UTF8 format.
      • encodeBase64

        public static String encodeBase64​(String dataUriDecoded)
        Encodes the specified data-uri string in XML/Base64 format.
      • generate

        public String generate​(com.google.gwt.safehtml.shared.SafeUri svgUri)
        Generates an SVG data-uri with no external resource references, if any.

        GWT DataResource does not support external references, used for example by the use element.

      • generate

        public String generate​(com.google.gwt.safehtml.shared.SafeUri svgUri,
                               Collection<com.google.gwt.safehtml.shared.SafeUri> svgDefs,
                               Collection<String> validUseRefIds)
        Generates a single SVG data-uri from different input SVG declarations.

        As GWT DataResource does not like SVG external references, this is an alternative solution that creates a composite SVG declaration by joining each SVG declaration given from the arguments.

        Given a parent SVG declaration, which contain external resource references to other SVG declarations, and its given SVG referenced declarations, it aggregates the referenced elements in a SVG Def element, and generates a composite SVG which encapsulates the parent and referenced ones in a single declaration. It also handles the external resource references, within the parent SVG, in order to match the actual content, and removes the invalid references.

        It generates a valid SVG declaration as:

        Parameters:
        svgUri - The parent SVG declaration data-uri, which may contain external resource references.
        svgDefs - The external resources which are referenced in the parent SVG declaration, given by the svgUri argument.
        validUseRefIds - The valid external reference identifiers to keep in the resulting declaration. Usually those are the SVG identifiers for each of the declarations given by the svgDefs argument.