1. /* $Id: ObjectParamRule.java,v 1.10 2004/05/10 06:52:50 skitching Exp $
  2. *
  3. * Copyright 2002-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 org.xml.sax.Attributes;
  19. /**
  20. * <p>Rule implementation that saves a parameter for use by a surrounding
  21. * <code>CallMethodRule<code>.</p>
  22. *
  23. * <p>This parameter may be:
  24. * <ul>
  25. * <li>an arbitrary Object defined programatically, assigned when the element pattern associated with the Rule is matched
  26. * See {@link #ObjectParamRule(int paramIndex, Object param)}
  27. * <li>an arbitrary Object defined programatically, assigned if the element pattern AND specified attribute name are matched
  28. * See {@link #ObjectParamRule(int paramIndex, String attributeName, Object param)}
  29. * </ul>
  30. * </p>
  31. *
  32. * @since 1.4
  33. */
  34. public class ObjectParamRule extends Rule {
  35. // ----------------------------------------------------------- Constructors
  36. /**
  37. * Construct a "call parameter" rule that will save the given Object as
  38. * the parameter value.
  39. *
  40. * @param paramIndex The zero-relative parameter number
  41. * @param param the parameter to pass along
  42. */
  43. public ObjectParamRule(int paramIndex, Object param) {
  44. this(paramIndex, null, param);
  45. }
  46. /**
  47. * Construct a "call parameter" rule that will save the given Object as
  48. * the parameter value, provided that the specified attribute exists.
  49. *
  50. * @param paramIndex The zero-relative parameter number
  51. * @param attributeName The name of the attribute to match
  52. * @param param the parameter to pass along
  53. */
  54. public ObjectParamRule(int paramIndex, String attributeName, Object param) {
  55. this.paramIndex = paramIndex;
  56. this.attributeName = attributeName;
  57. this.param = param;
  58. }
  59. // ----------------------------------------------------- Instance Variables
  60. /**
  61. * The attribute which we are attempting to match
  62. */
  63. protected String attributeName = null;
  64. /**
  65. * The zero-relative index of the parameter we are saving.
  66. */
  67. protected int paramIndex = 0;
  68. /**
  69. * The parameter we wish to pass to the method call
  70. */
  71. protected Object param = null;
  72. // --------------------------------------------------------- Public Methods
  73. /**
  74. * Process the start of this element.
  75. *
  76. * @param attributes The attribute list for this element
  77. */
  78. public void begin(String namespace, String name,
  79. Attributes attributes) throws Exception {
  80. Object anAttribute = null;
  81. Object parameters[] = (Object[]) digester.peekParams();
  82. if (attributeName != null) {
  83. anAttribute = attributes.getValue(attributeName);
  84. if(anAttribute != null) {
  85. parameters[paramIndex] = param;
  86. }
  87. // note -- if attributeName != null and anAttribute == null, this rule
  88. // will pass null as its parameter!
  89. }else{
  90. parameters[paramIndex] = param;
  91. }
  92. }
  93. /**
  94. * Render a printable version of this Rule.
  95. */
  96. public String toString() {
  97. StringBuffer sb = new StringBuffer("ObjectParamRule[");
  98. sb.append("paramIndex=");
  99. sb.append(paramIndex);
  100. sb.append(", attributeName=");
  101. sb.append(attributeName);
  102. sb.append(", param=");
  103. sb.append(param);
  104. sb.append("]");
  105. return (sb.toString());
  106. }
  107. }