001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/importexport/SetParentRule.java,v 1.7 2007/01/15 10:27:01 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.7 $
005: * $Date: 2007/01/15 10:27:01 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Igor Manic
039: */
040: package com.mvnforum.admin.importexport;
041:
042: import org.apache.commons.beanutils.MethodUtils;
043: import org.apache.commons.digester.Digester;
044: import org.apache.commons.digester.Rule;
045: import org.xml.sax.Attributes;
046:
047: /**
048: * @author Igor Manic
049: * @version $Revision: 1.7 $, $Date: 2007/01/15 10:27:01 $
050: * <br/>
051: * <code>SetParentRule</code> implements a digester rule that calls a "set parent"
052: * method on the top (child) object, passing the (top-1) (parent) object as an argument.
053: * This is same as in <code>SetTopRule</code>, except that this rule is calling
054: * the desired method at the XML element begin (not at the end).
055: */
056: public class SetParentRule extends Rule {
057:
058: protected String methodName = null;
059: protected String paramType = null;
060: protected boolean useExactMatch = false;
061:
062: public SetParentRule(Digester digester, String methodName) {
063: this (methodName);
064: }
065:
066: public SetParentRule(Digester digester, String methodName,
067: String paramType) {
068: this (methodName, paramType);
069: }
070:
071: public SetParentRule(String methodName) {
072: this (methodName, null);
073: }
074:
075: public SetParentRule(String methodName, String paramType) {
076: this .methodName = methodName;
077: this .paramType = paramType;
078: }
079:
080: public boolean isExactMatch() {
081: return useExactMatch;
082: }
083:
084: public void setExactMatch(boolean useExactMatch) {
085: this .useExactMatch = useExactMatch;
086: }
087:
088: public String toString() {
089: StringBuffer sb = new StringBuffer("SetParentRule[");
090: sb.append("methodName=");
091: sb.append(methodName);
092: sb.append(", paramType=");
093: sb.append(paramType);
094: sb.append("]");
095: return (sb.toString());
096: }
097:
098: public void begin(String namespace, String name,
099: Attributes attributes) throws Exception {
100: begin(attributes);
101: }
102:
103: public void begin(Attributes attributes) throws Exception {
104: // Identify the objects to be used
105: Object child = digester.peek(0);
106: Object parent = digester.peek(1);
107:
108: /*
109: if (digester.log.isDebugEnabled()) {
110: if (child == null) {
111: digester.log.debug("[SetParentRule]{" + digester.match +
112: "} Call [NULL CHILD]." +
113: methodName + "(" + parent + ")");
114: } else {
115: digester.log.debug("[SetParentRule]{" + digester.match +
116: "} Call " + child.getClass().getName() + "." +
117: methodName + "(" + parent + ")");
118: }
119: }*/
120:
121: // Call the specified method
122: Class paramTypes[] = new Class[1];
123: if (paramType != null) {
124: paramTypes[0] = digester.getClassLoader().loadClass(
125: paramType);
126: } else {
127: paramTypes[0] = parent.getClass();
128: }
129:
130: if (useExactMatch) {
131: MethodUtils.invokeExactMethod(child, methodName,
132: new Object[] { parent }, paramTypes);
133: } else {
134: MethodUtils.invokeMethod(child, methodName,
135: new Object[] { parent }, paramTypes);
136: }
137: }
138:
139: }
|