1 51 package org.apache.fop.fo; 52 53 import org.apache.fop.messaging.MessageHandler; 54 55 import org.apache.fop.apps.FOPException; 56 57 import org.xml.sax.Attributes ; 58 59 import java.util.HashMap ; 60 61 public class PropertyListBuilder { 62 63 66 private static final String FONTSIZEATTR = "font-size"; 67 68 private HashMap propertyListTable; 69 private HashMap elementTable; 70 71 public PropertyListBuilder() { 72 this.propertyListTable = new HashMap (); 73 this.elementTable = new HashMap (); 74 } 75 76 public void addList(HashMap list) { 77 propertyListTable.putAll(list); 78 } 79 80 public void addElementList(String element, HashMap list) { 81 elementTable.put(element, list); 82 } 83 84 public Property computeProperty(PropertyList propertyList, String space, 85 String element, String propertyName) { 86 87 Property p = null; 88 Property.Maker propertyMaker = findMaker(space, element, 89 propertyName); 90 if (propertyMaker != null) { 91 try { 92 p = propertyMaker.compute(propertyList); 93 } catch (FOPException e) { 94 MessageHandler.errorln("exception occurred while computing" 95 + " value of property '" 96 + propertyName + "': " 97 + e.getMessage()); 98 } 99 } else { 100 MessageHandler.errorln("property " + propertyName 101 + " ignored"); 102 } 103 return p; 104 } 105 106 public boolean isInherited(String space, String element, 107 String propertyName) { 108 boolean b; 109 110 Property.Maker propertyMaker = findMaker(space, element, 111 propertyName); 112 if (propertyMaker != null) { 113 b = propertyMaker.isInherited(); 114 } else { 115 b = true; 117 } 118 return b; 119 } 120 121 public PropertyList makeList(String ns, String elementName, Attributes attributes, 122 PropertyList parentPropertyList, 123 FObj parentFO) throws FOPException { 124 String space = "http://www.w3.org/TR/1999/XSL/Format"; 125 if (ns != null) { 126 space = ns; 127 } 128 129 PropertyList par = null; 130 if (parentPropertyList != null 131 && space.equals(parentPropertyList.getNameSpace())) { 132 par = parentPropertyList; 133 } 134 PropertyList p = new PropertyList(par, space, 135 elementName); 136 p.setBuilder(this); 137 HashMap table; 138 table = (HashMap )elementTable.get(elementName); 139 140 141 StringBuffer propsDone = new StringBuffer (256); 142 propsDone.append(' '); 143 144 150 String fontsizeval = attributes.getValue(FONTSIZEATTR); 151 if (fontsizeval != null) { 152 Property.Maker propertyMaker = findMaker(table, FONTSIZEATTR); 153 if (propertyMaker != null) { 154 try { 155 p.put(FONTSIZEATTR, 156 propertyMaker.make(p, fontsizeval, parentFO)); 157 } catch (FOPException e) {} 158 } 159 propsDone.append(FONTSIZEATTR + ' '); 161 } 162 163 for (int i = 0; i < attributes.getLength(); i++) { 164 String attributeName = attributes.getQName(i); 165 166 int sepchar = attributeName.indexOf('.'); 167 String propName = attributeName; 168 String subpropName = null; 169 Property propVal = null; 170 if (sepchar > -1) { 171 propName = attributeName.substring(0, sepchar); 172 subpropName = attributeName.substring(sepchar + 1); 173 } else if (propsDone.toString().indexOf(' ' + propName + ' ') 174 != -1) { 175 continue; 178 } 179 180 Property.Maker propertyMaker = findMaker(table, propName); 181 182 if (propertyMaker != null) { 183 try { 184 if (subpropName != null) { 185 Property baseProp = p.getExplicitBaseProp(propName); 186 if (baseProp == null) { 187 String baseValue = attributes.getValue(propName); 189 if (baseValue != null) { 190 baseProp = propertyMaker.make(p, baseValue, 191 parentFO); 192 propsDone.append(propName + ' '); 193 } 194 } 196 propVal = propertyMaker.make(baseProp, subpropName, 197 p, 198 attributes.getValue(i), 199 parentFO); 200 } else { 201 propVal = propertyMaker.make(p, 202 attributes.getValue(i), 203 parentFO); 204 } 205 if (propVal != null) { 206 p.put(propName, propVal); 207 } 208 } catch (FOPException e) { 209 MessageHandler.errorln(e.getMessage()); 210 } 211 } else { 212 if (!attributeName.startsWith("xmlns")) 213 MessageHandler.errorln("property '" 214 + attributeName + "' ignored"); 215 } 216 } 217 218 return p; 219 } 220 221 public Property getSubpropValue(String space, String element, 222 String propertyName, Property p, 223 String subpropName) { 224 Property.Maker maker = findMaker(space, element, propertyName); 225 if (maker != null) { 226 return maker.getSubpropValue(p, subpropName); 227 } else 228 return null; 229 } 230 231 232 public boolean isCorrespondingForced(PropertyList propertyList, 233 String space, String element, 234 String propertyName) { 235 Property.Maker propertyMaker = findMaker(space, element, 236 propertyName); 237 if (propertyMaker != null) { 238 return propertyMaker.isCorrespondingForced(propertyList); 239 } else { 240 MessageHandler.errorln("no Maker for " + propertyName); 241 } 242 return false; 243 } 244 245 public Property getShorthand(PropertyList propertyList, String space, 246 String element, String propertyName) { 247 Property.Maker propertyMaker = findMaker(space, element, 248 propertyName); 249 if (propertyMaker != null) { 250 return propertyMaker.getShorthand(propertyList); 251 } else { 252 MessageHandler.errorln("no Maker for " + propertyName); 253 return null; 254 } 255 } 256 257 258 public Property makeProperty(PropertyList propertyList, String space, 259 String element, 260 String propertyName) throws FOPException { 261 262 Property p = null; 263 264 Property.Maker propertyMaker = findMaker(space, element, 265 propertyName); 266 if (propertyMaker != null) { 267 p = propertyMaker.make(propertyList); 268 } else { 269 MessageHandler.errorln("property " + propertyName 270 + " ignored"); 271 } 272 return p; 273 } 274 275 protected Property.Maker findMaker(String space, String elementName, 276 String propertyName) { 277 return findMaker((HashMap )elementTable.get(elementName), 278 propertyName); 279 } 280 281 289 private Property.Maker findMaker(HashMap elemTable, 290 String propertyName) { 291 Property.Maker propertyMaker = null; 292 if (elemTable != null) { 293 propertyMaker = (Property.Maker)elemTable.get(propertyName); 294 } 295 if (propertyMaker == null) { 296 propertyMaker = 297 (Property.Maker)propertyListTable.get(propertyName); 298 } 299 return propertyMaker; 300 } 301 302 } 303 | Popular Tags |