1 4 package org.appfuse.buildtools; 5 6 import java.io.IOException ; 7 import java.io.Reader ; 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 17 public class EscapeHtmlEntities extends BaseParamFilterReader implements ChainableReader { 18 19 public static final String ESCAPE = "escape"; 20 public static final String UNESCAPE = "unescape"; 21 22 private String queuedData = null; 23 private String mode; 24 25 public EscapeHtmlEntities() { 26 super(); 27 } 28 29 public EscapeHtmlEntities(final Reader rdr) { 30 super(rdr); 31 } 32 33 36 public int read() throws IOException { 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 65 private String handleEntities(String queuedData2) { 66 if(ESCAPE.equalsIgnoreCase(mode)){ 67 return StringEscapeUtils.escapeHtml(queuedData2); 68 } else { 69 return StringEscapeUtils.unescapeHtml(queuedData2); 70 } 71 } 72 73 76 public Reader chain(Reader rdr) { 77 EscapeHtmlEntities filter = new EscapeHtmlEntities(rdr); 78 return filter; 79 } 80 81 84 public String getMode() { 85 return mode; 86 } 87 88 91 public void setMode(String mode) { 92 this.mode = mode; 93 } 94 95 } 96 | Popular Tags |