Class MarkupFactory


  • public class MarkupFactory
    extends Object
    Factory to load markup either from cache or from a resource.

    This class is the main entry point to load markup. Nothing else should be required by Components. It manages caching markup as well as loading and merging (inheritance) of markup.

    The markup returned is immutable as it gets re-used across multiple Component instances.

    Author:
    Juergen Donnerstag
    • Method Detail

      • get

        public static MarkupFactory get()
        Returns:
        Gets the markup factory registered with the Wicket application
      • getMarkupLoader

        public IMarkupLoader getMarkupLoader()
        MarkupLoaders are responsible to find and load the markup for a component. That may be a single file, but e.g. like in markup inheritance it could also be that the markup from different sources must be merged.
        Returns:
        By default an instance of DefaultMarkupLoader will be returned. Via subclassing you may return your own markup loader (chain).
      • newMarkupParser

        public MarkupParser newMarkupParser​(MarkupResourceStream resource)
        Create a new markup parser. Markup parsers read the markup and dissect it in Wicket relevant pieces MarkupElement's (kind of Wicket's DOM).

        MarkupParser's can be extended by means of IMarkupFilter. You can add your own filter as follows:

            public MyMarkupFactory {
              ...
              public MarkupParser newMarkupParser(final MarkupResourceStream resource) {
                 MarkupParser parser = super.newMarkupParser(resource);
                 parser.add(new MyFilter());
                 return parser;
              }
            }
         
        Parameters:
        resource - The resource containing the markup
        Returns:
        A fresh instance of MarkupParser
        See Also:
        onAppendMarkupFilter(IMarkupFilter)
      • onAppendMarkupFilter

        protected IMarkupFilter onAppendMarkupFilter​(IMarkupFilter filter)
        A callback method that is invoked prior to any IMarkupFilter being registered with MarkupParser. Hence it allows to:
        • tweak the default configuration of a filter
        • replace a filter with another one
        • avoid filters being used by returning null
        Note that a new MarkupParser instance is created for each markup resources being loaded.

        Parameters:
        filter - The filter to be registered with the MarkupParser
        Returns:
        The filter to be added. Null to ignore.
      • getMarkupCache

        public IMarkupCache getMarkupCache()
        Get the markup cache which is registered with the factory. Since the factory is registered with the application, only one cache per application exists.

        Please note that markup cache is a pull through cache. It'll invoke a factory method getMarkupResourceStream(MarkupContainer, Class) to load the markup if not yet available in the cache.

        Returns:
        Null, to disable caching.
      • hasMarkupCache

        public boolean hasMarkupCache()
        Returns:
        true if markup cache is available. Make sure you called getMarkupCache() at least once before to initialize the cache.
      • getMarkup

        public final Markup getMarkup​(MarkupContainer container,
                                      boolean enforceReload)
        Get the markup associated with the container.
        Parameters:
        container - The container to find the markup for
        enforceReload - If true, the cache will be ignored and all, including inherited markup files, will be reloaded. Whatever is in the cache, it will be ignored
        Returns:
        The markup associated with the container. Null, if the markup was not found or could not yet be loaded (e.g. getMarkupType() == null). Wicket Exception in case of errors.
      • getMarkup

        public final Markup getMarkup​(MarkupContainer container,
                                      Class<?> clazz,
                                      boolean enforceReload)
        Get the markup associated with the container. Check the cache first. If not found, than load the markup and update the cache.

        The clazz parameter usually can be null, except for base (inherited) markup.

        There are several means to disable markup caching. Caching can be disabled alltogether - getMarkupCache() return null -, or individually (cacheKey == null).

        Parameters:
        container - The container to find the markup for
        clazz - Must be the container class or any of its super classes. May be null.
        enforceReload - The cache will be ignored and all, including inherited markup files, will be reloaded. Whatever is in the cache, it will be ignored
        Returns:
        The markup associated with the container. Null, if the markup was not found or could not yet be loaded (e.g. getMarkupType() == null). Wicket Exception in case of errors.
      • checkMarkupType

        protected final boolean checkMarkupType​(MarkupContainer container)
        Without a markup type we can not search for a file and we can not construct the cacheKey. We can not even load associated markup as required for Panels. Though every MarkupContainer can provide it's own type, by default they refer to the Page. Hence, no markup type is an indicator, that the component or any of its parents, has not yet been added.
        Parameters:
        container - The MarkupContainer which markup type has to checked
        Returns:
        true, if container.getMarkupType() != null
      • getMarkupResourceStream

        public final MarkupResourceStream getMarkupResourceStream​(MarkupContainer container,
                                                                  Class<?> clazz)
        Create a new markup resource stream for the container and optionally the Class. The Class must be provided in case of base (inherited) markup. Else it might be null (standard use case).
        Parameters:
        container - The MarkupContainer which requests to load the Markup resource stream
        clazz - Either the container class or any super class. Might be null.
        Returns:
        A IResourceStream if the resource was found
      • getContainerClass

        public final Class<?> getContainerClass​(MarkupContainer container,
                                                Class<?> clazz)
        Gets and checks the container class
        Parameters:
        container - The MarkupContainer which requests to load the Markup resource stream
        clazz - Either null, or a super class of container
        Returns:
        The container class to be used
      • loadMarkup

        public final Markup loadMarkup​(MarkupContainer container,
                                       MarkupResourceStream markupResourceStream,
                                       boolean enforceReload)
        Loads markup from a resource stream. It'll call the registered markup loader to load the markup.

        Though the 'enforceReload' attribute seem to imply that the cache is consulted to retrieve the markup, the cache in fact is only checked for retrieving the base (inherited) markup. Please see getMarkup(MarkupContainer, boolean) as well.

        Parameters:
        container - The original requesting markup container
        markupResourceStream - The markup resource stream to load, if already known.
        enforceReload - The cache will be ignored and all, including inherited markup files, will be reloaded. Whatever is in the cache, it will be ignored
        Returns:
        The markup. Null, if the markup was not found. Wicket Exception in case of errors.