1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import org.springframework.util.StringUtils; 20 import org.springframework.util.Assert; 21 22 import javax.servlet.jsp.JspException ; 23 import java.io.IOException ; 24 import java.io.Writer ; 25 import java.util.Stack ; 26 27 35 public class TagWriter { 36 37 40 private final SafeWriter writer; 41 42 43 46 private Stack tagState = new Stack (); 47 48 49 55 public TagWriter(Writer writer) { 56 Assert.notNull(writer); 57 this.writer = new SafeWriter(writer); 58 } 59 60 61 66 public void startTag(String tagName) throws JspException { 67 if (inTag()) { 68 closeTagAndMarkAsBlock(); 69 } 70 push(tagName); 71 this.writer.append("<").append(tagName); 72 } 73 74 80 public void writeAttribute(String attributeName, String attributeValue) throws JspException { 81 if (currentState().isBlockTag()) { 82 throw new IllegalStateException ("Cannot write attributes after opening tag is closed."); 83 } 84 this.writer.append(" ").append(attributeName).append("=\"") 85 .append(attributeValue).append("\""); 86 } 87 88 93 public void writeOptionalAttributeValue(String attributeName, String attributeValue) throws JspException { 94 if (StringUtils.hasText(attributeValue)) { 95 writeAttribute(attributeName, attributeValue); 96 } 97 } 98 99 104 public void appendValue(String value) throws JspException { 105 if (!inTag()) { 106 throw new IllegalStateException ("Cannot write tag value. No open tag available."); 107 } 108 closeTagAndMarkAsBlock(); 109 this.writer.append(value); 110 } 111 112 113 119 public void forceBlock() throws JspException { 120 if (currentState().isBlockTag()) { 121 return; } 123 closeTagAndMarkAsBlock(); 124 } 125 126 131 public void endTag() throws JspException { 132 if (!inTag()) { 133 throw new IllegalStateException ("Cannot write end of tag. No open tag available."); 134 } 135 if (currentState().isBlockTag()) { 136 this.writer.append("</").append(currentState().getTagName()).append(">"); 138 } 139 else { 140 this.writer.append("/>"); 141 } 142 this.tagState.pop(); 143 } 144 145 146 149 private void push(String tagName) { 150 this.tagState.push(new TagStateEntry(tagName)); 151 } 152 153 156 private void closeTagAndMarkAsBlock() throws JspException { 157 if (!currentState().isBlockTag()) { 158 currentState().markAsBlockTag(); 159 this.writer.append(">"); 160 } 161 } 162 163 private boolean inTag() { 164 return this.tagState.size() > 0; 165 } 166 167 private TagStateEntry currentState() { 168 return (TagStateEntry) this.tagState.peek(); 169 } 170 171 172 175 private static final class TagStateEntry { 176 177 private final String tagName; 178 179 private boolean blockTag; 180 181 182 public TagStateEntry(String tagName) { 183 this.tagName = tagName; 184 } 185 186 187 public String getTagName() { 188 return tagName; 189 } 190 191 public boolean isBlockTag() { 192 return blockTag; 193 } 194 195 196 public void markAsBlockTag() { 197 this.blockTag = true; 198 } 199 200 } 201 202 203 207 private static final class SafeWriter { 208 209 private final Writer writer; 210 211 212 public SafeWriter(Writer writer) { 213 this.writer = writer; 214 } 215 216 217 public SafeWriter append(String value) throws JspException { 218 try { 219 this.writer.write(String.valueOf(value)); 220 return this; 221 } 222 catch (IOException ex) { 223 throw new JspException ("Unable to write to JspWriter", ex); 224 } 225 } 226 227 } 228 229 } 230 | Popular Tags |