001: /*
002: * $Id: SimplePropertyConfiguration.java 10256 2008-01-08 15:20:25Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring.parsers.assembly.configuration;
012:
013: import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Properties;
021: import java.util.Set;
022:
023: import org.springframework.core.Conventions;
024: import org.springframework.util.StringUtils;
025:
026: /**
027: * A direct implementation of {@link PropertyConfiguration}
028: */
029: public class SimplePropertyConfiguration implements
030: PropertyConfiguration {
031:
032: private List references = new ArrayList();
033: private Properties nameMappings = new Properties();
034: private Map valueMappings = new HashMap();
035: private Set collections = new HashSet();
036: private Map ignored = new HashMap();
037: private boolean ignoreAll = false;
038:
039: public void addReference(String propertyName) {
040: references.add(dropRef(propertyName));
041: }
042:
043: public void addMapping(String propertyName, Map mappings) {
044: valueMappings.put(propertyName, new NamedValueMap(propertyName,
045: mappings));
046: }
047:
048: public void addMapping(String propertyName, String mappings) {
049: valueMappings.put(propertyName, new NamedValueMap(propertyName,
050: mappings));
051: }
052:
053: public void addMapping(String propertyName, ValueMap mappings) {
054: valueMappings.put(propertyName, new NamedValueMap(propertyName,
055: mappings));
056: }
057:
058: public void addAlias(String alias, String propertyName) {
059: nameMappings.put(alias, propertyName);
060: }
061:
062: public void addCollection(String propertyName) {
063: collections.add(dropRef(propertyName));
064: }
065:
066: public void addIgnored(String propertyName) {
067: ignored.put(dropRef(propertyName), Boolean.TRUE);
068: }
069:
070: public void removeIgnored(String propertyName) {
071: ignored.put(dropRef(propertyName), Boolean.FALSE);
072: }
073:
074: public void setIgnoredDefault(boolean ignoreAll) {
075: this .ignoreAll = ignoreAll;
076: }
077:
078: public String getAttributeMapping(String alias) {
079: return getAttributeMapping(alias, alias);
080: }
081:
082: public String getAttributeMapping(String alias, String deflt) {
083: return nameMappings.getProperty(alias, deflt);
084: }
085:
086: public boolean isCollection(String propertyName) {
087: return collections.contains(dropRef(propertyName));
088: }
089:
090: public boolean isIgnored(String propertyName) {
091: String name = dropRef(propertyName);
092: if (ignored.containsKey(name)) {
093: return ((Boolean) ignored.get(name)).booleanValue();
094: } else {
095: return ignoreAll;
096: }
097: }
098:
099: /**
100: * A property can be explicitly registered as a bean reference via registerBeanReference()
101: * or it can simply use the "-ref" suffix.
102: * @param attributeName true if the name appears to correspond to a reference
103: */
104: public boolean isReference(String attributeName) {
105: return (references.contains(dropRef(attributeName))
106: || attributeName
107: .endsWith(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF_SUFFIX)
108: || attributeName
109: .endsWith(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REFS_SUFFIX) || attributeName
110: .equals(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF));
111: }
112:
113: public SingleProperty getSingleProperty(String name) {
114: return new SinglePropertyWrapper(name, this );
115: }
116:
117: /**
118: * Extract a JavaBean property name from the supplied attribute name.
119: * <p>The default implementation uses the {@link org.springframework.core.Conventions#attributeNameToPropertyName(String)}
120: * method to perform the extraction.
121: * <p>The name returned must obey the standard JavaBean property name
122: * conventions. For example for a class with a setter method
123: * '<code>setBingoHallFavourite(String)</code>', the name returned had
124: * better be '<code>bingoHallFavourite</code>' (with that exact casing).
125: *
126: * @param oldName the attribute name taken straight from the XML element being parsed; will never be <code>null</code>
127: * @return the extracted JavaBean property name; must never be <code>null</code>
128: */
129: public String translateName(String oldName) {
130: // Remove the bean reference suffix if any.
131: String name = dropRef(oldName);
132: // Map to the real property name if necessary.
133: name = getAttributeMapping(name);
134: // JavaBeans property convention.
135: name = Conventions.attributeNameToPropertyName(name);
136: if (!StringUtils.hasText(name)) {
137: throw new IllegalStateException(
138: "Illegal property name for " + oldName
139: + ": cannot be null or empty.");
140: }
141: return name;
142: }
143:
144: protected String dropRef(String name) {
145: return org.mule.util.StringUtils
146: .chomp(
147: org.mule.util.StringUtils
148: .chomp(
149: name,
150: AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF_SUFFIX),
151: AbstractMuleBeanDefinitionParser.ATTRIBUTE_REFS_SUFFIX);
152: }
153:
154: public Object translateValue(String name, String value) {
155: NamedValueMap vm = (NamedValueMap) valueMappings.get(name);
156: if (vm != null) {
157: return vm.getValue(value);
158: } else {
159: return value;
160: }
161: }
162:
163: public static class NamedValueMap {
164: private String propertyName;
165: private ValueMap valueMap;
166:
167: public NamedValueMap(String propertyName, String mappingsString) {
168: this .propertyName = propertyName;
169: valueMap = new MapValueMap(mappingsString);
170: }
171:
172: public NamedValueMap(String propertyName, Map valueMap) {
173: this .propertyName = propertyName;
174: this .valueMap = new MapValueMap(valueMap);
175: }
176:
177: public NamedValueMap(String propertyName, ValueMap valueMap) {
178: this .propertyName = propertyName;
179: this .valueMap = valueMap;
180: }
181:
182: public String getPropertyName() {
183: return propertyName;
184: }
185:
186: public Object getValue(String key) {
187: return valueMap.rewrite(key);
188: }
189: }
190:
191: public static class MapValueMap implements ValueMap {
192:
193: private Map map;
194:
195: public MapValueMap(Map map) {
196: this .map = map;
197: }
198:
199: public MapValueMap(String definition) {
200: map = new HashMap();
201:
202: String[] values = StringUtils.tokenizeToStringArray(
203: definition, ",");
204: for (int i = 0; i < values.length; i++) {
205: String value = values[i];
206: int x = value.indexOf("=");
207: if (x == -1) {
208: throw new IllegalArgumentException(
209: "Mappings string not properly defined: "
210: + definition);
211: }
212: map.put(value.substring(0, x), value.substring(x + 1));
213: }
214:
215: }
216:
217: public Object rewrite(String value) {
218: Object result = map.get(value);
219: if (null == result) {
220: return value;
221: } else {
222: return result.toString();
223: }
224: }
225:
226: }
227:
228: }
|