Class ThymeleafModelBuilder

java.lang.Object
com.foreach.across.modules.web.thymeleaf.ThymeleafModelBuilder

public final class ThymeleafModelBuilder extends Object
Specialized builder class wrapping around a Thymeleaf IModelFactory. Meant for writing HTML style elements.

When an element is opened, its attributes can be modified until it is flushed to the Thymeleaf model. Flushing happens when either a new element is opened, or the current element is closed.

An attribute can have multiple values, these will be joined together with a DOUBLE space character.

Since:
2.0.0
Author:
Arne Vandamme
  • Constructor Details

    • ThymeleafModelBuilder

      public ThymeleafModelBuilder(@NonNull @NonNull org.thymeleaf.context.ITemplateContext templateContext, @NonNull @NonNull ViewElementModelWriterRegistry nodeBuilderRegistry, @NonNull @NonNull HtmlIdStore htmlIdStore, @NonNull @NonNull ViewElementAttributeConverter attributeConverter, @NonNull @NonNull com.foreach.across.modules.web.thymeleaf.AttributeNameGenerator attributeNameGenerator, boolean developmentMode)
  • Method Details

    • getTemplateContext

      public org.thymeleaf.context.ITemplateContext getTemplateContext()
      Returns:
      Thymeleaf template context
    • getModelFactory

      public org.thymeleaf.model.IModelFactory getModelFactory()
      Returns:
      Thymeleaf model factory
    • retrieveHtmlId

      public String retrieveHtmlId(ViewElement viewElement)
      Get a unique id for the specific element. Takes into account the id property set if it is a HtmlViewElement but will ensure duplicates return a unique value.
      Parameters:
      viewElement - to get a unique id for
      Returns:
      unique id
    • addViewElement

      public void addViewElement(ViewElement viewElement)
      Add the element to the model. Will lookup the ViewElementModelWriter for the element type in the ViewElementModelWriterRegistry attached to this model builder.

      If the request contains a WebTemplateInterceptor.RENDER_VIEW_ELEMENT attribute, it is considered to be the name of the ViewElement that should have its actual output rendered. All other ViewElements will still get built, but their markup suppressed. In case of a partial rendering, special processing instructions are added to tell the PartialViewElementTemplateProcessor to allow the markup.

      Parameters:
      viewElement - to add
    • createViewElementModel

      public org.thymeleaf.model.IModel createViewElementModel(ViewElement viewElement)
      Create a separate IModel for a ViewElement. The model will use the configuration of this ThymeleafModelBuilder but will not yet been added. This method is useful if you want to manually post-process a model before adding it. Adding the child model can be done through addModel(IModel).
      Parameters:
      viewElement - to create the model for
      Returns:
      model
    • createChildModelBuilder

      public ThymeleafModelBuilder createChildModelBuilder()
      Returns:
      a separate model builder using the same configuration as the current one
    • addModel

      public void addModel(org.thymeleaf.model.IModel childModel)
      Directly add a child model to the current IModel this builder represents.
      Parameters:
      childModel - to add
    • retrieveModel

      public org.thymeleaf.model.IModel retrieveModel()
      Return the current model, ensures pending tags have been written.
      Returns:
      model
    • addText

      public void addText(String text)
      Add some text to the model. Text will be escaped.
      Parameters:
      text - to add
    • addHtml

      public void addHtml(String html)
      Add some HTML to the model.
      Parameters:
      html - to add
    • addText

      public void addText(String text, boolean escapeXml)
      Add some text to the model.
      Parameters:
      text - to add
      escapeXml - true if text should be escaped
    • addAttributes

      public void addAttributes(Map<String,Collection<Object>> attributes)
      Adds all attributes to the current open element. Will throw an exception if no element is currently opened and still available for modification. Any other attributes will remain, but attributes with the same name will be replaced if there is at least one valid value for that attribute.
      Parameters:
      attributes - to set
      See Also:
    • addBooleanAttribute

      public void addBooleanAttribute(String attributeName, boolean value)
      Add a boolean attribute. A boolean attribute is an attribute that is either present or not, its value is determined by its presence. In regular html for example this would be written as required="required".

      This method will add the attribute if is true. Any existing attribute with that name will be replaced, or removed if is false.

      Requires an open element.

      Parameters:
      attributeName - name of the attribute
      value - true if it should be added
    • addAttribute

      public void addAttribute(String attributeName, Object... values)
      Set the attribute with the given name. Will replace any existing attribute values.

      If is empty, a single value identical to will be added. If is not empty but contains only null, the attribute will be ignored and any previously registered value will be kept. All values will be XML escaped and duplicate values will be ignored.

      Requires an open element.

      Parameters:
      attributeName - name of the attribute to set
      values - to set for the attribute
    • addAttributeValue

      public void addAttributeValue(String attributeName, Object... values)
      Add values for a specific attribute. Any other values will remain. Values will be XML escaped and duplicate values will be ignored.

      Requires an open element.

      Parameters:
      attributeName - name of the attribute to modify
      values - to add to the attribute
    • removeAttribute

      public void removeAttribute(String attributeName)
      Remove a single attribute for the open element.

      Requires an open element.

      Parameters:
      attributeName - name of the attribute to remove
    • removeAttributes

      public void removeAttributes()
      Removes all currently declared attributes for the open element.
    • removeAttributeValue

      public void removeAttributeValue(String attributeName, Object... values)
      Remove one or more values for a particular attribute. If no values remain, the entire attribute will be removed.

      Requires an open element.

      Parameters:
      attributeName - name of the attribute from which you want to remove some values
      values - to remove
    • addOpenElement

      public void addOpenElement(String tagName)
      Start a new element with the specific tag name. After the element is opened, attributes can be modified until it is flushed.
      Parameters:
      tagName - of the element
    • changeOpenElement

      public void changeOpenElement(String tagName)
      Change the open element to the specific tag. Will throw IllegalStateException if there is no currently open element that has not yet been flushed.
      Parameters:
      tagName - of the element
    • addCloseElement

      public void addCloseElement()
      Closes the current element, flushes it to the model.