You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertyListBuilder.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo;
  8. import org.apache.fop.fo.properties.*;
  9. import org.apache.fop.svg.*;
  10. import org.apache.fop.datatypes.*;
  11. import org.apache.fop.apps.FOPException;
  12. import org.xml.sax.Attributes;
  13. import java.util.HashMap;
  14. public class PropertyListBuilder {
  15. /**
  16. * Name of font-size property attribute to set first.
  17. */
  18. private static final String FONTSIZEATTR = "font-size";
  19. private HashMap propertyListTable;
  20. private HashMap elementTable;
  21. public PropertyListBuilder() {
  22. this.propertyListTable = new HashMap();
  23. this.elementTable = new HashMap();
  24. }
  25. public void addList(HashMap list) {
  26. propertyListTable.putAll(list);
  27. }
  28. public void addElementList(String element, HashMap list) {
  29. elementTable.put(element, list);
  30. }
  31. public Property computeProperty(PropertyList propertyList, String space,
  32. String element, String propertyName) {
  33. Property p = null;
  34. Property.Maker propertyMaker = findMaker(space, element,
  35. propertyName);
  36. if (propertyMaker != null) {
  37. try {
  38. p = propertyMaker.compute(propertyList);
  39. } catch (FOPException e) {
  40. //log.error("exception occurred while computing"
  41. // + " value of property '"
  42. // + propertyName + "': "
  43. // + e.getMessage());
  44. }
  45. } else {
  46. //log.error("property " + propertyName
  47. // + " ignored");
  48. }
  49. return p;
  50. }
  51. public boolean isInherited(String space, String element,
  52. String propertyName) {
  53. boolean b;
  54. Property.Maker propertyMaker = findMaker(space, element,
  55. propertyName);
  56. if (propertyMaker != null) {
  57. b = propertyMaker.isInherited();
  58. } else {
  59. // log.error("Unknown property " + propertyName);
  60. b = true;
  61. }
  62. return b;
  63. }
  64. public PropertyList makeList(String ns, String elementName, Attributes attributes,
  65. PropertyList parentPropertyList,
  66. FObj parentFO) throws FOPException {
  67. String space = "http://www.w3.org/TR/1999/XSL/Format";
  68. if (ns != null) {
  69. space = ns;
  70. }
  71. PropertyList par = null;
  72. if (parentPropertyList != null
  73. && space.equals(parentPropertyList.getNameSpace())) {
  74. par = parentPropertyList;
  75. }
  76. PropertyList p = new PropertyList(par, space,
  77. elementName);
  78. p.setBuilder(this);
  79. HashMap table;
  80. table = (HashMap)elementTable.get(elementName);
  81. /* Store names of properties already set. */
  82. StringBuffer propsDone = new StringBuffer(256);
  83. propsDone.append(' ');
  84. /*
  85. * If font-size is set on this FO, must set it first, since
  86. * other attributes specified in terms of "ems" depend on it.
  87. * When we do "shorthand" properties, must handle the "font"
  88. * property as well to see if font-size is set.
  89. */
  90. String fontsizeval = attributes.getValue(FONTSIZEATTR);
  91. if (fontsizeval != null) {
  92. Property.Maker propertyMaker = findMaker(table, FONTSIZEATTR);
  93. if (propertyMaker != null) {
  94. try {
  95. p.put(FONTSIZEATTR,
  96. propertyMaker.make(p, fontsizeval, parentFO));
  97. } catch (FOPException e) {}
  98. }
  99. // Put in the "done" list even if error or no Maker.
  100. propsDone.append(FONTSIZEATTR + ' ');
  101. }
  102. for (int i = 0; i < attributes.getLength(); i++) {
  103. String attributeName = attributes.getQName(i);
  104. /* Handle "compound" properties, ex. space-before.minimum */
  105. int sepchar = attributeName.indexOf('.');
  106. String propName = attributeName;
  107. String subpropName = null;
  108. Property propVal = null;
  109. if (sepchar > -1) {
  110. propName = attributeName.substring(0, sepchar);
  111. subpropName = attributeName.substring(sepchar + 1);
  112. } else if (propsDone.toString().indexOf(' ' + propName + ' ')
  113. != -1) {
  114. // Already processed this property (base property
  115. // for a property with sub-components or font-size)
  116. continue;
  117. }
  118. Property.Maker propertyMaker = findMaker(table, propName);
  119. if (propertyMaker != null) {
  120. try {
  121. if (subpropName != null) {
  122. Property baseProp = p.getExplicitBaseProp(propName);
  123. if (baseProp == null) {
  124. // See if it is specified later in this list
  125. String baseValue = attributes.getValue(propName);
  126. if (baseValue != null) {
  127. baseProp = propertyMaker.make(p, baseValue,
  128. parentFO);
  129. propsDone.append(propName + ' ');
  130. }
  131. // else baseProp = propertyMaker.makeCompound(p, parentFO);
  132. }
  133. propVal = propertyMaker.make(baseProp, subpropName,
  134. p,
  135. attributes.getValue(i),
  136. parentFO);
  137. } else {
  138. propVal = propertyMaker.make(p,
  139. attributes.getValue(i),
  140. parentFO);
  141. }
  142. if (propVal != null) {
  143. p.put(propName, propVal);
  144. }
  145. } catch (FOPException e) { /* Do other props. */
  146. //log.error(e.getMessage());
  147. }
  148. } else {
  149. if (!attributeName.startsWith("xmlns")) {
  150. //log.error("property '"
  151. // + attributeName + "' ignored");
  152. }
  153. }
  154. }
  155. return p;
  156. }
  157. public Property getSubpropValue(String space, String element,
  158. String propertyName, Property p,
  159. String subpropName) {
  160. Property.Maker maker = findMaker(space, element, propertyName);
  161. if (maker != null) {
  162. return maker.getSubpropValue(p, subpropName);
  163. } else
  164. return null;
  165. }
  166. public boolean isCorrespondingForced(PropertyList propertyList,
  167. String space, String element,
  168. String propertyName) {
  169. Property.Maker propertyMaker = findMaker(space, element,
  170. propertyName);
  171. if (propertyMaker != null) {
  172. return propertyMaker.isCorrespondingForced(propertyList);
  173. } else {
  174. //log.error("no Maker for " + propertyName);
  175. }
  176. return false;
  177. }
  178. public Property getShorthand(PropertyList propertyList, String space,
  179. String element, String propertyName) {
  180. Property.Maker propertyMaker = findMaker(space, element,
  181. propertyName);
  182. if (propertyMaker != null) {
  183. return propertyMaker.getShorthand(propertyList);
  184. } else {
  185. //log.error("no Maker for " + propertyName);
  186. return null;
  187. }
  188. }
  189. public Property makeProperty(PropertyList propertyList, String space,
  190. String element,
  191. String propertyName) throws FOPException {
  192. Property p = null;
  193. Property.Maker propertyMaker = findMaker(space, element,
  194. propertyName);
  195. if (propertyMaker != null) {
  196. p = propertyMaker.make(propertyList);
  197. } else {
  198. //log.error("property " + propertyName
  199. // + " ignored");
  200. }
  201. return p;
  202. }
  203. protected Property.Maker findMaker(String space, String elementName,
  204. String propertyName) {
  205. return findMaker((HashMap)elementTable.get(elementName),
  206. propertyName);
  207. }
  208. /**
  209. * Convenience function to return the Maker for a given property
  210. * given the HashMap containing properties specific to this element.
  211. * If table is non-null and
  212. * @param elemTable Element-specific properties or null if none.
  213. * @param propertyName Name of property.
  214. * @return A Maker for this property.
  215. */
  216. private Property.Maker findMaker(HashMap elemTable,
  217. String propertyName) {
  218. Property.Maker propertyMaker = null;
  219. if (elemTable != null) {
  220. propertyMaker = (Property.Maker)elemTable.get(propertyName);
  221. }
  222. if (propertyMaker == null) {
  223. propertyMaker =
  224. (Property.Maker)propertyListTable.get(propertyName);
  225. }
  226. return propertyMaker;
  227. }
  228. }