NIHVIVO-3114 Fix problems with dump of IndividualTemplateModel.qrData()
This commit is contained in:
parent
d438bca997
commit
1f9bcac9b1
4 changed files with 41 additions and 14 deletions
|
@ -58,9 +58,9 @@ import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
|
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.jena.ExtendedLinkedDataUtils;
|
import edu.cornell.mannlib.vitro.webapp.utils.jena.ExtendedLinkedDataUtils;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
|
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividual;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividual;
|
||||||
import freemarker.ext.beans.ReadOnlyBeansWrapper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles requests for entity information.
|
* Handles requests for entity information.
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
package freemarker.ext.beans;
|
package edu.cornell.mannlib.vitro.webapp.web.beanswrappers;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
/** BeansWrapper that is more restrictive than EXPOSE_SAFE, by
|
import freemarker.ext.beans.BeansWrapper;
|
||||||
|
import freemarker.ext.beans.BeansWrapper.MethodAppearanceDecision;
|
||||||
|
|
||||||
|
/** A BeansWrapper that is more restrictive than EXPOSE_SAFE, by
|
||||||
* exposing getters but not setters. A setter is defined for this
|
* exposing getters but not setters. A setter is defined for this
|
||||||
* purpose as a method that returns void, or whose name
|
* purpose as a method that returns void, or whose name
|
||||||
* starts with "set".
|
* starts with "set". It also hides built-in methods of Java
|
||||||
|
* utility classes like Map.put(), etc.
|
||||||
*
|
*
|
||||||
* @author rjy7
|
* @author rjy7
|
||||||
*
|
*
|
||||||
|
@ -27,12 +31,26 @@ public class ReadOnlyBeansWrapper extends BeansWrapper {
|
||||||
@Override
|
@Override
|
||||||
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) {
|
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) {
|
||||||
|
|
||||||
// How to define a setter? This is an approximation: a method whose name
|
// How to define a setter? This is a weak approximation: a method whose name
|
||||||
// starts with "set" or returns void.
|
// starts with "set" or returns void.
|
||||||
if ( method.getName().startsWith("set") ) {
|
if ( method.getName().startsWith("set") ) {
|
||||||
decision.setExposeMethodAs(null);
|
decision.setExposeMethodAs(null);
|
||||||
|
|
||||||
} else if ( method.getReturnType().getName().equals("void") ) {
|
} else if ( method.getReturnType().getName().equals("void") ) {
|
||||||
decision.setExposeMethodAs(null);
|
decision.setExposeMethodAs(null);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
Class<?> declaringClass = method.getDeclaringClass();
|
||||||
|
if (declaringClass.equals(java.lang.Object.class)) {
|
||||||
|
decision.setExposeMethodAs(null);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Package pkg = declaringClass.getPackage();
|
||||||
|
if (pkg.getName().equals("java.util")) {
|
||||||
|
decision.setExposeMethodAs(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
package freemarker.ext.beans;
|
package freemarker.ext.beans;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to extract wrapper used to wrap an object into a template model object.
|
* Class to extract wrapper used to wrap an object into a template model object.
|
||||||
* Used as workaround to gap in Freemarker template model API (can't get wrapper
|
* Used as workaround to gap in Freemarker template model API (can't get wrapper
|
||||||
|
@ -16,4 +18,5 @@ public class WrapperExtractor {
|
||||||
public static int getWrapperExposureLevel(BeanModel model) {
|
public static int getWrapperExposureLevel(BeanModel model) {
|
||||||
return model.wrapper.getExposureLevel();
|
return model.wrapper.getExposureLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import freemarker.core.Environment;
|
||||||
import freemarker.ext.beans.BeanModel;
|
import freemarker.ext.beans.BeanModel;
|
||||||
import freemarker.ext.beans.BeansWrapper;
|
import freemarker.ext.beans.BeansWrapper;
|
||||||
import freemarker.ext.beans.CollectionModel;
|
import freemarker.ext.beans.CollectionModel;
|
||||||
|
import freemarker.ext.beans.MapModel;
|
||||||
import freemarker.ext.beans.SimpleMethodModel;
|
import freemarker.ext.beans.SimpleMethodModel;
|
||||||
import freemarker.ext.beans.StringModel;
|
import freemarker.ext.beans.StringModel;
|
||||||
import freemarker.ext.beans.WrapperExtractor;
|
import freemarker.ext.beans.WrapperExtractor;
|
||||||
|
@ -175,6 +176,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> getDump(TemplateModel model) throws TemplateModelException {
|
private Map<String, Object> getDump(TemplateModel model) throws TemplateModelException {
|
||||||
|
|
||||||
Map<String, Object> map = new HashMap<String, Object>();
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
|
||||||
// Don't return null if model == null. We still want to send the map to the template.
|
// Don't return null if model == null. We still want to send the map to the template.
|
||||||
|
@ -308,7 +310,8 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
if ( unwrappedModel instanceof Map ) {
|
if ( unwrappedModel instanceof Map ) {
|
||||||
return getMapDump(model);
|
return getMapDump(model);
|
||||||
}
|
}
|
||||||
// Java objects are wrapped as TemplateHashModelEx-s.
|
|
||||||
|
// Java objects are wrapped as TemplateHashModelEx-s.
|
||||||
return getObjectDump(model, unwrappedModel);
|
return getObjectDump(model, unwrappedModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,13 +323,18 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
TemplateModelIterator iModel = keys.iterator();
|
TemplateModelIterator iModel = keys.iterator();
|
||||||
while (iModel.hasNext()) {
|
while (iModel.hasNext()) {
|
||||||
String key = iModel.next().toString();
|
String key = iModel.next().toString();
|
||||||
|
// Workaround this oddity: model.object does not contain
|
||||||
|
// values for "empty" and "keys", but model.keys() does.
|
||||||
|
if ("class".equals(key) || "empty".equals(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
TemplateModel value = model.get(key);
|
TemplateModel value = model.get(key);
|
||||||
items.put(key, getDump(value));
|
items.put(key, getDump(value));
|
||||||
}
|
}
|
||||||
map.put(Key.VALUE.toString(), items);
|
map.put(Key.VALUE.toString(), items);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> getObjectDump(TemplateHashModelEx model, Object object) throws TemplateModelException {
|
private Map<String, Object> getObjectDump(TemplateHashModelEx model, Object object) throws TemplateModelException {
|
||||||
|
|
||||||
Map<String, Object> map = new HashMap<String, Object>();
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
@ -388,11 +396,9 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Else look for the entire methodName in the key set. Include those
|
|
||||||
// starting with "get" or "is" that were not found above.
|
// Else look for the entire methodName in the key set, to include
|
||||||
// NB This does not properly account for methods exposed as properties
|
// those that are exposed as methods rather than properties.
|
||||||
// using BeansWrapper.finetuneMethodAppearance(), and perhaps other
|
|
||||||
// changes to method exposure through that method.
|
|
||||||
if (keySet.contains(methodName)) {
|
if (keySet.contains(methodName)) {
|
||||||
String methodDisplayName = getMethodDisplayName(method);
|
String methodDisplayName = getMethodDisplayName(method);
|
||||||
// If no arguments, invoke the method to get the result
|
// If no arguments, invoke the method to get the result
|
||||||
|
|
Loading…
Add table
Reference in a new issue