1 50 51 package com.lowagie.text.html; 52 53 import java.awt.Color ; 54 55 import com.lowagie.text.Element; 56 57 84 85 public class HtmlEncoder { 86 87 89 90 private static final String [] htmlCode = new String [256]; 91 92 static { 93 for (int i = 0; i < 10; i++) { 94 htmlCode[i] = "�" + i + ";"; 95 } 96 97 for (int i = 10; i < 32; i++) { 98 htmlCode[i] = "�" + i + ";"; 99 } 100 101 for (int i = 32; i < 128; i++) { 102 htmlCode[i] = String.valueOf((char)i); 103 } 104 105 htmlCode['\t'] = "\t"; 107 htmlCode['\n'] = "<" + HtmlTags.NEWLINE + " />\n"; 108 htmlCode['\"'] = """; htmlCode['&'] = "&"; htmlCode['<'] = "<"; htmlCode['>'] = ">"; 113 for (int i = 128; i < 256; i++) { 114 htmlCode[i] = "&#" + i + ";"; 115 } 116 } 117 118 119 121 126 127 private HtmlEncoder () { } 128 129 131 137 138 public static String encode(String string) { 139 int n = string.length(); 140 char character; 141 StringBuffer buffer = new StringBuffer (); 142 for (int i = 0; i < n; i++) { 144 character = string.charAt(i); 145 if (character < 256) { 147 buffer.append(htmlCode[character]); 148 } 149 else { 150 buffer.append("&#").append((int)character).append(';'); 152 } 153 } 154 return buffer.toString(); 155 } 156 157 163 164 public static String encode(Color color) { 165 StringBuffer buffer = new StringBuffer ("#"); 166 if (color.getRed() < 16) { 167 buffer.append('0'); 168 } 169 buffer.append(Integer.toString(color.getRed(), 16)); 170 if (color.getGreen() < 16) { 171 buffer.append('0'); 172 } 173 buffer.append(Integer.toString(color.getGreen(), 16)); 174 if (color.getBlue() < 16) { 175 buffer.append('0'); 176 } 177 buffer.append(Integer.toString(color.getBlue(), 16)); 178 return buffer.toString(); 179 } 180 181 187 188 public static String getAlignment(int alignment) { 189 switch(alignment) { 190 case Element.ALIGN_LEFT: 191 return HtmlTags.ALIGN_LEFT; 192 case Element.ALIGN_CENTER: 193 return HtmlTags.ALIGN_CENTER; 194 case Element.ALIGN_RIGHT: 195 return HtmlTags.ALIGN_RIGHT; 196 case Element.ALIGN_JUSTIFIED: 197 case Element.ALIGN_JUSTIFIED_ALL: 198 return HtmlTags.ALIGN_JUSTIFIED; 199 case Element.ALIGN_TOP: 200 return HtmlTags.ALIGN_TOP; 201 case Element.ALIGN_MIDDLE: 202 return HtmlTags.ALIGN_MIDDLE; 203 case Element.ALIGN_BOTTOM: 204 return HtmlTags.ALIGN_BOTTOM; 205 case Element.ALIGN_BASELINE: 206 return HtmlTags.ALIGN_BASELINE; 207 default: 208 return ""; 209 } 210 } 211 } | Popular Tags |