1 package org.sapia.gumby.factory; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import org.sapia.util.xml.confix.ConfigurationException; 7 import org.sapia.util.xml.confix.CreationStatus; 8 import org.sapia.util.xml.confix.ObjectCreationException; 9 import org.sapia.util.xml.confix.ReflectionFactory; 10 11 26 public class NamespacedFactory extends ReflectionFactory { 27 28 private Map _defs = new HashMap (); 29 30 33 public NamespacedFactory() { 34 super(new String [0]); 35 } 36 37 43 public void addDef(Def def) throws ConfigurationException { 44 if(_defs.get(def.getName()) != null) { 45 throw new ConfigurationException("Definition already declared: " 46 + def.getName() + ", " + def.getClassName()); 47 } 48 49 _defs.put(def.getName(), def); 50 } 51 52 public boolean hasDefFor(String name) { 53 return _defs.get(name) != null; 54 } 55 56 60 public CreationStatus newObjectFor(String prefix, String uri, String name, 61 Object parent) throws ObjectCreationException { 62 if((prefix == null) || (prefix.length() == 0)) { 63 return super.newObjectFor(prefix, uri, name, parent); 64 } 65 66 Def def = (Def) _defs.get(name); 67 68 if(def == null) { 69 throw new ObjectCreationException("Unknown element: " + name); 70 } else { 71 try { 72 return CreationStatus.create(Class.forName(def.getClassName()) 73 .newInstance()); 74 } catch(Exception e) { 75 throw new ObjectCreationException( 76 "Could not create object for " + name, e); 77 } 78 } 79 } 80 81 } 82 | Popular Tags |