本文整理汇总了Java中freemarker.template.TemplateModel.getClass方法的典型用法代码示例。如果您正苦于以下问题:Java TemplateModel.getClass方法的具体用法?Java TemplateModel.getClass怎么用?Java TemplateModel.getClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freemarker.template.TemplateModel
的用法示例。
在下文中一共展示了TemplateModel.getClass方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBooleanArg
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
/**
* Gets boolean arg.
* <p>
* Will automatically interpret string true/false as boolean.
*/
public static Boolean getBooleanArg(TemplateModel obj, Boolean defaultValue) throws TemplateModelException {
if (obj instanceof TemplateBooleanModel) {
return ((TemplateBooleanModel) obj).getAsBoolean();
}
else if (obj instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) obj;
String val = s.getAsString();
// SCIPIO: empty check is desirable and makes it so caller can request default by specifying ""
if (!val.isEmpty()) {
return "true".equalsIgnoreCase(s.getAsString());
}
} else if (obj != null) {
throw new TemplateModelException("Expected boolean model or string model representation of boolean, but got a " +
obj.getClass() + " instead");
}
return defaultValue;
}
示例2: getStringArg
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
/**
* Gets string arg.
* <p>
* If number or date passed, will be coerced to string. Other types such as maps or lists
* will throw TemplateModelException.
*/
public static String getStringArg(TemplateModel obj, String defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException {
String result = null;
if (obj instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) obj;
result = LangFtlUtil.getAsString(s, nonEscaping);
} else if (obj == null) {
return defaultValue;
} else if (obj instanceof TemplateNumberModel || obj instanceof TemplateDateModel) {
// TODO: optimize this call
result = LangFtlUtil.execStringBuiltIn(obj, FreeMarkerWorker.getCurrentEnvironment()).getAsString();
} else {
throw new TemplateModelException("Expected string model or something coercible to string, but got a " +
obj.getClass() + " instead");
}
if (useDefaultWhenEmpty && result.isEmpty()) {
return defaultValue;
}
return result;
}
示例3: getIntegerArg
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
/**
* Gets integer arg.
* <p>
* If string passed, will be parsed as integer. Other types such as maps or lists
* will throw TemplateModelException.
*/
public static Integer getIntegerArg(TemplateModel obj, Integer defaultValue) throws TemplateModelException, NumberFormatException {
if (obj instanceof TemplateNumberModel) {
return ((TemplateNumberModel) obj).getAsNumber().intValue();
} else if (obj instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) obj;
String strResult = LangFtlUtil.getAsString(s, true);
if (strResult.isEmpty()) {
return defaultValue;
} else {
return Integer.parseInt(strResult);
}
} else if (obj == null) {
return defaultValue;
} else {
throw new TemplateModelException("Expected integer model or string representing of integer, but got a " +
obj.getClass() + " instead");
}
}
示例4: getMapArg
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
/**
* Gets a deep-unwrapped map.
* FIXME: nonEscaping bool is currently not handled... it may bypass escaping in some cases but not others...
*/
public static <K,V> Map<K, V> getMapArg(TemplateModel obj, Map<K, V> defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException {
if (!nonEscaping) {
throw new UnsupportedOperationException("getMapArg currently only supports escaping-bypassing (nonEscaping true)");
}
Map<K, V> result = null;
if (obj instanceof TemplateHashModel) {
result = UtilGenerics.checkMap(LangFtlUtil.unwrapAlways(obj));
} else if (obj == null) {
return defaultValue;
} else {
throw new TemplateModelException("Expected hash/map model or something coercible to map, but got a " +
obj.getClass() + " instead");
}
if (useDefaultWhenEmpty && result.isEmpty()) {
return defaultValue;
}
return result;
}
示例5: getAsRawSequence
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
static Collection<String> getAsRawSequence(TemplateModel elems) throws TemplateModelException {
if (elems == null || elems instanceof TemplateBooleanModel) {
return Collections.emptyList();
} else if (elems instanceof TemplateCollectionModel || elems instanceof TemplateSequenceModel) {
return (Collection<String>) LangFtlUtil.unwrapAlways(elems);
} else if (elems instanceof TemplateHashModelEx) {
return (Collection<String>) LangFtlUtil.getMapKeys(elems);
} else {
throw new TemplateModelException("invalid parameter type, can't interpret as sequence: " + elems.getClass());
}
}
示例6: addToSimpleList
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
public static void addToSimpleList(SimpleSequence dest, TemplateModel source) throws TemplateModelException {
if (source instanceof TemplateCollectionModel) {
addToSimpleList(dest, (TemplateCollectionModel) source);
}
else if (source instanceof TemplateSequenceModel) {
addToSimpleList(dest, (TemplateSequenceModel) source);
}
else {
throw new TemplateModelException("Can't add to simple list from source type (non-list type): " + source.getClass());
}
}
示例7: toSimpleSequence
import freemarker.template.TemplateModel; //导入方法依赖的package包/类
/**
* Supposed to convert to simple sequence.
* <p>
* WARN: Bypasses auto-escaping for complex maps, caller must decide how to handle.
* (e.g. the object wrapper used to rewrap the result).
* <p>
* DEV NOTE: I stopped writing/testing this when found out most of the problems w.r.t. collections are not
* the FTL types this time but the way they're used in Ofbiz templates.
* FTL's CollectionModel (subclass of TemplateCollectionModel) is supposed to cover everything and
* won't suffer from the same problems maps have.
*/
@SuppressWarnings("unchecked")
@Deprecated
private static TemplateSequenceModel toSimpleSequence(TemplateModel object, ObjectWrapper objectWrapper) throws TemplateModelException {
if (object instanceof TemplateSequenceModel) {
return (TemplateSequenceModel) object;
}
else if (object instanceof WrapperTemplateModel) {
WrapperTemplateModel wrapperModel = (WrapperTemplateModel) object;
// WARN: bypasses auto-escaping
Object wrappedObject = wrapperModel.getWrappedObject();
if (wrappedObject instanceof List) {
return DefaultListAdapter.adapt((List<Object>) wrappedObject, (RichObjectWrapper) objectWrapper);
}
else if (wrappedObject instanceof Object[]) {
return DefaultArrayAdapter.adapt((Object[]) wrappedObject, (ObjectWrapperAndUnwrapper) objectWrapper);
}
else if (wrappedObject instanceof Set) {
throw new UnsupportedOperationException("Not yet implemented");
}
else if (wrappedObject instanceof Collection) {
throw new UnsupportedOperationException("Not yet implemented");
}
else if (wrappedObject instanceof Iterable) {
throw new UnsupportedOperationException("Not yet implemented");
}
else {
throw new TemplateModelException("Cannot convert bean-wrapped object of type " + (object != null ? object.getClass() : "null") + " to simple sequence");
}
}
else if (object instanceof TemplateCollectionModel) {
TemplateCollectionModel collModel = (TemplateCollectionModel) object;
SimpleSequence res = new SimpleSequence(objectWrapper);
TemplateModelIterator it = collModel.iterator();
while(it.hasNext()) {
res.add(it.next());
}
return res;
}
else {
throw new TemplateModelException("Cannot convert object of type " + (object != null ? object.getClass() : "null") + " to simple sequence");
}
}