All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bluenimble.platform.templating.tests.TestTemplateWithScripting Maven / Gradle / Ivy

There is a newer version: 2.50.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bluenimble.platform.templating.tests;

import java.util.Map;

import com.bluenimble.platform.Json;
import com.bluenimble.platform.json.JsonObject;
import com.bluenimble.platform.templating.VariableResolver;
import com.bluenimble.platform.templating.impls.DefaultExpressionCompiler;

public class TestTemplateWithScripting {
	
	public static void main (String [] args) {
	
		String exp = "simple <% model.b %> <%= model.b > 400 ? 600 : 800 %>/<% beta | 'alpha' %>";

		final JsonObject model = (JsonObject)new JsonObject ().set ("a", "A Value").set ("b", 409).set ("c", "Hello");
		
		VariableResolver vr = new VariableResolver () {
			private static final long serialVersionUID = -485939153491337463L;

			@Override
			public Object resolve (String namespace, String... property) {
				if (namespace == null) {
					return null;
				}
				if (namespace == null || namespace.equals ("model")) {
					return Json.find (model, property);
				}
				return null;
			}

			@SuppressWarnings("unchecked")
			@Override
			public Map bindings () {
				return (JsonObject)new JsonObject ().set ("model", model);
			}
			
		};
		
		DefaultExpressionCompiler compiler = new DefaultExpressionCompiler ("<%", "%>").withScripting (true);
		
		System.out.println ("value: " + compiler.compile (exp, null).eval (vr));
		
		System.out.println ("value: " + compiler.compile (exp, null).eval (vr));
		

	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy