1 package org.sapia.util.xml.confix; 2 3 4 import java.util.HashMap ; 7 import java.util.Map ; 8 9 10 22 public class CompositeObjectFactory implements ObjectFactoryIF { 23 27 28 private Map _theFactories; 29 30 34 private boolean _mapToNameSpacePrefix = false; 35 36 40 43 public CompositeObjectFactory() { 44 _theFactories = new HashMap (); 45 } 46 47 56 public void setMapToPrefix(boolean mapToPrefix) { 57 _mapToNameSpacePrefix = mapToPrefix; 58 } 59 60 64 72 public void registerFactory(String aNamespaceURIorPrefix, 73 ObjectFactoryIF aFactory) { 74 if (_theFactories.containsKey(aNamespaceURIorPrefix)) { 75 throw new IllegalArgumentException ( 76 "An object factory is already registered for the namespace URI or prefix " + 77 aNamespaceURIorPrefix); 78 } 79 80 _theFactories.put(aNamespaceURIorPrefix, aFactory); 81 } 82 83 92 public ObjectFactoryIF getFactoryFor(String aNamespaceURIorPrefix) 93 throws IllegalArgumentException { 94 ObjectFactoryIF fac = (ObjectFactoryIF) _theFactories.get(aNamespaceURIorPrefix); 95 96 if (fac == null) { 97 throw new IllegalArgumentException ( 98 "No object factory registered for the namespace URI or prefix " + 99 aNamespaceURIorPrefix); 100 } 101 102 return fac; 103 } 104 105 112 public boolean containsObjectFactory(String aNamespaceURIorPrefix) { 113 return _theFactories.get(aNamespaceURIorPrefix) != null; 114 } 115 116 120 133 public CreationStatus newObjectFor(String aPrefix, String aNamespaceURI, 134 String anElementName, Object aParent) throws ObjectCreationException { 135 ObjectFactoryIF aFactory; 136 137 if (_mapToNameSpacePrefix) { 138 if ((aPrefix == null) || (aPrefix.length() == 0)) { 139 throw new ObjectCreationException("Prefix no defined"); 140 } 141 142 aFactory = (ObjectFactoryIF) _theFactories.get(aPrefix); 143 } else { 144 if ((aNamespaceURI == null) || (aNamespaceURI.length() == 0)) { 145 throw new ObjectCreationException("Namespace no defined"); 146 } 147 148 aFactory = (ObjectFactoryIF) _theFactories.get(aNamespaceURI); 149 } 150 151 if (aFactory == null) { 152 throw new ObjectCreationException("No factory found for the namespace " + 153 aNamespaceURI); 154 } else { 155 return aFactory.newObjectFor(aPrefix, aNamespaceURI, anElementName, 156 aParent); 157 } 158 } 159 } 160 | Popular Tags |