001: /* $Id: ObjectParamRule.java 467222 2006-10-24 03:17:11Z markt $
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.tomcat.util.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 pattern associated with the Rule is matched
030: * See {@link #ObjectParamRule(int paramIndex, Object param)}
031: * <li>an arbitrary Object defined programatically, assigned if the element pattern AND specified attribute name are matched
032: * See {@link #ObjectParamRule(int paramIndex, String attributeName, Object param)}
033: * </ul>
034: * </p>
035: *
036: * @since 1.4
037: */
038:
039: public class ObjectParamRule extends Rule {
040: // ----------------------------------------------------------- Constructors
041: /**
042: * Construct a "call parameter" rule that will save the given Object as
043: * the parameter value.
044: *
045: * @param paramIndex The zero-relative parameter number
046: * @param param the parameter to pass along
047: */
048: public ObjectParamRule(int paramIndex, Object param) {
049: this (paramIndex, null, param);
050: }
051:
052: /**
053: * Construct a "call parameter" rule that will save the given Object as
054: * the parameter value, provided that the specified attribute exists.
055: *
056: * @param paramIndex The zero-relative parameter number
057: * @param attributeName The name of the attribute to match
058: * @param param the parameter to pass along
059: */
060: public ObjectParamRule(int paramIndex, String attributeName,
061: Object param) {
062: this .paramIndex = paramIndex;
063: this .attributeName = attributeName;
064: this .param = param;
065: }
066:
067: // ----------------------------------------------------- Instance Variables
068:
069: /**
070: * The attribute which we are attempting to match
071: */
072: protected String attributeName = null;
073:
074: /**
075: * The zero-relative index of the parameter we are saving.
076: */
077: protected int paramIndex = 0;
078:
079: /**
080: * The parameter we wish to pass to the method call
081: */
082: protected Object param = null;
083:
084: // --------------------------------------------------------- Public Methods
085:
086: /**
087: * Process the start of this element.
088: *
089: * @param attributes The attribute list for this element
090: */
091: public void begin(String namespace, String name,
092: Attributes attributes) throws Exception {
093: Object anAttribute = null;
094: Object parameters[] = (Object[]) digester.peekParams();
095:
096: if (attributeName != null) {
097: anAttribute = attributes.getValue(attributeName);
098: if (anAttribute != null) {
099: parameters[paramIndex] = param;
100: }
101: // note -- if attributeName != null and anAttribute == null, this rule
102: // will pass null as its parameter!
103: } else {
104: parameters[paramIndex] = param;
105: }
106: }
107:
108: /**
109: * Render a printable version of this Rule.
110: */
111: public String toString() {
112: StringBuffer sb = new StringBuffer("ObjectParamRule[");
113: sb.append("paramIndex=");
114: sb.append(paramIndex);
115: sb.append(", attributeName=");
116: sb.append(attributeName);
117: sb.append(", param=");
118: sb.append(param);
119: sb.append("]");
120: return (sb.toString());
121: }
122: }
|