1 package org.codehaus.plexus.component.configurator.converters.basic;
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 org.codehaus.plexus.component.configurator.ComponentConfigurationException;
28 import org.codehaus.plexus.component.configurator.ConfigurationListener;
29 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
30 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
31 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
32 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
34 import org.codehaus.plexus.configuration.PlexusConfiguration;
35
36
37
38 public abstract class AbstractBasicConverter extends AbstractConfigurationConverter {
39 protected abstract Object fromString(String str) throws ComponentConfigurationException;
40
41 protected Object fromExpression(
42 PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, Class type)
43 throws ComponentConfigurationException {
44 Object v = null;
45
46 String value = configuration.getValue(null);
47
48 if (value != null && value.length() > 0) {
49
50
51
52 try {
53 if (expressionEvaluator instanceof TypeAwareExpressionEvaluator) {
54 v = ((TypeAwareExpressionEvaluator) expressionEvaluator).evaluate(value, type);
55 } else {
56 v = expressionEvaluator.evaluate(value);
57 }
58 } catch (ExpressionEvaluationException e) {
59 String msg = "Error evaluating the expression '" + value + "' for configuration value '"
60 + configuration.getName() + "'";
61 throw new ComponentConfigurationException(configuration, msg, e);
62 }
63 }
64
65 if (v == null) {
66 value = configuration.getAttribute("default-value", null);
67
68 if (value != null && value.length() > 0) {
69 try {
70 if (expressionEvaluator instanceof TypeAwareExpressionEvaluator) {
71 v = ((TypeAwareExpressionEvaluator) expressionEvaluator).evaluate(value, type);
72 } else {
73 v = expressionEvaluator.evaluate(value);
74 }
75 } catch (ExpressionEvaluationException e) {
76 String msg = "Error evaluating the expression '" + value + "' for configuration value '"
77 + configuration.getName() + "'";
78 throw new ComponentConfigurationException(configuration, msg, e);
79 }
80 }
81 }
82
83
84
85
86
87
88 return v;
89 }
90
91 public Object fromConfiguration(
92 ConverterLookup converterLookup,
93 PlexusConfiguration configuration,
94 Class type,
95 Class baseType,
96 ClassLoader classLoader,
97 ExpressionEvaluator expressionEvaluator,
98 ConfigurationListener listener)
99 throws ComponentConfigurationException {
100 if (configuration.getChildCount() > 0) {
101 throw new ComponentConfigurationException("When configuring a basic element the configuration cannot "
102 + "contain any child elements. " + "Configuration element '" + configuration.getName() + "'.");
103 }
104
105 Object retValue = fromExpression(configuration, expressionEvaluator, type);
106
107 if (retValue instanceof String) {
108 try {
109 retValue = fromString((String) retValue);
110 } catch (ComponentConfigurationException e) {
111 if (e.getFailedConfiguration() == null) {
112 e.setFailedConfiguration(configuration);
113 }
114
115 throw e;
116 }
117 }
118
119 return retValue;
120 }
121 }