1 50 51 package com.lowagie.text.markup; 52 53 import java.awt.Color; 54 import java.util.Properties; 55 import java.util.StringTokenizer; 56 57 import com.lowagie.text.FontFactory; 58 59 64 public class MarkupParser { 65 66 67 private MarkupParser() { 68 } 69 70 76 77 public static Properties parseAttributes(String string) { 78 Properties result = new Properties(); 79 if (string == null) return result; 80 StringTokenizer keyValuePairs = new StringTokenizer(string, ";"); 81 StringTokenizer keyValuePair; 82 String key; 83 String value; 84 while (keyValuePairs.hasMoreTokens()) { 85 keyValuePair = new StringTokenizer(keyValuePairs.nextToken(), ":"); 86 if (keyValuePair.hasMoreTokens()) key = keyValuePair.nextToken().trim(); 87 else continue; 88 if (keyValuePair.hasMoreTokens()) value = keyValuePair.nextToken().trim(); 89 else continue; 90 if (value.startsWith("\"")) value = value.substring(1); 91 if (value.endsWith("\"")) value = value.substring(0, value.length() - 1); 92 result.setProperty(key, value); 93 } 94 return result; 95 } 96 102 103 public static Properties parseFont(String string) { 104 Properties result = new Properties(); 105 if (string == null) return result; 106 int pos = 0; 107 String value; 108 string = string.trim(); 109 while (string.length() > 0) { 110 pos = string.indexOf(" ", pos); 111 if (pos == -1) { 112 value = string; 113 string = ""; 114 } 115 else { 116 value = string.substring(0, pos); 117 string = string.substring(pos).trim(); 118 } 119 if (value.equalsIgnoreCase("bold")) { 120 result.setProperty(MarkupTags.CSS_FONTWEIGHT, MarkupTags.CSS_BOLD); 121 continue; 122 } 123 if (value.equalsIgnoreCase("italic")) { 124 result.setProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_ITALIC); 125 continue; 126 } 127 if (value.equalsIgnoreCase("oblique")) { 128 result.setProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_OBLIQUE); 129 continue; 130 } 131 float f; 132 if ((f = parseLength(value)) > 0) { 133 result.setProperty(MarkupTags.CSS_FONTSIZE, String.valueOf(f) + "pt"); 134 int p = value.indexOf("/"); 135 if (p > -1 && p < value.length() - 1) { 136 result.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(value.substring(p + 1)) + "pt"); 137 } 138 } 139 if (value.endsWith(",")) { 140 value = value.substring(0, value.length() - 1); 141 if (FontFactory.contains(value)) { 142 result.setProperty(MarkupTags.CSS_FONTFAMILY, value); 143 return result; 144 } 145 } 146 if ("".equals(string) && FontFactory.contains(value)) { 147 result.setProperty(MarkupTags.CSS_FONTFAMILY, value); 148 } 149 } 150 return result; 151 } 152 153 159 160 public static float parseLength(String string) { 161 int pos = 0; 162 int length = string.length(); 163 boolean ok = true; 164 while (ok && pos < length) { 165 switch(string.charAt(pos)) { 166 case '+': 167 case '-': 168 case '0': 169 case '1': 170 case '2': 171 case '3': 172 case '4': 173 case '5': 174 case '6': 175 case '7': 176 case '8': 177 case '9': 178 case '.': 179 pos++; 180 break; 181 default: 182 ok = false; 183 } 184 } 185 if (pos == 0) return 0f; 186 if (pos == length) return Float.valueOf(string + "f").floatValue(); 187 float f = Float.valueOf(string.substring(0, pos) + "f").floatValue(); 188 string = string.substring(pos); 189 if (string.startsWith("in")) { 191 return f * 72f; 192 } 193 if (string.startsWith("cm")) { 195 return (f / 2.54f) * 72f; 196 } 197 if (string.startsWith("mm")) { 199 return (f / 25.4f) * 72f; 200 } 201 if (string.startsWith("pc")) { 203 return f * 12f; 204 } 205 return f; 207 } 208 209 215 216 public static Color decodeColor(String color) { 217 int red = 0; 218 int green = 0; 219 int blue = 0; 220 try { 221 red = Integer.parseInt(color.substring(1, 3), 16); 222 green = Integer.parseInt(color.substring(3, 5), 16); 223 blue = Integer.parseInt(color.substring(5), 16); 224 } 225 catch(Exception sioobe) { 226 } 228 return new Color(red, green, blue); 229 } 230 } | Popular Tags |