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.servlet.taglib.util.OutputData;
018    import com.liferay.portal.kernel.util.ServerDetector;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    
023    import javax.servlet.ServletRequest;
024    import javax.servlet.jsp.JspException;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class OutputTag extends PositionTagSupport {
030    
031            public static StringBundler getData(
032                    ServletRequest servletRequest, String webKey) {
033    
034                    OutputData outputData = _getOutputData(servletRequest);
035    
036                    return outputData.getMergedData(webKey);
037            }
038    
039            public OutputTag(String stringBundlerKey) {
040                    _webKey = stringBundlerKey;
041            }
042    
043            @Override
044            public int doEndTag() throws JspException {
045                    try {
046                            if (_output) {
047                                    OutputData outputData = _getOutputData(
048                                            pageContext.getRequest());
049    
050                                    outputData.addData(
051                                            _outputKey, _webKey, getBodyContentAsStringBundler());
052                            }
053    
054                            return EVAL_PAGE;
055                    }
056                    catch (Exception e) {
057                            throw new JspException(e);
058                    }
059                    finally {
060                            if (!ServerDetector.isResin()) {
061                                    cleanUp();
062                            }
063                    }
064            }
065    
066            @Override
067            public int doStartTag() {
068                    if (Validator.isNotNull(_outputKey)) {
069                            OutputData outputData = _getOutputData(pageContext.getRequest());
070    
071                            if (!outputData.addOutputKey(_outputKey)) {
072                                    _output = false;
073    
074                                    return SKIP_BODY;
075                            }
076                    }
077    
078                    if (isPositionInLine()) {
079                            _output = false;
080    
081                            return EVAL_BODY_INCLUDE;
082                    }
083    
084                    _output = true;
085    
086                    return EVAL_BODY_BUFFERED;
087            }
088    
089            public void setOutputKey(String outputKey) {
090                    _outputKey = outputKey;
091            }
092    
093            private static OutputData _getOutputData(ServletRequest servletRequest) {
094                    OutputData outputData = (OutputData)servletRequest.getAttribute(
095                            WebKeys.OUTPUT_DATA);
096    
097                    if (outputData == null) {
098                            outputData = new OutputData();
099    
100                            servletRequest.setAttribute(WebKeys.OUTPUT_DATA, outputData);
101                    }
102    
103                    return outputData;
104            }
105    
106            private boolean _output;
107            private String _outputKey;
108            private String _webKey;
109    
110    }