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