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                            JspWriter jspWriter = pageContext.getOut();
072    
073                            jspWriter.write(value);
074    
075                            return EVAL_PAGE;
076                    }
077                    catch (Exception e) {
078                            throw new JspException(e);
079                    }
080                    finally {
081                            if (!ServerDetector.isResin()) {
082                                    _arguments = null;
083                                    _key = null;
084                                    _localizeKey = true;
085                                    _translateArguments = true;
086                                    _unicode = false;
087                            }
088                    }
089            }
090    
091            public void setArguments(Object argument) {
092                    if (argument == null) {
093                            _arguments = null;
094    
095                            return;
096                    }
097    
098                    Class<?> clazz = argument.getClass();
099    
100                    if (clazz.isArray()) {
101                            _arguments = (Object[])argument;
102                    }
103                    else {
104                            _arguments = new Object[] {argument};
105                    }
106            }
107    
108            public void setKey(String key) {
109                    _key = key;
110            }
111    
112            public void setLocalizeKey(boolean localizeKey) {
113                    _localizeKey = localizeKey;
114            }
115    
116            public void setTranslateArguments(boolean translateArguments) {
117                    _translateArguments = translateArguments;
118            }
119    
120            public void setUnicode(boolean unicode) {
121                    _unicode = unicode;
122            }
123    
124            private Object[] _arguments;
125            private String _key;
126            private boolean _localizeKey = true;
127            private boolean _translateArguments = true;
128            private boolean _unicode;
129    
130    }