use of freemarker.core._DelayedJQuote in project freemarker by apache.
the class Configuration method setSetting.
@Override
public void setSetting(String name, String value) throws TemplateException {
boolean unknown = false;
try {
if ("TemplateUpdateInterval".equalsIgnoreCase(name)) {
name = TEMPLATE_UPDATE_DELAY_KEY;
} else if ("DefaultEncoding".equalsIgnoreCase(name)) {
name = DEFAULT_ENCODING_KEY;
}
if (DEFAULT_ENCODING_KEY_SNAKE_CASE.equals(name) || DEFAULT_ENCODING_KEY_CAMEL_CASE.equals(name)) {
if (JVM_DEFAULT.equalsIgnoreCase(value)) {
setDefaultEncoding(getJVMDefaultEncoding());
} else {
setDefaultEncoding(value);
}
} else if (LOCALIZED_LOOKUP_KEY_SNAKE_CASE.equals(name) || LOCALIZED_LOOKUP_KEY_CAMEL_CASE.equals(name)) {
setLocalizedLookup(StringUtil.getYesNo(value));
} else if (STRICT_SYNTAX_KEY_SNAKE_CASE.equals(name) || STRICT_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
setStrictSyntaxMode(StringUtil.getYesNo(value));
} else if (WHITESPACE_STRIPPING_KEY_SNAKE_CASE.equals(name) || WHITESPACE_STRIPPING_KEY_CAMEL_CASE.equals(name)) {
setWhitespaceStripping(StringUtil.getYesNo(value));
} else if (AUTO_ESCAPING_POLICY_KEY_SNAKE_CASE.equals(name) || AUTO_ESCAPING_POLICY_KEY_CAMEL_CASE.equals(name)) {
if ("enable_if_default".equals(value) || "enableIfDefault".equals(value)) {
setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY);
} else if ("enable_if_supported".equals(value) || "enableIfSupported".equals(value)) {
setAutoEscapingPolicy(ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
} else if ("disable".equals(value)) {
setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY);
} else {
throw invalidSettingValueException(name, value);
}
} else if (OUTPUT_FORMAT_KEY_SNAKE_CASE.equals(name) || OUTPUT_FORMAT_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetOutputFormat();
} else {
OutputFormat stdOF = STANDARD_OUTPUT_FORMATS.get(value);
setOutputFormat(stdOF != null ? stdOF : (OutputFormat) _ObjectBuilderSettingEvaluator.eval(value, OutputFormat.class, true, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (REGISTERED_CUSTOM_OUTPUT_FORMATS_KEY_SNAKE_CASE.equals(name) || REGISTERED_CUSTOM_OUTPUT_FORMATS_KEY_CAMEL_CASE.equals(name)) {
List list = (List) _ObjectBuilderSettingEvaluator.eval(value, List.class, true, _SettingEvaluationEnvironment.getCurrent());
for (Object item : list) {
if (!(item instanceof OutputFormat)) {
throw new _MiscTemplateException(getEnvironment(), "Invalid value for setting ", new _DelayedJQuote(name), ": List items must be " + OutputFormat.class.getName() + " instances, in: ", value);
}
}
setRegisteredCustomOutputFormats(list);
} else if (RECOGNIZE_STANDARD_FILE_EXTENSIONS_KEY_SNAKE_CASE.equals(name) || RECOGNIZE_STANDARD_FILE_EXTENSIONS_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetRecognizeStandardFileExtensions();
} else {
setRecognizeStandardFileExtensions(StringUtil.getYesNo(value));
}
} else if (CACHE_STORAGE_KEY_SNAKE_CASE.equals(name) || CACHE_STORAGE_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetCacheStorage();
}
if (value.indexOf('.') == -1) {
int strongSize = 0;
int softSize = 0;
Map map = StringUtil.parseNameValuePairList(value, String.valueOf(Integer.MAX_VALUE));
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
String pname = (String) ent.getKey();
int pvalue;
try {
pvalue = Integer.parseInt((String) ent.getValue());
} catch (NumberFormatException e) {
throw invalidSettingValueException(name, value);
}
if ("soft".equalsIgnoreCase(pname)) {
softSize = pvalue;
} else if ("strong".equalsIgnoreCase(pname)) {
strongSize = pvalue;
} else {
throw invalidSettingValueException(name, value);
}
}
if (softSize == 0 && strongSize == 0) {
throw invalidSettingValueException(name, value);
}
setCacheStorage(new MruCacheStorage(strongSize, softSize));
} else {
setCacheStorage((CacheStorage) _ObjectBuilderSettingEvaluator.eval(value, CacheStorage.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_UPDATE_DELAY_KEY_SNAKE_CASE.equals(name) || TEMPLATE_UPDATE_DELAY_KEY_CAMEL_CASE.equals(name)) {
long multipier;
String valueWithoutUnit;
if (value.endsWith("ms")) {
multipier = 1;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 2));
} else if (value.endsWith("s")) {
multipier = 1000;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else if (value.endsWith("m")) {
multipier = 1000 * 60;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else if (value.endsWith("h")) {
multipier = 1000 * 60 * 60;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else {
// Default is seconds for backward compatibility
multipier = 1000;
valueWithoutUnit = value;
}
setTemplateUpdateDelayMilliseconds(Integer.parseInt(valueWithoutUnit) * multipier);
} else if (TAG_SYNTAX_KEY_SNAKE_CASE.equals(name) || TAG_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
if ("auto_detect".equals(value) || "autoDetect".equals(value)) {
setTagSyntax(AUTO_DETECT_TAG_SYNTAX);
} else if ("angle_bracket".equals(value) || "angleBracket".equals(value)) {
setTagSyntax(ANGLE_BRACKET_TAG_SYNTAX);
} else if ("square_bracket".equals(value) || "squareBracket".equals(value)) {
setTagSyntax(SQUARE_BRACKET_TAG_SYNTAX);
} else {
throw invalidSettingValueException(name, value);
}
} else if (INTERPOLATION_SYNTAX_KEY_SNAKE_CASE.equals(name) || INTERPOLATION_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
if ("legacy".equals(value)) {
setInterpolationSyntax(LEGACY_INTERPOLATION_SYNTAX);
} else if ("dollar".equals(value)) {
setInterpolationSyntax(DOLLAR_INTERPOLATION_SYNTAX);
} else if ("square_bracket".equals(value) || "squareBracket".equals(value)) {
setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);
} else {
throw invalidSettingValueException(name, value);
}
} else if (NAMING_CONVENTION_KEY_SNAKE_CASE.equals(name) || NAMING_CONVENTION_KEY_CAMEL_CASE.equals(name)) {
if ("auto_detect".equals(value) || "autoDetect".equals(value)) {
setNamingConvention(AUTO_DETECT_NAMING_CONVENTION);
} else if ("legacy".equals(value)) {
setNamingConvention(LEGACY_NAMING_CONVENTION);
} else if ("camel_case".equals(value) || "camelCase".equals(value)) {
setNamingConvention(CAMEL_CASE_NAMING_CONVENTION);
} else {
throw invalidSettingValueException(name, value);
}
} else if (TAB_SIZE_KEY_SNAKE_CASE.equals(name) || TAB_SIZE_KEY_CAMEL_CASE.equals(name)) {
setTabSize(Integer.parseInt(value));
} else if (INCOMPATIBLE_IMPROVEMENTS_KEY_SNAKE_CASE.equals(name) || INCOMPATIBLE_IMPROVEMENTS_KEY_CAMEL_CASE.equals(name)) {
setIncompatibleImprovements(new Version(value));
} else if (INCOMPATIBLE_ENHANCEMENTS.equals(name)) {
setIncompatibleEnhancements(value);
} else if (TEMPLATE_LOADER_KEY_SNAKE_CASE.equals(name) || TEMPLATE_LOADER_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateLoader();
} else {
setTemplateLoader((TemplateLoader) _ObjectBuilderSettingEvaluator.eval(value, TemplateLoader.class, true, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_LOOKUP_STRATEGY_KEY_SNAKE_CASE.equals(name) || TEMPLATE_LOOKUP_STRATEGY_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateLookupStrategy();
} else {
setTemplateLookupStrategy((TemplateLookupStrategy) _ObjectBuilderSettingEvaluator.eval(value, TemplateLookupStrategy.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_NAME_FORMAT_KEY_SNAKE_CASE.equals(name) || TEMPLATE_NAME_FORMAT_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateNameFormat();
} else if (value.equalsIgnoreCase("default_2_3_0")) {
setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_3_0);
} else if (value.equalsIgnoreCase("default_2_4_0")) {
setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0);
} else {
throw invalidSettingValueException(name, value);
}
} else if (TEMPLATE_CONFIGURATIONS_KEY_SNAKE_CASE.equals(name) || TEMPLATE_CONFIGURATIONS_KEY_CAMEL_CASE.equals(name)) {
if (value.equals(NULL)) {
setTemplateConfigurations(null);
} else {
setTemplateConfigurations((TemplateConfigurationFactory) _ObjectBuilderSettingEvaluator.eval(value, TemplateConfigurationFactory.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else {
unknown = true;
}
} catch (Exception e) {
throw settingValueAssignmentException(name, value, e);
}
if (unknown) {
super.setSetting(name, value);
}
}
use of freemarker.core._DelayedJQuote in project freemarker by apache.
the class SimpleHash method get.
public TemplateModel get(String key) throws TemplateModelException {
Object result;
try {
result = map.get(key);
} catch (ClassCastException e) {
throw new _TemplateModelException(e, "ClassCastException while getting Map entry with String key ", new _DelayedJQuote(key));
} catch (NullPointerException e) {
throw new _TemplateModelException(e, "NullPointerException while getting Map entry with String key ", new _DelayedJQuote(key));
}
// The key to use for putting -- it's the key that already exists in
// the map (either key or charKey below). This way, we'll never put a
// new key in the map, avoiding spurious ConcurrentModificationException
// from another thread iterating over the map, see bug #1939742 in
// SourceForge tracker.
Object putKey = null;
if (result == null) {
// In SortedMap-s, however, we can't do that safely, as it can cause ClassCastException.
if (key.length() == 1 && !(map instanceof SortedMap)) {
Character charKey = Character.valueOf(key.charAt(0));
try {
result = map.get(charKey);
if (result != null || map.containsKey(charKey)) {
putKey = charKey;
}
} catch (ClassCastException e) {
throw new _TemplateModelException(e, "ClassCastException while getting Map entry with Character key ", new _DelayedJQuote(key));
} catch (NullPointerException e) {
throw new _TemplateModelException(e, "NullPointerException while getting Map entry with Character key ", new _DelayedJQuote(key));
}
}
if (putKey == null) {
if (!map.containsKey(key)) {
return null;
} else {
putKey = key;
}
}
} else {
putKey = key;
}
if (result instanceof TemplateModel) {
return (TemplateModel) result;
}
TemplateModel tm = wrap(result);
if (!putFailed) {
try {
map.put(putKey, tm);
} catch (Exception e) {
// If it's immutable or something, we just keep going.
putFailed = true;
}
}
return tm;
}
use of freemarker.core._DelayedJQuote in project freemarker by apache.
the class BeanModel method get.
/**
* Uses Beans introspection to locate a property or method with name
* matching the key name. If a method or property is found, it's wrapped
* into {@link freemarker.template.TemplateMethodModelEx} (for a method or
* indexed property), or evaluated on-the-fly and the return value wrapped
* into appropriate model (for a non-indexed property) Models for various
* properties and methods are cached on a per-class basis, so the costly
* introspection is performed only once per property or method of a class.
* (Side-note: this also implies that any class whose method has been called
* will be strongly referred to by the framework and will not become
* unloadable until this class has been unloaded first. Normally this is not
* an issue, but can be in a rare scenario where you create many classes on-
* the-fly. Also, as the cache grows with new classes and methods introduced
* to the framework, it may appear as if it were leaking memory. The
* framework does, however detect class reloads (if you happen to be in an
* environment that does this kind of things--servlet containers do it when
* they reload a web application) and flushes the cache. If no method or
* property matching the key is found, the framework will try to invoke
* methods with signature
* <tt>non-void-return-type get(java.lang.String)</tt>,
* then <tt>non-void-return-type get(java.lang.Object)</tt>, or
* alternatively (if the wrapped object is a resource bundle)
* <tt>Object getObject(java.lang.String)</tt>.
* @throws TemplateModelException if there was no property nor method nor
* a generic <tt>get</tt> method to invoke.
*/
public TemplateModel get(String key) throws TemplateModelException {
Class<?> clazz = object.getClass();
Map<Object, Object> classInfo = wrapper.getClassIntrospector().get(clazz);
TemplateModel retval = null;
try {
if (wrapper.isMethodsShadowItems()) {
Object fd = classInfo.get(key);
if (fd != null) {
retval = invokeThroughDescriptor(fd, classInfo);
} else {
retval = invokeGenericGet(classInfo, clazz, key);
}
} else {
TemplateModel model = invokeGenericGet(classInfo, clazz, key);
final TemplateModel nullModel = wrapper.wrap(null);
if (model != nullModel && model != UNKNOWN) {
return model;
}
Object fd = classInfo.get(key);
if (fd != null) {
retval = invokeThroughDescriptor(fd, classInfo);
if (retval == UNKNOWN && model == nullModel) {
// This is the (somewhat subtle) case where the generic get() returns null
// and we have no bean info, so we respect the fact that
// the generic get() returns null and return null. (JR)
retval = nullModel;
}
}
}
if (retval == UNKNOWN) {
if (wrapper.isStrict()) {
throw new InvalidPropertyException("No such bean property: " + key);
} else if (LOG.isDebugEnabled()) {
logNoSuchKey(key, classInfo);
}
retval = wrapper.wrap(null);
}
return retval;
} catch (TemplateModelException e) {
throw e;
} catch (Exception e) {
throw new _TemplateModelException(e, "An error has occurred when reading existing sub-variable ", new _DelayedJQuote(key), "; see cause exception! The type of the containing value was: ", new _DelayedFTLTypeDescription(this));
}
}
use of freemarker.core._DelayedJQuote in project freemarker by apache.
the class JspTagModelBase method setupTag.
void setupTag(Object tag, Map args, ObjectWrapper wrapper) throws TemplateModelException, InvocationTargetException, IllegalAccessException {
if (args != null && !args.isEmpty()) {
ObjectWrapperAndUnwrapper unwrapper = wrapper instanceof ObjectWrapperAndUnwrapper ? (ObjectWrapperAndUnwrapper) wrapper : // [2.4] Throw exception in this case
BeansWrapper.getDefaultInstance();
final Object[] argArray = new Object[1];
for (Iterator iter = args.entrySet().iterator(); iter.hasNext(); ) {
final Map.Entry entry = (Map.Entry) iter.next();
final Object arg = unwrapper.unwrap((TemplateModel) entry.getValue());
argArray[0] = arg;
final Object paramName = entry.getKey();
Method setterMethod = (Method) propertySetters.get(paramName);
if (setterMethod == null) {
if (dynaSetter == null) {
throw new TemplateModelException("Unknown property " + StringUtil.jQuote(paramName.toString()) + " on instance of " + tagClass.getName());
} else {
dynaSetter.invoke(tag, null, paramName, argArray[0]);
}
} else {
if (arg instanceof BigDecimal) {
argArray[0] = BeansWrapper.coerceBigDecimal((BigDecimal) arg, setterMethod.getParameterTypes()[0]);
}
try {
setterMethod.invoke(tag, argArray);
} catch (Exception e) {
final Class setterType = setterMethod.getParameterTypes()[0];
final _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder("Failed to set JSP tag parameter ", new _DelayedJQuote(paramName), " (declared type: ", new _DelayedShortClassName(setterType) + ", actual value's type: ", (argArray[0] != null ? (Object) new _DelayedShortClassName(argArray[0].getClass()) : "Null"), "). See cause exception for the more specific cause...");
if (e instanceof IllegalArgumentException && !(setterType.isAssignableFrom(String.class)) && argArray[0] != null && argArray[0] instanceof String) {
desc.tip("This problem is often caused by unnecessary parameter quotation. Paramters " + "aren't quoted in FTL, similarly as they aren't quoted in most languages. " + "For example, these parameter assignments are wrong: ", "<@my.tag p1=\"true\" p2=\"10\" p3=\"${someVariable}\" p4=\"${x+1}\" />", ". The correct form is: ", "<@my.tag p1=true p2=10 p3=someVariable p4=x+1 />", ". Only string literals are quoted (regardless of where they occur): ", "<@my.box style=\"info\" message=\"Hello ${name}!\" width=200 />", ".");
}
throw new _TemplateModelException(e, null, desc);
}
}
}
}
}
Aggregations