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 setMarkupView(String markupView) {
112                    _markupView = markupView;
113            }
114    
115            public void setMessage(String message) {
116                    _message = message;
117            }
118    
119            public void setMethod(String method) {
120                    _method = method;
121            }
122    
123            public void setOnClick(String onClick) {
124                    _onClick = onClick;
125            }
126    
127            public void setSrc(String src) {
128                    _src = src;
129            }
130    
131            public void setSrcHover(String srcHover) {
132                    _srcHover = srcHover;
133            }
134    
135            public void setTarget(String target) {
136                    _target = target;
137            }
138    
139            public void setToolTip(boolean toolTip) {
140                    _toolTip = toolTip;
141            }
142    
143            public void setUrl(String url) {
144                    _url = url;
145            }
146    
147            public void setUseDialog(boolean useDialog) {
148                    _useDialog = useDialog;
149            }
150    
151            @Override
152            protected void cleanUp() {
153                    _alt = null;
154                    _ariaRole = null;
155                    _cssClass = null;
156                    _data = null;
157                    _iconCssClass = null;
158                    _id = null;
159                    _image = null;
160                    _imageHover = null;
161                    _label = null;
162                    _lang = null;
163                    _linkCssClass = null;
164                    _localizeMessage = true;
165                    _markupView = null;
166                    _message = null;
167                    _method = null;
168                    _onClick = null;
169                    _src = null;
170                    _srcHover = null;
171                    _target = null;
172                    _toolTip = false;
173                    _url = null;
174                    _useDialog = false;
175            }
176    
177            protected Map<String, Object> getData() {
178                    Map<String, Object> data = null;
179    
180                    if (_data != null) {
181                            data = new HashMap<>(_data);
182                    }
183                    else {
184                            data = new HashMap<>(1);
185                    }
186    
187                    if (_useDialog && Validator.isNull(data.get("title"))) {
188                            String message = getProcessedMessage();
189    
190                            if (_localizeMessage) {
191                                    message = LanguageUtil.get(request, message);
192                            }
193    
194                            data.put("title", HtmlUtil.stripHtml(message));
195                    }
196    
197                    return data;
198            }
199    
200            protected String getDetails() {
201                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
202                            WebKeys.THEME_DISPLAY);
203    
204                    String details = null;
205    
206                    if (_alt != null) {
207                            details = " alt=\"" + LanguageUtil.get(request, _alt) + "\"";
208                    }
209                    else if (isLabel()) {
210                            details = " alt=\"\"";
211                    }
212                    else {
213                            StringBundler sb = new StringBundler(6);
214    
215                            sb.append(" alt=\"");
216                            sb.append(LanguageUtil.get(request, getProcessedMessage()));
217                            sb.append("\"");
218    
219                            if (_toolTip) {
220                                    sb.append(" onmouseover=\"Liferay.Portal.ToolTip.show(this, '");
221                                    sb.append(
222                                            UnicodeLanguageUtil.get(request, getProcessedMessage()));
223                                    sb.append("')\"");
224                            }
225                            else {
226                                    sb.append(" title=\"");
227                                    sb.append(LanguageUtil.get(request, getProcessedMessage()));
228                                    sb.append("\"");
229                            }
230    
231                            details = sb.toString();
232                    }
233    
234                    if (Validator.isNull(_src) || !themeDisplay.isThemeImagesFastLoad() ||
235                            isAUIImage()) {
236    
237                            return details;
238                    }
239    
240                    SpriteImage spriteImage = null;
241                    String spriteFileName = null;
242                    String spriteFileURL = null;
243    
244                    String imageFileName = StringUtil.replace(_src, "common/../", "");
245    
246                    if (imageFileName.contains(Http.PROTOCOL_DELIMITER)) {
247                            String portalURL = PortalUtil.getPortalURL(request);
248    
249                            if (imageFileName.startsWith(portalURL)) {
250                                    imageFileName = imageFileName.substring(portalURL.length());
251                            }
252                            else {
253                                    URL imageURL = null;
254    
255                                    try {
256                                            imageURL = new URL(imageFileName);
257    
258                                            imageFileName = imageURL.getPath();
259                                    }
260                                    catch (MalformedURLException e) {
261                                    }
262                            }
263                    }
264    
265                    Theme theme = themeDisplay.getTheme();
266    
267                    String contextPath = theme.getContextPath();
268    
269                    String imagesPath = contextPath.concat(theme.getImagesPath());
270    
271                    if (imageFileName.startsWith(imagesPath)) {
272                            spriteImage = theme.getSpriteImage(imageFileName);
273    
274                            if (spriteImage != null) {
275                                    spriteFileName = spriteImage.getSpriteFileName();
276    
277                                    if (BrowserSnifferUtil.isIe(request) &&
278                                            (BrowserSnifferUtil.getMajorVersion(request) < 7)) {
279    
280                                            spriteFileName = StringUtil.replace(
281                                                    spriteFileName, ".png", ".gif");
282                                    }
283    
284                                    String cdnBaseURL = themeDisplay.getCDNBaseURL();
285    
286                                    spriteFileURL = cdnBaseURL.concat(spriteFileName);
287                            }
288                    }
289    
290                    if (spriteImage == null) {
291                            Portlet portlet = (Portlet)request.getAttribute(
292                                    "liferay-portlet:icon_portlet:portlet");
293    
294                            if (portlet == null) {
295                                    portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
296                            }
297    
298                            if (portlet != null) {
299                                    PortletApp portletApp = portlet.getPortletApp();
300    
301                                    spriteImage = portletApp.getSpriteImage(imageFileName);
302    
303                                    if (spriteImage != null) {
304                                            spriteFileName = spriteImage.getSpriteFileName();
305    
306                                            if (BrowserSnifferUtil.isIe(request) &&
307                                                    (BrowserSnifferUtil.getMajorVersion(request) < 7)) {
308    
309                                                    spriteFileName = StringUtil.replace(
310                                                            spriteFileName, ".png", ".gif");
311                                            }
312    
313                                            String cdnBaseURL = themeDisplay.getCDNBaseURL();
314    
315                                            spriteFileURL = cdnBaseURL.concat(spriteFileName);
316                                    }
317                            }
318                    }
319    
320                    if (spriteImage != null) {
321                            String themeImagesPath = themeDisplay.getPathThemeImages();
322    
323                            _src = themeImagesPath.concat("/spacer.png");
324    
325                            StringBundler sb = new StringBundler(10);
326    
327                            sb.append(details);
328                            sb.append(" style=\"background-image: url('");
329                            sb.append(spriteFileURL);
330                            sb.append("'); background-position: 50% -");
331                            sb.append(spriteImage.getOffset());
332                            sb.append("px; background-repeat: no-repeat; height: ");
333                            sb.append(spriteImage.getHeight());
334                            sb.append("px; width: ");
335                            sb.append(spriteImage.getWidth());
336                            sb.append("px;\"");
337    
338                            details = sb.toString();
339                    }
340    
341                    return details;
342            }
343    
344            protected String getId() {
345                    if (Validator.isNotNull(_id)) {
346                            return _id;
347                    }
348    
349                    String id = (String)request.getAttribute("liferay-ui:icon-menu:id");
350    
351                    String message = _message;
352    
353                    if (Validator.isNull(message)) {
354                            message = _image;
355                    }
356    
357                    if (Validator.isNotNull(id) && Validator.isNotNull(message)) {
358                            id = id.concat(StringPool.UNDERLINE).concat(
359                                    FriendlyURLNormalizerUtil.normalize(message));
360    
361                            PortletResponse portletResponse =
362                                    (PortletResponse)request.getAttribute(
363                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
364    
365                            String namespace = StringPool.BLANK;
366    
367                            if (portletResponse != null) {
368                                    namespace = portletResponse.getNamespace();
369                            }
370    
371                            id = PortalUtil.getUniqueElementId(
372                                    getOriginalServletRequest(), namespace, id);
373                    }
374                    else {
375                            id = PortalUtil.generateRandomKey(request, IconTag.class.getName());
376                    }
377    
378                    return id;
379            }
380    
381            protected String getImage() {
382                    return _image;
383            }
384    
385            protected String getMessage() {
386                    return _message;
387            }
388    
389            protected String getMethod() {
390                    if (Validator.isNotNull(_method)) {
391                            return _method;
392                    }
393    
394                    String url = getUrl();
395    
396                    if (url.contains("p_p_lifecycle=0")) {
397                            return "get";
398                    }
399    
400                    return "post";
401            }
402    
403            protected String getOnClick() {
404                    String onClick = StringPool.BLANK;
405    
406                    if (Validator.isNotNull(_onClick)) {
407                            onClick = _onClick;
408    
409                            if (!onClick.endsWith(StringPool.SEMICOLON)) {
410                                    onClick = onClick + StringPool.SEMICOLON;
411                            }
412                    }
413    
414                    if (isForcePost()) {
415                            StringBundler sb = new StringBundler(8);
416    
417                            sb.append("event.preventDefault();");
418                            sb.append(onClick);
419                            sb.append("submitForm(document.hrefFm, '");
420                            sb.append(getUrl());
421                            sb.append("')");
422    
423                            onClick = sb.toString();
424                    }
425    
426                    return onClick;
427            }
428    
429            @Override
430            protected String getPage() {
431                    return "/html/taglib/ui/icon/page.jsp";
432            }
433    
434            protected String getProcessedMessage() {
435                    if (_message != null) {
436                            return _message;
437                    }
438    
439                    return StringUtil.replace(
440                            _image, new String[] {StringPool.UNDERLINE, _AUI_PATH},
441                            new String[] {StringPool.DASH, StringPool.BLANK});
442            }
443    
444            protected String getProcessedUrl() {
445                    if (isForcePost()) {
446                            return "javascript:;";
447                    }
448    
449                    return _url;
450            }
451    
452            protected String getSrc() {
453                    if (Validator.isNotNull(_src)) {
454                            return _src;
455                    }
456    
457                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
458                            WebKeys.THEME_DISPLAY);
459    
460                    if (isAUIImage()) {
461                            return themeDisplay.getPathThemeImages().concat("/spacer.png");
462                    }
463                    else if (Validator.isNotNull(_image)) {
464                            StringBundler sb = new StringBundler(4);
465    
466                            sb.append(themeDisplay.getPathThemeImages());
467                            sb.append("/common/");
468                            sb.append(_image);
469                            sb.append(".png");
470    
471                            return StringUtil.replace(sb.toString(), "common/../", "");
472                    }
473    
474                    return StringPool.BLANK;
475            }
476    
477            protected String getSrcHover() {
478                    if (Validator.isNotNull(_srcHover) || Validator.isNull(_imageHover)) {
479                            return _srcHover;
480                    }
481    
482                    StringBundler sb = new StringBundler(4);
483    
484                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
485                            WebKeys.THEME_DISPLAY);
486    
487                    sb.append(themeDisplay.getPathThemeImages());
488    
489                    sb.append("/common/");
490                    sb.append(_imageHover);
491                    sb.append(".png");
492    
493                    return sb.toString();
494            }
495    
496            protected String getUrl() {
497                    return GetterUtil.getString(_url);
498            }
499    
500            protected boolean isAUIImage() {
501                    if ((_image != null) && _image.startsWith(_AUI_PATH)) {
502                            return true;
503                    }
504    
505                    return false;
506            }
507    
508            @Override
509            protected boolean isCleanUpSetAttributes() {
510                    return _CLEAN_UP_SET_ATTRIBUTES;
511            }
512    
513            protected boolean isForcePost() {
514                    String method = getMethod();
515    
516                    if (method.equals("post")) {
517                            String url = getUrl();
518    
519                            if (url.startsWith(Http.HTTP_WITH_SLASH) ||
520                                    url.startsWith(Http.HTTPS_WITH_SLASH)) {
521    
522                                    return true;
523                            }
524                    }
525    
526                    return false;
527            }
528    
529            protected boolean isLabel() {
530                    if (_label != null) {
531                            return _label;
532                    }
533    
534                    IntegerWrapper iconListIconCount = (IntegerWrapper)request.getAttribute(
535                            "liferay-ui:icon-list:icon-count");
536    
537                    Boolean iconListSingleIcon = (Boolean)request.getAttribute(
538                            "liferay-ui:icon-list:single-icon");
539    
540                    if ((iconListIconCount != null) || (iconListSingleIcon != null)) {
541                            _label = true;
542    
543                            return _label;
544                    }
545    
546                    IntegerWrapper iconMenuIconCount = (IntegerWrapper)request.getAttribute(
547                            "liferay-ui:icon-menu:icon-count");
548    
549                    Boolean iconMenuSingleIcon = (Boolean)request.getAttribute(
550                            "liferay-ui:icon-menu:single-icon");
551    
552                    if ((iconMenuIconCount != null) || (iconMenuSingleIcon != null)) {
553                            _label = true;
554    
555                            return _label;
556                    }
557    
558                    _label = false;
559    
560                    return _label;
561            }
562    
563            @Override
564            protected void setAttributes(HttpServletRequest request) {
565                    request.setAttribute("liferay-ui:icon:alt", _alt);
566                    request.setAttribute("liferay-ui:icon:ariaRole", _ariaRole);
567                    request.setAttribute(
568                            "liferay-ui:icon:auiImage", String.valueOf(isAUIImage()));
569                    request.setAttribute("liferay-ui:icon:cssClass", _cssClass);
570                    request.setAttribute("liferay-ui:icon:data", getData());
571                    request.setAttribute("liferay-ui:icon:details", getDetails());
572                    request.setAttribute("liferay-ui:icon:iconCssClass", _iconCssClass);
573                    request.setAttribute("liferay-ui:icon:id", getId());
574                    request.setAttribute("liferay-ui:icon:image", _image);
575                    request.setAttribute("liferay-ui:icon:imageHover", _imageHover);
576                    request.setAttribute(
577                            "liferay-ui:icon:forcePost", String.valueOf(isForcePost()));
578                    request.setAttribute(
579                            "liferay-ui:icon:label", String.valueOf(isLabel()));
580                    request.setAttribute("liferay-ui:icon:lang", _lang);
581                    request.setAttribute("liferay-ui:icon:linkCssClass", _linkCssClass);
582                    request.setAttribute(
583                            "liferay-ui:icon:localizeMessage",
584                            String.valueOf(_localizeMessage));
585                    request.setAttribute("liferay-ui:icon:markupView", _markupView);
586                    request.setAttribute("liferay-ui:icon:message", getProcessedMessage());
587                    request.setAttribute("liferay-ui:icon:method", getMethod());
588                    request.setAttribute("liferay-ui:icon:onClick", getOnClick());
589                    request.setAttribute("liferay-ui:icon:src", getSrc());
590                    request.setAttribute("liferay-ui:icon:srcHover", getSrcHover());
591                    request.setAttribute("liferay-ui:icon:target", _target);
592                    request.setAttribute(
593                            "liferay-ui:icon:toolTip", String.valueOf(_toolTip));
594                    request.setAttribute("liferay-ui:icon:url", getProcessedUrl());
595                    request.setAttribute(
596                            "liferay-ui:icon:useDialog", String.valueOf(_useDialog));
597            }
598    
599            private static final String _AUI_PATH = "../aui/";
600    
601            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
602    
603            private String _alt;
604            private String _ariaRole;
605            private String _cssClass;
606            private Map<String, Object> _data;
607            private String _iconCssClass;
608            private String _id;
609            private String _image;
610            private String _imageHover;
611            private Boolean _label;
612            private String _lang;
613            private String _linkCssClass;
614            private boolean _localizeMessage = true;
615            private String _markupView;
616            private String _message;
617            private String _method;
618            private String _onClick;
619            private String _src;
620            private String _srcHover;
621            private String _target = "_self";
622            private boolean _toolTip;
623            private String _url;
624            private boolean _useDialog = false;
625    
626    }