Coverage Report - org.webslinger.macros.FileBasedMacro
 
Classes in this File Line Coverage Branch Coverage Complexity
FileBasedMacro
78%
29/37
67%
4/6
0
FileBasedMacro$1
100%
5/5
N/A
0
FileBasedMacro$ParameterList
100%
10/10
100%
2/2
0
 
 1  
 package org.webslinger.macros;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.Writer;
 5  
 import java.util.ArrayList;
 6  
 import java.util.Arrays;
 7  
 import java.util.HashMap;
 8  
 import java.util.Map;
 9  
 import javax.servlet.ServletException;
 10  
 
 11  
 import org.apache.bsf.BSFException;
 12  
 import org.apache.commons.vfs.FileContent;
 13  
 import org.apache.commons.vfs.FileObject;
 14  
 
 15  
 import org.webslinger.PathContext;
 16  
 import org.webslinger.Webslinger;
 17  
 import org.webslinger.WebslingerInvoker;
 18  
 import org.webslinger.WebslingerServletContext;
 19  
 import org.webslinger.template.TemplateMacro;
 20  
 import org.webslinger.template.TemplateManager;
 21  
 import org.webslinger.util.GeneratedResult;
 22  
 import org.webslinger.util.TTLCachedObject;
 23  
 import org.webslinger.util.TTLObject;
 24  
 
 25  78
 public class FileBasedMacro implements TemplateMacro {
 26  
     static {
 27  1
         TTLObject.setDefaultTTLForClass(ParameterList.class, 1000);
 28  
     }
 29  
 
 30  
     private final String name;
 31  
     private final PathContext pc;
 32  442
     private final ParameterList parameterList = new ParameterList();
 33  
     private final WebslingerServletContext context;
 34  
 
 35  442
     public FileBasedMacro(WebslingerServletContext context, String name, PathContext pc) {
 36  442
         this.context = context;
 37  442
         this.name = name;
 38  442
         this.pc = pc;
 39  442
     }
 40  
 
 41  
     public String getName() {
 42  442
         return name;
 43  
     }
 44  
 
 45  
     public boolean isBlock() {
 46  
         try {
 47  80
             boolean isBlock = "true".equals(pc.getAttribute("macro-is-block"));
 48  
             //System.err.println("isBlock(" + pc + "<" + pc.getFile().exists() + ">)=" + isBlock);
 49  80
             return isBlock;
 50  0
         } catch (IOException e) {
 51  
             // FIXME: log this e.printStackTrace();
 52  0
             return false;
 53  
         }
 54  
     }
 55  
 
 56  1
     public static final TemplateManager.MacroBody MACRO_BODY = new TemplateManager.MacroBody() {
 57  
         public boolean render(TemplateManager manager, Object parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException {
 58  40
             WebslingerInvoker invoker = ((Webslinger) parent).getInvoker();
 59  40
             Webslinger old = invoker.popFrame();
 60  
             try {
 61  40
                 return super.render(manager, parent, writer, args, context, body);
 62  
             } finally {
 63  40
                 invoker.pushFrame(old);
 64  
             }
 65  
         }
 66  
     };
 67  
 
 68  
     public boolean render(TemplateManager manager, Object parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException {
 69  104
         Map<String, Integer> parameters = parameterList.getObject();
 70  104
         if (args.pList != null) {
 71  104
             for (Map.Entry<String, Integer> entry: parameters.entrySet()) {
 72  106
                 Integer index = entry.getValue();
 73  106
                 if (index.intValue() < args.pList.length) {
 74  97
                     context.put(entry.getKey(), args.pList[index.intValue()]);
 75  
                 }
 76  106
             }
 77  
         } else {
 78  0
             for (String key: parameters.keySet()) {
 79  0
                 if (!args.pMap.containsKey(key)) {
 80  
                 }
 81  0
                 context.put(key, args.pMap.get(key));
 82  
             }
 83  
         }
 84  104
         manager.pushBody(body);
 85  
         try {
 86  104
             boolean hadCW = context.containsKey("callingWebslinger");
 87  104
             Object oldCW = (Webslinger) context.get("callingWebslinger");
 88  
             try {
 89  104
                 context.put("callingWebslinger", body.getSource());
 90  104
                 context.put("templateManager", manager);
 91  104
                 this.context.getInvoker().run(pc, null, context, "merge", null, writer);
 92  
             } finally {
 93  104
                 if (hadCW) context.put("callingWebslinger", oldCW);
 94  
             }
 95  0
         } catch (ServletException e) {
 96  0
             throw (IOException) new IOException(e.getMessage()).initCause(e);
 97  
         } finally {
 98  104
             manager.popBody();
 99  104
         }
 100  104
         return true;
 101  
     }
 102  
 
 103  
     public TemplateMacro newInstance(TemplateManager manager, Object parent, Args args) throws IOException, BSFException {
 104  0
         return this;
 105  
     }
 106  
 
 107  520
     protected class ParameterList extends TTLCachedObject<Map<String, Integer>> {
 108  
         protected long getTimestamp(Map<String, Integer> old) throws IOException {
 109  45
             FileObject file = pc.getFile();
 110  45
             return file.exists() ? file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP;
 111  
         }
 112  
 
 113  
         protected GeneratedResult<Map<String, Integer>> generate(Map<String, Integer> old) throws IOException {
 114  33
             FileContent fc = pc.getFile().getContent();
 115  33
             HashMap<String, Integer> parameters = new HashMap<String, Integer>();
 116  57
             for (int i = 0; ; i++) {
 117  57
                 String parameterName = (String) fc.getAttribute("macro-parameter-" + i);
 118  57
                 if (parameterName == null) break;
 119  24
                 parameters.put(parameterName, i);
 120  
             }
 121  33
             return new GeneratedResult<Map<String, Integer>>(fc.getLastModifiedTime(), parameters);
 122  
         }
 123  
     }
 124  
 }