1 4 package org.oddjob.arooa; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.InputStream ; 11 12 import org.apache.commons.beanutils.BeanUtilsBean; 13 import org.apache.log4j.Logger; 14 import org.oddjob.OddjobException; 15 import org.oddjob.arooa.handlers.DefaultComponentHandler; 16 import org.oddjob.arooa.handlers.DocumentStartHandler; 17 import org.oddjob.arooa.handlers.MainHandler; 18 import org.oddjob.arooa.handlers.MappedPropertyHandler; 19 import org.oddjob.arooa.handlers.NestedElementHandler; 20 import org.oddjob.arooa.handlers.NestedPropertyHandler; 21 import org.oddjob.arooa.registry.ComponentRegistry; 22 import org.oddjob.arooa.xml.XMLDefinitionHelper; 23 import org.xml.sax.InputSource ; 24 25 34 public class ArooaFactory { 35 private static final Logger logger = Logger.getLogger(ArooaFactory.class); 36 37 private String documentTag; 38 private ObjectFactory componentFactory; 39 private ObjectFactory valueFactory; 40 private PropertyProxyResolver propertProxyResolver; 41 private ArooaHandler documentStartHandler; 42 private ArooaHandler componentHandler; 43 private ArooaHandler propertyHandler; 44 private ArooaHandler mappedHandler; 45 private ArooaHandler elementHandler; 46 private ComponentRegistry componentRegistry; 47 private String substitutionPolicy; 48 private Object objectToConfigure; 49 private BeanUtilsBean beanUtilsBean; 50 private ComponentProxyResolver componentProxyResolver; 51 52 59 public Object build(File file) { 60 61 InputSource inputSource = null; 62 try { 63 InputStream inputStream = new FileInputStream (file); 64 inputSource = new InputSource (inputStream); 65 } catch (FileNotFoundException exc) { 66 throw new OddjobException(exc); 67 } 68 inputSource.setSystemId(new File (file.getAbsolutePath()).toURI().toString()); 69 return build(inputSource); 70 } 71 72 78 public Object build(String xml) { 79 return build(new ByteArrayInputStream (xml.getBytes())); 80 } 81 82 89 public Object build(InputStream in) { 90 InputSource inputSource = new InputSource (in); 91 inputSource.setSystemId("urn:stream"); 92 return build(inputSource); 93 } 94 95 103 public Object build(InputSource inputSource) { 104 105 ArooaContext context = new ArooaContext(); 106 context.set(ArooaConstants.COMPONENT_FACTORY, getComponentFactory()); 107 context.set(ArooaConstants.VALUE_FACTORY, getValueFactory()); 108 context.set(ArooaConstants.PROPERTY_PROXY_RESOLVER, getPropertProxyResolver()); 109 context.set(ArooaConstants.PROPERTY_HANDLER, getPropertyHandler()); 110 context.set(ArooaConstants.MAPPED_HANDLER, getMappedHandler()); 111 context.set(ArooaConstants.ELEMENT_HANDLER, getElementHandler()); 112 context.set(ArooaConstants.SUBSTITUTION_POLICY, getSubstitutionPolicy()); 113 context.set(ArooaConstants.COMPONENT_REGISTRY, getComponentRegistry()); 114 context.set(ArooaConstants.BEAN_UTILS_BEAN, getBeanUtilsBean()); 115 if (componentProxyResolver != null) { 116 context.set(ArooaConstants.COMPONENT_PROXY_RESOLVER, 117 componentProxyResolver); 118 } 119 120 if (componentHandler == null) { 121 componentHandler = new DefaultComponentHandler(); 122 } 123 context.set(ArooaConstants.COMPONENT_HANDLER, componentHandler); 124 125 Object rootComponent = objectToConfigure; 126 ArooaHandler startHandler = documentStartHandler; 127 RuntimeConfiguration rtc = null; 128 129 if (startHandler == null) { 130 if (rootComponent == null) { 131 startHandler = componentHandler; 132 rootComponent = new ComponentRoot(); 133 } 134 else { 135 startHandler = new DocumentStartHandler(objectToConfigure); 136 } 137 rtc = new ArooaRuntime(rootComponent, 138 documentTag == null ? "documentTag" : documentTag, context); 139 context.set(ArooaConstants.CURRENTLY_CONFIGURING, rtc); 140 } 141 142 XMLDefinitionHelper ph = new XMLDefinitionHelper(context); 143 144 if (documentTag == null) { 145 ph.parse(inputSource, new MainHandler(startHandler)); 146 } 147 else { 148 ph.parse(inputSource, new MainHandler(documentTag, startHandler)); 149 } 150 151 rtc = (RuntimeConfiguration) context.get(ArooaConstants.CURRENTLY_CONFIGURING); 153 if (rtc != null) { 154 rtc.configure(); 155 } 156 if (objectToConfigure != null) { 157 return objectToConfigure; 158 } 159 if (rootComponent == null) { 160 return null; 161 } 162 return ((ComponentRoot)rootComponent).component; 163 } 164 165 public class ComponentRoot { 166 Object component; 167 public void addComponent(Object component) { 168 this.component = component; 169 } 170 } 171 172 175 public void setComponentFactory(ObjectFactory componentFactory) { 176 this.componentFactory = componentFactory; 177 } 178 181 public void setComponentHandler(ArooaHandler componentHandler) { 182 this.componentHandler = componentHandler; 183 } 184 187 public void setComponentRegistry(ComponentRegistry componentRegistry) { 188 this.componentRegistry = componentRegistry; 189 } 190 193 public void setDocumentTag(String documentTag) { 194 this.documentTag = documentTag; 195 } 196 199 public void setElementHandler(ArooaHandler elementHandler) { 200 this.elementHandler = elementHandler; 201 } 202 203 206 public void setSubstitutionPolicy(String substitutionPolicy) { 207 this.substitutionPolicy = substitutionPolicy; 208 } 209 212 public void setPropertProxyResolver(PropertyProxyResolver typeManager) { 213 this.propertProxyResolver = typeManager; 214 } 215 218 public void setValueFactory(ObjectFactory valueFactory) { 219 this.valueFactory = valueFactory; 220 } 221 224 public void setPropertyHandler(ArooaHandler valueHandler) { 225 this.propertyHandler = valueHandler; 226 } 227 230 public void setObjectToConfigure(Object objectToConfigure) { 231 this.objectToConfigure = objectToConfigure; 232 } 233 236 public void setMappedHandler(ArooaHandler mappedHandler) { 237 this.mappedHandler = mappedHandler; 238 } 239 240 243 public BeanUtilsBean getBeanUtilsBean() { 244 if (beanUtilsBean == null) { 245 beanUtilsBean = BeanUtilsBean.getInstance(); 246 } 247 return beanUtilsBean; 248 } 249 252 public void setBeanUtilsBean(BeanUtilsBean bub) { 253 this.beanUtilsBean = bub; 254 } 255 258 public ObjectFactory getComponentFactory() { 259 if (componentFactory == null) { 260 componentFactory = new SimpleObjectFactory(); 261 } 262 return componentFactory; 263 } 264 265 268 public ArooaHandler getComponentHandler() { 269 return componentHandler; 270 } 271 274 public ComponentRegistry getComponentRegistry() { 275 if (componentRegistry == null) { 276 logger.debug("Creating default ComponentRegistry."); 277 componentRegistry = new ComponentRegistry(); 278 } 279 return componentRegistry; 280 } 281 284 public String getDocumentTag() { 285 return documentTag; 286 } 287 288 291 public ArooaHandler getElementHandler() { 292 if (elementHandler == null) { 293 elementHandler = new NestedElementHandler(); 294 } 295 return elementHandler; 296 } 297 300 public ArooaHandler getMappedHandler() { 301 if (mappedHandler == null) { 302 mappedHandler = new MappedPropertyHandler(); 303 } 304 return mappedHandler; 305 } 306 307 310 public ArooaHandler getPropertyHandler() { 311 if (propertyHandler == null) { 312 propertyHandler = new NestedPropertyHandler(); 313 } 314 return propertyHandler; 315 } 316 317 320 public String getSubstitutionPolicy() { 321 return substitutionPolicy; 322 } 323 326 public PropertyProxyResolver getPropertProxyResolver() { 327 if (propertProxyResolver == null) { 328 propertProxyResolver = new PropertyProxyResolver(); 329 } 330 return propertProxyResolver; 331 } 332 335 public ObjectFactory getValueFactory() { 336 if (valueFactory == null) { 337 valueFactory = new SimpleObjectFactory(); 338 } 339 return valueFactory; 340 } 341 350 public void setDocumentStartHandler(ArooaHandler documentStartHandler) { 351 this.documentStartHandler = documentStartHandler; 352 } 353 354 358 public ArooaHandler getDocumentStartHandler() { 359 return documentStartHandler; 360 } 361 362 public ComponentProxyResolver getComponentProxyResolver() { 363 return componentProxyResolver; 364 } 365 366 public void setComponentProxyResolver( 367 ComponentProxyResolver componentProxyResolver) { 368 this.componentProxyResolver = componentProxyResolver; 369 } 370 371 } 372 | Popular Tags |