001    /**
002     * Copyright (c) 2000-2012 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.ServerDetector;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import javax.servlet.jsp.JspException;
023    import javax.servlet.jsp.JspWriter;
024    import javax.servlet.jsp.tagext.TagSupport;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class MessageTag extends TagSupport {
030    
031            @Override
032            public int doEndTag() throws JspException {
033                    try {
034                            String value = StringPool.BLANK;
035    
036                            if (_arguments == null) {
037                                    if (_unicode) {
038                                            value = UnicodeLanguageUtil.get(pageContext, _key);
039                                    }
040                                    else {
041                                            value = LanguageUtil.get(pageContext, _key);
042                                    }
043                            }
044                            else {
045                                    if (_unicode) {
046                                            value = UnicodeLanguageUtil.format(
047                                                    pageContext, _key, _arguments, _translateArguments);
048                                    }
049                                    else {
050                                            value = LanguageUtil.format(
051                                                    pageContext, _key, _arguments, _translateArguments);
052                                    }
053                            }
054    
055                            JspWriter jspWriter = pageContext.getOut();
056    
057                            jspWriter.write(value);
058    
059                            return EVAL_PAGE;
060                    }
061                    catch (Exception e) {
062                            throw new JspException(e);
063                    }
064                    finally {
065                            if (!ServerDetector.isResin()) {
066                                    _arguments = null;
067                                    _key = null;
068                                    _translateArguments = true;
069                                    _unicode = false;
070                            }
071                    }
072            }
073    
074            public void setArguments(Object argument) {
075                    _arguments = new Object[] {argument};
076            }
077    
078            public void setArguments(Object[] arguments) {
079                    _arguments = arguments;
080            }
081    
082            public void setKey(String key) {
083                    _key = key;
084            }
085    
086            public void setTranslateArguments(boolean translateArguments) {
087                    _translateArguments = translateArguments;
088            }
089    
090            public void setUnicode(boolean unicode) {
091                    _unicode = unicode;
092            }
093    
094            private Object[] _arguments;
095            private String _key;
096            private boolean _translateArguments = true;
097            private boolean _unicode;
098    
099    }