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