view src/nabble/view/web/tools/TestMacro.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source


package nabble.view.web.tools;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fschmidt.html.Html;
import fschmidt.util.servlet.JtpContext;
import nabble.naml.namespaces.BasicNamespace;
import nabble.naml.compiler.Template;
import nabble.naml.compiler.Program;
import nabble.naml.compiler.CommandSpec;
import nabble.naml.compiler.IPrintWriter;
import nabble.naml.compiler.ScopedInterpreter;
import nabble.naml.compiler.CompileException;
import nabble.naml.compiler.Source;
import nabble.model.Site;
import nabble.model.ModelHome;
import nabble.view.lib.Jtp;
import nabble.view.web.template.NabbleNamespace;
import nabble.view.web.template.ServletNamespace;
import nabble.view.web.template.NodeNamespace;
import nabble.modules.ModuleManager;
import nabble.modules.NamlModule;
import nabble.naml.compiler.Module;


public final class TestMacro extends HttpServlet {

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		PrintWriter out = response.getWriter();
		Site site = Jtp.getSiteNotNull(request);
		final String macro = request.getParameter("macro");
		if( macro==null ) {
			
		out.print( "\r\n<html>\r\n<head>\r\n<title>Test Macro</title>\r\n</head>\r\n<body>\r\n<form method=\"post\">\r\n<p>Macro:<br/>\r\n<textarea name=\"macro\" rows=\"30\" cols=\"90\" wrap=\"off\">\r\n<macro name=\"test\">\r\n\r\n</macro>\r\n</textarea>\r\n</macro>\r\n<p><input type=\"submit\" value=\"run macro\"></p>\r\n</form>\r\n</body>\r\n</html>\r\n" );

			return;
		}
		response.setContentType("text/plain");
		List<Module> modules = ModuleManager.getGenericModules();
		Source source = Source.getInstance("test", macro);
		Module module = new NamlModule( "test", Collections.singleton(source), Collections.<String>emptySet() );
		modules.add(module);
		Program program = Program.getInstance(modules);
		try {
			Template template = program.getTemplate("test",
				BasicNamespace.class, NabbleNamespace.class, ServletNamespace.class
			);
			ModuleManager.run( template, out, Collections.<String,Object>emptyMap(),
				new BasicNamespace(template), new NabbleNamespace(site), new ServletNamespace(request, response)
			);
		} catch(CompileException e) {
			throw new RuntimeException(e);
		}
	}

}