1 /** 2 * Licensed under the Artistic License; you may not use this file 3 * except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://displaytag.sourceforge.net/license.html 7 * 8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR 9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 11 */ 12 package org.displaytag.tags; 13 14 import javax.servlet.jsp.JspException; 15 import javax.servlet.jsp.tagext.BodyTagSupport; 16 17 import org.displaytag.exception.MissingAttributeException; 18 import org.displaytag.exception.TagStructureException; 19 20 21 /** 22 * @author epesh 23 * @author Fabrizio Giustina 24 * @version $Revision: 1081 $ ($Author: fgiust $) 25 */ 26 public class SetPropertyTag extends BodyTagSupport 27 { 28 29 /** 30 * D1597A17A6. 31 */ 32 private static final long serialVersionUID = 899149338534L; 33 34 /** 35 * property name. 36 */ 37 private String name; 38 39 /** 40 * property value. 41 */ 42 private String value; 43 44 /** 45 * is this the first iteration? 46 */ 47 private boolean firstIteration; 48 49 /** 50 * Sets the name of the property. 51 * @param propertyName String 52 */ 53 public void setName(String propertyName) 54 { 55 this.name = propertyName; 56 } 57 58 /** 59 * Sets the value of the property. 60 * @param propertyValue String 61 */ 62 public void setValue(String propertyValue) 63 { 64 this.value = propertyValue; 65 } 66 67 /** 68 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 69 */ 70 public int doStartTag() throws JspException 71 { 72 TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class); 73 74 if (tableTag == null) 75 { 76 throw new TagStructureException(getClass(), "setProperty", "table"); //$NON-NLS-1$ //$NON-NLS-2$ 77 } 78 79 // read body only once 80 if (tableTag.isFirstIteration()) 81 { 82 this.firstIteration = true; 83 // using int to avoid deprecation error in compilation using j2ee 1.3 (EVAL_BODY_TAG) 84 return 2; 85 } 86 87 this.firstIteration = false; 88 return SKIP_BODY; 89 90 } 91 92 /** 93 * Passes attribute information up to the parent TableTag. 94 * <p> 95 * When we hit the end of the tag, we simply let our parent (which better be a TableTag) know what the user wants to 96 * change a property value, and we pass the name/value pair that the user gave us, up to the parent 97 * </p> 98 * @return <code>TagSupport.EVAL_PAGE</code> 99 * @throws MissingAttributeException if no value or body content has been set 100 * @see javax.servlet.jsp.tagext.Tag#doEndTag() 101 */ 102 public int doEndTag() throws MissingAttributeException 103 { 104 105 if (this.firstIteration) 106 { 107 TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class); 108 109 // tableTag can't be null, it has been checked in doStartTag 110 if (this.value == null) 111 { 112 if (getBodyContent() == null) 113 { 114 throw new MissingAttributeException(getClass(), // 115 new String[]{"value", "body content"}); //$NON-NLS-1$ //$NON-NLS-2$ 116 } 117 this.value = getBodyContent().getString(); 118 } 119 120 tableTag.setProperty(this.name, this.value); 121 122 this.name = null; 123 this.value = null; 124 } 125 return EVAL_PAGE; 126 } 127 128 }