001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp;
029
030import org.opencms.file.CmsObject;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.main.CmsLog;
033import org.opencms.workplace.CmsWorkplace;
034
035import javax.servlet.ServletRequest;
036import javax.servlet.jsp.JspException;
037import javax.servlet.jsp.tagext.BodyTagSupport;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Implementation of the <code>&lt;cms:jquery/&gt;</code> tag.<p>
043 *
044 * OpenCms version 7.0.5 introduced a core module providing jQuery plus some additional plugins.
045 * This tag will include the JQuery javascript library depending on the current project. If the current
046 * Project is offline the unpacked version is used, if online the packed version will be used.
047 *
048 * @since 7.0.5
049 *
050 * @deprecated jQuery should be provided by standard includes directly in the template.
051 * The jQuery version shipping with OpenCms used by this tag is outdated.
052 * This tag is kept only for backward compatibility with old templates.
053 */
054@Deprecated
055public class CmsJspTagJQuery extends BodyTagSupport {
056
057    /** File extension constant. */
058    private static final String EXTENSION_CSS = ".css";
059
060    /** File extension constant. */
061    private static final String EXTENSION_JS = ".js";
062
063    /** The log object for this class. */
064    private static final Log LOG = CmsLog.getLog(CmsJspTagJQuery.class);
065
066    /** Serial version UID required for safe serialization. */
067    private static final long serialVersionUID = 3257908962507552558L;
068
069    /** VFS path constant. */
070    private static final String VFS_PATH_CSS = "jquery/css/";
071
072    /** VFS path constant. */
073    private static final String VFS_PATH_JQUERY = "jquery/";
074
075    /** VFS path constant. */
076    private static final String VFS_PATH_LOAD_JS = "jquery/load.js";
077
078    /** VFS path constant. */
079    private static final String VFS_PATH_PACKED = "packed/";
080
081    /** VFS path constant. */
082    private static final String VFS_PATH_UNPACKED = "unpacked/";
083
084    /** The optional css file to include. */
085    protected String m_css;
086
087    /** If the inclusion should be dynamic with js or not. */
088    protected String m_dynamic;
089
090    /** The javascript file to include. */
091    protected String m_js;
092
093    /**
094     * Opens the direct edit tag, if manual mode is set then the next
095     * start HTML for the direct edit buttons is printed to the page.<p>
096     *
097     * @return {@link #EVAL_BODY_INCLUDE}
098     *
099     * @throws JspException in case something goes wrong
100     */
101    @Override
102    public int doStartTag() throws JspException {
103
104        ServletRequest req = pageContext.getRequest();
105
106        // This will always be true if the page is called through OpenCms
107        if (!CmsFlexController.isCmsRequest(req)) {
108            return SKIP_BODY;
109        }
110        if (getJs() == null) {
111            if (isDynamic()) {
112                // in case we want to include the needed js functions
113                try {
114                    pageContext.getOut().print(
115                        "<script src='" + CmsWorkplace.getSkinUri() + VFS_PATH_LOAD_JS + "' ></script>");
116                } catch (Exception ex) {
117                    if (LOG.isErrorEnabled()) {
118                        LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "jquery"), ex);
119                    }
120                    throw new JspException(ex);
121                }
122            }
123            return SKIP_BODY;
124        }
125
126        // get the server prefix
127        CmsObject cms = CmsFlexController.getCmsObject(req);
128
129        // first handle js file
130        String path = VFS_PATH_JQUERY;
131        if (cms.getRequestContext().getCurrentProject().isOnlineProject()) {
132            // online
133            path += VFS_PATH_PACKED;
134        } else {
135            // offline
136            path += VFS_PATH_UNPACKED;
137        }
138        String file = path + getJs() + EXTENSION_JS;
139        try {
140            cms.readResource(CmsWorkplace.VFS_PATH_RESOURCES + file);
141            if (isDynamic()) {
142                pageContext.getOut().print(
143                    "<script>load_script('" + CmsWorkplace.getSkinUri() + file + "', 'js');</script>");
144            } else {
145                pageContext.getOut().print("<script src='" + CmsWorkplace.getSkinUri() + file + "' ></script>");
146            }
147        } catch (Exception ex) {
148            if (LOG.isErrorEnabled()) {
149                LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "jquery"), ex);
150            }
151            throw new JspException(ex);
152        }
153        if (getCss() == null) {
154            return SKIP_BODY;
155        }
156
157        // now handle css file
158        path = VFS_PATH_CSS;
159        file = path + getCss() + EXTENSION_CSS;
160        try {
161            cms.readResource(CmsWorkplace.VFS_PATH_RESOURCES + file);
162            pageContext.getOut().println();
163            if (isDynamic()) {
164                pageContext.getOut().print(
165                    "<script>load_script('" + CmsWorkplace.getSkinUri() + file + "', 'css');</script>");
166            } else {
167                pageContext.getOut().print(
168                    "<link href='" + CmsWorkplace.getSkinUri() + file + "' rel='stylesheet' type='text/css' >");
169            }
170        } catch (Exception ex) {
171            if (LOG.isErrorEnabled()) {
172                LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "jquery"), ex);
173            }
174            throw new JspException(ex);
175        }
176        return SKIP_BODY;
177    }
178
179    /**
180     * Returns the optional css file to include.<p>
181     *
182     * @return the optional css file to include
183     */
184    public String getCss() {
185
186        return m_css;
187    }
188
189    /**
190     * Returns the dynamic flag.<p>
191     *
192     * @return the dynamic flag
193     */
194    public String getDynamic() {
195
196        return m_dynamic;
197    }
198
199    /**
200     * Returns the javascript file to include.<p>
201     *
202     * @return the javascript file
203     */
204    public String getJs() {
205
206        return m_js;
207    }
208
209    /**
210     * Releases any resources we may have (or inherit).<p>
211     */
212    @Override
213    public void release() {
214
215        super.release();
216        m_js = null;
217        m_css = null;
218    }
219
220    /**
221     * Sets the optional css file to include.<p>
222     *
223     * @param css the css file to set
224     */
225    public void setCss(String css) {
226
227        m_css = css;
228    }
229
230    /**
231     * Sets the dynamic flag.<p>
232     *
233     * @param dynamic the dynamic flag to set
234     */
235    public void setDynamic(String dynamic) {
236
237        m_dynamic = dynamic;
238    }
239
240    /**
241     * Sets the javascript file to include.<p>
242     *
243     * @param js the javascript file to set
244     */
245    public void setJs(String js) {
246
247        if (js != null) {
248            m_js = js;
249        }
250    }
251
252    /**
253     * Checks if the inclusion is dynamic or not.<p>
254     *
255     * @return <code>true</code> if the inclusion is dynamic
256     */
257    private boolean isDynamic() {
258
259        return Boolean.valueOf(getDynamic()).booleanValue();
260    }
261}