| 1 package org.sapia.soto.util; 2 3 import org.sapia.util.xml.confix.ConfigurationException; 4 import org.sapia.util.xml.confix.CreationStatus; 5 import org.sapia.util.xml.confix.ObjectCreationException; 6 import org.sapia.util.xml.confix.ReflectionFactory; 7 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 12 24 public class DefObjectFactory extends ReflectionFactory { 25 private Map _defs = new HashMap (); 26 27 30 public DefObjectFactory() { 31 super(new String [0]); 32 } 33 34 39 public void addDef(Def def) throws ConfigurationException { 40 if (_defs.get(def.getName()) != null) { 41 throw new ConfigurationException("Definition already declared: " + 42 def.getName() + ", " + def.getClazz()); 43 } 44 45 _defs.put(def.getName(), def); 46 } 47 48 51 public CreationStatus newObjectFor(String prefix, String uri, String name, 52 Object parent) throws ObjectCreationException { 53 if ((prefix == null) || (prefix.length() == 0)) { 54 return super.newObjectFor(prefix, uri, name, parent); 55 } 56 57 Def def = (Def) _defs.get(name); 58 59 if (def == null) { 60 throw new ObjectCreationException("Unknown element: " + name); 61 } else { 62 try { 63 return CreationStatus.create(Class.forName(def.getClazz()).newInstance()); 64 } catch (Exception e) { 65 throw new ObjectCreationException("Could not create object for " + 66 name, e); 67 } 68 } 69 } 70 } 71 | Popular Tags |