001 /******************************************************************************* 002 * Copyright (C) PicoContainer Organization. All rights reserved. 003 * --------------------------------------------------------------------------- 004 * The software in this package is published under the terms of the BSD style 005 * license a copy of which has been included with this distribution in the 006 * LICENSE.txt file. 007 ******************************************************************************/ 008 package org.picocontainer.script.util; 009 010 import org.picocontainer.Parameter; 011 import org.picocontainer.classname.ClassLoadingPicoContainer; 012 import org.picocontainer.classname.ClassName; 013 import org.picocontainer.script.ScriptedPicoContainerMarkupException; 014 015 import java.util.Properties; 016 017 018 public class ComponentElementHelper { 019 020 public static Object makeComponent(Object classNamekey, Object key, Parameter[] parameters, Object classValue, ClassLoadingPicoContainer current, Object instance, Properties[] properties) { 021 ClassLoadingPicoContainer container = current; 022 if (properties.length != 0) { 023 container = (ClassLoadingPicoContainer) current.as(properties); 024 } 025 if (classNamekey != null) { 026 key = new ClassName((String)classNamekey); 027 } 028 029 if (classValue instanceof Class) { 030 Class<?> clazz = (Class<?>) classValue; 031 key = key == null ? clazz : key; 032 return container.addComponent(key, clazz, parameters); 033 } else if (classValue instanceof String) { 034 String className = (String) classValue; 035 key = key == null ? className : key; 036 return container.addComponent(key, new ClassName(className), parameters); 037 } else if (instance != null) { 038 key = key == null ? instance.getClass() : key; 039 return container.addComponent(key, instance); 040 } else { 041 throw new ScriptedPicoContainerMarkupException("Must specify a 'class' attribute for a component as a class name (string) or Class."); 042 } 043 } 044 045 public static Object makeComponent(Object classNameKey, 046 Object key, 047 Parameter[] parameters, 048 Object classValue, 049 ClassLoadingPicoContainer container, Object instance) { 050 return makeComponent(classNameKey, key, parameters, classValue, container, instance, new Properties[0]); 051 } 052 }