KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > jelly > OutTag


1 package org.kohsuke.stapler.jelly;
2
3 import org.apache.commons.jelly.JellyTagException;
4 import org.apache.commons.jelly.MissingAttributeException;
5 import org.apache.commons.jelly.TagSupport;
6 import org.apache.commons.jelly.XMLOutput;
7 import org.apache.commons.jelly.expression.Expression;
8 import org.xml.sax.SAXException JavaDoc;
9
10 /**
11  * Tag that outputs the specified value but with escaping,
12  * so that you can escape a portion even if the
13  * {@link XMLOutput} is not escaping.
14  *
15  * @author Kohsuke Kawaguchi
16  */

17 public class OutTag extends TagSupport {
18     private Expression value;
19
20     public void setValue(Expression value) {
21         this.value = value;
22     }
23
24     public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
25         final String JavaDoc text = value.evaluateAsString(context);
26         if (text != null) {
27             StringBuilder JavaDoc buf = new StringBuilder JavaDoc(text.length());
28             for (int i=0; i<text.length(); i++ ) {
29                 char ch = text.charAt(i);
30                 switch(ch) {
31                 case '<': buf.append("&lt;"); break;
32                 case '&': buf.append("&amp;"); break;
33                 default: buf.append(ch);
34                 }
35             }
36
37             try {
38                 output.write(buf.toString());
39             }
40             catch (SAXException JavaDoc e) {
41                 throw new JellyTagException("could not write the XMLOutput",e);
42             }
43         }
44     }
45 }
46
Popular Tags