1 package org.sapia.validator.config; 2 3 import org.sapia.util.xml.confix.CompositeObjectFactory; 4 import org.sapia.util.xml.confix.CreationStatus; 5 import org.sapia.util.xml.confix.ObjectCreationException; 6 import org.sapia.util.xml.confix.ObjectFactoryIF; 7 import org.sapia.util.xml.confix.ReflectionFactory; 8 import org.sapia.validator.Defs; 9 import org.sapia.validator.Namespace; 10 import org.sapia.validator.RuleRef; 11 import org.sapia.validator.RuleSet; 12 import org.sapia.validator.RuleSetRef; 13 import org.sapia.validator.Vlad; 14 15 import java.util.*; 16 17 28 public class VladObjectFactory implements ObjectFactoryIF { 29 private CompositeObjectFactory _comp = new CompositeObjectFactory(); 30 private List _factories = new ArrayList(); 31 private List _listeners = new ArrayList(); 32 private Vlad _vlad; 33 34 37 public VladObjectFactory( 38 ) { 39 _factories.add(new ReflectionFactory(new String [0])); 40 } 41 42 public VladObjectFactory(Vlad vlad ) { 43 this(); 44 _vlad = vlad; 45 } 46 47 public void registerDef(String prefix, String localName, Def def) { 48 if (_vlad == null) { 49 throw new IllegalStateException ("Vlad not set"); 50 } 51 52 XmlObjectFactory fac; 53 54 if (_comp.containsObjectFactory(prefix)) { 55 fac = (XmlObjectFactory) _comp.getFactoryFor(prefix); 56 } else { 57 fac = new XmlObjectFactory(_vlad); 58 _comp.registerFactory(prefix, fac); 59 } 60 61 fac.registerDef(localName, def); 62 } 63 64 public void addFactoryFor(String [] packages) { 65 _factories.add(new ReflectionFactory(packages)); 66 } 67 68 71 public CreationStatus newObjectFor(String prefix, String uri, 72 String localName, Object parent) 73 throws ObjectCreationException { 74 if ((prefix != null) && (prefix.length() != 0)) { 75 return getFactoryFor(prefix).newObjectFor(prefix, uri, localName, parent); 76 } 77 78 ObjectFactoryIF fac; 79 80 for (int i = 0; i < _factories.size(); i++) { 81 fac = (ObjectFactoryIF) _factories.get(i); 82 83 try { 84 return fac.newObjectFor(prefix, uri, localName, parent); 85 } catch (Throwable e) { 86 } 88 } 89 90 if (localName.equalsIgnoreCase("vlad")) { 91 return CreationStatus.create(_vlad); 92 } else if (localName.equalsIgnoreCase("namespace")) { 93 return CreationStatus.create(new Namespace()).assigned(false); 94 } else if (localName.equalsIgnoreCase("ruleset")) { 95 return CreationStatus.create(new RuleSet()).assigned(false); 96 } else if (localName.equalsIgnoreCase("ruleSetRef")) { 97 return CreationStatus.create(new RuleSetRef()).assigned(false); 98 } else if (localName.equalsIgnoreCase("ruleRef")) { 99 return CreationStatus.create(new RuleRef()).assigned(false); 100 } else if (localName.equalsIgnoreCase("defs")) { 101 return CreationStatus.create(new Defs(this)).assigned(false); 102 } 103 104 String name; 105 106 if (prefix != null) { 107 name = prefix + ':' + localName; 108 } else { 109 name = localName; 110 } 111 112 throw new ObjectCreationException("Could not creation object for: " + name); 113 } 114 115 protected ObjectFactoryIF getFactoryFor(String prefix) 116 throws IllegalArgumentException { 117 ObjectFactoryIF fac = (ObjectFactoryIF) _comp.getFactoryFor(prefix); 118 119 if (fac == null) { 120 throw new IllegalStateException ("No factory for prefix: " + prefix); 121 } 122 123 return fac; 124 } 125 } 126 | Popular Tags |