| 1 package com.quadcap.io; 2 3 40 41 import java.io.IOException ; 42 import java.io.Writer ; 43 44 49 public class HtmlifyWriter extends Writer { 50 Writer os; 51 static final char[] ampLT = { 52 '&', 'l', 't', ';' 53 }; 54 static final char[] ampGT = { 55 '&', 'g', 't', ';' 56 }; 57 static final char[] ampAMP = { 58 '&', 'a', 'm', 'p', ';' 59 }; 60 61 public HtmlifyWriter(Writer os) { 62 this.os = os; 63 } 64 65 public void setWriter(Writer w) { 66 this.os = w; 67 } 68 69 public void write(int c) throws IOException { 70 switch (c) { 71 case '<': 72 os.write(ampLT); 73 break; 74 case '>': 75 os.write(ampGT); 76 break; 77 case '&': 78 os.write(ampAMP); 79 break; 80 default: 81 os.write(c); 82 } 83 } 84 85 public void write(char cbuf[], int offset, int len) throws IOException { 86 int lim = offset + len; 87 while (offset < lim) write(cbuf[offset++]); 88 } 89 90 public void flush() throws IOException { 91 os.flush(); 92 } 93 94 public void close() throws IOException { 95 os.close(); 96 } 97 98 public void println(String s) throws IOException { 99 print(s); 100 print(System.getProperty("line.separator")); 101 } 102 103 public void print(String s) throws IOException { 104 for (int i = 0; i < s.length(); i++) write(s.charAt(i)); 105 } 106 } 107 | Popular Tags |