| 1 | |
package org.webslinger.types; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.Collection; |
| 6 | |
import java.util.HashSet; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
import java.util.Vector; |
| 10 | |
import javax.servlet.ServletException; |
| 11 | |
import javax.servlet.ServletRequest; |
| 12 | |
import javax.servlet.ServletResponse; |
| 13 | |
import javax.servlet.http.HttpServletRequest; |
| 14 | |
import javax.servlet.http.HttpServletResponse; |
| 15 | |
|
| 16 | |
import org.apache.bsf.BSFException; |
| 17 | |
import org.apache.commons.vfs.FileObject; |
| 18 | |
|
| 19 | |
import org.webslinger.PathContext; |
| 20 | |
import org.webslinger.TypeHandler; |
| 21 | |
import org.webslinger.Webslinger; |
| 22 | |
import org.webslinger.WebslingerInvoker; |
| 23 | |
import org.webslinger.WebslingerServletContext; |
| 24 | |
import org.webslinger.bsf.LanguageEngine; |
| 25 | |
import org.webslinger.bsf.LanguageManager; |
| 26 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 27 | |
import org.webslinger.template.TemplateMacro; |
| 28 | |
import org.webslinger.template.TemplateManager; |
| 29 | |
import org.webslinger.macros.ContentMacro; |
| 30 | |
import org.webslinger.macros.ContentPathMacro; |
| 31 | |
import org.webslinger.macros.FileBasedMacro; |
| 32 | |
import org.webslinger.macros.MergeMacro; |
| 33 | |
import org.webslinger.macros.SectionMacro; |
| 34 | |
|
| 35 | 25 | public class template extends TypeHandler { |
| 36 | |
public static final String DEFAULT_LANGUAGE = "velocity"; |
| 37 | |
|
| 38 | 1 | private static final String[] paramNames = new String[5]; |
| 39 | 1 | private static final Class[] paramTypes = new Class[5]; |
| 40 | |
|
| 41 | |
static { |
| 42 | 1 | paramNames[0] ="writer"; |
| 43 | 1 | paramTypes[0] = Writer.class; |
| 44 | 1 | paramNames[1] ="context"; |
| 45 | 1 | paramTypes[1] = Map.class; |
| 46 | 1 | paramNames[2] ="webslinger"; |
| 47 | 1 | paramTypes[2] = Webslinger.class; |
| 48 | 1 | paramNames[3] = "request"; |
| 49 | 1 | paramTypes[3] = HttpServletRequest.class; |
| 50 | 1 | paramNames[4] = "response"; |
| 51 | 1 | paramTypes[4] = HttpServletResponse.class; |
| 52 | 1 | } |
| 53 | |
|
| 54 | |
protected TemplateManager templateManager; |
| 55 | |
protected event eventHandler; |
| 56 | 25 | protected HashSet<String> extensions = new HashSet<String>(); |
| 57 | 25 | protected HashSet<String> loadedMacros = new HashSet<String>(); |
| 58 | |
|
| 59 | |
protected void scanForMacros() throws BSFException, IOException { |
| 60 | 25 | FileObject macroDir = getContext().getFile("/WEB-INF/Macros"); |
| 61 | 25 | VFSUtil.refresh(macroDir); |
| 62 | 25 | HashSet<String> fileNames = new HashSet<String>(); |
| 63 | 25 | if (macroDir.exists()) { |
| 64 | 19 | FileObject[] children = macroDir.getChildren(); |
| 65 | 461 | for (FileObject child: macroDir.getChildren()) { |
| 66 | 442 | String baseName = child.getName().getBaseName(); |
| 67 | 442 | int period = baseName.lastIndexOf('.'); |
| 68 | 442 | if (period == -1) continue; |
| 69 | 442 | String ext = baseName.substring(period); |
| 70 | 442 | if (extensions.contains(ext)) fileNames.add(baseName.substring(0, period)); |
| 71 | |
} |
| 72 | |
} |
| 73 | 25 | fileNames.removeAll(templateManager.getMacroNames()); |
| 74 | 25 | synchronized (this) { |
| 75 | 25 | for (String loadedMacroName: loadedMacros) { |
| 76 | 0 | if (fileNames.remove(loadedMacroName)) continue; |
| 77 | 0 | undeclareBean(loadedMacroName); |
| 78 | |
} |
| 79 | 25 | for (String name: fileNames) { |
| 80 | 442 | declareBean(name, new FileBasedMacro(getContext(), name, getContext().resolve("/WEB-INF/Macros/" + name)), TemplateMacro.class); |
| 81 | 442 | loadedMacros.add(name); |
| 82 | |
} |
| 83 | 25 | } |
| 84 | 25 | } |
| 85 | |
|
| 86 | |
protected void undeclareBean(String name) throws BSFException { |
| 87 | 0 | templateManager.undeclareBean(name); |
| 88 | 0 | eventHandler.undeclareBean(name); |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
protected <T> void declareBean(String name, T bean, Class<T> type) throws BSFException { |
| 92 | 467 | templateManager.declareBean(name, bean, type); |
| 93 | 467 | eventHandler.declareBean(name, bean, type); |
| 94 | 467 | } |
| 95 | |
|
| 96 | |
public TemplateMacro findMacro(String name) throws IOException { |
| 97 | 0 | if (templateManager == null) throw new InternalError("template type not initialized"); |
| 98 | 0 | return (TemplateMacro) templateManager.lookupBean(name); |
| 99 | |
} |
| 100 | |
|
| 101 | |
public void init(WebslingerServletContext context, String type) throws IOException { |
| 102 | 25 | super.init(context, type); |
| 103 | 25 | eventHandler = (event) context.getTypeHandler("event"); |
| 104 | |
|
| 105 | 397 | for (String extension: context.getContainer().getExtensionList()) { |
| 106 | 372 | extensions.add(extension); |
| 107 | |
} |
| 108 | |
try { |
| 109 | 25 | templateManager = new TemplateManager(context.getVFSDelegate(), logger); |
| 110 | 25 | templateManager.setClassLoader(context.getClassLoader()); |
| 111 | 25 | declareBean("MacroBody", FileBasedMacro.MACRO_BODY, TemplateMacro.class); |
| 112 | 25 | scanForMacros(); |
| 113 | 0 | } catch (BSFException e) { |
| 114 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 115 | 25 | } |
| 116 | 25 | } |
| 117 | |
|
| 118 | |
public Object run(Webslinger webslinger) throws IOException, ServletException { |
| 119 | |
|
| 120 | |
|
| 121 | |
try { |
| 122 | 501 | ServletRequest request = webslinger.getRequest(); |
| 123 | 501 | ServletResponse response = webslinger.getResponse(); |
| 124 | 501 | response.flushBuffer(); |
| 125 | 501 | Object[] params = new Object[5]; |
| 126 | 501 | params[0] = response.getWriter(); |
| 127 | 501 | params[1] = webslinger.getContext(); |
| 128 | 501 | params[2] = webslinger; |
| 129 | 501 | params[3] = webslinger.getRequest(); |
| 130 | 501 | params[4] = webslinger.getResponse(); |
| 131 | 501 | String language = (String) webslinger.getAttribute("template-language"); |
| 132 | 501 | if (language == null) language = DEFAULT_LANGUAGE; |
| 133 | 501 | templateManager.apply(language, "top", 0, 0, webslinger, paramNames, params, paramTypes); |
| 134 | 501 | return null; |
| 135 | 0 | } catch (BSFException e) { |
| 136 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 137 | |
} |
| 138 | |
} |
| 139 | |
} |