[ Back | Previous | Next ]

How to auto-translate an object to a TemplateModel

Package:
freemarker.template.TemplateModel
Product:
FreeMarker
Release:
1.52
Related Links:
SimpleScalar
Comment:

This is how to call the translation:
/**
 * get method comment.
 */
public freemarker.template.TemplateModel get(java.lang.String key) throws freemarker.template.TemplateModelException
{
	// key is the name used in the template
	// to represent the data
	if (key.equals("id")) {
		return transformToScallar(getItem(),key,id);
	} else 	if (key.equals("title")) {
		return transformToScallar(getItem(),key,title);
	} else 	if (key.equals("from")) {
		return transformToScallar(getItem(),key,from);
	} else 	if (key.equals("product")) {
		return transformToScallar(getItem(),key,product);
	} else 	if (key.equals("release")) {
		return transformToScallar(getItem(),key,release);
	} else 	if (key.equals("name")) {
		return transformToScallar(getItem(),key,name);
	} else 	if (key.equals("comments")) {
		return transformToScallar(getItem(),key,comments);
	} else {
	}	
	return null;
}



/**
 * Insert the method's description here.
 * Creation date: (5/11/01 9:17:48 PM)
 * @return java.lang.Object
 * @param ref java.lang.Object
 * @param res java.sql.ResultSet
 */
public freemarker.template.TemplateModel transformToScallar(Object ref, String colName, freemarker.template.TemplateModel value)
{
	if (value == null)
	{
		try
		{
			try
			{
				java.lang.reflect.Field f = ref.getClass().getDeclaredField(colName.toLowerCase());
				//

				if (f != null)
				{
					//akaan.util.Debug.printDebugMessage("ScratchBookItemTM.transform()", colName + "=" + String.valueOf(f.get(ref)));
					return value = new freemarker.template.SimpleScalar(String.valueOf(f.get(ref)));
				}
			}
			catch (NoSuchFieldException nfe)
			{
				akaan.util.Debug.printDebugMessage("ScratchBook.transformResult", "skipping " + colName);
			}
			catch (IllegalArgumentException il)
			{
				akaan.util.Debug.printDebugMessage("ScratchBook.transformResult", "illegal value for " + colName);
			}
		}
		catch (Throwable t)
		{
			akaan.util.Debug.printThrowable("ScratchBook.transformResult", t);
		}
	}
	return value;
}
1