001: /* $Id: ObjectParamRule.java 471661 2006-11-06 08:09:25Z skitching $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.commons.digester;
020:
021: import org.xml.sax.Attributes;
022:
023: /**
024: * <p>Rule implementation that saves a parameter for use by a surrounding
025: * <code>CallMethodRule<code>.</p>
026: *
027: * <p>This parameter may be:
028: * <ul>
029: * <li>an arbitrary Object defined programatically, assigned when the element
030: * pattern associated with the Rule is matched. See
031: * {@link #ObjectParamRule(int paramIndex, Object param)}.
032: * <li>an arbitrary Object defined programatically, assigned if the element
033: * pattern AND specified attribute name are matched. See
034: * {@link #ObjectParamRule(int paramIndex, String attributeName, Object param)}.
035: * </ul>
036: * </p>
037: *
038: * @since 1.4
039: */
040:
041: public class ObjectParamRule extends Rule {
042: // ----------------------------------------------------------- Constructors
043: /**
044: * Construct a "call parameter" rule that will save the given Object as
045: * the parameter value.
046: *
047: * @param paramIndex The zero-relative parameter number
048: * @param param the parameter to pass along
049: */
050: public ObjectParamRule(int paramIndex, Object param) {
051: this (paramIndex, null, param);
052: }
053:
054: /**
055: * Construct a "call parameter" rule that will save the given Object as
056: * the parameter value, provided that the specified attribute exists.
057: *
058: * @param paramIndex The zero-relative parameter number
059: * @param attributeName The name of the attribute to match
060: * @param param the parameter to pass along
061: */
062: public ObjectParamRule(int paramIndex, String attributeName,
063: Object param) {
064: this .paramIndex = paramIndex;
065: this .attributeName = attributeName;
066: this .param = param;
067: }
068:
069: // ----------------------------------------------------- Instance Variables
070:
071: /**
072: * The attribute which we are attempting to match
073: */
074: protected String attributeName = null;
075:
076: /**
077: * The zero-relative index of the parameter we are saving.
078: */
079: protected int paramIndex = 0;
080:
081: /**
082: * The parameter we wish to pass to the method call
083: */
084: protected Object param = null;
085:
086: // --------------------------------------------------------- Public Methods
087:
088: /**
089: * Process the start of this element.
090: *
091: * @param attributes The attribute list for this element
092: */
093: public void begin(String namespace, String name,
094: Attributes attributes) throws Exception {
095: Object anAttribute = null;
096: Object parameters[] = (Object[]) digester.peekParams();
097:
098: if (attributeName != null) {
099: anAttribute = attributes.getValue(attributeName);
100: if (anAttribute != null) {
101: parameters[paramIndex] = param;
102: }
103: // note -- if attributeName != null and anAttribute == null, this rule
104: // will pass null as its parameter!
105: } else {
106: parameters[paramIndex] = param;
107: }
108: }
109:
110: /**
111: * Render a printable version of this Rule.
112: */
113: public String toString() {
114: StringBuffer sb = new StringBuffer("ObjectParamRule[");
115: sb.append("paramIndex=");
116: sb.append(paramIndex);
117: sb.append(", attributeName=");
118: sb.append(attributeName);
119: sb.append(", param=");
120: sb.append(param);
121: sb.append("]");
122: return (sb.toString());
123: }
124: }
|