1. /* $Id: SetPropertiesRule.java,v 1.19 2004/05/10 06:30:06 skitching Exp $
  2. *
  3. * Copyright 2001-2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.digester;
  18. import java.util.HashMap;
  19. import org.apache.commons.beanutils.BeanUtils;
  20. import org.xml.sax.Attributes;
  21. /**
  22. * <p>Rule implementation that sets properties on the object at the top of the
  23. * stack, based on attributes with corresponding names.</p>
  24. *
  25. * <p>This rule supports custom mapping of attribute names to property names.
  26. * The default mapping for particular attributes can be overridden by using
  27. * {@link #SetPropertiesRule(String[] attributeNames, String[] propertyNames)}.
  28. * This allows attributes to be mapped to properties with different names.
  29. * Certain attributes can also be marked to be ignored.</p>
  30. */
  31. public class SetPropertiesRule extends Rule {
  32. // ----------------------------------------------------------- Constructors
  33. /**
  34. * Default constructor sets only the the associated Digester.
  35. *
  36. * @param digester The digester with which this rule is associated
  37. *
  38. * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
  39. * Use {@link #SetPropertiesRule()} instead.
  40. */
  41. public SetPropertiesRule(Digester digester) {
  42. this();
  43. }
  44. /**
  45. * Base constructor.
  46. */
  47. public SetPropertiesRule() {
  48. // nothing to set up
  49. }
  50. /**
  51. * <p>Convenience constructor overrides the mapping for just one property.</p>
  52. *
  53. * <p>For details about how this works, see
  54. * {@link #SetPropertiesRule(String[] attributeNames, String[] propertyNames)}.</p>
  55. *
  56. * @param attributeName map this attribute
  57. * @param propertyName to a property with this name
  58. */
  59. public SetPropertiesRule(String attributeName, String propertyName) {
  60. attributeNames = new String[1];
  61. attributeNames[0] = attributeName;
  62. propertyNames = new String[1];
  63. propertyNames[0] = propertyName;
  64. }
  65. /**
  66. * <p>Constructor allows attribute->property mapping to be overriden.</p>
  67. *
  68. * <p>Two arrays are passed in.
  69. * One contains the attribute names and the other the property names.
  70. * The attribute name / property name pairs are match by position
  71. * In order words, the first string in the attribute name list matches
  72. * to the first string in the property name list and so on.</p>
  73. *
  74. * <p>If a property name is null or the attribute name has no matching
  75. * property name, then this indicates that the attibute should be ignored.</p>
  76. *
  77. * <h5>Example One</h5>
  78. * <p> The following constructs a rule that maps the <code>alt-city</code>
  79. * attribute to the <code>city</code> property and the <code>alt-state</code>
  80. * to the <code>state</code> property.
  81. * All other attributes are mapped as usual using exact name matching.
  82. * <code><pre>
  83. * SetPropertiesRule(
  84. * new String[] {"alt-city", "alt-state"},
  85. * new String[] {"city", "state"});
  86. * </pre></code>
  87. *
  88. * <h5>Example Two</h5>
  89. * <p> The following constructs a rule that maps the <code>class</code>
  90. * attribute to the <code>className</code> property.
  91. * The attribute <code>ignore-me</code> is not mapped.
  92. * All other attributes are mapped as usual using exact name matching.
  93. * <code><pre>
  94. * SetPropertiesRule(
  95. * new String[] {"class", "ignore-me"},
  96. * new String[] {"className"});
  97. * </pre></code>
  98. *
  99. * @param attributeNames names of attributes to map
  100. * @param propertyNames names of properties mapped to
  101. */
  102. public SetPropertiesRule(String[] attributeNames, String[] propertyNames) {
  103. // create local copies
  104. this.attributeNames = new String[attributeNames.length];
  105. for (int i=0, size=attributeNames.length; i<size; i++) {
  106. this.attributeNames[i] = attributeNames[i];
  107. }
  108. this.propertyNames = new String[propertyNames.length];
  109. for (int i=0, size=propertyNames.length; i<size; i++) {
  110. this.propertyNames[i] = propertyNames[i];
  111. }
  112. }
  113. // ----------------------------------------------------- Instance Variables
  114. /**
  115. * Attribute names used to override natural attribute->property mapping
  116. */
  117. private String [] attributeNames;
  118. /**
  119. * Property names used to override natural attribute->property mapping
  120. */
  121. private String [] propertyNames;
  122. // --------------------------------------------------------- Public Methods
  123. /**
  124. * Process the beginning of this element.
  125. *
  126. * @param attributes The attribute list of this element
  127. */
  128. public void begin(Attributes attributes) throws Exception {
  129. // Build a set of attribute names and corresponding values
  130. HashMap values = new HashMap();
  131. // set up variables for custom names mappings
  132. int attNamesLength = 0;
  133. if (attributeNames != null) {
  134. attNamesLength = attributeNames.length;
  135. }
  136. int propNamesLength = 0;
  137. if (propertyNames != null) {
  138. propNamesLength = propertyNames.length;
  139. }
  140. for (int i = 0; i < attributes.getLength(); i++) {
  141. String name = attributes.getLocalName(i);
  142. if ("".equals(name)) {
  143. name = attributes.getQName(i);
  144. }
  145. String value = attributes.getValue(i);
  146. // we'll now check for custom mappings
  147. for (int n = 0; n<attNamesLength; n++) {
  148. if (name.equals(attributeNames[n])) {
  149. if (n < propNamesLength) {
  150. // set this to value from list
  151. name = propertyNames[n];
  152. } else {
  153. // set name to null
  154. // we'll check for this later
  155. name = null;
  156. }
  157. break;
  158. }
  159. }
  160. if (digester.log.isDebugEnabled()) {
  161. digester.log.debug("[SetPropertiesRule]{" + digester.match +
  162. "} Setting property '" + name + "' to '" +
  163. value + "'");
  164. }
  165. if (name != null) {
  166. values.put(name, value);
  167. }
  168. }
  169. // Populate the corresponding properties of the top object
  170. Object top = digester.peek();
  171. if (digester.log.isDebugEnabled()) {
  172. if (top != null) {
  173. digester.log.debug("[SetPropertiesRule]{" + digester.match +
  174. "} Set " + top.getClass().getName() +
  175. " properties");
  176. } else {
  177. digester.log.debug("[SetPropertiesRule]{" + digester.match +
  178. "} Set NULL properties");
  179. }
  180. }
  181. BeanUtils.populate(top, values);
  182. }
  183. /**
  184. * <p>Add an additional attribute name to property name mapping.
  185. * This is intended to be used from the xml rules.
  186. */
  187. public void addAlias(String attributeName, String propertyName) {
  188. // this is a bit tricky.
  189. // we'll need to resize the array.
  190. // probably should be synchronized but digester's not thread safe anyway
  191. if (attributeNames == null) {
  192. attributeNames = new String[1];
  193. attributeNames[0] = attributeName;
  194. propertyNames = new String[1];
  195. propertyNames[0] = propertyName;
  196. } else {
  197. int length = attributeNames.length;
  198. String [] tempAttributes = new String[length + 1];
  199. for (int i=0; i<length; i++) {
  200. tempAttributes[i] = attributeNames[i];
  201. }
  202. tempAttributes[length] = attributeName;
  203. String [] tempProperties = new String[length + 1];
  204. for (int i=0; i<length && i< propertyNames.length; i++) {
  205. tempProperties[i] = propertyNames[i];
  206. }
  207. tempProperties[length] = propertyName;
  208. propertyNames = tempProperties;
  209. attributeNames = tempAttributes;
  210. }
  211. }
  212. /**
  213. * Render a printable version of this Rule.
  214. */
  215. public String toString() {
  216. StringBuffer sb = new StringBuffer("SetPropertiesRule[");
  217. sb.append("]");
  218. return (sb.toString());
  219. }
  220. }