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