1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 30 31 37 public class ElementTag extends AbstractBodyTag { 38 39 40 protected static final boolean TRIM_VALUES = true; 41 42 43 private String name; 44 private List attributeNames; 45 private Map attributeValues; 46 47 public ElementTag() { 49 } 50 51 public void addAttribute( String name ) { 52 if ( attributeNames == null ) { 53 attributeNames = new ArrayList (); 54 } 55 attributeNames.add( name ); 56 } 57 58 public void setAttributeValue( String name, String value ) { 59 if ( attributeValues == null ) { 60 attributeValues = new HashMap (); 61 } 62 attributeValues.put( name, value ); 63 } 64 65 public void release() { 68 super.release(); 69 name = null; 70 attributeNames = null; 71 attributeValues = null; 72 } 73 74 public int doStartTag() throws JspException { 75 return EVAL_BODY_TAG; 76 } 77 78 public int doAfterBody() throws JspException { 79 JspWriter out = bodyContent.getEnclosingWriter(); 80 try { 82 out.print( "<" + getName() ); 83 printAttributes( out ); 84 85 String content = bodyContent.getString(); 86 if ( content == null || content.length() <= 0 ) { 87 out.print( "/>" ); 88 } 89 else { 90 out.print( ">" ); 91 out.print( content ); 92 out.print( "</" + getName() + ">" ); 93 } 94 bodyContent.clearBody(); 95 } 96 catch ( IOException e ) { 97 handleException( e ); 98 } 99 return SKIP_BODY; 100 } 101 102 105 108 public String getName() { 109 return name; 110 } 111 114 public void setName(String name) { 115 this.name = name; 116 } 117 118 protected void printAttributes( JspWriter out ) throws IOException { 121 if ( attributeNames != null ) { 122 int size = attributeNames.size(); 123 for ( int i = 0; i < size; i++ ) { 124 String attributeName = (String ) attributeNames.get( i ); 125 printAttribute( out, attributeName ); 126 } 127 } 128 } 129 130 protected void printAttribute( JspWriter out, String attributeName ) throws IOException { 131 Object value = null; 132 if ( attributeValues != null ) { 133 value = attributeValues.get( attributeName ); 134 } 135 String text = attributeName; 136 if ( value != null ) { 137 if ( TRIM_VALUES ) { 138 text = value.toString().trim(); 139 } 140 else { 141 text = value.toString(); 142 } 143 } 144 out.print( " " + attributeName + "=\"" + text + "\"" ); 145 } 146 } 147 | Popular Tags |