1 package org.sapia.gumby.factory; 2 3 import java.util.List ; 4 5 import org.sapia.gumby.RenderContext; 6 import org.sapia.gumby.RenderContextFactory; 7 import org.sapia.util.xml.confix.CompositeObjectFactory; 8 import org.sapia.util.xml.confix.ConfigurationException; 9 import org.sapia.util.xml.confix.CreationStatus; 10 import org.sapia.util.xml.confix.ObjectCreationException; 11 import org.sapia.util.xml.confix.ReflectionFactory; 12 13 28 public class GumbyObjectFactory extends CompositeObjectFactory { 29 30 private static final String SWING_PREFIX = "swing"; 31 private static final String GUMBY_PREFIX = "gumby"; 32 33 private static final String TAG_NAMESPACE = "namespace"; 34 private static final String TAG_CONFIG = "config"; 35 36 private ReflectionFactory _invoker = new ReflectionFactory( 37 new String [0]); 38 private GumbyObjectFactory _parent; 39 private RenderContext _ctx; 40 41 public GumbyObjectFactory() { 42 super.setMapToPrefix(true); 43 super.registerFactory(SWING_PREFIX, new SwingFactory()); 44 _ctx = RenderContextFactory.newInstance(); 45 } 46 47 public GumbyObjectFactory(GumbyObjectFactory parent, RenderContext context) { 48 super.setMapToPrefix(true); 49 super.registerFactory(SWING_PREFIX, new SwingFactory()); 50 _ctx = context; 51 _parent = parent; 52 } 53 54 58 public synchronized CreationStatus newObjectFor(String prefix, String uri, 59 String elementName, Object parent) throws ObjectCreationException { 60 61 CreationStatus stat = doNewObjectFor(prefix, uri, elementName, parent); 62 if(stat.getCreated() instanceof ContextAware) { 63 ((ContextAware) stat.getCreated()).handleContext(_ctx); 64 } 65 if(stat.getCreated() instanceof ParentAware) { 66 ((ParentAware) stat.getCreated()).handleParent(parent); 67 } 68 return stat; 69 } 70 71 public synchronized void registerDefs(Namespace defs) 72 throws ConfigurationException { 73 NamespacedFactory fac; 74 75 if(super.containsObjectFactory(defs.getPrefix())) { 76 fac = (NamespacedFactory) super.getFactoryFor(defs.getPrefix()); 77 } else { 78 super.registerFactory(defs.getPrefix(), fac = new NamespacedFactory()); 79 } 80 81 Def def; 82 List lst = defs.getDefs(); 83 84 for(int i = 0; i < lst.size(); i++) { 85 def = (Def) lst.get(i); 86 fac.addDef(def); 87 } 88 } 89 90 private CreationStatus doNewObjectFor(String prefix, String uri, 91 String elementName, Object parent) throws ObjectCreationException { 92 if(_parent != null && prefix != null && prefix.length() > 0) { 93 if(_parent.containsObjectFactory(prefix)) { 94 try { 95 return _parent.getFactoryFor(prefix).newObjectFor(prefix, uri, 96 elementName, parent); 97 } catch(ObjectCreationException e) { 98 } 99 } 100 } 101 if(prefix == null || prefix.length() == 0) { 102 return _invoker.newObjectFor(prefix, uri, elementName, parent); 103 } else if(prefix.equals(GUMBY_PREFIX)) { 104 if(elementName.equals(TAG_NAMESPACE)) { 105 return CreationStatus.create(new Namespace(this)).assigned(true); 106 } else if(elementName.equals(TAG_CONFIG)) { 107 return CreationStatus.create(new Config()).assigned(true); 108 } 109 return super.newObjectFor(prefix, uri, elementName, parent); 110 } else { 111 return super.newObjectFor(prefix, uri, elementName, parent); 112 } 113 } 114 115 } 116 | Popular Tags |