001package org.unix4j.codegen.loader; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import fmpp.Engine; 007import fmpp.tdd.DataLoader; 008import freemarker.template.SimpleSequence; 009import freemarker.template.TemplateModel; 010 011/** 012 * A {@link DataLoader} for the fmpp/freemarker codegenerator passing existing 013 * classes to a {@link ClassBasedDataLoader} for further analysis. The fully 014 * qualified class names are expected as input parameters in the arguments list 015 * of {@link #load(Engine, List)}. 016 */ 017public class ClassInspectionDataLoader implements DataLoader { 018 019 private final ClassBasedDataLoader templateLoader; 020 021 public ClassInspectionDataLoader(ClassBasedDataLoader templateLoader) { 022 this.templateLoader = templateLoader; 023 } 024 025 @SuppressWarnings("rawtypes") 026 @Override 027 public SimpleSequence load(Engine engine, List args) throws Exception { 028 return loadDataModel(engine, args); 029 } 030 031 private SimpleSequence loadDataModel(Engine engine, List<?> args) throws Exception { 032 final List<Class<?>> classes = loadClasses(engine, args); 033 final List<TemplateModel> templateModels = new ArrayList<TemplateModel>(); 034 for (final Class<?> clazz : classes) { 035 final TemplateModel templateModel = templateLoader.load(clazz); 036 if (templateModel != null) { 037 templateModels.add(templateModel); 038 } 039 } 040 return new SimpleSequence(templateModels); 041 } 042 043 private List<Class<?>> loadClasses(Engine engine, List<?> args) throws ClassNotFoundException { 044 final List<Class<?>> classes = new ArrayList<Class<?>>(args.size()); 045 for (final Object arg : args) { 046 classes.add(loadClass(String.valueOf(arg))); 047 } 048 return classes; 049 } 050 051 private Class<?> loadClass(String className) throws ClassNotFoundException { 052 return Class.forName(className); 053 } 054 055}