Class SimpleImageMediaMarkupBuilder

  • All Implemented Interfaces:
    MediaMarkupBuilder

    @Model(adaptables={org.apache.sling.api.SlingHttpServletRequest.class,org.apache.sling.api.resource.Resource.class})
    @ConsumerType
    public class SimpleImageMediaMarkupBuilder
    extends AbstractImageMediaMarkupBuilder
    Basic implementation of MediaMarkupBuilder for images.

    If image sizes or picture sources are set on the media handler this markup builder also generates markup for responsive images using img with sizes and srcset attributes or picture with source elements.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean accepts​(@NotNull Media media)
      Checks whether this builder can generate markup for the given media.
      protected @Nullable io.wcm.handler.commons.dom.HtmlElement applyImageMap​(@Nullable io.wcm.handler.commons.dom.HtmlElement element, @NotNull Media media)
      If a image map was resolved apply map markup to given image element.
      io.wcm.handler.commons.dom.HtmlElement build​(@NotNull Media media)
      Build media element markup
      protected @NotNull java.lang.String buildImageMapName​(@NotNull java.util.List<ImageMapArea> map, @NotNull Media media)
      Builds an ID for the image map that is unique within the page.
      protected @Nullable MediaFormat getFirstMediaFormat​(@NotNull Media media)
      Get first media format from the resolved media renditions.
      protected @Nullable MediaFormat getFirstMediaFormatWithRatio​(@NotNull Media media)
      Get first media format from the media formats of the media args that has a ratio set.
      protected @Nullable io.wcm.handler.commons.dom.HtmlElement getImageElement​(@NotNull Media media)
      Create an img element that displays the given rendition image.
      protected @Nullable io.wcm.handler.commons.dom.HtmlElement getMediaElement​(@NotNull Media media)
      Create img or picture media element.
      protected @Nullable io.wcm.handler.commons.dom.HtmlElement getPictureElement​(@NotNull Media media)
      Create an img element that displays the given rendition image.
      protected @Nullable java.lang.String getSrcSetRenditions​(@NotNull Media media, @NotNull MediaFormat mediaFormat, long @NotNull ... widths)
      Generate srcset list from the resolved renditions for the ratio of the given media formats and the given widths.
      protected @Nullable java.lang.String getSrcSetRenditions​(@NotNull Media media, @NotNull MediaFormat mediaFormat, @NotNull MediaArgs.WidthOption @Nullable ... widths)
      Generate srcset list from the resolved renditions for the ratio of the given media formats and the given widths.
      boolean isValidMedia​(@NotNull io.wcm.handler.commons.dom.HtmlElement element)
      Checks if the given HTML element is valid.
      • Methods inherited from class java.lang.Object

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

      • SimpleImageMediaMarkupBuilder

        public SimpleImageMediaMarkupBuilder()
    • Method Detail

      • accepts

        public final boolean accepts​(@NotNull
                                     @NotNull Media media)
        Description copied from interface: MediaMarkupBuilder
        Checks whether this builder can generate markup for the given media.
        Parameters:
        media - Media metadata
        Returns:
        true if this markup builder can handle the given media
      • build

        public final io.wcm.handler.commons.dom.HtmlElement build​(@NotNull
                                                                  @NotNull Media media)
        Description copied from interface: MediaMarkupBuilder
        Build media element markup
        Parameters:
        media - Media metadata
        Returns:
        Media element or null if media is invalid
      • getMediaElement

        @Nullable
        protected @Nullable io.wcm.handler.commons.dom.HtmlElement getMediaElement​(@NotNull
                                                                                   @NotNull Media media)
        Create img or picture media element.
        Parameters:
        media - Media metadata
        Returns:
        Media element with properties or null if media metadata is invalid
      • getPictureElement

        @Nullable
        protected @Nullable io.wcm.handler.commons.dom.HtmlElement getPictureElement​(@NotNull
                                                                                     @NotNull Media media)
        Create an img element that displays the given rendition image.
        Parameters:
        media - Media metadata
        Returns:
        img element with properties or null if media metadata is invalid
      • getImageElement

        @Nullable
        protected @Nullable io.wcm.handler.commons.dom.HtmlElement getImageElement​(@NotNull
                                                                                   @NotNull Media media)
        Create an img element that displays the given rendition image.
        Parameters:
        media - Media metadata
        Returns:
        img element with properties or null if media metadata is invalid
      • getSrcSetRenditions

        @Nullable
        protected @Nullable java.lang.String getSrcSetRenditions​(@NotNull
                                                                 @NotNull Media media,
                                                                 @NotNull
                                                                 @NotNull MediaFormat mediaFormat,
                                                                 @NotNull
                                                                 @NotNull MediaArgs.WidthOption @Nullable ... widths)
        Generate srcset list from the resolved renditions for the ratio of the given media formats and the given widths. Widths that have no match are ignored.
        Parameters:
        media - Media
        mediaFormat - Media format
        widths - widths
        Returns:
        srcset String or null if no matching renditions found
      • getSrcSetRenditions

        @Nullable
        protected @Nullable java.lang.String getSrcSetRenditions​(@NotNull
                                                                 @NotNull Media media,
                                                                 @NotNull
                                                                 @NotNull MediaFormat mediaFormat,
                                                                 long @NotNull ... widths)
        Generate srcset list from the resolved renditions for the ratio of the given media formats and the given widths. Widths that have no match are ignored.
        Parameters:
        media - Media
        mediaFormat - Media format
        widths - widths
        Returns:
        srcset String or null if no matching renditions found
      • getFirstMediaFormatWithRatio

        @Nullable
        protected final @Nullable MediaFormat getFirstMediaFormatWithRatio​(@NotNull
                                                                           @NotNull Media media)
        Get first media format from the media formats of the media args that has a ratio set.
        Parameters:
        media - Media
        Returns:
        Media format or null if none found
      • getFirstMediaFormat

        @Nullable
        protected final @Nullable MediaFormat getFirstMediaFormat​(@NotNull
                                                                  @NotNull Media media)
        Get first media format from the resolved media renditions.
        Parameters:
        media - Media
        Returns:
        Media format or null if none found
      • applyImageMap

        @Nullable
        protected final @Nullable io.wcm.handler.commons.dom.HtmlElement applyImageMap​(@Nullable
                                                                                       @Nullable io.wcm.handler.commons.dom.HtmlElement element,
                                                                                       @NotNull
                                                                                       @NotNull Media media)
        If a image map was resolved apply map markup to given image element. As a result both image and map markup are wrapped in a span element.
        Parameters:
        element - Image Element
        media - Media
        Returns:
        Unchanged element or wrapped element with map
      • buildImageMapName

        @NotNull
        protected final @NotNull java.lang.String buildImageMapName​(@NotNull
                                                                    @NotNull java.util.List<ImageMapArea> map,
                                                                    @NotNull
                                                                    @NotNull Media media)
        Builds an ID for the image map that is unique within the page.
        Parameters:
        map - Map data
        media - Media
        Returns:
        Unique ID
      • isValidMedia

        public final boolean isValidMedia​(@NotNull
                                          @NotNull io.wcm.handler.commons.dom.HtmlElement element)
        Description copied from interface: MediaMarkupBuilder
        Checks if the given HTML element is valid. It is treated as invalid if it is null, or if it is a simple IMG element containing the dummy image.
        Parameters:
        element - Media markup element.
        Returns:
        true if media element is invalid