001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.binding.convert.support;
017:
018: import org.springframework.binding.convert.ConversionExecutor;
019: import org.springframework.binding.convert.ConversionService;
020: import org.springframework.binding.expression.Expression;
021:
022: /**
023: * Base class for converters that use other converters to convert things, thus
024: * they are conversion-service aware.
025: *
026: * @author Keith Donald
027: */
028: public abstract class ConversionServiceAwareConverter extends
029: AbstractConverter implements ConversionServiceAware {
030:
031: /**
032: * The conversion service this converter is aware of.
033: */
034: private ConversionService conversionService;
035:
036: /**
037: * Default constructor, expectes to conversion service to be injected
038: * using {@link #setConversionService(ConversionService)}.
039: */
040: protected ConversionServiceAwareConverter() {
041: }
042:
043: /**
044: * Create a converter using given conversion service.
045: */
046: protected ConversionServiceAwareConverter(
047: ConversionService conversionService) {
048: setConversionService(conversionService);
049: }
050:
051: /**
052: * Returns the conversion service used.
053: */
054: public ConversionService getConversionService() {
055: if (conversionService == null) {
056: throw new IllegalStateException(
057: "Conversion service not yet set: set it first before calling this method");
058: }
059: return conversionService;
060: }
061:
062: public void setConversionService(ConversionService conversionService) {
063: this .conversionService = conversionService;
064: }
065:
066: /**
067: * Returns a conversion executor capable of converting string objects to the
068: * specified target class.
069: * @param targetClass the target class
070: * @return the conversion executor, never null
071: */
072: protected ConversionExecutor fromStringTo(Class targetClass) {
073: return getConversionService().getConversionExecutor(
074: String.class, targetClass);
075: }
076:
077: /**
078: * Returns a conversion executor capable of converting string objects to the
079: * target class aliased by the provided alias.
080: * @param targetAlias the target class alias, e.g "long" or "float"
081: * @return the conversion executor, or <code>null</code> if no suitable
082: * converter exists for alias
083: */
084: protected ConversionExecutor fromStringToAliased(String targetAlias) {
085: return getConversionService()
086: .getConversionExecutorByTargetAlias(String.class,
087: targetAlias);
088: }
089:
090: /**
091: * Returns a conversion executor capable of converting objects from one
092: * class to another.
093: * @param sourceClass the source class to convert from
094: * @param targetClass the target class to convert to
095: * @return the conversion executor, never null
096: */
097: protected ConversionExecutor converterFor(Class sourceClass,
098: Class targetClass) {
099: return getConversionService().getConversionExecutor(
100: sourceClass, targetClass);
101: }
102:
103: /**
104: * Helper that parsers the given expression string into an expression, using
105: * the installed String->Expression converter.
106: * @param expressionString the expression string to parse
107: * @return the parsed, evaluatable expression
108: */
109: protected Expression parseExpression(String expressionString) {
110: return (Expression) fromStringTo(Expression.class).execute(
111: expressionString);
112: }
113: }
|