KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Base


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 //java imports
23

24 import org.apache.beehive.netui.tags.AbstractSimpleTag;
25 import org.apache.beehive.netui.tags.IAttributeConsumer;
26 import org.apache.beehive.netui.tags.rendering.AbstractAttributeState;
27 import org.apache.beehive.netui.tags.rendering.BaseTag;
28 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
29 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
30 import org.apache.beehive.netui.util.Bundle;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.PageContext JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 /**
38  * Provides the base for every URL on this page.
39  * @jsptagref.tagdescription Provides the base for every URL on the page.
40  * @example In this sample, the Base tag is simply dropped in and then automatically determines
41  * the base for each URL on this page.
42  * <pre>
43  * &lt;head>
44  * &lt;netui:base />
45  * &lt;/head>
46  * </pre>
47  * @netui:tag name="base" body-content="scriptless" description="Provides the base for every URL on this page."
48  */

49 public class Base extends AbstractSimpleTag
50         implements IAttributeConsumer, HtmlConstants
51 {
52     private BaseTag.State _state = new BaseTag.State();
53
54     /**
55      * Returns the name of the Tag.
56      */

57     public String JavaDoc getTagName()
58     {
59         return "Base";
60     }
61
62     /**
63      * Set the default window target.
64      * @param target the window target.
65      * @jsptagref.attributedescription The default window target.
66      * @jsptagref.databindable false
67      * @jsptagref.attributesyntaxvalue <i>string_windowTarget</i>
68      * @netui:attribute required="false" rtexprvalue="true"
69      * description="The default window target."
70      */

71     public void setTarget(String JavaDoc target)
72     {
73         _state.target = setNonEmptyValueAttribute(target);
74     }
75
76     /**
77      * Base support for the attribute tag. The <code>href</code> may not bet set.
78      * @param name The name of the attribute. This value may not be null or the empty string.
79      * @param value The value of the attribute. This may contain an expression.
80      * @param facet The name of a facet to which the attribute will be applied. This is optional.
81      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
82      */

83     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
84             throws JspException JavaDoc
85     {
86         boolean error = false;
87
88         // validate the name attribute, in the case of an error simply return.
89
if (name == null || name.length() <= 0) {
90             String JavaDoc s = Bundle.getString("Tags_AttributeNameNotSet");
91             registerTagError(s, null);
92             error = true;
93         }
94         if (facet != null) {
95             String JavaDoc s = Bundle.getString("Tags_AttributeFacetNotSupported", new Object JavaDoc[]{facet});
96             registerTagError(s, null);
97             error = true;
98         }
99
100         // it's not legal to set the href attributes this way
101
if (name != null && name.equals(HREF)) {
102             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
103             registerTagError(s, null);
104         }
105         if (error)
106             return;
107
108         // set the state variables that will override the tag settings...
109
if (name.equals(TARGET)) {
110             _state.target = value;
111             return;
112         }
113         else if (name.equals(HREF)) {
114             _state.href = value;
115             return;
116         }
117
118         _state.registerAttribute(AbstractAttributeState.ATTR_GENERAL, name, value);
119     }
120
121     /**
122      * Render the hyperlink.
123      * @throws JspException if a JSP exception has occurred
124      */

125     public void doTag()
126             throws JspException JavaDoc, IOException JavaDoc
127     {
128         PageContext JavaDoc pageContext = getPageContext();
129         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
130         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.BASE_TAG, request);
131
132         // evaluate the body, this is called basically so any attributes my be applied.
133
getBufferBody(false);
134
135         InternalStringBuilder buf = new InternalStringBuilder(64);
136
137         // calculate the href
138
buf.append(request.getScheme());
139         buf.append("://");
140         buf.append(request.getServerName());
141
142         String JavaDoc scheme = request.getScheme();
143         int port = request.getServerPort();
144         if ("http".equals(scheme) && (80 == port)) {
145             //Do nothing
146
}
147         else if ("https".equals(scheme) && (443 == port)) {
148             //Do nothing
149
}
150         else {
151             buf.append(":");
152             buf.append(request.getServerPort());
153         }
154         buf.append(request.getRequestURI());
155         _state.href = buf.toString();
156
157         // render the tag.
158
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
159         br.doStartTag(writer, _state);
160         br.doEndTag(writer);
161
162         // This will produce invalid HTML/XHTML if there are errors
163
// because we are going to put markup out into the head.
164
if (hasErrors())
165             reportErrors();
166     }
167 }
168
169
170
171
Popular Tags