1 package org.codehaus.plexus.component.configurator.converters.composite;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.lang.reflect.Array;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
32 import org.codehaus.plexus.component.configurator.ConfigurationListener;
33 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
34 import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
35 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
36 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
37 import org.codehaus.plexus.configuration.PlexusConfiguration;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43 public class ArrayConverter extends AbstractConfigurationConverter {
44 public boolean canConvert(Class type) {
45 return type.isArray();
46 }
47
48 public Object fromConfiguration(
49 ConverterLookup converterLookup,
50 PlexusConfiguration configuration,
51 Class type,
52 Class baseType,
53 ClassLoader classLoader,
54 ExpressionEvaluator expressionEvaluator,
55 ConfigurationListener listener)
56 throws ComponentConfigurationException {
57 Object retValue = fromExpression(configuration, expressionEvaluator, type);
58 if (retValue != null) {
59 return retValue;
60 }
61
62 List values = new ArrayList();
63
64 for (int i = 0; i < configuration.getChildCount(); i++) {
65 PlexusConfiguration childConfiguration = configuration.getChild(i);
66
67 String configEntry = childConfiguration.getName();
68
69 String name = fromXML(configEntry);
70
71 Class childType = getClassForImplementationHint(null, childConfiguration, classLoader);
72
73
74
75 if (childType == null && name.indexOf('.') > 0) {
76 try {
77 childType = classLoader.loadClass(name);
78 } catch (ClassNotFoundException e) {
79
80 }
81 }
82
83 if (childType == null) {
84
85
86
87 String baseTypeName = baseType.getName();
88
89 int lastDot = baseTypeName.lastIndexOf('.');
90
91 String className;
92
93 if (lastDot == -1) {
94 className = name;
95 } else {
96 String basePackage = baseTypeName.substring(0, lastDot);
97 className = basePackage + "." + StringUtils.capitalizeFirstLetter(name);
98 }
99
100 try {
101 childType = classLoader.loadClass(className);
102 } catch (ClassNotFoundException e) {
103
104 }
105 }
106
107
108
109 if (childType == null) {
110 childType = type.getComponentType();
111 }
112
113 ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);
114
115 Object object = converter.fromConfiguration(
116 converterLookup,
117 childConfiguration,
118 childType,
119 baseType,
120 classLoader,
121 expressionEvaluator,
122 listener);
123
124 values.add(object);
125 }
126
127 try {
128 return values.toArray((Object[]) Array.newInstance(type.getComponentType(), 0));
129 } catch (ArrayStoreException e) {
130 throw new ComponentConfigurationException("Cannot assign configuration values to array of type "
131 + type.getComponentType().getName() + ": " + values);
132 }
133 }
134 }