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

cat.inspiracio.orange.Template Maven / Gradle / Ivy

Go to download

Orange Servlet provides HTML templating with server-side Java. OrangeServlet is a servlet that is registered on *.html. It reads the html file at the right location according to URL. It renders the file as html, executing some scripting: * data-if="E" attributes for server-side conditional, * data-for="T x : E" attributes for server-side loops, * data-import="C" attributes for server-side class imports, * data-substitute="file.html" substitute an element with the processed contents of a file, * ${expressions} in element bodies and attribute values.

There is a newer version: 5.0.0
Show newest version
package cat.inspiracio.orange;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

public abstract class Template {
	private static final String NL="\n";
	
	// state -----------------------------------------------
	//All these variables are visible in the write() method.

	protected JspWriter out;	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected PageContext pageContext;
	protected ServletContext application;
	protected ServletConfig config;
	protected Template page;
	protected Throwable exception;

	// construction --------------------------------
	
	public Template(){}

	/** The only setter you need to call.
	 * It sets all fields. */
	public final void setPageContext(PageContext pc){
		out=pc.getOut();
		pageContext=pc;
		request=(HttpServletRequest)pc.getRequest();
		response=(HttpServletResponse)pc.getResponse();
		session=pc.getSession();
		application=pc.getServletContext();
		config=pc.getServletConfig();
		page=this;//Or better?
		exception=pc.getException();
	}
	
	// abstract method -----------------------------
	
	/** This is the method that template must implement. 
	 * @throws Exception Java-islands threw an exception. */
	public abstract void write()throws Exception;

	// orange logic ------------------------------
	
	/** Called for an element with attribute data-substitute=v.
	 * 
	 * The attribute value is a path. The servlet reads a file at the path. 
	 * The file contains an HTML fragment. The fragment is inserted in the 
	 * document, substituting the original element.
	 * 
	 * Example:
	 * 
* * The attribute value is literally a path. It is not a javascript expression * which when evaluated results in a path. * * The path is relative to the location of the document in which it occurs, * or absolute if it starts with /. * * @throws IOException */ protected final void substitute(String v) throws Exception{ String n=resolve(v); //instantiate the class and call it @SuppressWarnings("unchecked") Class