001    /**
002     * Copyright (c) 2000-2011 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.util;
016    
017    import com.liferay.portal.kernel.util.ServerDetector;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.util.HashSet;
022    import java.util.Set;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.JspWriter;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class OutputTag extends PositionTagSupport {
032    
033            public OutputTag(String stringBundlerKey) {
034                    _webKey = stringBundlerKey;
035            }
036    
037            @Override
038            public int doEndTag() throws JspException {
039                    try {
040                            if (!_output) {
041                                    return EVAL_PAGE;
042                            }
043    
044                            if (isPositionInLine()) {
045                                    JspWriter jspWriter = pageContext.getOut();
046    
047                                    StringBundler bodyContent = getBodyContentAsStringBundler();
048    
049                                    jspWriter.write(bodyContent.toString());
050                            }
051                            else {
052                                    HttpServletRequest request =
053                                            (HttpServletRequest)pageContext.getRequest();
054    
055                                    StringBundler sb = (StringBundler)request.getAttribute(_webKey);
056    
057                                    if (sb == null) {
058                                            sb = new StringBundler();
059    
060                                            request.setAttribute(_webKey, sb);
061                                    }
062    
063                                    sb.append(getBodyContentAsStringBundler());
064                            }
065    
066                            return EVAL_PAGE;
067                    }
068                    catch (Exception e) {
069                            throw new JspException(e);
070                    }
071                    finally {
072                            if (!ServerDetector.isResin()) {
073                                    cleanUp();
074                            }
075                    }
076            }
077    
078            @Override
079            public int doStartTag() {
080                    if (Validator.isNotNull(_outputKey)) {
081                            Set<String> outputKeys = getOutputKeys();
082    
083                            if (!outputKeys.add(_outputKey)) {
084                                    _output = false;
085    
086                                    return SKIP_BODY;
087                            }
088                    }
089    
090                    _output = true;
091    
092                    return EVAL_BODY_BUFFERED;
093            }
094    
095            public void setOutputKey(String outputKey) {
096                    _outputKey = outputKey;
097            }
098    
099            protected Set<String> getOutputKeys() {
100                    HttpServletRequest request =
101                            (HttpServletRequest)pageContext.getRequest();
102    
103                    Set<String> outputKeys = (Set<String>)request.getAttribute(
104                            OutputTag.class.getName());
105    
106                    if (outputKeys == null) {
107                            outputKeys = new HashSet<String>();
108    
109                            request.setAttribute(OutputTag.class.getName(), outputKeys);
110                    }
111    
112                    return outputKeys;
113            }
114    
115            private boolean _output;
116            private String _outputKey;
117            private String _webKey;
118    
119    }