001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
019    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
020    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.Http;
024    import com.liferay.portal.kernel.util.IntegerWrapper;
025    import com.liferay.portal.kernel.util.JavaConstants;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.util.WebKeys;
031    import com.liferay.portal.model.Portlet;
032    import com.liferay.portal.model.PortletApp;
033    import com.liferay.portal.model.SpriteImage;
034    import com.liferay.portal.model.Theme;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.taglib.util.IncludeTag;
038    
039    import java.net.MalformedURLException;
040    import java.net.URL;
041    
042    import java.util.HashMap;
043    import java.util.Map;
044    
045    import javax.portlet.PortletResponse;
046    
047    import javax.servlet.http.HttpServletRequest;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     */
052    public class IconTag extends IncludeTag {
053    
054            @Override
055            public int doStartTag() {
056                    return EVAL_BODY_INCLUDE;
057            }
058    
059            public String getCssClass() {
060                    return _cssClass;
061            }
062    
063            public void setAlt(String alt) {
064                    _alt = alt;
065            }
066    
067            public void setAriaRole(String ariaRole) {
068                    _ariaRole = ariaRole;
069            }
070    
071            public void setCssClass(String cssClass) {
072                    _cssClass = cssClass;
073            }
074    
075            public void setData(Map<String, Object> data) {
076                    _data = data;
077            }
078    
079            public void setIconCssClass(String iconCssClass) {
080                    _iconCssClass = iconCssClass;
081            }
082    
083            public void setId(String id) {
084                    _id = id;
085            }
086    
087            public void setImage(String image) {
088                    _image = image;
089            }
090    
091            public void setImageHover(String imageHover) {
092                    _imageHover = imageHover;
093            }
094    
095            public void setLabel(boolean label) {
096                    _label = label;
097            }
098    
099            public void setLang(String lang) {
100                    _lang = lang;
101            }
102    
103            public void setLinkCssClass(String linkCssClass) {
104                    _linkCssClass = linkCssClass;
105            }
106    
107            public void setLocalizeMessage(boolean localizeMessage) {
108                    _localizeMessage = localizeMessage;
109            }
110    
111            public void setMessage(String message) {
112                    _message = message;
113            }
114    
115            public void setMethod(String method) {
116                    _method = method;
117            }
118    
119            public void setOnClick(String onClick) {
120                    _onClick = onClick;
121            }
122    
123            public void setSrc(String src) {
124                    _src = src;
125            }
126    
127            public void setSrcHover(String srcHover) {
128                    _srcHover = srcHover;
129            }
130    
131            public void setTarget(String target) {
132                    _target = target;
133            }
134    
135            public void setToolTip(boolean toolTip) {
136                    _toolTip = toolTip;
137            }
138    
139            public void setUrl(String url) {
140                    _url = url;
141            }
142    
143            public void setUseDialog(boolean useDialog) {
144                    _useDialog = useDialog;
145            }
146    
147            @Override
148            protected void cleanUp() {
149                    _alt = null;
150                    _ariaRole = null;
151                    _cssClass = null;
152                    _data = null;
153                    _iconCssClass = null;
154                    _id = null;
155                    _image = null;
156                    _imageHover = null;
157                    _label = null;
158                    _lang = null;
159                    _linkCssClass = null;
160                    _localizeMessage = true;
161                    _message = null;
162                    _method = null;
163                    _onClick = null;
164                    _src = null;
165                    _srcHover = null;
166                    _target = null;
167                    _toolTip = false;
168                    _url = null;
169                    _useDialog = false;
170            }
171    
172            protected Map<String, Object> getData() {
173                    Map<String, Object> data = null;
174    
175                    if (_data != null) {
176                            data = new HashMap<>(_data);
177                    }
178                    else {
179                            data = new HashMap<>(1);
180                    }
181    
182                    if (_useDialog && Validator.isNull(data.get("title"))) {
183                            String message = getProcessedMessage();
184    
185                            if (_localizeMessage) {
186                                    message = LanguageUtil.get(request, message);
187                            }
188    
189                            data.put("title", HtmlUtil.stripHtml(message));
190                    }
191    
192                    return data;
193            }
194    
195            protected String getDetails() {
196                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
197                            WebKeys.THEME_DISPLAY);
198    
199                    String details = null;
200    
201                    if (_alt != null) {
202                            details = " alt=\"" + LanguageUtil.get(request, _alt) + "\"";
203                    }
204                    else if (isLabel()) {
205                            details = " alt=\"\"";
206                    }
207                    else {
208                            StringBundler sb = new StringBundler(6);
209    
210                            sb.append(" alt=\"");
211                            sb.append(LanguageUtil.get(request, getProcessedMessage()));
212                            sb.append("\"");
213    
214                            if (_toolTip) {
215                                    sb.append(" onmouseover=\"Liferay.Portal.ToolTip.show(this, '");
216                                    sb.append(
217                                            UnicodeLanguageUtil.get(request, getProcessedMessage()));
218                                    sb.append("')\"");
219                            }
220                            else {
221                                    sb.append(" title=\"");
222                                    sb.append(LanguageUtil.get(request, getProcessedMessage()));
223                                    sb.append("\"");
224                            }
225    
226                            details = sb.toString();
227                    }
228    
229                    if (Validator.isNull(_src) || !themeDisplay.isThemeImagesFastLoad() ||
230                            isAUIImage()) {
231    
232                            return details;
233                    }
234    
235                    SpriteImage spriteImage = null;
236                    String spriteFileName = null;
237                    String spriteFileURL = null;
238    
239                    String imageFileName = StringUtil.replace(_src, "common/../", "");
240    
241                    if (imageFileName.contains(Http.PROTOCOL_DELIMITER)) {
242                            String portalURL = PortalUtil.getPortalURL(request);
243    
244                            if (imageFileName.startsWith(portalURL)) {
245                                    imageFileName = imageFileName.substring(portalURL.length());
246                            }
247                            else {
248                                    URL imageURL = null;
249    
250                                    try {
251                                            imageURL = new URL(imageFileName);
252    
253                                            imageFileName = imageURL.getPath();
254                                    }
255                                    catch (MalformedURLException e) {
256                                    }
257                            }
258                    }
259    
260                    Theme theme = themeDisplay.getTheme();
261    
262                    String contextPath = theme.getContextPath();
263    
264                    String imagesPath = contextPath.concat(theme.getImagesPath());
265    
266                    if (imageFileName.startsWith(imagesPath)) {
267                            spriteImage = theme.getSpriteImage(imageFileName);
268    
269                            if (spriteImage != null) {
270                                    spriteFileName = spriteImage.getSpriteFileName();
271    
272                                    if (BrowserSnifferUtil.isIe(request) &&
273                                            (BrowserSnifferUtil.getMajorVersion(request) < 7)) {
274    
275                                            spriteFileName = StringUtil.replace(
276                                                    spriteFileName, ".png", ".gif");
277                                    }
278    
279                                    String cdnBaseURL = themeDisplay.getCDNBaseURL();
280    
281                                    spriteFileURL = cdnBaseURL.concat(spriteFileName);
282                            }
283                    }
284    
285                    if (spriteImage == null) {
286                            Portlet portlet = (Portlet)request.getAttribute(
287                                    "liferay-portlet:icon_portlet:portlet");
288    
289                            if (portlet == null) {
290                                    portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
291                            }
292    
293                            if (portlet != null) {
294                                    PortletApp portletApp = portlet.getPortletApp();
295    
296                                    spriteImage = portletApp.getSpriteImage(imageFileName);
297    
298                                    if (spriteImage != null) {
299                                            spriteFileName = spriteImage.getSpriteFileName();
300    
301                                            if (BrowserSnifferUtil.isIe(request) &&
302                                                    (BrowserSnifferUtil.getMajorVersion(request) < 7)) {
303    
304                                                    spriteFileName = StringUtil.replace(
305                                                            spriteFileName, ".png", ".gif");
306                                            }
307    
308                                            String cdnBaseURL = themeDisplay.getCDNBaseURL();
309    
310                                            spriteFileURL = cdnBaseURL.concat(spriteFileName);
311                                    }
312                            }
313                    }
314    
315                    if (spriteImage != null) {
316                            String themeImagesPath = themeDisplay.getPathThemeImages();
317    
318                            _src = themeImagesPath.concat("/spacer.png");
319    
320                            StringBundler sb = new StringBundler(10);
321    
322                            sb.append(details);
323                            sb.append(" style=\"background-image: url('");
324                            sb.append(spriteFileURL);
325                            sb.append("'); background-position: 50% -");
326                            sb.append(spriteImage.getOffset());
327                            sb.append("px; background-repeat: no-repeat; height: ");
328                            sb.append(spriteImage.getHeight());
329                            sb.append("px; width: ");
330                            sb.append(spriteImage.getWidth());
331                            sb.append("px;\"");
332    
333                            details = sb.toString();
334                    }
335    
336                    return details;
337            }
338    
339            protected String getId() {
340                    if (Validator.isNotNull(_id)) {
341                            return _id;
342                    }
343    
344                    String id = (String)request.getAttribute("liferay-ui:icon-menu:id");
345    
346                    String message = _message;
347    
348                    if (Validator.isNull(message)) {
349                            message = _image;
350                    }
351    
352                    if (Validator.isNotNull(id) && Validator.isNotNull(message)) {
353                            id = id.concat(StringPool.UNDERLINE).concat(
354                                    FriendlyURLNormalizerUtil.normalize(message));
355    
356                            PortletResponse portletResponse =
357                                    (PortletResponse)request.getAttribute(
358                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
359    
360                            String namespace = StringPool.BLANK;
361    
362                            if (portletResponse != null) {
363                                    namespace = portletResponse.getNamespace();
364                            }
365    
366                            id = PortalUtil.getUniqueElementId(
367                                    getOriginalServletRequest(), namespace, id);
368                    }
369                    else {
370                            id = PortalUtil.generateRandomKey(request, IconTag.class.getName());
371                    }
372    
373                    return id;
374            }
375    
376            protected String getImage() {
377                    return _image;
378            }
379    
380            protected String getMessage() {
381                    return _message;
382            }
383    
384            protected String getMethod() {
385                    if (Validator.isNotNull(_method)) {
386                            return _method;
387                    }
388    
389                    String url = getUrl();
390    
391                    if (url.contains("p_p_lifecycle=0")) {
392                            return "get";
393                    }
394    
395                    return "post";
396            }
397    
398            protected String getOnClick() {
399                    String onClick = StringPool.BLANK;
400    
401                    if (Validator.isNotNull(_onClick)) {
402                            onClick = _onClick;
403    
404                            if (!onClick.endsWith(StringPool.SEMICOLON)) {
405                                    onClick = onClick + StringPool.SEMICOLON;
406                            }
407                    }
408    
409                    if (isForcePost()) {
410                            StringBundler sb = new StringBundler(8);
411    
412                            sb.append("event.preventDefault();");
413                            sb.append(onClick);
414                            sb.append("submitForm(document.hrefFm, '");
415                            sb.append(getUrl());
416                            sb.append("')");
417    
418                            onClick = sb.toString();
419                    }
420    
421                    return onClick;
422            }
423    
424            @Override
425            protected String getPage() {
426                    return _PAGE;
427            }
428    
429            protected String getProcessedMessage() {
430                    if (_message != null) {
431                            return _message;
432                    }
433    
434                    return StringUtil.replace(
435                            _image, new String[] {StringPool.UNDERLINE, _AUI_PATH},
436                            new String[] {StringPool.DASH, StringPool.BLANK});
437            }
438    
439            protected String getProcessedUrl() {
440                    if (isForcePost()) {
441                            return "javascript:;";
442                    }
443    
444                    return _url;
445            }
446    
447            protected String getSrc() {
448                    if (Validator.isNotNull(_src)) {
449                            return _src;
450                    }
451    
452                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
453                            WebKeys.THEME_DISPLAY);
454    
455                    if (isAUIImage()) {
456                            return themeDisplay.getPathThemeImages().concat("/spacer.png");
457                    }
458                    else if (Validator.isNotNull(_image)) {
459                            StringBundler sb = new StringBundler(4);
460    
461                            sb.append(themeDisplay.getPathThemeImages());
462                            sb.append("/common/");
463                            sb.append(_image);
464                            sb.append(".png");
465    
466                            return StringUtil.replace(sb.toString(), "common/../", "");
467                    }
468    
469                    return StringPool.BLANK;
470            }
471    
472            protected String getSrcHover() {
473                    if (Validator.isNotNull(_srcHover) || Validator.isNull(_imageHover)) {
474                            return _srcHover;
475                    }
476    
477                    StringBundler sb = new StringBundler(4);
478    
479                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
480                            WebKeys.THEME_DISPLAY);
481    
482                    sb.append(themeDisplay.getPathThemeImages());
483    
484                    sb.append("/common/");
485                    sb.append(_imageHover);
486                    sb.append(".png");
487    
488                    return sb.toString();
489            }
490    
491            protected String getUrl() {
492                    return GetterUtil.getString(_url);
493            }
494    
495            protected boolean isAUIImage() {
496                    if ((_image != null) && _image.startsWith(_AUI_PATH)) {
497                            return true;
498                    }
499    
500                    return false;
501            }
502    
503            @Override
504            protected boolean isCleanUpSetAttributes() {
505                    return _CLEAN_UP_SET_ATTRIBUTES;
506            }
507    
508            protected boolean isForcePost() {
509                    String method = getMethod();
510    
511                    if (method.equals("post")) {
512                            String url = getUrl();
513    
514                            if (url.startsWith(Http.HTTP_WITH_SLASH) ||
515                                    url.startsWith(Http.HTTPS_WITH_SLASH)) {
516    
517                                    return true;
518                            }
519                    }
520    
521                    return false;
522            }
523    
524            protected boolean isLabel() {
525                    if (_label != null) {
526                            return _label;
527                    }
528    
529                    IntegerWrapper iconListIconCount = (IntegerWrapper)request.getAttribute(
530                            "liferay-ui:icon-list:icon-count");
531    
532                    Boolean iconListSingleIcon = (Boolean)request.getAttribute(
533                            "liferay-ui:icon-list:single-icon");
534    
535                    if ((iconListIconCount != null) || (iconListSingleIcon != null)) {
536                            _label = true;
537    
538                            return _label;
539                    }
540    
541                    IntegerWrapper iconMenuIconCount = (IntegerWrapper)request.getAttribute(
542                            "liferay-ui:icon-menu:icon-count");
543    
544                    Boolean iconMenuSingleIcon = (Boolean)request.getAttribute(
545                            "liferay-ui:icon-menu:single-icon");
546    
547                    if ((iconMenuIconCount != null) || (iconMenuSingleIcon != null)) {
548                            _label = true;
549    
550                            return _label;
551                    }
552    
553                    _label = false;
554    
555                    return _label;
556            }
557    
558            @Override
559            protected void setAttributes(HttpServletRequest request) {
560                    request.setAttribute("liferay-ui:icon:alt", _alt);
561                    request.setAttribute("liferay-ui:icon:ariaRole", _ariaRole);
562                    request.setAttribute(
563                            "liferay-ui:icon:auiImage", String.valueOf(isAUIImage()));
564                    request.setAttribute("liferay-ui:icon:cssClass", _cssClass);
565                    request.setAttribute("liferay-ui:icon:data", getData());
566                    request.setAttribute("liferay-ui:icon:details", getDetails());
567                    request.setAttribute("liferay-ui:icon:iconCssClass", _iconCssClass);
568                    request.setAttribute("liferay-ui:icon:id", getId());
569                    request.setAttribute("liferay-ui:icon:image", _image);
570                    request.setAttribute("liferay-ui:icon:imageHover", _imageHover);
571                    request.setAttribute(
572                            "liferay-ui:icon:forcePost", String.valueOf(isForcePost()));
573                    request.setAttribute(
574                            "liferay-ui:icon:label", String.valueOf(isLabel()));
575                    request.setAttribute("liferay-ui:icon:lang", _lang);
576                    request.setAttribute("liferay-ui:icon:linkCssClass", _linkCssClass);
577                    request.setAttribute(
578                            "liferay-ui:icon:localizeMessage",
579                            String.valueOf(_localizeMessage));
580                    request.setAttribute("liferay-ui:icon:message", getProcessedMessage());
581                    request.setAttribute("liferay-ui:icon:method", getMethod());
582                    request.setAttribute("liferay-ui:icon:onClick", getOnClick());
583                    request.setAttribute("liferay-ui:icon:src", getSrc());
584                    request.setAttribute("liferay-ui:icon:srcHover", getSrcHover());
585                    request.setAttribute("liferay-ui:icon:target", _target);
586                    request.setAttribute(
587                            "liferay-ui:icon:toolTip", String.valueOf(_toolTip));
588                    request.setAttribute("liferay-ui:icon:url", getProcessedUrl());
589                    request.setAttribute(
590                            "liferay-ui:icon:useDialog", String.valueOf(_useDialog));
591            }
592    
593            private static final String _AUI_PATH = "../aui/";
594    
595            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
596    
597            private static final String _PAGE = "/html/taglib/ui/icon/page.jsp";
598    
599            private String _alt;
600            private String _ariaRole;
601            private String _cssClass;
602            private Map<String, Object> _data;
603            private String _iconCssClass;
604            private String _id;
605            private String _image;
606            private String _imageHover;
607            private Boolean _label;
608            private String _lang;
609            private String _linkCssClass;
610            private boolean _localizeMessage = true;
611            private String _message;
612            private String _method;
613            private String _onClick;
614            private String _src;
615            private String _srcHover;
616            private String _target = "_self";
617            private boolean _toolTip;
618            private String _url;
619            private boolean _useDialog = false;
620    
621    }