001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbutils.wrappers;
018
019import java.lang.reflect.InvocationHandler;
020import java.lang.reflect.Method;
021import java.sql.ResultSet;
022
023import org.apache.commons.dbutils.ProxyFactory;
024
025/**
026 * Wraps a {@code ResultSet} to trim strings returned by the
027 * {@code getString()} and {@code getObject()} methods.
028 *
029 * <p>
030 * Usage Example:
031 * This example shows how to decorate ResultSets so processing continues as
032 * normal but all Strings are trimmed before being returned from the
033 * {@code ResultSet}.
034 * </p>
035 *
036 * <pre>
037 * ResultSet resultSet = // somehow get a ResultSet;
038 *
039 * // Substitute wrapped ResultSet with additional behavior for real ResultSet
040 * resultSet = StringTrimmedResultSet.wrap(resultSet);
041 *
042 * // Pass wrapped ResultSet to processor
043 * List list = new BasicRowProcessor().toBeanList(resultSet);
044 * </pre>
045 */
046public class StringTrimmedResultSet implements InvocationHandler {
047
048    /**
049     * Wraps the {@code ResultSet} in an instance of this class.  This is
050     * equivalent to:
051     * <pre>
052     * ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet));
053     * </pre>
054     *
055     * @param resultSet The {@code ResultSet} to wrap.
056     * @return wrapped ResultSet
057     */
058    public static ResultSet wrap(final ResultSet resultSet) {
059        return ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet));
060    }
061
062    /**
063     * The wrapped result.
064     */
065    private final ResultSet resultSet;
066
067    /**
068     * Constructs a new instance of {@code StringTrimmedResultSet}
069     * to wrap the specified {@code ResultSet}.
070     * @param resultSet ResultSet to wrap
071     */
072    public StringTrimmedResultSet(final ResultSet resultSet) {
073        this.resultSet = resultSet;
074    }
075
076    /**
077     * Intercept calls to the {@code getString()} and
078     * {@code getObject()} methods and trim any Strings before they're
079     * returned.
080     *
081     * @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])
082     * @param proxy Not used; all method calls go to the internal result set
083     * @param method The method to invoke on the result set
084     * @param args The arguments to pass to the result set
085     * @return string trimmed result
086     * @throws Throwable error
087     */
088    @Override
089    public Object invoke(final Object proxy, final Method method, final Object[] args)
090        throws Throwable {
091
092        Object result = method.invoke(this.resultSet, args);
093
094        if (result instanceof String
095                && (method.getName().equals("getObject")
096                || method.getName().equals("getString"))) {
097            result = ((String) result).trim();
098        }
099
100        return result;
101    }
102
103}