KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > buildtools > EscapeHtmlEntities


1 /**
2  *
3  */

4 package org.appfuse.buildtools;
5
6 import java.io.IOException JavaDoc;
7 import java.io.Reader JavaDoc;
8
9 import org.apache.commons.lang.StringEscapeUtils;
10 import org.apache.tools.ant.filters.BaseParamFilterReader;
11 import org.apache.tools.ant.filters.ChainableReader;
12
13 /**
14  * @author Mika
15  *
16  */

17 public class EscapeHtmlEntities extends BaseParamFilterReader implements ChainableReader {
18
19     public static final String JavaDoc ESCAPE = "escape";
20     public static final String JavaDoc UNESCAPE = "unescape";
21     /** Data that must be read from, if not null. */
22     private String JavaDoc queuedData = null;
23     private String JavaDoc mode;
24     
25     public EscapeHtmlEntities() {
26         super();
27     }
28
29     public EscapeHtmlEntities(final Reader JavaDoc rdr) {
30         super(rdr);
31     }
32     
33     /* (non-Javadoc)
34      * @see java.io.FilterReader#read()
35      */

36     public int read() throws IOException JavaDoc {
37         int ch = -1;
38
39         if (queuedData != null && queuedData.length() == 0) {
40             queuedData = null;
41         }
42
43         if (queuedData != null) {
44             ch = queuedData.charAt(0);
45             queuedData = queuedData.substring(1);
46             if (queuedData.length() == 0) {
47                 queuedData = null;
48             }
49         } else {
50             queuedData = readFully();
51             if (queuedData == null) {
52                 ch = -1;
53             } else {
54                 queuedData = handleEntities(queuedData);
55                 return read();
56             }
57         }
58         return ch;
59     }
60
61     /**
62      * @param queuedData2
63      * @return
64      */

65     private String JavaDoc handleEntities(String JavaDoc queuedData2) {
66         if(ESCAPE.equalsIgnoreCase(mode)){
67             return StringEscapeUtils.escapeHtml(queuedData2);
68         } else {
69             return StringEscapeUtils.unescapeHtml(queuedData2);
70         }
71     }
72
73     /* (non-Javadoc)
74      * @see org.apache.tools.ant.filters.ChainableReader#chain(java.io.Reader)
75      */

76     public Reader JavaDoc chain(Reader JavaDoc rdr) {
77         EscapeHtmlEntities filter = new EscapeHtmlEntities(rdr);
78         return filter;
79     }
80
81     /**
82      * @return Returns the mode.
83      */

84     public String JavaDoc getMode() {
85         return mode;
86     }
87
88     /**
89      * @param mode The mode to set.
90      */

91     public void setMode(String JavaDoc mode) {
92         this.mode = mode;
93     }
94
95 }
96
Popular Tags