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.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    }