1 52 53 package freemarker.testcase.models; 54 55 import freemarker.template.*; 56 import java.io.*; 57 import java.util.Map ; 58 59 66 public class TransformModel1 implements TemplateTransformModel { 67 68 private boolean m_bAmpersands = false; 69 private boolean m_bQuotes = false; 70 private boolean m_bTags = false; 71 private String m_aComment = ""; 72 73 private static final int READER_BUFFER_SIZE = 4096; 74 75 public Writer getWriter(final Writer out, 76 final Map args) 77 { 78 final StringBuffer buf = new StringBuffer (); 79 return new Writer(out) { 80 public void write(char cbuf[], int off, int len) { 81 buf.append(cbuf, off, len); 82 } 83 84 public void flush() { 85 } 86 87 public void close() throws IOException { 88 StringReader sr = new StringReader(buf.toString()); 89 StringWriter sw = new StringWriter(); 90 transform(sr, sw); 91 out.write(sw.toString()); 92 } 93 }; 94 } 95 96 97 102 public void setAmpersands( boolean bAmpersands ) { 103 m_bAmpersands = bAmpersands; 104 } 105 106 111 public void setQuotes( boolean bQuotes ) { 112 m_bQuotes = bQuotes; 113 } 114 115 120 public void setTags( boolean bTags ) { 121 m_bTags = bTags; 122 } 123 124 129 public void setComment( String aComment ) { 130 m_aComment = aComment; 131 } 132 133 139 public void transform(Reader source, Writer output) 140 throws IOException 141 { 142 int n = 0; 145 boolean bCommentSent = false; 146 char[] aBuffer = new char[ READER_BUFFER_SIZE ]; 147 int i = source.read( aBuffer ); 148 while (i >= 0) { 149 for ( int j = 0; j < i; j++ ) { 150 char c = aBuffer[j]; 151 switch (c) { 152 case '&': 153 if ( m_bAmpersands ) { 154 output.write("&"); 155 } else { 156 output.write( c ); 157 } 158 break; 159 case '<': 160 if ( m_bTags ) { 161 output.write("<"); 162 } else { 163 output.write( c ); 164 } 165 break; 166 case '>': 167 if ( m_bTags ) { 168 output.write(">"); 169 } else { 170 output.write( c ); 171 } 172 break; 173 case '"': 174 if ( m_bQuotes ) { 175 output.write("""); 176 } else { 177 output.write( c ); 178 } 179 break; 180 case '\'': 181 if ( m_bQuotes ) { 182 output.write("'"); 183 } else { 184 output.write( c ); 185 } 186 break; 187 case '*': 188 if ( ! bCommentSent ) { 189 output.write( m_aComment ); 190 bCommentSent = true; 191 } else { 192 output.write( c ); 193 } 194 break; 195 default: 196 output.write(c); 197 } 198 n++; 199 } 200 i = source.read( aBuffer ); 201 } 202 } 203 } 204 | Popular Tags |