An object wrapper that wraps Jython objects into FreeMarker template models and vice versa. @version $Id: JythonWrapper.java,v 1.25 2005/10/04 12:08:25 revusky Exp $ @author Attila Szegedi
| 91 | * @author Attila Szegedi |
| 92 | */ |
| 93 | public class JythonWrapper implements ObjectWrapper |
| 94 | { |
| 95 | private static final Class PYOBJECT_CLASS = PyObject.class; |
| 96 | public static final JythonWrapper INSTANCE = new JythonWrapper(); |
| 97 | |
| 98 | private final ModelCache modelCache = new JythonModelCache(this); |
| 99 | |
| 100 | private boolean attributesShadowItems = true; |
| 101 | |
| 102 | public JythonWrapper() |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Sets whether this wrapper caches model instances. Default is false. |
| 108 | * When set to true, calling {@link #wrap(Object)} multiple times for |
| 109 | * the same object will return the same model. |
| 110 | */ |
| 111 | public void setUseCache(boolean useCache) |
| 112 | { |
| 113 | modelCache.setUseCache(useCache); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets whether attributes shadow items in wrapped objects. When true |
| 118 | * (this is the default value), <code>${object.name}</code> will first |
| 119 | * try to locate a python attribute with the specified name on the object |
| 120 | * using {@link PyObject#__findattr__(java.lang.String)}, and only if it |
| 121 | * doesn't find the attribute will it call |
| 122 | * {@link PyObject#__getitem__(org.python.core.PyObject)}. |
| 123 | * When set to false, the lookup order is reversed and items |
| 124 | * are looked up before attributes. |
| 125 | */ |
| 126 | public synchronized void setAttributesShadowItems(boolean attributesShadowItems) |
| 127 | { |
| 128 | this.attributesShadowItems = attributesShadowItems; |
| 129 | } |
| 130 | |
| 131 | boolean isAttributesShadowItems() |
| 132 | { |
| 133 | return attributesShadowItems; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Wraps the passed Jython object into a FreeMarker template model. If |
| 138 | * the object is not a Jython object, it is first coerced into one using |
| 139 | * {@link Py#java2py(java.lang.Object)}. {@link PyDictionary} and {@link |
| 140 | * PyStringMap} are wrapped into a hash model, {@link PySequence} |
| 141 | * descendants are wrapped into a sequence model, {@link PyInteger}, {@link |
| 142 | * PyLong}, and {@link PyFloat} are wrapped into a number model. All objects |
| 143 | * are wrapped into a scalar model (using {@link Object#toString()} and a |
| 144 | * boolean model (using {@link PyObject#__nonzero__()}. For internal |
| 145 | * general-purpose {@link PyObject}s returned from a call to {@link |
| 146 | * #unwrap(TemplateModel)}, the template model that was passed to |
| 147 | * <code>unwrap</code> is returned. |
| 148 | */ |
| 149 | public TemplateModel wrap(Object obj) |
| 150 | { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…