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

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

There is a newer version: 0.4.3
Show newest version
/*
 * (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 cookxml.core.DecodeEngine;
import cookxml.core.FunctionHandler;
import cookxml.core.exception.SetterException;
import cookxml.core.interfaces.Handler;
import cookxml.core.interfaces.Setter;
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.
 * 
 * @see HookReferenceSetter
 * @author Heng Yuan
 * @version $Id: HookVariableSetter.java 266 2007-06-10 18:47:58Z coconut $
 * @since CookXml 1.0
 */
public class HookVariableSetter implements Setter
{
	private final String m_funcName;
	private final Class m_targetClass;

	public HookVariableSetter (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 || !(value instanceof String))
			return;

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

			Object fieldObj = null;

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

			Handler handler = decodeEngine.getHandler (ns, tag, 'v', 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, 'v', 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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy