1 52 53 package freemarker.template.utility; 54 55 import freemarker.template.TemplateTransformModel; 56 import java.io.*; 57 import java.util.Map ; 58 59 93 public class NormalizeNewlines implements TemplateTransformModel { 94 95 public Writer getWriter(final Writer out, 96 final Map args) 97 { 98 final StringBuffer buf = new StringBuffer (); 99 return new Writer() { 100 public void write(char cbuf[], int off, int len) { 101 buf.append(cbuf, off, len); 102 } 103 104 public void flush() throws IOException { 105 out.flush(); 106 } 107 108 public void close() throws IOException { 109 StringReader sr = new StringReader(buf.toString()); 110 StringWriter sw = new StringWriter(); 111 transform(sr, sw); 112 out.write(sw.toString()); 113 } 114 }; 115 } 116 117 123 public void transform(Reader in, Writer out) throws IOException 124 { 125 BufferedReader br = (in instanceof BufferedReader) 126 ? (BufferedReader) in 127 : new BufferedReader(in); 128 PrintWriter pw = (out instanceof PrintWriter) 129 ? (PrintWriter) out 130 : new PrintWriter(out); 131 String line = br.readLine(); 132 if (line != null) { 133 if ( line.length() > 0 ) { 134 pw.println(line); 135 } 136 } 137 while ((line = br.readLine()) != null) { 138 pw.println(line); 139 } 140 } 141 } 142 | Popular Tags |