1 package org.sapia.regis.loader; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Collections ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 import org.sapia.regis.DuplicateNodeException; 13 import org.sapia.regis.RWNode; 14 import org.sapia.regis.util.Utils; 15 import org.sapia.util.text.MapContext; 16 import org.sapia.util.text.SystemContext; 17 import org.sapia.util.text.TemplateContextIF; 18 import org.sapia.util.text.TemplateElementIF; 19 import org.sapia.util.text.TemplateFactory; 20 import org.sapia.util.xml.confix.CreationStatus; 21 import org.sapia.util.xml.confix.Dom4jProcessor; 22 import org.sapia.util.xml.confix.ObjectCreationException; 23 import org.sapia.util.xml.confix.ReflectionFactory; 24 25 public class RegistryConfigLoader { 26 27 private RWNode _root; 28 29 public RegistryConfigLoader(RWNode root){ 30 _root = root; 31 } 32 33 public void load(File file, Map vars) throws IOException , DuplicateNodeException, Exception { 34 doLoad(new FileInputStream (file), vars); 35 } 36 37 public void load(File file) throws IOException , DuplicateNodeException, Exception { 38 doLoad(new FileInputStream (file), Collections.EMPTY_MAP); 39 } 40 41 public void load(InputStream is, Map vars) throws IOException , DuplicateNodeException, Exception { 42 doLoad(is, vars); 43 } 44 45 public void load(InputStream is) throws IOException , DuplicateNodeException, Exception { 46 doLoad(is, Collections.EMPTY_MAP); 47 } 48 49 private RegistryTag doLoad(InputStream is, Map vars) 50 throws IOException , DuplicateNodeException, Exception { 51 52 try{ 53 String content = Utils.loadAsString(is); 54 TemplateFactory tmplFac = new TemplateFactory("$[", "]"); 55 TemplateElementIF template = tmplFac.parse(content); 56 TemplateContextIF ctx = new MapContext(vars, new SystemContext(), false); 57 content = template.render(ctx); 58 ConfigObjectFactory fac = new ConfigObjectFactory(ctx); 59 Dom4jProcessor proc = new Dom4jProcessor(fac); 60 is = new ByteArrayInputStream (content.getBytes()); 61 RegistryTag conf = (RegistryTag)proc.process(is); 62 conf.create(_root); 63 return conf; 64 }finally{ 65 is.close(); 66 } 67 } 68 69 public static final class ConfigObjectFactory extends ReflectionFactory{ 70 71 static final String REGISTRY = "registry"; 72 73 static final Map CLASSES = new HashMap (); 74 75 static{ 76 77 CLASSES.put("choose", ChooseTag.class); 78 CLASSES.put("if", IfTag.class); 79 CLASSES.put("include", IncludeTag.class); 80 CLASSES.put("link", LinkTag.class); 81 CLASSES.put("node", NodeTag.class); 82 CLASSES.put("paramRef", ParamRefTag.class); 83 CLASSES.put("property", PropertyTag.class); 84 CLASSES.put("registry", RegistryTag.class); 85 CLASSES.put("unless", UnlessTag.class); 86 } 87 88 private TemplateContextIF _ctx; 89 90 public ConfigObjectFactory(TemplateContextIF ctx){ 91 super(new String [0]); 92 _ctx = ctx; 93 } 94 95 public CreationStatus newObjectFor(String prefix, String uri, String name, Object parent) throws ObjectCreationException { 96 Class clazz = (Class )CLASSES.get(name); 97 if(clazz == null){ 98 return super.newObjectFor(prefix, uri, name, parent); 99 } 100 else{ 101 try{ 102 Object instance = clazz.newInstance(); 103 if(instance instanceof TagFactory){ 104 instance = ((TagFactory)instance).create(_ctx, this); 105 } 106 if(instance instanceof TagNameAware){ 107 ((TagNameAware)instance).setTagName(name); 108 } 109 return CreationStatus.create(instance); 110 }catch(Exception e){ 111 throw new ObjectCreationException("Could not create object for: " + name, e); 112 } 113 } 114 } 115 } 116 117 } 118 | Popular Tags |