Coverage Report - org.webslinger.template.TemplateManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TemplateManager
74%
37/50
100%
10/10
0
TemplateManager$MacroBody
88%
7/8
N/A
0
 
 1  
 package org.webslinger.template;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.Writer;
 5  
 import java.util.ArrayList;
 6  
 import java.util.Iterator;
 7  
 import java.util.Map;
 8  
 import java.util.Set;
 9  
 import java.util.concurrent.ConcurrentHashMap;
 10  
 import java.util.logging.Logger;
 11  
 import javax.imageio.spi.ServiceRegistry;
 12  
 
 13  
 import org.apache.bsf.BSFEngine;
 14  
 import org.apache.bsf.BSFException;
 15  
 
 16  
 import org.webslinger.bsf.AbstractManager;
 17  
 import org.webslinger.bsf.LanguageEngine;
 18  
 import org.webslinger.vfs.VFSDelegate;
 19  
 import org.webslinger.vfs.TypeVFSDelegate;
 20  
 
 21  
 public class TemplateManager extends AbstractManager {
 22  25
     protected final ConcurrentHashMap<String, TemplateMacro> macros = new ConcurrentHashMap<String, TemplateMacro>();
 23  
 
 24  
     public TemplateManager(Logger logger) throws BSFException {
 25  0
         this(TypeVFSDelegate.DEFAULT, logger);
 26  0
     }
 27  
 
 28  
     public TemplateManager(VFSDelegate<?, Object, ?> vfsDelegate, Logger logger) throws BSFException {
 29  25
         super(vfsDelegate, logger);
 30  25
         postConstruct();
 31  25
     }
 32  
 
 33  
     public void findLanguageEngines(ClassLoader loader) throws BSFException {
 34  50
         if (loader == null) return;
 35  50
         Iterator<TemplateEngineInfo> it = ServiceRegistry.lookupProviders(TemplateEngineInfo.class, loader);
 36  250
         while (it.hasNext()) {
 37  
             try {
 38  200
                 TemplateEngineInfo info = it.next();
 39  450
                 for (String name: info.getNames()) {
 40  250
                     registerLanguageEngine(name, info.getImplClassName(name), info.getExtensions(name));
 41  
                 }
 42  0
             } catch (RuntimeException e) {
 43  0
             } catch (UnsupportedClassVersionError e) {
 44  200
             }
 45  
         }
 46  50
         Iterator<TemplateMacro> it1 = ServiceRegistry.lookupProviders(TemplateMacro.class, loader);
 47  50
         while (it1.hasNext()) {
 48  
             try {
 49  0
                 TemplateMacro macro = it1.next();
 50  0
                 declareBean(macro.getName(), macro, TemplateMacro.class);
 51  0
             } catch (RuntimeException e) {
 52  0
             } catch (UnsupportedClassVersionError e) {
 53  0
             }
 54  
         }
 55  50
         Iterator<TemplateMacroInfo> it2 = ServiceRegistry.lookupProviders(TemplateMacroInfo.class, loader);
 56  300
         while (it2.hasNext()) {
 57  250
             TemplateMacroInfo info = it2.next();
 58  250
             TemplateMacro[] macros = info.getMacros();
 59  250
             if (macros == null) continue;
 60  750
             for (int i = 0; i < macros.length; i++) {
 61  500
                 declareBean(macros[i].getName(), macros[i], TemplateMacro.class);
 62  
             }
 63  250
         }
 64  50
     }
 65  
 
 66  
     public void declareBean(String name, Object bean, Class type) throws BSFException {
 67  967
         if (!(bean instanceof TemplateMacro)) throw new IllegalArgumentException("not a bean(" + name + ")");
 68  967
         super.declareBean(name, bean, type);
 69  967
         macros.put(name, (TemplateMacro) bean);
 70  967
     }
 71  
 
 72  
     public void undeclareBean(String name) throws BSFException {
 73  0
         macros.remove(name);
 74  0
         super.undeclareBean(name);
 75  0
     }
 76  
 
 77  
     public Set<String> getMacroNames() {
 78  25
         return macros.keySet();
 79  
     }
 80  
 
 81  
     protected LanguageEngine getLanguageEngineConvertor(String language, BSFEngine engine) throws BSFException {
 82  0
         throw new UnsupportedOperationException("Template engines can't be implemented as standard BSFEngines.");
 83  
     }
 84  
 
 85  1
     public static abstract class MacroBody implements TemplateMacro {
 86  
         public String getName() {
 87  25
             return "MacroBody";
 88  
         }
 89  
 
 90  
         public boolean isBlock() {
 91  19
             return false;
 92  
         }
 93  
 
 94  
         public boolean render(TemplateManager manager, Object parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException {
 95  40
             Body outerBody = popBody();
 96  
             try {
 97  40
                 outerBody.render(writer, context);
 98  40
                 return false;
 99  
             } finally {
 100  40
                 pushBody(outerBody);
 101  
             }
 102  
         }
 103  
 
 104  
         public TemplateMacro newInstance(TemplateManager manager, Object parent, Args args) throws IOException, BSFException {
 105  0
             return this;
 106  
         }
 107  
     }
 108  
 
 109  1
     protected static final ThreadLocal<ArrayList<TemplateMacro.Body>> tl = new ThreadLocal<ArrayList<TemplateMacro.Body>>();
 110  
 
 111  
     public static void pushBody(TemplateMacro.Body body) {
 112  144
         ArrayList<TemplateMacro.Body> bodies = tl.get();
 113  144
         if (bodies == null) {
 114  140
             bodies = new ArrayList<TemplateMacro.Body>();
 115  140
             tl.set(bodies);
 116  
         }
 117  144
         bodies.add(body);
 118  144
     }
 119  
 
 120  
     public static TemplateMacro.Body popBody() {
 121  144
         ArrayList<TemplateMacro.Body> bodies = tl.get();
 122  
         try {
 123  144
             return bodies.remove(bodies.size() - 1);
 124  
         } finally {
 125  144
             if (bodies.isEmpty()) tl.set(null);
 126  
         }
 127  
     }
 128  
 }