View Javadoc
1   package org.andromda.taglibs.formatting;
2   
3   import org.apache.commons.lang.StringEscapeUtils;
4   
5   import javax.servlet.jsp.JspException;
6   import javax.servlet.jsp.tagext.BodyTagSupport;
7   import java.io.IOException;
8   
9   /**
10   *
11   */
12  public class EscapeTag extends BodyTagSupport
13  {
14      private static final long serialVersionUID = 34L;
15      private String language = null;
16  
17      /**
18       * @return language
19       */
20      public String getLanguage()
21      {
22          return language;
23      }
24  
25      /**
26       * @param language
27       */
28      public void setLanguage(String language)
29      {
30          this.language = language;
31      }
32  
33      /**
34       * @see javax.servlet.jsp.tagext.BodyTagSupport#doAfterBody()
35       */
36      public int doAfterBody() throws JspException
37      {
38          String escapedString = this.getBodyContent().getString();
39  
40          final String[] languages = this.language.split(",");
41          for (String language : languages)
42          {
43              language = language.trim();
44  
45              if ("html".equalsIgnoreCase(language))
46              {
47                  escapedString = StringEscapeUtils.escapeHtml(escapedString);
48              }
49              else if ("javascript".equalsIgnoreCase(language))
50              {
51                  escapedString = StringEscapeUtils.escapeJavaScript(escapedString);
52              }
53              else if ("java".equalsIgnoreCase(language))
54              {
55                  escapedString = StringEscapeUtils.escapeJava(escapedString);
56              }
57          }
58  
59          try
60          {
61              this.getPreviousOut().print(escapedString);
62          }
63          catch (IOException e)
64          {
65              throw new JspException("Unable to print out escaped body text");
66          }
67          return super.doAfterBody();
68      }
69  }