1 16 package org.apache.commons.jelly; 17 18 import java.io.StringWriter ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.apache.commons.jelly.util.TagUtils; 24 25 31 32 public abstract class TagSupport implements Tag { 33 34 35 protected Tag parent; 36 37 38 protected Script body; 39 40 41 protected Boolean shouldTrim; 42 protected boolean hasTrimmed; 43 44 protected JellyContext context; 45 46 47 private boolean escapeText = true; 48 49 57 public static Tag findAncestorWithClass(Tag from, Class tagClass) { 58 while (from != null) { 62 if (tagClass.isInstance(from)) { 63 return from; 64 } 65 from = from.getParent(); 66 } 67 return null; 68 } 69 70 78 public static Tag findAncestorWithClass(Tag from, Collection tagClasses) { 79 while (from != null) { 80 for(Iterator iter = tagClasses.iterator();iter.hasNext();) { 81 Class klass = (Class )(iter.next()); 82 if (klass.isInstance(from)) { 83 return from; 84 } 85 } 86 from = from.getParent(); 87 } 88 return null; 89 } 90 91 100 public static Tag findAncestorWithClass(Tag from, Class [] tagClasses) { 101 return findAncestorWithClass(from,Arrays.asList(tagClasses)); 102 } 103 104 public TagSupport() { 105 } 106 107 public TagSupport(boolean shouldTrim) { 108 setTrim( shouldTrim ); 109 } 110 111 115 public void setTrim(boolean shouldTrim) { 116 if ( shouldTrim ) { 117 this.shouldTrim = Boolean.TRUE; 118 } 119 else { 120 this.shouldTrim = Boolean.FALSE; 121 } 122 } 123 124 public boolean isTrim() { 125 if ( this.shouldTrim == null ) { 126 Tag parent = getParent(); 127 if ( parent == null ) { 128 return true; 129 } 130 else { 131 if ( parent instanceof TagSupport ) { 132 TagSupport parentSupport = (TagSupport) parent; 133 134 this.shouldTrim = ( parentSupport.isTrim() ? Boolean.TRUE : Boolean.FALSE ); 135 } 136 else { 137 this.shouldTrim = Boolean.TRUE; 138 } 139 } 140 } 141 142 return this.shouldTrim.booleanValue(); 143 } 144 145 146 public Tag getParent() { 147 return parent; 148 } 149 150 151 public void setParent(Tag parent) { 152 this.parent = parent; 153 } 154 155 156 public Script getBody() { 157 if (! hasTrimmed) { 158 hasTrimmed = true; 159 if (isTrim()) { 160 trimBody(); 161 } 162 } 163 return body; 164 } 165 166 167 public void setBody(Script body) { 168 this.body = body; 169 this.hasTrimmed = false; 170 } 171 172 173 public JellyContext getContext() { 174 return context; 175 } 176 177 178 public void setContext(JellyContext context) throws JellyTagException { 179 this.context = context; 180 } 181 182 185 public void invokeBody(XMLOutput output) throws JellyTagException { 186 getBody().run(context, output); 187 } 188 189 195 protected Tag findAncestorWithClass(Class parentClass) { 196 return findAncestorWithClass(getParent(), parentClass); 197 } 198 199 204 protected Tag findAncestorWithClass(Class [] parentClasses) { 205 return findAncestorWithClass(getParent(),parentClasses); 206 } 207 208 212 protected Tag findAncestorWithClass(Collection parentClasses) { 213 return findAncestorWithClass(getParent(),parentClasses); 214 } 215 216 221 protected String getBodyText() throws JellyTagException { 222 return getBodyText(escapeText); 223 } 224 225 232 protected String getBodyText(boolean shouldEscape) throws JellyTagException { 233 StringWriter writer = new StringWriter (); 234 invokeBody(XMLOutput.createXMLOutput(writer, shouldEscape)); 235 return writer.toString(); 236 } 237 238 239 243 protected void trimBody() { 244 TagUtils.trimScript(body); 245 } 246 247 250 public boolean isEscapeText() { 251 return escapeText; 252 } 253 254 258 public void setEscapeText(boolean escapeText) { 259 this.escapeText = escapeText; 260 } 261 } 262 | Popular Tags |