1. /* $Id: PathCallParamRule.java,v 1.8 2004/05/10 06:52:50 skitching Exp $
  2. *
  3. * Copyright 2003-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 containing the
  21. * <code>Digester</code> matching path for use by a surrounding
  22. * <code>CallMethodRule</code>. This Rule is most useful when making
  23. * extensive use of wildcards in rule patterns.</p>
  24. *
  25. * @since 1.6
  26. */
  27. public class PathCallParamRule extends Rule {
  28. // ----------------------------------------------------------- Constructors
  29. /**
  30. * Construct a "call parameter" rule that will save the body text of this
  31. * element as the parameter value.
  32. *
  33. * @param paramIndex The zero-relative parameter number
  34. */
  35. public PathCallParamRule(int paramIndex) {
  36. this.paramIndex = paramIndex;
  37. }
  38. // ----------------------------------------------------- Instance Variables
  39. /**
  40. * The zero-relative index of the parameter we are saving.
  41. */
  42. protected int paramIndex = 0;
  43. // --------------------------------------------------------- Public Methods
  44. /**
  45. * Process the start of this element.
  46. *
  47. * @param namespace the namespace URI of the matching element, or an
  48. * empty string if the parser is not namespace aware or the element has
  49. * no namespace
  50. * @param name the local name if the parser is namespace aware, or just
  51. * the element name otherwise
  52. * @param attributes The attribute list for this element
  53. */
  54. public void begin(String namespace, String name, Attributes attributes) throws Exception {
  55. String param = getDigester().getMatch();
  56. if(param != null) {
  57. Object parameters[] = (Object[]) digester.peekParams();
  58. parameters[paramIndex] = param;
  59. }
  60. }
  61. /**
  62. * Render a printable version of this Rule.
  63. */
  64. public String toString() {
  65. StringBuffer sb = new StringBuffer("PathCallParamRule[");
  66. sb.append("paramIndex=");
  67. sb.append(paramIndex);
  68. sb.append("]");
  69. return (sb.toString());
  70. }
  71. }