All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.src.cookxml.common.setter.ObjectOrHookVariableSetter Maven / Gradle / Ivy

/*
 * (c) Copyright 2004 by Heng Yuan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package cookxml.common.setter;

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import cookxml.core.DecodeEngine;
import cookxml.core.FunctionHandler;
import cookxml.core.doclet.DocletActionReporter;
import cookxml.core.exception.SetterException;
import cookxml.core.interfaces.Handler;
import cookxml.core.interfaces.Setter;
import cookxml.core.interfaces.TagLibrary;
import cookxml.core.util.ClassUtils;

/**
 * This class is a way to generate a setter that hooks a function call to a attribute, similar
 * to HookReferenceSetter.  The difference is that the attribute value is the name of the
 * variable of the variable object.
 *
 * @cxdoc
 * If the value is a string, then it is a variable name which is used to lookup the
 * actual object, else the value is used as is.
 *
 * @see HookReferenceSetter
 * @author Heng Yuan
 * @version $Id: ObjectOrHookVariableSetter.java 266 2007-06-10 18:47:58Z coconut $
 * @since CookXml 1.0
 */
public class ObjectOrHookVariableSetter implements Setter, DocletActionReporter
{
	private final String m_funcName;
	private final Class m_targetClass;

	public ObjectOrHookVariableSetter (String funcName, Class targetClass)
	{
		m_funcName = funcName;
		m_targetClass = targetClass;
	}

	public void setAttribute (String ns, String tag, String attrNS, String attr, Object obj, Object value, DecodeEngine decodeEngine) throws SetterException
	{
		if (value == null)
			return;

		try
		{
			String funcName = m_funcName;
			Class targetClass = m_targetClass;

			Object fieldObj = null;

			if (value instanceof String)
			{
				if (!ClassUtils.NULL_VAR.equals (value))
				{
					fieldObj = decodeEngine.getVariable ((String)value);
					if (fieldObj == null)
						return;
					if (!targetClass.isAssignableFrom (fieldObj.getClass ()))
						return;
				}
			}
			else if (targetClass == null || targetClass.isInstance (value))
			{
				fieldObj = value;
			}

			Handler handler = decodeEngine.getHandler (ns, tag, 'f', funcName, targetClass);

			// get the handler first.
			// if the handler is not in cache, then we need to find it and then put
			// it into the cache.
			if (handler == null)
			{
				handler = FunctionHandler.getHandler (obj, funcName, targetClass);
				if (handler != null)
					decodeEngine.setHandler (ns, tag, 'f', funcName, targetClass, handler);
			}

			if (handler != null)
			{
				handler.invoke (ns, obj, fieldObj, decodeEngine);
				return;
			}
		}
		catch (Exception ex)
		{
			throw new SetterException (decodeEngine, ex, this, ns, tag, attrNS, attr, obj, value);
		}
		throw new SetterException (decodeEngine, null, this, ns, tag, attrNS, attr, obj, value);
	}

	public Object[] getActions (TagLibrary tagLibrary, String tagNS, String tag, String attrNS, String attr, Class cl)
	{
		if (cl == null || attr == null)
			return new Member[0];
		Method[] methods = cl.getMethods ();

		String funcName = m_funcName;
		Class valueClass = m_targetClass;
		Set set = new HashSet ();
		for (int i = 0; i < methods.length; ++i)
		{
			Method method = methods[i];
			if (!funcName.equals (method.getName ()))
				continue;
			Class[] params = method.getParameterTypes ();
			if (params.length != 1)
				continue;
			if (params[0].isAssignableFrom (valueClass))
				set.add (method);
		}

		return set.toArray ();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy