Package freemarker.ext.jython
Class JythonWrapper
java.lang.Object
freemarker.ext.jython.JythonWrapper
- All Implemented Interfaces:
ObjectWrapper
An object wrapper that wraps Jython objects into FreeMarker template models
and vice versa.
-
Field Summary
FieldsFields inherited from interface freemarker.template.ObjectWrapper
BEANS_WRAPPER, DEFAULT_WRAPPER, SIMPLE_WRAPPER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidsetAttributesShadowItems(boolean attributesShadowItems)Sets whether attributes shadow items in wrapped objects.voidsetUseCache(boolean useCache)Sets whether this wrapper caches model instances.org.python.core.PyObjectunwrap(TemplateModel model)Coerces a template model into aPyObject.Wraps the passed Jython object into a FreeMarker template model.
-
Field Details
-
INSTANCE
-
-
Constructor Details
-
JythonWrapper
public JythonWrapper()
-
-
Method Details
-
setUseCache
public void setUseCache(boolean useCache)Sets whether this wrapper caches model instances. Default is false. When set to true, callingwrap(Object)multiple times for the same object will return the same model. -
setAttributesShadowItems
public void setAttributesShadowItems(boolean attributesShadowItems)Sets whether attributes shadow items in wrapped objects. When true (this is the default value),${object.name}will first try to locate a python attribute with the specified name on the object usingPyObject.__findattr__(java.lang.String), and only if it doesn't find the attribute will it callPyObject.__getitem__(org.python.core.PyObject). When set to false, the lookup order is reversed and items are looked up before attributes. -
wrap
Wraps the passed Jython object into a FreeMarker template model. If the object is not a Jython object, it's first coerced into one usingPy.java2py(java.lang.Object).PyDictionaryandPyStringMapare wrapped into a hash model,PySequencedescendants are wrapped into a sequence model,PyInteger,PyLong, andPyFloatare wrapped into a number model. All objects are wrapped into a scalar model (usingObject.toString()and a boolean model (usingPyObject.__nonzero__(). For internal general-purposePyObjects returned from a call tounwrap(TemplateModel), the template model that was passed tounwrapis returned.- Specified by:
wrapin interfaceObjectWrapper- Parameters:
obj- The object to wrap into aTemplateModel. If it already implementsTemplateModel, it should just return the object as is. If it'snull, the method should returnnull(however,BeansWrapper, has a legacy option for returning a null model object instead, but it's not a good idea).- Returns:
- a
TemplateModelwrapper of the object passed in. To support un-wrapping, you may consider the return value to implementWrapperTemplateModelandAdapterTemplateModel. The default expectation is that theTemplateModelisn't less thread safe than the wrapped object. If theObjectWrapperreturns less thread safe objects, that should be clearly documented, as it restricts how it can be used, like, then it can't be used to wrap "shared variables" (Configuration.setSharedVaribles(Map)).
-
unwrap
Coerces a template model into aPyObject.- Parameters:
model- the model to coerce- Returns:
- the coerced model.
AdapterTemplateModels (i.e.BeanModel) are marshalled using the standard Python marshallerPy.java2py(Object)on the result ofgetWrappedObject(PyObject.class)s. The native JythonModel instances will just return the underlying PyObject.- All other models that are
scalarsare marshalled asPyString. - All other models that are
numbersare marshalled using the standard Python marshallerPy.java2py(Object)on their underlyingNumber - All other models are marshalled to a generic internal
PyObjectsubclass that'll correctly pass__finditem__,__len__,__nonzero__, and__call__invocations to appropriate hash, sequence, and method models.
- Throws:
TemplateModelException
-