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<String, Object>(_data);
177                    }
178                    else {
179                            data = new HashMap<String, Object>(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,
436                            new String[] {
437                                    StringPool.UNDERLINE, _AUI_PATH
438                            },
439                            new String[] {
440                                    StringPool.DASH, StringPool.BLANK
441                            });
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:message", getProcessedMessage());
586                    request.setAttribute("liferay-ui:icon:method", getMethod());
587                    request.setAttribute("liferay-ui:icon:onClick", getOnClick());
588                    request.setAttribute("liferay-ui:icon:src", getSrc());
589                    request.setAttribute("liferay-ui:icon:srcHover", getSrcHover());
590                    request.setAttribute("liferay-ui:icon:target", _target);
591                    request.setAttribute(
592                            "liferay-ui:icon:toolTip", String.valueOf(_toolTip));
593                    request.setAttribute("liferay-ui:icon:url", getProcessedUrl());
594                    request.setAttribute(
595                            "liferay-ui:icon:useDialog", String.valueOf(_useDialog));
596            }
597    
598            private static final String _AUI_PATH = "../aui/";
599    
600            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
601    
602            private static final String _PAGE = "/html/taglib/ui/icon/page.jsp";
603    
604            private String _alt;
605            private String _ariaRole;
606            private String _cssClass;
607            private Map<String, Object> _data;
608            private String _iconCssClass;
609            private String _id;
610            private String _image;
611            private String _imageHover;
612            private Boolean _label;
613            private String _lang;
614            private String _linkCssClass;
615            private boolean _localizeMessage = true;
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    }