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 if ((_maxContentLength <= 0) ||
050 (_content.length() <= _maxContentLength)) {
051
052 return _content;
053 }
054
055 if (!ArrayUtil.isEmpty(_queryTerms)) {
056 int index = StringUtil.indexOfAny(_content, _queryTerms);
057
058 if (index > _maxContentLength) {
059 _content = _content.substring(index);
060 }
061 }
062
063 _content = StringUtil.shorten(_content, _maxContentLength);
064
065 return _content;
066 }
067
068 public String getHighlightedContent() {
069 return _escapeAndHighlight(_content);
070 }
071
072 public String getHighlightedTitle() {
073 return _escapeAndHighlight(_title);
074 }
075
076 public Locale getLocale() {
077 return _locale;
078 }
079
080 public int getMaxContentLength() {
081 return _maxContentLength;
082 }
083
084 public String[] getQueryTerms() {
085 return _queryTerms;
086 }
087
088 public String getTitle() {
089 if (Validator.isNull(_title)) {
090 return StringPool.BLANK;
091 }
092
093 return _title;
094 }
095
096 public boolean isEscape() {
097 return _escape;
098 }
099
100 public boolean isHighlight() {
101 return _highlight;
102 }
103
104 public void setContent(String content) {
105 _content = content;
106 }
107
108 public void setEscape(boolean escape) {
109 _escape = escape;
110 }
111
112 public void setHighlight(boolean highlight) {
113 _highlight = highlight;
114 }
115
116 public void setLocale(Locale locale) {
117 _locale = locale;
118 }
119
120 public void setMaxContentLength(int maxContentLength) {
121 _maxContentLength = maxContentLength;
122 }
123
124 public void setQueryTerms(String[] queryTerms) {
125 if (ArrayUtil.isEmpty(queryTerms)) {
126 return;
127 }
128
129 _queryTerms = queryTerms;
130 }
131
132 public void setTitle(String title) {
133 _title = title;
134 }
135
136 private String _escapeAndHighlight(String text) {
137 if (!_highlight || Validator.isNull(text) ||
138 ArrayUtil.isEmpty(_queryTerms)) {
139
140 if (_escape) {
141 return HtmlUtil.escape(text);
142 }
143
144 return text;
145 }
146
147 text = HighlightUtil.highlight(
148 text, _queryTerms, _ESCAPE_SAFE_HIGHLIGHTS[0],
149 _ESCAPE_SAFE_HIGHLIGHTS[1]);
150
151 if (_escape) {
152 text = HtmlUtil.escape(text);
153 }
154
155 return StringUtil.replace(
156 text, _ESCAPE_SAFE_HIGHLIGHTS, HighlightUtil.HIGHLIGHTS);
157 }
158
159 private static final String[] _ESCAPE_SAFE_HIGHLIGHTS =
160 {"[@HIGHLIGHT1@]", "[@HIGHLIGHT2@]"};
161
162 private String _content;
163 private boolean _escape = true;
164 private boolean _highlight;
165 private Locale _locale;
166 private int _maxContentLength;
167 private String[] _queryTerms;
168 private String _title;
169
170 }