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.tags.internal.PageFlowTagUtils;
022: import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
023: import org.apache.beehive.netui.tags.rendering.ImageTag;
024: import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
025: import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
026: import org.apache.beehive.netui.util.Bundle;
027: import org.apache.beehive.netui.util.ParamHelper;
028:
029: import javax.servlet.ServletRequest;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.jsp.JspException;
033: import java.net.URISyntaxException;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: /**
038: * Generates an image with the specified attributes. Image ignores its
039: * body content.
040: * @jsptagref.tagdescription Renders an HTML <img> tag with specified attributes.
041: * @example In this sample, an Image shows "friends.jpg" at 150 x 175 pixels, with the id "Friends".
042: * <pre><netui:image src="friends.jpg" tagId="Friends" height="150" width="175" /></pre>
043: * @netui:tag name="image" description="Places an image file type on your page."
044: */
045: public class Image extends HtmlBaseTag implements IUrlParams {
046: private ImageTag.State _state = new ImageTag.State();
047:
048: private String _location = null; // The location hash to append to the url.
049: private Map _params;
050:
051: /**
052: * Return the name of the Tag.
053: */
054: public String getTagName() {
055: return "Image";
056: }
057:
058: /**
059: * This method will return the state associated with the tag. This is used by this
060: * base class to access the individual state objects created by the tags.
061: * @return a subclass of the <code>AbstractHtmlState</code> class.
062: */
063: protected AbstractHtmlState getState() {
064: return _state;
065: }
066:
067: /**
068: * Base support for the attribute tag. This is overridden to prevent setting the <code>src</code>
069: * attribute.
070: * @param name The name of the attribute. This value may not be null or the empty string.
071: * @param value The value of the attribute. This may contain an expression.
072: * @param facet The name of a facet to which the attribute will be applied. This is optional.
073: * @throws JspException A JspException may be thrown if there is an error setting the attribute.
074: */
075: public void setAttribute(String name, String value, String facet)
076: throws JspException {
077: if (name != null && name.equals(SRC)) {
078: String s = Bundle.getString("Tags_AttributeMayNotBeSet",
079: new Object[] { name });
080: registerTagError(s, null);
081: }
082: super .setAttribute(name, value, facet);
083: }
084:
085: /**
086: * Sets the property to specify where to align the image.
087: * @param align the image alignment.
088: * @jsptagref.attributedescription The alignment of the image.
089: * @jsptagref.databindable false
090: * @jsptagref.attributesyntaxvalue <i>string_align</i>
091: * @netui:attribute required="false" rtexprvalue="true"
092: * description="The alignment of the image."
093: */
094: public void setAlign(String align) {
095: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALIGN,
096: align);
097: }
098:
099: /**
100: * Sets the property to specify the alt text of the image.
101: * @param alt the image alignment.
102: * @jsptagref.attributedescription The alternative text of the image
103: * @jsptagref.databindable Read Only
104: * @jsptagref.attributesyntaxvalue <i>string_alt</i>
105: * @netui:attribute required="false" rtexprvalue="true"
106: * description="The alternative text of the image."
107: */
108: public void setAlt(String alt) {
109: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT,
110: alt, true);
111: }
112:
113: /**
114: * Sets the property to specify a link to the the long description to supplement
115: * the short description in the <code>alt</code> attribute.
116: * @param longdesc the longdesc.
117: * @jsptagref.attributedescription Specifies a link to the the long description.
118: * @jsptagref.databindable false
119: * @jsptagref.attributesyntaxvalue <i>string_longdesc</i>
120: * @netui:attribute required="false" rtexprvalue="true"
121: * description="Specifies a link to the the long description."
122: */
123: public void setLongdesc(String longdesc) {
124: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
125: LONGDESC, longdesc);
126: }
127:
128: /**
129: * Sets the border size around the image.
130: * @param border the border size.
131: * @jsptagref.attributedescription The border size around the image
132: * @jsptagref.databindable false
133: * @jsptagref.attributesyntaxvalue <i>integer_pixelBorder</i>
134: * @netui:attribute required="false" rtexprvalue="true"
135: * description="The border size around the image."
136: */
137: public void setBorder(String border) {
138: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
139: BORDER, border);
140: }
141:
142: /**
143: * Sets the image height.
144: * @param height the height.
145: * @jsptagref.attributedescription The image height
146: * @jsptagref.databindable Read Only
147: * @jsptagref.attributesyntaxvalue <i>integer_height</i>
148: * @netui:attribute required="false" rtexprvalue="true"
149: * description="The image height."
150: */
151: public void setHeight(String height) {
152: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
153: HEIGHT, height);
154: }
155:
156: /**
157: * Sets the the horizontal spacing around the image.
158: * @param hspace the horizontal spacing.
159: * @jsptagref.attributedescription The horizontal spacing around the image.
160: * @jsptagref.databindable Read Only
161: * @jsptagref.attributesyntaxvalue <i>integer_hspace</i>
162: * @netui:attribute required="false" rtexprvalue="true"
163: * description="The horizontal spacing around the image."
164: */
165: public void setHspace(String hspace) {
166: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
167: HSPACE, hspace);
168: }
169:
170: /**
171: * Sets the server-side image map declaration.
172: * @param ismap the image map declaration.
173: * @jsptagref.attributedescription The server-side map declaration.
174: * @jsptagref.databindable false
175: * @jsptagref.attributesyntaxvalue <i>string_isMap</i>
176: * @netui:attribute required="false" rtexprvalue="true"
177: * description="The server-side map declaration."
178: */
179: public void setIsmap(String ismap) {
180: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ISMAP,
181: ismap);
182: }
183:
184: /**
185: * Sets the location hash to append to the url.
186: * @param location the location hash.
187: * @jsptagref.attributedescription The location hash to append to the URL.
188: * @jsptagref.databindable false
189: * @jsptagref.attributesyntaxvalue <i>string_location</i>
190: * @netui:attribute required="false" rtexprvalue="true"
191: * description="The location hash to append to the URL."
192: */
193: public void setLocation(String location) {
194: _location = location;
195: }
196:
197: /**
198: * Sets the image source URI.
199: * @param src the source URI.
200: * @jsptagref.attributedescription The image source URI
201: * @jsptagref.databindable Read Only
202: * @jsptagref.attributesyntaxvalue <i>string_src</i>
203: * @netui:attribute required="false" rtexprvalue="true"
204: * description="The image source URI"
205: */
206: public void setSrc(String src) throws JspException {
207: _state.src = src;
208: }
209:
210: /**
211: * Sets the client-side image map declaration.
212: * @param usemap the map declaration.
213: * @jsptagref.attributedescription The client-side image map declaration
214: * @jsptagref.databindable false
215: * @jsptagref.attributesyntaxvalue <i>string_useMap</i>
216: * @netui:attribute required="false" rtexprvalue="true"
217: * description="The client-side image map declaration"
218: */
219: public void setUsemap(String usemap) {
220: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
221: USEMAP, usemap);
222: }
223:
224: /**
225: * Sets the vertical spacing around the image.
226: * @param vspace the vertical spacing.
227: * @jsptagref.attributedescription The vertical spacing around the image.
228: * @jsptagref.databindable Read Only
229: * @jsptagref.attributesyntaxvalue <i>string_vspace</i>
230: * @netui:attribute required="false" rtexprvalue="true"
231: * description="The vertical spacing around the image."
232: */
233: public void setVspace(String vspace) {
234: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
235: VSPACE, vspace);
236: }
237:
238: /**
239: * Sets the image width.
240: * @param width the image width.
241: * @jsptagref.attributedescription The image width.
242: * @jsptagref.databindable Read Only
243: * @jsptagref.attributesyntaxvalue <i>integer_pixelWidth</i>
244: * @netui:attribute required="false" rtexprvalue="true"
245: * description="The image width."
246: */
247: public void setWidth(String width) {
248: _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, WIDTH,
249: width);
250: }
251:
252: /**
253: * Adds a URL parameter to the generated hyperlink.
254: * @param name the name of the parameter to be added.
255: * @param value the value of the parameter to be added (a String or String[]).
256: * @param facet
257: */
258: public void addParameter(String name, Object value, String facet)
259: throws JspException {
260: assert (name != null) : "Parameter 'name' must not be null";
261:
262: if (_params == null) {
263: _params = new HashMap();
264: }
265: ParamHelper.addParam(_params, name, value);
266: }
267:
268: /**
269: * Render the beginning of the IMG tag.
270: * @throws JspException if a JSP exception has occurred
271: */
272: public int doStartTag() throws JspException {
273: // Evaluate the body of this tag
274: return EVAL_BODY_BUFFERED;
275: }
276:
277: /**
278: * Render the end of the IMG tag.
279: * @throws JspException if a JSP exception has occurred
280: */
281: public int doEndTag() throws JspException {
282: ServletRequest req = pageContext.getRequest();
283: String scriptId = null;
284:
285: // report errors that may have occurred
286: if (hasErrors())
287: return reportAndExit(EVAL_PAGE);
288:
289: // Generate the name definition or image element
290: String uri = null;
291: if (_state.src != null) {
292: try {
293: uri = PageFlowTagUtils.rewriteResourceURL(pageContext,
294: _state.src, _params, _location);
295: } catch (URISyntaxException e) {
296: // report the error...
297: String s = Bundle.getString("Tags_Image_URLException",
298: new Object[] { _state.src, e.getMessage() });
299: registerTagError(s, e);
300: }
301: }
302:
303: if (uri != null) {
304: _state.src = ((HttpServletResponse) pageContext
305: .getResponse()).encodeURL(uri);
306: }
307:
308: // we assume that tagId will over have override id if both
309: // are defined.
310: if (_state.id != null) {
311: scriptId = renderNameAndId((HttpServletRequest) req,
312: _state, null);
313: }
314:
315: WriteRenderAppender writer = new WriteRenderAppender(
316: pageContext);
317: TagRenderingBase br = TagRenderingBase.Factory.getRendering(
318: TagRenderingBase.IMAGE_TAG, req);
319: br.doStartTag(writer, _state);
320: br.doEndTag(writer);
321:
322: if (scriptId != null)
323: write(scriptId);
324:
325: // Evaluate the remainder of this page
326: localRelease();
327: return EVAL_PAGE;
328: }
329:
330: /**
331: * Release any acquired resources.
332: */
333: protected void localRelease() {
334: super.localRelease();
335:
336: _state.clear();
337:
338: _location = null;
339: _params = null;
340: }
341: }
|