001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: //java imports
024:
025: import org.apache.beehive.netui.tags.AbstractSimpleTag;
026: import org.apache.beehive.netui.tags.IAttributeConsumer;
027: import org.apache.beehive.netui.tags.rendering.AbstractAttributeState;
028: import org.apache.beehive.netui.tags.rendering.BaseTag;
029: import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
030: import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
031: import org.apache.beehive.netui.util.Bundle;
032:
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.jsp.JspException;
035: import javax.servlet.jsp.PageContext;
036: import java.io.IOException;
037:
038: /**
039: * Provides the base for every URL on this page.
040: * @jsptagref.tagdescription Provides the base for every URL on the page.
041: * @example In this sample, the Base tag is simply dropped in and then automatically determines
042: * the base for each URL on this page.
043: * <pre>
044: * <head>
045: * <netui:base />
046: * </head>
047: * </pre>
048: * @netui:tag name="base" body-content="scriptless" description="Provides the base for every URL on this page."
049: */
050: public class Base extends AbstractSimpleTag implements
051: IAttributeConsumer, HtmlConstants {
052: private BaseTag.State _state = new BaseTag.State();
053:
054: /**
055: * Returns the name of the Tag.
056: */
057: public String getTagName() {
058: return "Base";
059: }
060:
061: /**
062: * Set the default window target.
063: * @param target the window target.
064: * @jsptagref.attributedescription The default window target.
065: * @jsptagref.databindable false
066: * @jsptagref.attributesyntaxvalue <i>string_windowTarget</i>
067: * @netui:attribute required="false" rtexprvalue="true"
068: * description="The default window target."
069: */
070: public void setTarget(String target) {
071: _state.target = setNonEmptyValueAttribute(target);
072: }
073:
074: /**
075: * Base support for the attribute tag. The <code>href</code> may not bet set.
076: * @param name The name of the attribute. This value may not be null or the empty string.
077: * @param value The value of the attribute. This may contain an expression.
078: * @param facet The name of a facet to which the attribute will be applied. This is optional.
079: * @throws JspException A JspException may be thrown if there is an error setting the attribute.
080: */
081: public void setAttribute(String name, String value, String facet)
082: throws JspException {
083: boolean error = false;
084:
085: // validate the name attribute, in the case of an error simply return.
086: if (name == null || name.length() <= 0) {
087: String s = Bundle.getString("Tags_AttributeNameNotSet");
088: registerTagError(s, null);
089: error = true;
090: }
091: if (facet != null) {
092: String s = Bundle.getString(
093: "Tags_AttributeFacetNotSupported",
094: new Object[] { facet });
095: registerTagError(s, null);
096: error = true;
097: }
098:
099: // it's not legal to set the href attributes this way
100: if (name != null && name.equals(HREF)) {
101: String s = Bundle.getString("Tags_AttributeMayNotBeSet",
102: new Object[] { name });
103: registerTagError(s, null);
104: error = true;
105: }
106: if (error)
107: return;
108:
109: // set the state variables that will override the tag settings...
110: if (name.equals(TARGET)) {
111: _state.target = value;
112: return;
113: }
114:
115: _state.registerAttribute(AbstractAttributeState.ATTR_GENERAL,
116: name, value);
117: }
118:
119: /**
120: * Render the hyperlink.
121: * @throws JspException if a JSP exception has occurred
122: */
123: public void doTag() throws JspException, IOException {
124: PageContext pageContext = getPageContext();
125: HttpServletRequest request = (HttpServletRequest) pageContext
126: .getRequest();
127: TagRenderingBase br = TagRenderingBase.Factory.getRendering(
128: TagRenderingBase.BASE_TAG, request);
129:
130: // evaluate the body, this is called basically so any attributes my be applied.
131: getBufferBody(false);
132:
133: InternalStringBuilder buf = new InternalStringBuilder(64);
134:
135: // calculate the href
136: buf.append(request.getScheme());
137: buf.append("://");
138: buf.append(request.getServerName());
139:
140: String scheme = request.getScheme();
141: int port = request.getServerPort();
142: if ("http".equals(scheme) && (80 == port)) {
143: //Do nothing
144: } else if ("https".equals(scheme) && (443 == port)) {
145: //Do nothing
146: } else {
147: buf.append(":");
148: buf.append(request.getServerPort());
149: }
150: buf.append(request.getRequestURI());
151: _state.href = buf.toString();
152:
153: // render the tag.
154: WriteRenderAppender writer = new WriteRenderAppender(
155: pageContext);
156: br.doStartTag(writer, _state);
157: br.doEndTag(writer);
158:
159: // This will produce invalid HTML/XHTML if there are errors
160: // because we are going to put markup out into the head.
161: if (hasErrors())
162: reportErrors();
163: }
164: }
|