📄 cmssetnextrule.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/configuration/CmsSetNextRule.java,v $
* Date : $Date: 2006/03/27 14:52:46 $
* Version: $Revision: 1.8 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* This file is based upon:
* org.apache.commons.digester.CallMethodRule.
*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opencms.configuration;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsLog;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.logging.Log;
import org.xml.sax.Attributes;
/**
* Rule implementation that invokes a method on the (top-1) (parent) object,
* passing as implicit first argument of type <code>{@link org.opencms.file.CmsObject}</code>
* and as a further argument the top stack instance. <p>
*
* If no subsequent <code>CallParamRule</code> are matched for <code>CmsObject</code>
* which is the case in the OpenCms usage the first argument <code>CmsObject</code>
* will be null at method invocation time. <p>
* This is an alternative for <code>{@link org.apache.commons.digester.SetNextRule}</code>
* if a parent to child-property configuration has been done but the setter for that
* property requires additional arguments that are only available at real runtime
* of the application.<p>
*
* The top stack element (child) that has to be set is matched against the constructor
* given <code>{@link java.lang.Class}[]</code>: It is used as argument on the position
* where the <code>Class[]</code> has an instance of the same type as it's own <code>Class</code>.<p>
*
* @see org.apache.commons.digester.CallMethodRule
* @see org.apache.commons.digester.SetNextRule
*
* @author Craig McClanahan
* @author Achim Westermann
*
* @version $Revision: 1.8 $
*
* @since 6.0.0
*/
public class CmsSetNextRule extends Rule {
/** The log object of this class. */
private static final Log LOG = CmsLog.getLog(CmsSetNextRule.class);
/**
* The body text collected from this element.
*/
protected String m_bodyText = null;
/**
* The method name to call on the parent object.
*/
protected String m_methodName = null;
/**
* The number of parameters to collect from <code>MethodParam</code> rules.
* If this value is zero, a single parameter will be collected from the
* body of this element.
*/
protected int m_paramCount = 0;
/**
* The parameter types of the parameters to be collected.
*/
protected Class[] m_paramTypes = null;
/**
* Should <code>MethodUtils.invokeExactMethod</code> be used for reflection.
*/
protected boolean m_useExactMatch = false;
/**
* The names of the classes of the parameters to be collected.
* This attribute allows creation of the classes to be postponed until the digester is set.
*/
private String[] m_paramClassNames = null;
/**
* location of the target object for the call, relative to the
* top of the digester object stack. The default value of zero
* means the target object is the one on top of the stack.
*/
private int m_targetOffset = 0;
/**
* Construct a "call method" rule with the specified method name.<p>
*
*
* The 1<sup>st</sup> argument of the method will be of type <code>{@link CmsObject}</code>.
* It's value will remain null (except subsequent
* <code>{@link org.apache.commons.digester.CallParamRule}</code> would put a value
* which currently is impossible at initialization time within OpenCms).<p>
*
* The 2<sup>nd</sup> argument will be the top-stack element at digestion time.
* That instance has to be of the same type as the <code>clazz</code> argument to succeed.<p>
*
*
* @param methodName Method name of the parent method to call
* @param clazz The class of the top-stack element (child) that will be present at digestion-time
*/
public CmsSetNextRule(String methodName, Class clazz) {
this(methodName, new Class[] {clazz});
}
/**
* Construct a "call method" rule with the specified method name
* and additional parameters.<p>
*
*
* The 1<sup>st</sup> argument of the method will be of type <code>{@link CmsObject}</code>.
* It's value will remain null (except subsequent
* <code>{@link org.apache.commons.digester.CallParamRule}</code> would put a value
* which currently is impossible at initialization time within OpenCms).<p>
*
* The further arguments will be filled by the subsequent <code>{@link org.apache.commons.digester.CallParamRule}</code>
* matches. If the first <code>Class</code> in the given array matches the top stack element
* (child) that value will be used. If at digestion time no parameters are found for the given
* types their values for invocation of the method remain null.<p>
*
*
* @param methodName Method name of the parent method to call
* @param clazzes an array with all parameter types for the method to invoke at digestion time
*/
public CmsSetNextRule(String methodName, Class[] clazzes) {
m_targetOffset = 0;
m_methodName = methodName;
m_paramCount = clazzes.length + 1;
m_paramTypes = new Class[m_paramCount];
m_paramTypes[0] = CmsObject.class;
System.arraycopy(clazzes, 0, m_paramTypes, 1, clazzes.length);
}
/**
* Process the start of this element.
*
* @param attributes The attribute list for this element
* @param namespace the namespace URI of the matching element, or an empty string if the parser is not namespace
* aware or the element has no namespace
* @param name the local name if the parser is namespace aware, or just the element name otherwise
* @throws Exception if something goes wrong
*/
public void begin(java.lang.String namespace, java.lang.String name, Attributes attributes) throws Exception {
// not now: 6.0.0
//digester.setLogger(CmsLog.getLog(digester.getClass()));
// Push an array to capture the parameter values if necessary
if (m_paramCount > 0) {
Object[] parameters = new Object[m_paramCount];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = null;
}
digester.pushParams(parameters);
}
}
/**
* Process the body text of this element.<p>
*
* @param bodyText The body text of this element
* @param namespace the namespace URI of the matching element, or an empty string if the parser is not namespace
* aware or the element has no namespace
* @param name the local name if the parser is namespace aware, or just the element name otherwise
* @throws Exception if something goes wrong
*/
public void body(java.lang.String namespace, java.lang.String name, String bodyText) throws Exception {
if (m_paramCount == 0) {
m_bodyText = bodyText.trim();
}
}
/**
* Process the end of this element.<p>
*
* @param namespace the namespace URI of the matching element, or an empty string if the parser is not namespace
* aware or the element has no namespace
* @param name the local name if the parser is namespace aware, or just the element name otherwise
* @throws Exception if something goes wrong
*/
public void end(java.lang.String namespace, java.lang.String name) throws Exception {
// Determine the target object for the method call: the parent object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -