KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagLink


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagLink.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.flex.CmsFlexController;
35 import org.opencms.main.CmsLog;
36 import org.opencms.main.OpenCms;
37 import org.opencms.staticexport.CmsLinkManager;
38
39 import javax.servlet.ServletRequest JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
42
43 import org.apache.commons.logging.Log;
44
45 /**
46  * Implements the <code>&lt;cms:link&gt;[filename]&lt;/cms:link&gt;</code>
47  * tag to add OpenCms managed links to a JSP page, required for link
48  * management and the static
49  * export to work properly.<p>
50  *
51  * @author Alexander Kandzior
52  *
53  * @version $Revision: 1.16 $
54  *
55  * @since 6.0.0
56  */

57 public class CmsJspTagLink extends BodyTagSupport JavaDoc {
58
59     /** Serial version UID required for safe serialization. */
60     private static final long serialVersionUID = -2361021288258405388L;
61
62     /** The log object for this class. */
63     private static final Log LOG = CmsLog.getLog(CmsJspTagLink.class);
64
65     /**
66      * Internal action method.<p>
67      *
68      * Calulates a link using the OpenCms link export rules.<p>
69      *
70      * @param link the link that should be calculated, can be relative or absolute
71      * @param req the current request
72      * @return the calculated link
73      *
74      * @see org.opencms.staticexport.CmsLinkManager#substituteLink(org.opencms.file.CmsObject, String)
75      */

76     public static String JavaDoc linkTagAction(String JavaDoc link, ServletRequest JavaDoc req) {
77
78         CmsFlexController controller = CmsFlexController.getController(req);
79         if (link.indexOf(':') >= 0) {
80             return OpenCms.getLinkManager().substituteLink(controller.getCmsObject(), link);
81         } else {
82             return OpenCms.getLinkManager().substituteLink(
83                 controller.getCmsObject(),
84                 CmsLinkManager.getAbsoluteUri(link, controller.getCurrentRequest().getElementUri()));
85         }
86     }
87
88     /**
89      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
90      * @return EVAL_PAGE
91      * @throws JspException in case soemthing goes wrong
92      */

93     public int doEndTag() throws JspException JavaDoc {
94
95         ServletRequest JavaDoc req = pageContext.getRequest();
96
97         // This will always be true if the page is called through OpenCms
98
if (CmsFlexController.isCmsRequest(req)) {
99             try {
100                 // Get link-string from the body and reset body
101
String JavaDoc link = getBodyContent().getString();
102                 getBodyContent().clear();
103                 // Calculate the link substitution
104
String JavaDoc newlink = linkTagAction(link, req);
105                 // Write the result back to the page
106
getBodyContent().print(newlink);
107                 getBodyContent().writeOut(pageContext.getOut());
108
109             } catch (Exception JavaDoc ex) {
110                 if (LOG.isErrorEnabled()) {
111                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "link"), ex);
112                 }
113                 throw new JspException JavaDoc(ex);
114             }
115         }
116         return EVAL_PAGE;
117     }
118 }
119
Popular Tags