001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 (_unicode) {
051                                            value = UnicodeLanguageUtil.get(pageContext, _key);
052                                    }
053                                    else {
054                                            value = LanguageUtil.get(pageContext, _key);
055                                    }
056                            }
057                            else {
058                                    if (_unicode) {
059                                            value = UnicodeLanguageUtil.format(
060                                                    pageContext, _key, _arguments, _translateArguments);
061                                    }
062                                    else {
063                                            value = LanguageUtil.format(
064                                                    pageContext, _key, _arguments, _translateArguments);
065                                    }
066                            }
067    
068                            JspWriter jspWriter = pageContext.getOut();
069    
070                            jspWriter.write(value);
071    
072                            return EVAL_PAGE;
073                    }
074                    catch (Exception e) {
075                            throw new JspException(e);
076                    }
077                    finally {
078                            if (!ServerDetector.isResin()) {
079                                    _arguments = null;
080                                    _key = null;
081                                    _translateArguments = true;
082                                    _unicode = false;
083                            }
084                    }
085            }
086    
087            public void setArguments(Object argument) {
088                    _arguments = new Object[] {argument};
089            }
090    
091            public void setArguments(Object[] arguments) {
092                    _arguments = arguments;
093            }
094    
095            public void setKey(String key) {
096                    _key = key;
097            }
098    
099            public void setTranslateArguments(boolean translateArguments) {
100                    _translateArguments = translateArguments;
101            }
102    
103            public void setUnicode(boolean unicode) {
104                    _unicode = unicode;
105            }
106    
107            private Object[] _arguments;
108            private String _key;
109            private boolean _translateArguments = true;
110            private boolean _unicode;
111    
112    }