use of freemarker.template.SimpleNumber in project freemarker by apache.
the class TemplateTestCase method setUp.
/*
* This method just contains all the code to seed the data model
* ported over from the individual classes. This seems ugly and unnecessary.
* We really might as well just expose pretty much
* the same tree to all our tests. (JR)
*/
@Override
@SuppressWarnings("boxing")
public void setUp() throws Exception {
conf.setTemplateLoader(new CopyrightCommentRemoverTemplateLoader(new FileTemplateLoader(new File(getTestClassDirectory(), "templates"))));
BeansWrapper beansWrapper = new BeansWrapperBuilder(Configuration.VERSION_2_3_0).build();
dataModel.put(ASSERT_VAR_NAME, AssertDirective.INSTANCE);
dataModel.put(ASSERT_EQUALS_VAR_NAME, AssertEqualsDirective.INSTANCE);
dataModel.put(ASSERT_FAILS_VAR_NAME, AssertFailsDirective.INSTANCE);
dataModel.put(NO_OUTPUT_VAR_NAME, NoOutputDirective.INSTANCE);
dataModel.put(JAVA_OBJECT_INFO_VAR_NAME, JavaObjectInfo.INSTANCE);
dataModel.put(TEST_NAME_VAR_NAME, simpleTestName);
dataModel.put(ICI_INT_VALUE_VAR_NAME, conf.getIncompatibleImprovements().intValue());
dataModel.put("message", "Hello, world!");
if (simpleTestName.startsWith("api-builtin")) {
dataModel.put("map", ImmutableMap.of(1, "a", 2, "b", 3, "c"));
dataModel.put("list", ImmutableList.of(1, 2, 3));
dataModel.put("set", ImmutableSet.of("a", "b", "c"));
dataModel.put("s", "test");
} else if (simpleTestName.equals("bean-maps")) {
BeansWrapper w1 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w2 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w3 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w4 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w5 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w6 = new Java7MembersOnlyBeansWrapper();
BeansWrapper w7 = new Java7MembersOnlyBeansWrapper();
w1.setExposureLevel(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
w2.setExposureLevel(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
w3.setExposureLevel(BeansWrapper.EXPOSE_NOTHING);
w4.setExposureLevel(BeansWrapper.EXPOSE_NOTHING);
w5.setExposureLevel(BeansWrapper.EXPOSE_ALL);
w6.setExposureLevel(BeansWrapper.EXPOSE_ALL);
w1.setMethodsShadowItems(true);
w2.setMethodsShadowItems(false);
w3.setMethodsShadowItems(true);
w4.setMethodsShadowItems(false);
w5.setMethodsShadowItems(true);
w6.setMethodsShadowItems(false);
w7.setSimpleMapWrapper(true);
Object test = getTestMapBean();
dataModel.put("m1", w1.wrap(test));
dataModel.put("m2", w2.wrap(test));
dataModel.put("m3", w3.wrap(test));
dataModel.put("m4", w4.wrap(test));
dataModel.put("m5", w5.wrap(test));
dataModel.put("m6", w6.wrap(test));
dataModel.put("m7", w7.wrap(test));
dataModel.put("s1", w1.wrap("hello"));
dataModel.put("s2", w1.wrap("world"));
dataModel.put("s3", w5.wrap("hello"));
dataModel.put("s4", w5.wrap("world"));
} else if (simpleTestName.equals("beans")) {
dataModel.put("array", new String[] { "array-0", "array-1" });
dataModel.put("list", Arrays.asList(new String[] { "list-0", "list-1", "list-2" }));
Map<Object, Object> tmap = new HashMap<Object, Object>();
tmap.put("key", "value");
Object objKey = new Object();
tmap.put(objKey, "objValue");
dataModel.put("map", tmap);
dataModel.put("objKey", objKey);
dataModel.put("obj", new freemarker.test.templatesuite.models.BeanTestClass());
dataModel.put("resourceBundle", new ResourceBundleModel(ResourceBundle.getBundle("freemarker.test.templatesuite.models.BeansTestResources"), BeansWrapper.getDefaultInstance()));
dataModel.put("date", new GregorianCalendar(1974, 10, 14).getTime());
dataModel.put("statics", beansWrapper.getStaticModels());
dataModel.put("enums", beansWrapper.getEnumModels());
} else if (simpleTestName.equals("boolean")) {
dataModel.put("boolean1", TemplateBooleanModel.FALSE);
dataModel.put("boolean2", TemplateBooleanModel.TRUE);
dataModel.put("boolean3", TemplateBooleanModel.TRUE);
dataModel.put("boolean4", TemplateBooleanModel.TRUE);
dataModel.put("boolean5", TemplateBooleanModel.FALSE);
dataModel.put("list1", new BooleanList1());
dataModel.put("list2", new BooleanList2());
dataModel.put("hash1", new BooleanHash1());
dataModel.put("hash2", new BooleanHash2());
} else if (simpleTestName.startsWith("dateformat")) {
GregorianCalendar cal = new GregorianCalendar(2002, 10, 15, 14, 54, 13);
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
dataModel.put("date", new SimpleDate(cal.getTime(), TemplateDateModel.DATETIME));
dataModel.put("unknownDate", new SimpleDate(cal.getTime(), TemplateDateModel.UNKNOWN));
dataModel.put("javaGMT02", TimeZone.getTimeZone("GMT+02"));
dataModel.put("javaUTC", TimeZone.getTimeZone("UTC"));
dataModel.put("adaptedToStringScalar", new Object() {
@Override
public String toString() {
return "GMT+02";
}
});
dataModel.put("sqlDate", new java.sql.Date(1273955885023L));
dataModel.put("sqlTime", new java.sql.Time(74285023L));
} else if (templateName.equals("list.ftl") || templateName.equals("list2.ftl") || templateName.equals("list3.ftl") || simpleTestName.equals("listhash")) {
dataModel.put("listables", new Listables());
} else if (simpleTestName.startsWith("number-format")) {
dataModel.put("int", new SimpleNumber(Integer.valueOf(1)));
dataModel.put("double", new SimpleNumber(Double.valueOf(1.0)));
dataModel.put("double2", new SimpleNumber(Double.valueOf(1 + 1e-15)));
dataModel.put("double3", new SimpleNumber(Double.valueOf(1e-16)));
dataModel.put("double4", new SimpleNumber(Double.valueOf(-1e-16)));
dataModel.put("bigDecimal", new SimpleNumber(java.math.BigDecimal.valueOf(1)));
dataModel.put("bigDecimal2", new SimpleNumber(java.math.BigDecimal.valueOf(1, 16)));
} else if (simpleTestName.equals("simplehash-char-key")) {
HashMap<String, String> mStringC = new HashMap<String, String>();
mStringC.put("c", "string");
dataModel.put("mStringC", mStringC);
HashMap<String, String> mStringCNull = new HashMap<String, String>();
mStringCNull.put("c", null);
dataModel.put("mStringCNull", mStringCNull);
HashMap<Character, String> mCharC = new HashMap<Character, String>();
mCharC.put(Character.valueOf('c'), "char");
dataModel.put("mCharC", mCharC);
HashMap<String, String> mCharCNull = new HashMap<String, String>();
mCharCNull.put("c", null);
dataModel.put("mCharCNull", mCharCNull);
HashMap<Object, String> mMixed = new HashMap<Object, String>();
mMixed.put(Character.valueOf('c'), "char");
mMixed.put("s", "string");
mMixed.put("s2", "string2");
mMixed.put("s2n", null);
dataModel.put("mMixed", mMixed);
} else if (simpleTestName.equals("default-xmlns")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/defaultxmlns1.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.equals("multimodels")) {
dataModel.put("test", "selftest");
dataModel.put("self", "self");
dataModel.put("zero", Integer.valueOf(0));
dataModel.put("data", new MultiModel1());
} else if (simpleTestName.equals("stringbimethods")) {
dataModel.put("multi", new TestBoolean());
} else if (simpleTestName.startsWith("type-builtins")) {
dataModel.put("testmethod", new TestMethod());
dataModel.put("testnode", new TestNode());
dataModel.put("testcollection", new SimpleCollection(new ArrayList<Object>()));
dataModel.put("testcollectionEx", DefaultNonListCollectionAdapter.adapt(new HashSet<Object>(), null));
dataModel.put("bean", new TestBean());
} else if (simpleTestName.equals("date-type-builtins")) {
GregorianCalendar cal = new GregorianCalendar(2003, 4 - 1, 5, 6, 7, 8);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = cal.getTime();
dataModel.put("unknown", d);
dataModel.put("timeOnly", new java.sql.Time(d.getTime()));
dataModel.put("dateOnly", new java.sql.Date(d.getTime()));
dataModel.put("dateTime", new java.sql.Timestamp(d.getTime()));
} else if (simpleTestName.equals("var-layers")) {
dataModel.put("x", Integer.valueOf(4));
dataModel.put("z", Integer.valueOf(4));
conf.setSharedVariable("y", Integer.valueOf(7));
} else if (simpleTestName.equals("xml-fragment")) {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
DocumentBuilder db = f.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new InputSource(getClass().getResourceAsStream("models/xmlfragment.xml")));
NodeModel.simplify(doc);
dataModel.put("node", NodeModel.wrap(doc.getDocumentElement().getFirstChild().getFirstChild()));
} else if (simpleTestName.equals("xmlns1")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/xmlns.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.equals("xmlns2")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/xmlns2.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.equals("xmlns3") || simpleTestName.equals("xmlns4")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/xmlns3.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.equals("xmlns5")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/defaultxmlns1.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.equals("xml-ns_prefix-scope")) {
InputSource is = new InputSource(getClass().getResourceAsStream("models/xml-ns_prefix-scope.xml"));
NodeModel nm = NodeModel.parse(is);
dataModel.put("doc", nm);
} else if (simpleTestName.startsWith("sequence-builtins")) {
Set<String> abcSet = new TreeSet<String>();
abcSet.add("a");
abcSet.add("b");
abcSet.add("c");
dataModel.put("abcSet", abcSet);
dataModel.put("abcSetNonSeq", DefaultNonListCollectionAdapter.adapt(abcSet, beansWrapper));
List<String> listWithNull = new ArrayList<String>();
listWithNull.add("a");
listWithNull.add(null);
listWithNull.add("c");
dataModel.put("listWithNull", listWithNull);
List<String> listWithNullsOnly = new ArrayList<String>();
listWithNull.add(null);
listWithNull.add(null);
listWithNull.add(null);
dataModel.put("listWithNullsOnly", listWithNullsOnly);
dataModel.put("abcCollection", new SimpleCollection(abcSet));
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
dataModel.put("set", set);
} else if (simpleTestName.equals("number-to-date")) {
dataModel.put("bigInteger", new BigInteger("1305575275540"));
dataModel.put("bigDecimal", new BigDecimal("1305575275539.5"));
} else if (simpleTestName.equals("varargs")) {
dataModel.put("m", new VarArgTestModel());
} else if (simpleTestName.startsWith("overloaded-methods-") && !simpleTestName.startsWith("overloaded-methods-2-")) {
dataModel.put("obj", new OverloadedMethods());
} else if (simpleTestName.startsWith("boolean-formatting")) {
dataModel.put("beansBoolean", new BooleanModel(Boolean.TRUE, (BeansWrapper) conf.getObjectWrapper()));
dataModel.put("booleanAndString", new BooleanAndStringTemplateModel());
dataModel.put("booleanVsStringMethods", new BooleanVsStringMethods());
} else if (simpleTestName.startsWith("number-math-builtins")) {
dataModel.put("fNan", Float.valueOf(Float.NaN));
dataModel.put("dNan", Double.valueOf(Double.NaN));
dataModel.put("fNinf", Float.valueOf(Float.NEGATIVE_INFINITY));
dataModel.put("dPinf", Double.valueOf(Double.POSITIVE_INFINITY));
dataModel.put("fn", Float.valueOf(-0.05f));
dataModel.put("dn", Double.valueOf(-0.05));
dataModel.put("ineg", Integer.valueOf(-5));
dataModel.put("ln", Long.valueOf(-5));
dataModel.put("sn", Short.valueOf((short) -5));
dataModel.put("bn", Byte.valueOf((byte) -5));
dataModel.put("bin", BigInteger.valueOf(5));
dataModel.put("bdn", BigDecimal.valueOf(-0.05));
dataModel.put("fp", Float.valueOf(0.05f));
dataModel.put("dp", Double.valueOf(0.05));
dataModel.put("ip", Integer.valueOf(5));
dataModel.put("lp", Long.valueOf(5));
dataModel.put("sp", Short.valueOf((short) 5));
dataModel.put("bp", Byte.valueOf((byte) 5));
dataModel.put("bip", BigInteger.valueOf(5));
dataModel.put("bdp", BigDecimal.valueOf(0.05));
} else if (simpleTestName.startsWith("classic-compatible")) {
dataModel.put("array", new String[] { "a", "b", "c" });
dataModel.put("beansArray", beansWrapper.wrap(new String[] { "a", "b", "c" }));
dataModel.put("beanTrue", beansWrapper.wrap(Boolean.TRUE));
dataModel.put("beanFalse", beansWrapper.wrap(Boolean.FALSE));
} else if (simpleTestName.startsWith("overloaded-methods-2-")) {
dataModel.put("obj", new OverloadedMethods2());
final boolean dow = conf.getObjectWrapper() instanceof DefaultObjectWrapper;
dataModel.put("dow", dow);
dataModel.put("dowPre22", dow && ((DefaultObjectWrapper) conf.getObjectWrapper()).getIncompatibleImprovements().intValue() < _TemplateAPI.VERSION_INT_2_3_22);
}
}
use of freemarker.template.SimpleNumber in project freemarker by apache.
the class XalanXPathSupport method executeQuery.
/* " + ERRMSG_RECOMMEND_JAXEN;*/
public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
if (!(context instanceof Node)) {
if (context != null) {
if (isNodeList(context)) {
int cnt = ((List) context).size();
if (cnt != 0) {
throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node.");
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
} else {
throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
}
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
}
Node node = (Node) context;
try {
XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
int ctxtNode = xpathContext.getDTMHandleFromNode(node);
XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
if (xresult instanceof XNodeSet) {
NodeListModel result = new NodeListModel(node);
result.xpathSupport = this;
NodeIterator nodeIterator = xresult.nodeset();
Node n;
do {
n = nodeIterator.nextNode();
if (n != null) {
result.add(n);
}
} while (n != null);
return result.size() == 1 ? result.get(0) : result;
}
if (xresult instanceof XBoolean) {
return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
if (xresult instanceof XNull) {
return null;
}
if (xresult instanceof XString) {
return new SimpleScalar(xresult.toString());
}
if (xresult instanceof XNumber) {
return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
}
throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
} catch (TransformerException te) {
throw new TemplateModelException(te);
}
}
use of freemarker.template.SimpleNumber in project android by JetBrains.
the class FmTruncateStringMethodTest method check.
@SuppressWarnings("rawtypes")
private static void check(String s, int max, String expected) throws TemplateModelException {
FmTruncateStringMethod method = new FmTruncateStringMethod();
List list = Arrays.asList(new SimpleScalar(s), new SimpleNumber(max));
assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString());
}
use of freemarker.template.SimpleNumber in project pcgen by PCGen.
the class LoopDirective method execute.
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
// Check if no parameters were given:
int fromVal = 0;
Integer toVal = null;
int step = 1;
for (Object entryObj : params.entrySet()) {
Map.Entry entry = (Entry) entryObj;
String paramName = (String) entry.getKey();
TemplateModel paramValue = (TemplateModel) entry.getValue();
switch(paramName) {
case "from":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
fromVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
break;
case "to":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
toVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
break;
case "step":
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
}
step = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
if (step == 0) {
throw new TemplateModelException("The \"" + paramName + "\" parameter must not be 0.");
}
break;
}
}
if (toVal == null) {
throw new TemplateModelException("The \"to\" parameter must be provided.");
}
if (body == null) {
throw new TemplateModelException("This directive must have content.");
}
if (step > 0) {
for (int i = fromVal; i <= toVal; i += step) {
// Set the loop variable, if there is one:
if (loopVars.length > 0) {
loopVars[0] = new SimpleNumber(i);
}
if (loopVars.length > 1) {
loopVars[1] = i + step <= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
// Executes the nested body (same as <#nested> in FTL). In this
// case we don't provide a special writer as the parameter:
body.render(env.getOut());
}
} else {
for (int i = fromVal; i >= toVal; i += step) {
// Set the loop variable, if there is one:
if (loopVars.length > 0) {
loopVars[0] = new SimpleNumber(i);
}
if (loopVars.length > 1) {
loopVars[1] = i + step >= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
// Executes the nested body (same as <#nested> in FTL). In this
// case we don't provide a special writer as the parameter:
body.render(env.getOut());
}
}
}
use of freemarker.template.SimpleNumber in project freemarker by apache.
the class NumberFormatTest method testEnvironmentGetters.
@Test
public void testEnvironmentGetters() throws Exception {
Template t = new Template(null, "", getConfiguration());
Environment env = t.createProcessingEnvironment(null, null);
TemplateNumberFormat defF = env.getTemplateNumberFormat();
//
TemplateNumberFormat explF = env.getTemplateNumberFormat("0.00");
assertEquals("1.25", explF.formatToPlainText(new SimpleNumber(1.25)));
//
TemplateNumberFormat expl2F = env.getTemplateNumberFormat("@loc");
assertEquals("1.25_en_US", expl2F.formatToPlainText(new SimpleNumber(1.25)));
TemplateNumberFormat explFFr = env.getTemplateNumberFormat("0.00", Locale.FRANCE);
assertNotSame(explF, explFFr);
assertEquals("1,25", explFFr.formatToPlainText(new SimpleNumber(1.25)));
//
TemplateNumberFormat expl2FFr = env.getTemplateNumberFormat("@loc", Locale.FRANCE);
assertEquals("1.25_fr_FR", expl2FFr.formatToPlainText(new SimpleNumber(1.25)));
assertSame(env.getTemplateNumberFormat(), defF);
//
assertSame(env.getTemplateNumberFormat("0.00"), explF);
//
assertSame(env.getTemplateNumberFormat("@loc"), expl2F);
}
Aggregations