ResourceRichText.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.richtext.ui;

  21. import javax.annotation.PostConstruct;

  22. import org.apache.commons.lang3.StringUtils;
  23. import org.apache.sling.api.SlingHttpServletRequest;
  24. import org.apache.sling.api.resource.Resource;
  25. import org.apache.sling.models.annotations.Default;
  26. import org.apache.sling.models.annotations.Model;
  27. import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
  28. import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
  29. import org.apache.sling.models.annotations.injectorspecific.Self;
  30. import org.apache.sling.models.annotations.injectorspecific.SlingObject;
  31. import org.jetbrains.annotations.Nullable;

  32. import io.wcm.handler.richtext.RichTextBuilder;
  33. import io.wcm.handler.richtext.RichTextHandler;
  34. import io.wcm.handler.richtext.RichTextNameConstants;
  35. import io.wcm.handler.richtext.TextMode;

  36. /**
  37.  * Generic resource-based model for rendering formatted rich text.
  38.  * <p>
  39.  * Optional use parameters when referencing model from Sightly template:
  40.  * </p>
  41.  * <ul>
  42.  * <li><code>propertyName</code>: Property name in which the text is stored in the resource</li>
  43.  * <li><code>isRichText</code>: Set to false if text to format is plain text.</li>
  44.  * </ul>
  45.  */
  46. @Model(adaptables = SlingHttpServletRequest.class)
  47. public class ResourceRichText {

  48.   @RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
  49.   @Default(values = RichTextNameConstants.PN_TEXT)
  50.   private String propertyName;
  51.   @RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
  52.   @Default(booleanValues = true)
  53.   private boolean isRichText;

  54.   @Self
  55.   private RichTextHandler richTextHandler;
  56.   @SlingObject
  57.   private Resource resource;

  58.   private String markup;

  59.   @PostConstruct
  60.   private void activate() {
  61.     String text = resource.getValueMap().get(propertyName, String.class);

  62.     RichTextBuilder builder = richTextHandler.get(text);

  63.     if (!isRichText) {
  64.       builder.textMode(TextMode.PLAIN);
  65.     }

  66.     markup = builder.buildMarkup();
  67.   }

  68.   /**
  69.    * Returns true if rich text is present and valid.
  70.    * @return Rich text is valid
  71.    */
  72.   public boolean isValid() {
  73.     return StringUtils.isNotBlank(getMarkup());
  74.   }

  75.   /**
  76.    * Returns the formatted text as XHTML markup.
  77.    * @return Rich text markup
  78.    */
  79.   public @Nullable String getMarkup() {
  80.     return markup;
  81.   }

  82. }