001
014
015 package com.liferay.portal.kernel.search;
016
017 import com.liferay.portal.kernel.search.highlight.HighlightUtil;
018 import com.liferay.portal.kernel.util.ArrayUtil;
019 import com.liferay.portal.kernel.util.HtmlUtil;
020 import com.liferay.portal.kernel.util.LocaleThreadLocal;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portal.kernel.util.Validator;
024
025 import java.util.Locale;
026
027
032 public class Summary {
033
034 public Summary(Locale locale, String title, String content) {
035 _locale = locale;
036 _title = title;
037 _content = content;
038 }
039
040 public Summary(String title, String content) {
041 this(LocaleThreadLocal.getThemeDisplayLocale(), title, content);
042 }
043
044 public String getContent() {
045 if (Validator.isNull(_content)) {
046 return StringPool.BLANK;
047 }
048
049 return _content;
050 }
051
052 public String getHighlightedContent() {
053 return _escapeAndHighlight(_content);
054 }
055
056 public String getHighlightedTitle() {
057 return _escapeAndHighlight(_title);
058 }
059
060 public Locale getLocale() {
061 return _locale;
062 }
063
064 public int getMaxContentLength() {
065 return _maxContentLength;
066 }
067
068 public String[] getQueryTerms() {
069 return _queryTerms;
070 }
071
072 public String getTitle() {
073 if (Validator.isNull(_title)) {
074 return StringPool.BLANK;
075 }
076
077 return _title;
078 }
079
080 public boolean isHighlight() {
081 return _highlight;
082 }
083
084 public void setContent(String content) {
085 _content = content;
086
087 if ((_content != null) && (_maxContentLength > 0) &&
088 (_content.length() > _maxContentLength)) {
089
090 _content = StringUtil.shorten(_content, _maxContentLength);
091 }
092 }
093
094 public void setEscape(boolean escape) {
095 _escape = escape;
096 }
097
098 public void setHighlight(boolean highlight) {
099 _highlight = highlight;
100 }
101
102 public void setLocale(Locale locale) {
103 _locale = locale;
104 }
105
106 public void setMaxContentLength(int maxContentLength) {
107 _maxContentLength = maxContentLength;
108
109 setContent(_content);
110 }
111
112 public void setQueryTerms(String[] queryTerms) {
113 if (ArrayUtil.isEmpty(queryTerms)) {
114 return;
115 }
116
117 _queryTerms = queryTerms;
118 }
119
120 public void setTitle(String title) {
121 _title = title;
122 }
123
124 private String _escapeAndHighlight(String text) {
125 if (!_highlight || Validator.isNull(text) ||
126 ArrayUtil.isEmpty(_queryTerms)) {
127
128 if (_escape) {
129 return HtmlUtil.escape(text);
130 }
131
132 return text;
133 }
134
135 text = HighlightUtil.highlight(
136 text, _queryTerms, _ESCAPE_SAFE_HIGHLIGHTS[0],
137 _ESCAPE_SAFE_HIGHLIGHTS[1]);
138
139 if (_escape) {
140 text = HtmlUtil.escape(text);
141 }
142
143 return StringUtil.replace(
144 text, _ESCAPE_SAFE_HIGHLIGHTS, HighlightUtil.HIGHLIGHTS);
145 }
146
147 private static final String[] _ESCAPE_SAFE_HIGHLIGHTS =
148 {"[@HIGHLIGHT1@]", "[@HIGHLIGHT2@]"};
149
150 private String _content;
151 private boolean _escape = true;
152 private boolean _highlight;
153 private Locale _locale;
154 private int _maxContentLength;
155 private String[] _queryTerms;
156 private String _title;
157
158 }