01: /*****************************************************************************
02: * Copyright (C) NanoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: *****************************************************************************/package org.nanocontainer.script;
09:
10: import org.picocontainer.Parameter;
11: import org.nanocontainer.ClassNameKey;
12: import org.nanocontainer.NanoContainer;
13:
14: public class ComponentElementHelper {
15:
16: public static void makeComponent(Object cnkey, Object key,
17: Parameter[] parameters, Object klass,
18: NanoContainer current, Object instance) {
19: if (cnkey != null) {
20: key = new ClassNameKey((String) cnkey);
21: }
22:
23: if (klass instanceof Class) {
24: Class clazz = (Class) klass;
25: key = key == null ? clazz : key;
26: current.getPico().registerComponentImplementation(key,
27: clazz, parameters);
28: } else if (klass instanceof String) {
29: String className = (String) klass;
30: key = key == null ? className : key;
31: try {
32: current.registerComponentImplementation(key, className,
33: parameters);
34: } catch (ClassNotFoundException e) {
35: throw new NanoContainerMarkupException(
36: "ClassNotFoundException: " + e.getMessage(), e);
37: }
38: } else if (instance != null) {
39: key = key == null ? instance.getClass() : key;
40: current.getPico().registerComponentInstance(key, instance);
41: } else {
42: throw new NanoContainerMarkupException(
43: "Must specify a 'class' attribute for a component as a class name (string) or Class.");
44: }
45: }
46:
47: }
|