/*
* (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.converter.ObjectConverter;
import cookxml.core.exception.SetterException;
import cookxml.core.interfaces.Handler;
import cookxml.core.interfaces.Setter;
/**
* This class is a way to generate a setter that hooks a function call to a attribute.
*
* @author Heng Yuan
* @version $Id: HookReferenceSetter.java 266 2007-06-10 18:47:58Z coconut $
* @since CookXml 1.0
*/
public class HookReferenceSetter implements Setter
{
private final String m_funcName;
private final Class m_targetClass;
public HookReferenceSetter (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 = ObjectConverter.getInstance ().convert ((String)value, decodeEngine);
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);
}
}