View Javadoc

1   package com.atlassian.marketplace.client.model;
2   
3   import com.google.common.base.Function;
4   
5   import static com.google.common.base.Preconditions.checkNotNull;
6   import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
7   
8   /**
9    * Simple value wrapper for a string that can contain HTML markup.  Marketplace allows only a
10   * limited subset of HTML tags and will remove any unsupported tags.  Plain text newlines will
11   * be treated as whitespace rather than line breaks (as usual in HTML), so to add a paragraph
12   * break you should use {@code <p>}.
13   * @since 2.0.0
14   */
15  public class HtmlString
16  {
17      private final String value;
18      
19      private HtmlString(String value)
20      {
21          this.value = checkNotNull(value);
22      }
23      
24      public static HtmlString html(String value)
25      {
26          return new HtmlString(value);
27      }
28      
29      public String getHtml()
30      {
31          return value;
32      }
33      
34      @Override
35      public String toString()
36      {
37          return "Html(" + escapeHtml(value) + ")";
38      }
39      
40      @Override
41      public boolean equals(Object other)
42      {
43          return (other instanceof HtmlString) && ((HtmlString) other).value.equals(value);
44      }
45      
46      @Override
47      public int hashCode()
48      {
49          return value.hashCode();
50      }
51      
52      public static Function<HtmlString, String> htmlToString()
53      {
54          return new Function<HtmlString, String>()
55          {
56              @Override
57              public String apply(HtmlString input)
58              {
59                  return input.getHtml();
60              }
61          };
62      }
63  
64      public static Function<String, HtmlString> stringToHtml()
65      {
66          return new Function<String, HtmlString>()
67          {
68              @Override
69              public HtmlString apply(String input)
70              {
71                  return html(input);
72              }
73          };
74      }
75  }