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

com.blade.render.BeetlRender Maven / Gradle / Ivy

/**
 * Copyright (c) 2015, biezhi 王爵 ([email protected])
 *
 * Licensed 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.blade.render;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.Set;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.exception.BeetlException;
import org.beetl.core.resource.WebAppResourceLoader;

import com.blade.Blade;
import com.blade.context.BladeWebContext;
import com.blade.web.http.Request;
import com.blade.web.http.wrapper.Session;

import blade.kit.log.Logger;

/**
 * Beetl Render
 *
 * @author	biezhi
 * @since	1.0
 */
public class BeetlRender implements Render {
    
	private static final Logger LOGGER = Logger.getLogger(BeetlRender.class);
	
	/**
	 * Beetl GroupTemplate Object
	 */
	private GroupTemplate groupTemplate = null;
	
	private String webRoot;
	
	public BeetlRender() {
		try {
			Blade blade = Blade.me();
			this.webRoot = blade.webRoot();
			WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
			resourceLoader.setAutoCheck(true);
			resourceLoader.setRoot(this.webRoot);
			Configuration cfg = Configuration.defaultConfiguration();
			groupTemplate = new GroupTemplate(resourceLoader, cfg);
		} catch (IOException e) {
			LOGGER.error(e);
		}
	}
	
	public BeetlRender(GroupTemplate groupTemplate) {
		this.groupTemplate = groupTemplate;
	}
	
	public BeetlRender(Configuration configuration) {
		try {
			Blade blade = Blade.me();
			this.webRoot = blade.webRoot();
			WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
			resourceLoader.setAutoCheck(true);
			resourceLoader.setRoot(this.webRoot);
			Configuration cfg = Configuration.defaultConfiguration();
			groupTemplate = new GroupTemplate(resourceLoader, cfg);
			groupTemplate.setConf(configuration);
		} catch (IOException e) {
			LOGGER.error(e);
		}
	}
	
	@Override
	public void render(ModelAndView modelAndView, Writer writer) throws RenderException {
		Request request = BladeWebContext.request();
		Session session = request.session();
		
		Template template = groupTemplate.getTemplate(modelAndView.getView());
		
		Map context = modelAndView.getModel();
		if(null != context && context.size() > 0){
			Set keys = context.keySet();
			for(String key : keys){
				template.binding(key, context.get(key));
			}
		}
		
		Set attrs = request.attributes();
		if(null != attrs && attrs.size() > 0){
			for(String attr : attrs){
				template.binding(attr, request.attribute(attr));
			}
		}
		
		Set session_attrs = session.attributes();
		if(null != session_attrs && session_attrs.size() > 0){
			for(String attr : session_attrs){
				template.binding(attr, session.attribute(attr));
			}
		}
		try {
			template.renderTo(writer);
		} catch (BeetlException e) {
			e.printStackTrace();
			LOGGER.error(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy