1 50 51 package com.lowagie.text.html; 52 53 import java.awt.Color ; 54 import java.util.Properties ; 55 import java.util.StringTokenizer ; 56 57 58 61 62 public class Markup { 63 64 66 67 public static final String ITEXT_TAG = "tag"; 68 69 71 72 public static final String HTML_TAG_BODY = "body"; 73 74 75 public static final String HTML_TAG_DIV = "div"; 76 77 78 public static final String HTML_TAG_LINK = "link"; 79 80 81 public static final String HTML_TAG_SPAN = "span"; 82 83 85 86 public static final String HTML_ATTR_HEIGHT = "height"; 87 88 89 public static final String HTML_ATTR_HREF = "href"; 90 91 92 public static final String HTML_ATTR_REL = "rel"; 93 94 95 public static final String HTML_ATTR_STYLE = "style"; 96 97 98 public static final String HTML_ATTR_TYPE = "type"; 99 100 101 public static final String HTML_ATTR_STYLESHEET = "stylesheet"; 102 103 104 public static final String HTML_ATTR_WIDTH = "width"; 105 106 107 public static final String HTML_ATTR_CSS_CLASS = "class"; 108 109 110 public static final String HTML_ATTR_CSS_ID = "id"; 111 112 114 115 public static final String HTML_VALUE_JAVASCRIPT = "text/javascript"; 116 117 118 public static final String HTML_VALUE_CSS = "text/css"; 119 120 122 123 public static final String CSS_KEY_BGCOLOR = "background-color"; 124 125 126 public static final String CSS_KEY_COLOR = "color"; 127 128 129 public static final String CSS_KEY_DISPLAY = "display"; 130 131 132 public static final String CSS_KEY_FONTFAMILY = "font-family"; 133 134 135 public static final String CSS_KEY_FONTSIZE = "font-size"; 136 137 138 public static final String CSS_KEY_FONTSTYLE = "font-style"; 139 140 141 public static final String CSS_KEY_FONTWEIGHT = "font-weight"; 142 143 144 public static final String CSS_KEY_LINEHEIGHT = "line-height"; 145 146 147 public static final String CSS_KEY_MARGIN = "margin"; 148 149 150 public static final String CSS_KEY_MARGINLEFT = "margin-left"; 151 152 153 public static final String CSS_KEY_MARGINRIGHT = "margin-right"; 154 155 156 public static final String CSS_KEY_MARGINTOP = "margin-top"; 157 158 159 public static final String CSS_KEY_MARGINBOTTOM = "margin-bottom"; 160 161 162 public static final String CSS_KEY_PADDING = "padding"; 163 164 165 public static final String CSS_KEY_PADDINGLEFT = "padding-left"; 166 167 168 public static final String CSS_KEY_PADDINGRIGHT = "padding-right"; 169 170 171 public static final String CSS_KEY_PADDINGTOP = "padding-top"; 172 173 174 public static final String CSS_KEY_PADDINGBOTTOM = "padding-bottom"; 175 176 177 public static final String CSS_KEY_BORDERCOLOR = "border-color"; 178 179 180 public static final String CSS_KEY_BORDERWIDTH = "border-width"; 181 182 183 public static final String CSS_KEY_BORDERWIDTHLEFT = "border-left-width"; 184 185 186 public static final String CSS_KEY_BORDERWIDTHRIGHT = "border-right-width"; 187 188 189 public static final String CSS_KEY_BORDERWIDTHTOP = "border-top-width"; 190 191 192 public static final String CSS_KEY_BORDERWIDTHBOTTOM = "border-bottom-width"; 193 194 195 public static final String CSS_KEY_PAGE_BREAK_AFTER = "page-break-after"; 196 197 198 public static final String CSS_KEY_PAGE_BREAK_BEFORE = "page-break-before"; 199 200 201 public static final String CSS_KEY_TEXTALIGN = "text-align"; 202 203 204 public static final String CSS_KEY_TEXTDECORATION = "text-decoration"; 205 206 207 public static final String CSS_KEY_VERTICALALIGN = "vertical-align"; 208 209 210 public static final String CSS_KEY_VISIBILITY = "visibility"; 211 212 214 215 public static final String CSS_VALUE_ALWAYS = "always"; 216 217 218 public static final String CSS_VALUE_BLOCK = "block"; 219 220 221 public static final String CSS_VALUE_BOLD = "bold"; 222 223 224 public static final String CSS_VALUE_HIDDEN = "hidden"; 225 226 227 public static final String CSS_VALUE_INLINE = "inline"; 228 229 230 public static final String CSS_VALUE_ITALIC = "italic"; 231 232 233 public static final String CSS_VALUE_LINETHROUGH = "line-through"; 234 235 236 public static final String CSS_VALUE_LISTITEM = "list-item"; 237 238 239 public static final String CSS_VALUE_NONE = "none"; 240 241 242 public static final String CSS_VALUE_NORMAL = "normal"; 243 244 245 public static final String CSS_VALUE_OBLIQUE = "oblique"; 246 247 248 public static final String CSS_VALUE_TABLE = "table"; 249 250 251 public static final String CSS_VALUE_TABLEROW = "table-row"; 252 253 254 public static final String CSS_VALUE_TABLECELL = "table-cell"; 255 256 257 public static final String CSS_VALUE_TEXTALIGNLEFT = "left"; 258 259 260 public static final String CSS_VALUE_TEXTALIGNRIGHT = "right"; 261 262 263 public static final String CSS_VALUE_TEXTALIGNCENTER = "center"; 264 265 266 public static final String CSS_VALUE_TEXTALIGNJUSTIFY = "justify"; 267 268 269 public static final String CSS_VALUE_UNDERLINE = "underline"; 270 271 279 280 public static float parseLength(String string) { 281 int pos = 0; 282 int length = string.length(); 283 boolean ok = true; 284 while (ok && pos < length) { 285 switch (string.charAt(pos)) { 286 case '+': 287 case '-': 288 case '0': 289 case '1': 290 case '2': 291 case '3': 292 case '4': 293 case '5': 294 case '6': 295 case '7': 296 case '8': 297 case '9': 298 case '.': 299 pos++; 300 break; 301 default: 302 ok = false; 303 } 304 } 305 if (pos == 0) 306 return 0f; 307 if (pos == length) 308 return Float.parseFloat(string + "f"); 309 float f = Float.parseFloat(string.substring(0, pos) + "f"); 310 string = string.substring(pos); 311 if (string.startsWith("in")) { 313 return f * 72f; 314 } 315 if (string.startsWith("cm")) { 317 return (f / 2.54f) * 72f; 318 } 319 if (string.startsWith("mm")) { 321 return (f / 25.4f) * 72f; 322 } 323 if (string.startsWith("pc")) { 325 return f * 12f; 326 } 327 return f; 329 } 330 331 339 340 public static Color decodeColor(String s) { 341 if (s == null) 342 return null; 343 s = s.toLowerCase().trim(); 344 Color c = (Color )WebColors.getRGBColor(s); 345 if (c != null) 346 return c; 347 try { 348 if (s.startsWith("#")) { 349 if (s.length() == 4) 350 s = "#" + s.substring(1, 2) + s.substring(1, 2) 351 + s.substring(2, 3) + s.substring(2, 3) 352 + s.substring(3, 4) + s.substring(3, 4); 353 if (s.length() == 7) 354 return new Color (Integer.parseInt(s.substring(1), 16)); 355 } 356 else if (s.startsWith("rgb")) { 357 StringTokenizer tk = new StringTokenizer (s.substring(3), " \t\r\n\f(),"); 358 int[] cc = new int [3]; 359 for (int k = 0; k < 3; ++k) { 360 if (!tk.hasMoreTokens()) 361 return null; 362 String t = tk.nextToken(); 363 float n; 364 if (t.endsWith("%")) { 365 n = Float.parseFloat(t.substring(0, t.length() - 1)); 366 n = n * 255f / 100f; 367 } 368 else 369 n = Float.parseFloat(t); 370 int ni = (int)n; 371 if (ni > 255) 372 ni = 255; 373 else if (ni < 0) 374 ni = 0; 375 cc[k] = ni; 376 } 377 return new Color (cc[0], cc[1], cc[2]); 378 } 379 } 380 catch (Exception e) { 381 } 382 return null; 383 } 384 385 394 public static Properties parseAttributes(String string) { 395 Properties result = new Properties (); 396 if (string == null) 397 return result; 398 StringTokenizer keyValuePairs = new StringTokenizer (string, ";"); 399 StringTokenizer keyValuePair; 400 String key; 401 String value; 402 while (keyValuePairs.hasMoreTokens()) { 403 keyValuePair = new StringTokenizer (keyValuePairs.nextToken(), ":"); 404 if (keyValuePair.hasMoreTokens()) 405 key = keyValuePair.nextToken().trim(); 406 else 407 continue; 408 if (keyValuePair.hasMoreTokens()) 409 value = keyValuePair.nextToken().trim(); 410 else 411 continue; 412 if (value.startsWith("\"")) 413 value = value.substring(1); 414 if (value.endsWith("\"")) 415 value = value.substring(0, value.length() - 1); 416 result.setProperty(key.toLowerCase(), value); 417 } 418 return result; 419 } 420 421 432 public static String removeComment(String string, String startComment, 433 String endComment) { 434 StringBuffer result = new StringBuffer (); 435 int pos = 0; 436 int end = endComment.length(); 437 int start = string.indexOf(startComment, pos); 438 while (start > -1) { 439 result.append(string.substring(pos, start)); 440 pos = string.indexOf(endComment, start) + end; 441 start = string.indexOf(startComment, pos); 442 } 443 result.append(string.substring(pos)); 444 return result.toString(); 445 } 446 447 } | Popular Tags |