KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > HeadTag


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import java.io.IOException JavaDoc;
27 import com.methodhead.util.ServletUtils;
28 import com.methodhead.sitecontext.SiteContext;
29 import com.methodhead.shim.ShimUtils;
30
31 public class HeadTag
32 extends
33   TagSupport JavaDoc {
34
35   // constructors /////////////////////////////////////////////////////////////
36

37   // constants ////////////////////////////////////////////////////////////////
38

39   // classes //////////////////////////////////////////////////////////////////
40

41   // methods //////////////////////////////////////////////////////////////////
42

43   public int doStartTag()
44   throws
45     JspException JavaDoc {
46
47     try {
48
49       //
50
// defining panels?
51
//
52
if ( pageContext.getRequest().getAttribute(
53              ShimGlobals.PANELMAP_KEY ) != null )
54         return SKIP_BODY;
55
56       //
57
// get some things we need
58
//
59
HttpServletRequest JavaDoc request = ( HttpServletRequest JavaDoc )pageContext.getRequest();
60
61       Page page = ( Page )pageContext.getRequest().getAttribute( ShimGlobals.PAGE_KEY );
62
63       String JavaDoc mode = ( String JavaDoc )pageContext.getSession().getAttribute( ShimGlobals.MODE_KEY );
64
65       SiteContext siteContext = SiteContext.getContext( request );
66
67       String JavaDoc siteContextPath = siteContext.getString( "path" );
68       if ( !siteContextPath.equals( "" ) ) {
69         siteContextPath = "/" + siteContextPath;
70       }
71
72       //
73
// start printing
74
//
75
pageContext.getOut().println( "<head>" );
76
77       //
78
// print base tag
79
//
80
if ( ShimGlobals.MODE_EDIT.equals( mode ) || ShimGlobals.MODE_RICHEDIT.equals( mode ) ) {
81
82         //
83
// if we're logged in and editing, use a base url composed from the
84
// site context; otherwise requests will go to the wrong site if we
85
// switch sites
86
//
87
pageContext.getOut().println( "<base HREF=\"http://" + siteContext.getDomains().get( 0 ) + ":" + request.getServerPort() + request.getContextPath() + siteContextPath + "/\"/>" );
88       }
89       else {
90
91         //
92
// otherwise construct a base url from the request URL to keep requests consistent
93
//
94
pageContext.getOut().println( "<base HREF=\"" + ServletUtils.getBaseUrl( request ) + siteContextPath + "/\"/>" );
95       }
96
97       //
98
// print alttitle if it's set
99
//
100
if ( !"".equals( page.getString( "alttitle" ) ) )
101         pageContext.getOut().println(
102           "<title>" + page.getString( "alttitle" ) + "</title>" );
103       else
104         pageContext.getOut().println(
105           "<title>" + page.getString( "title" ) + "</title>" );
106
107       //
108
// print meta tags if they're set
109
//
110
if ( !"".equals( page.getString( "metadescription" ) ) )
111         pageContext.getOut().println(
112           "<meta name=\"description\" content=\"" +
113           page.getString( "metadescription" ) + "\"/>" );
114
115       if ( !"".equals( page.getString( "metakeywords" ) ) )
116         pageContext.getOut().println(
117           "<meta name=\"keywords\" content=\"" +
118           page.getString( "metakeywords" ) + "\"/>" );
119     }
120     catch ( IOException JavaDoc e ) {
121       throw new JspException JavaDoc(
122         "Unexpected IOException: " + e.toString() );
123     }
124
125     return EVAL_BODY_INCLUDE;
126   }
127
128   public int doEndTag()
129   throws
130     JspException JavaDoc {
131
132     try {
133       pageContext.getOut().println( "</head>" );
134     }
135     catch ( IOException JavaDoc e ) {
136       throw new JspException JavaDoc(
137         "Unexpected IOException: " + e.toString() );
138     }
139
140     return EVAL_PAGE;
141   }
142
143   // properties ///////////////////////////////////////////////////////////////
144

145   // attributes ///////////////////////////////////////////////////////////////
146
}
147
Popular Tags