1 47 48 package com.lowagie.text.html.simpleparser; 49 50 import java.util.ArrayList ; 51 import java.util.HashMap ; 52 53 public class ChainedProperties { 54 55 public final static int fontSizes[] = {8, 10, 12, 14, 18, 24, 36}; 56 public ArrayList chain = new ArrayList (); 57 58 59 public ChainedProperties() { 60 } 61 62 public String getProperty(String key) { 63 for (int k = chain.size() - 1; k >= 0; --k) { 64 Object obj[] = (Object [])chain.get(k); 65 HashMap prop = (HashMap )obj[1]; 66 String ret = (String )prop.get(key); 67 if (ret != null) 68 return ret; 69 } 70 return null; 71 } 72 73 public boolean hasProperty(String key) { 74 for (int k = chain.size() - 1; k >= 0; --k) { 75 Object obj[] = (Object [])chain.get(k); 76 HashMap prop = (HashMap )obj[1]; 77 if (prop.containsKey(key)) 78 return true; 79 } 80 return false; 81 } 82 83 public void addToChain(String key, HashMap prop) { 84 String value = (String )prop.get("size"); 86 if (value != null) { 87 if (value.endsWith("px")) { 88 prop.put("size", value.substring(0, value.length() - 2)); 89 } 90 else { 91 int s = 0; 92 if (value.startsWith("+") || value.startsWith("-")) { 93 String old = getProperty("basefontsize"); 94 if (old == null) 95 old = "12"; 96 float f = Float.parseFloat(old); 97 int c = (int)f; 98 for (int k = fontSizes.length - 1; k >= 0; --k) { 99 if (c >= fontSizes[k]) { 100 s = k; 101 break; 102 } 103 } 104 int inc = Integer.parseInt(value.startsWith("+") ? value.substring(1) : value); 105 s += inc; 106 } 107 else { 108 try { 109 s = Integer.parseInt(value) - 1; 110 } 111 catch(NumberFormatException nfe) { 112 s = 0; 113 } 114 } 115 if (s < 0) 116 s = 0; 117 else if (s >= fontSizes.length) 118 s = fontSizes.length - 1; 119 prop.put("size", Integer.toString(fontSizes[s])); 120 } 121 } 122 chain.add(new Object []{key, prop}); 123 } 124 125 public void removeChain(String key) { 126 for (int k = chain.size() - 1; k >= 0; --k) { 127 if (key.equals(((Object [])chain.get(k))[0])) { 128 chain.remove(k); 129 return; 130 } 131 } 132 } 133 } 134 | Popular Tags |