001    /**
002     * Copyright (c) 2000-present 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.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    
024    import javax.servlet.ServletRequest;
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 static StringBundler getData(
034                    ServletRequest servletRequest, String webKey) {
035    
036                    OutputData outputData = _getOutputData(servletRequest);
037    
038                    return outputData.getMergedData(webKey);
039            }
040    
041            public OutputTag(String stringBundlerKey) {
042                    _webKey = stringBundlerKey;
043            }
044    
045            @Override
046            public int doEndTag() throws JspException {
047                    try {
048                            if (_output) {
049                                    if (isPositionInLine()) {
050                                            StringBundler replaceSb = new StringBundler(3);
051    
052                                            replaceSb.append("<script data-outputkey=\"");
053                                            replaceSb.append(_outputKey);
054                                            replaceSb.append("\" ");
055    
056                                            String bodyContentString =
057                                                    getBodyContentAsStringBundler().toString();
058    
059                                            bodyContentString = StringUtil.replace(
060                                                    bodyContentString, "<script", replaceSb.toString());
061    
062                                            JspWriter jspWriter = pageContext.getOut();
063    
064                                            jspWriter.write(bodyContentString);
065                                    }
066                                    else {
067                                            OutputData outputData = _getOutputData(
068                                                    pageContext.getRequest());
069    
070                                            outputData.addData(
071                                                    _outputKey, _webKey, getBodyContentAsStringBundler());
072                                    }
073                            }
074    
075                            return EVAL_PAGE;
076                    }
077                    catch (Exception e) {
078                            throw new JspException(e);
079                    }
080                    finally {
081                            if (!ServerDetector.isResin()) {
082                                    cleanUp();
083                            }
084                    }
085            }
086    
087            @Override
088            public int doStartTag() {
089                    if (Validator.isNotNull(_outputKey)) {
090                            OutputData outputData = _getOutputData(pageContext.getRequest());
091    
092                            if (!outputData.addOutputKey(_outputKey)) {
093                                    _output = false;
094    
095                                    return SKIP_BODY;
096                            }
097                    }
098    
099                    _output = true;
100    
101                    return EVAL_BODY_BUFFERED;
102            }
103    
104            public void setOutputKey(String outputKey) {
105                    _outputKey = outputKey;
106            }
107    
108            private static OutputData _getOutputData(ServletRequest servletRequest) {
109                    OutputData outputData = (OutputData)servletRequest.getAttribute(
110                            WebKeys.OUTPUT_DATA);
111    
112                    if (outputData == null) {
113                            outputData = new OutputData();
114    
115                            servletRequest.setAttribute(WebKeys.OUTPUT_DATA, outputData);
116                    }
117    
118                    return outputData;
119            }
120    
121            private boolean _output;
122            private String _outputKey;
123            private final String _webKey;
124    
125    }