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 * Since OpenCms version 7.0.5, there is a new 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 */
050public class CmsJspTagJQuery extends BodyTagSupport {
051
052    /** File extension constant. */
053    private static final String EXTENSION_CSS = ".css";
054
055    /** File extension constant. */
056    private static final String EXTENSION_JS = ".js";
057
058    /** The log object for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsJspTagJQuery.class);
060
061    /** Serial version UID required for safe serialization. */
062    private static final long serialVersionUID = 3257908962507552558L;
063
064    /** VFS path constant. */
065    private static final String VFS_PATH_CSS = "jquery/css/";
066
067    /** VFS path constant. */
068    private static final String VFS_PATH_JQUERY = "jquery/";
069
070    /** VFS path constant. */
071    private static final String VFS_PATH_LOAD_JS = "jquery/load.js";
072
073    /** VFS path constant. */
074    private static final String VFS_PATH_PACKED = "packed/";
075
076    /** VFS path constant. */
077    private static final String VFS_PATH_UNPACKED = "unpacked/";
078
079    /** The optional css file to include. */
080    protected String m_css;
081
082    /** If the inclusion should be dynamic with js or not. */
083    protected String m_dynamic;
084
085    /** The javascript file to include. */
086    protected String m_js;
087
088    /**
089     * Opens the direct edit tag, if manual mode is set then the next
090     * start HTML for the direct edit buttons is printed to the page.<p>
091     *
092     * @return {@link #EVAL_BODY_INCLUDE}
093     *
094     * @throws JspException in case something goes wrong
095     */
096    @Override
097    public int doStartTag() throws JspException {
098
099        ServletRequest req = pageContext.getRequest();
100
101        // This will always be true if the page is called through OpenCms
102        if (!CmsFlexController.isCmsRequest(req)) {
103            return SKIP_BODY;
104        }
105        if (getJs() == null) {
106            if (isDynamic()) {
107                // in case we want to include the needed js functions
108                try {
109                    pageContext.getOut().print("<script type='text/javascript' src='"
110                        + CmsWorkplace.getSkinUri()
111                        + VFS_PATH_LOAD_JS
112                        + "' ></script>");
113                } catch (Exception ex) {
114                    if (LOG.isErrorEnabled()) {
115                        LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "jquery"), ex);
116                    }
117                    throw new JspException(ex);
118                }
119            }
120            return SKIP_BODY;
121        }
122
123        // get the server prefix
124        CmsObject cms = CmsFlexController.getCmsObject(req);
125
126        // first handle js file
127        String path = VFS_PATH_JQUERY;
128        if (cms.getRequestContext().getCurrentProject().isOnlineProject()) {
129            // online
130            path += VFS_PATH_PACKED;
131        } else {
132            // offline
133            path += VFS_PATH_UNPACKED;
134        }
135        String file = path + getJs() + EXTENSION_JS;
136        try {
137            cms.readResource(CmsWorkplace.VFS_PATH_RESOURCES + file);
138            if (isDynamic()) {
139                pageContext.getOut().print("<script type='text/javascript'>load_script('"
140                    + CmsWorkplace.getSkinUri()
141                    + file
142                    + "', 'js');</script>");
143            } else {
144                pageContext.getOut().print(
145                    "<script type='text/javascript' 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("<script type='text/javascript'>load_script('"
165                    + CmsWorkplace.getSkinUri()
166                    + file
167                    + "', 'css');</script>");
168            } else {
169                pageContext.getOut().print(
170                    "<link href='" + CmsWorkplace.getSkinUri() + file + "' rel='stylesheet' type='text/css' >");
171            }
172        } catch (Exception ex) {
173            if (LOG.isErrorEnabled()) {
174                LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "jquery"), ex);
175            }
176            throw new JspException(ex);
177        }
178        return SKIP_BODY;
179    }
180
181    /**
182     * Returns the optional css file to include.<p>
183     *
184     * @return the optional css file to include
185     */
186    public String getCss() {
187
188        return m_css;
189    }
190
191    /**
192     * Returns the dynamic flag.<p>
193     *
194     * @return the dynamic flag
195     */
196    public String getDynamic() {
197
198        return m_dynamic;
199    }
200
201    /**
202     * Returns the javascript file to include.<p>
203     *
204     * @return the javascript file
205     */
206    public String getJs() {
207
208        return m_js;
209    }
210
211    /**
212     * Releases any resources we may have (or inherit).<p>
213     */
214    @Override
215    public void release() {
216
217        super.release();
218        m_js = null;
219        m_css = null;
220    }
221
222    /**
223     * Sets the optional css file to include.<p>
224     *
225     * @param css the css file to set
226     */
227    public void setCss(String css) {
228
229        m_css = css;
230    }
231
232    /**
233     * Sets the dynamic flag.<p>
234     *
235     * @param dynamic the dynamic flag to set
236     */
237    public void setDynamic(String dynamic) {
238
239        m_dynamic = dynamic;
240    }
241
242    /**
243     * Sets the javascript file to include.<p>
244     *
245     * @param js the javascript file to set
246     */
247    public void setJs(String js) {
248
249        if (js != null) {
250            m_js = js;
251        }
252    }
253
254    /**
255     * Checks if the inclusion is dynamic or not.<p>
256     *
257     * @return <code>true</code> if the inclusion is dynamic
258     */
259    private boolean isDynamic() {
260
261        return Boolean.valueOf(getDynamic()).booleanValue();
262    }
263}