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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.ServerDetector;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.taglib.util.TagResourceBundleUtil;
026    
027    import java.util.ResourceBundle;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.jsp.JspException;
031    import javax.servlet.jsp.JspWriter;
032    import javax.servlet.jsp.tagext.TagSupport;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class MessageTag extends TagSupport {
038    
039            @Override
040            public int doEndTag() throws JspException {
041                    try {
042                            String value = StringPool.BLANK;
043    
044                            HttpServletRequest request =
045                                    (HttpServletRequest)pageContext.getRequest();
046    
047                            boolean unicode = GetterUtil.getBoolean(
048                                    request.getAttribute(WebKeys.JAVASCRIPT_CONTEXT));
049    
050                            if (unicode) {
051                                    _unicode = unicode;
052                            }
053    
054                            ResourceBundle resourceBundle =
055                                    TagResourceBundleUtil.getResourceBundle(pageContext);
056    
057                            if (_arguments == null) {
058                                    if (!_localizeKey) {
059                                            value = _key;
060                                    }
061                                    else if (_escape) {
062                                            value = HtmlUtil.escape(
063                                                    LanguageUtil.get(resourceBundle, _key));
064                                    }
065                                    else if (_escapeAttribute) {
066                                            value = HtmlUtil.escapeAttribute(
067                                                    LanguageUtil.get(resourceBundle, _key));
068                                    }
069                                    else if (_unicode) {
070                                            value = UnicodeLanguageUtil.get(resourceBundle, _key);
071                                    }
072                                    else {
073                                            value = LanguageUtil.get(resourceBundle, _key);
074                                    }
075                            }
076                            else {
077                                    if (_unicode) {
078                                            value = UnicodeLanguageUtil.format(
079                                                    resourceBundle, _key, _arguments, _translateArguments);
080                                    }
081                                    else {
082                                            value = LanguageUtil.format(
083                                                    resourceBundle, _key, _arguments, _translateArguments);
084                                    }
085                            }
086    
087                            if (Validator.isNotNull(value)) {
088                                    JspWriter jspWriter = pageContext.getOut();
089    
090                                    jspWriter.write(value);
091                            }
092    
093                            return EVAL_PAGE;
094                    }
095                    catch (Exception e) {
096                            throw new JspException(e);
097                    }
098                    finally {
099                            if (!ServerDetector.isResin()) {
100                                    _arguments = null;
101                                    _escape = false;
102                                    _escapeAttribute = false;
103                                    _key = null;
104                                    _localizeKey = true;
105                                    _translateArguments = true;
106                                    _unicode = false;
107                            }
108                    }
109            }
110    
111            public void setArguments(Object argument) {
112                    if (argument == null) {
113                            _arguments = null;
114    
115                            return;
116                    }
117    
118                    Class<?> clazz = argument.getClass();
119    
120                    if (clazz.isArray()) {
121                            _arguments = (Object[])argument;
122                    }
123                    else {
124                            _arguments = new Object[] {argument};
125                    }
126            }
127    
128            public void setEscape(boolean escape) {
129                    _escape = escape;
130            }
131    
132            public void setEscapeAttribute(boolean escapeAttribute) {
133                    _escapeAttribute = escapeAttribute;
134            }
135    
136            public void setKey(String key) {
137                    _key = key;
138            }
139    
140            public void setLocalizeKey(boolean localizeKey) {
141                    _localizeKey = localizeKey;
142            }
143    
144            public void setTranslateArguments(boolean translateArguments) {
145                    _translateArguments = translateArguments;
146            }
147    
148            public void setUnicode(boolean unicode) {
149                    _unicode = unicode;
150            }
151    
152            private Object[] _arguments;
153            private boolean _escape;
154            private boolean _escapeAttribute;
155            private String _key;
156            private boolean _localizeKey = true;
157            private boolean _translateArguments = true;
158            private boolean _unicode;
159    
160    }