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 rs = // somehow get a ResultSet;
038 *
039 * // Substitute wrapped ResultSet with additional behavior for real ResultSet
040 * rs = StringTrimmedResultSet.wrap(rs);
041 *
042 * // Pass wrapped ResultSet to processor
043 * List list = new BasicRowProcessor().toBeanList(rs);
044 * </pre>
045 */
046public class StringTrimmedResultSet implements InvocationHandler {
047
048    /**
049     * The factory to create proxies with.
050     */
051    private static final ProxyFactory factory = ProxyFactory.instance();
052
053    /**
054     * Wraps the {@code ResultSet} in an instance of this class.  This is
055     * equivalent to:
056     * <pre>
057     * ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(rs));
058     * </pre>
059     *
060     * @param rs The {@code ResultSet} to wrap.
061     * @return wrapped ResultSet
062     */
063    public static ResultSet wrap(final ResultSet rs) {
064        return factory.createResultSet(new StringTrimmedResultSet(rs));
065    }
066
067    /**
068     * The wrapped result.
069     */
070    private final ResultSet rs;
071
072    /**
073     * Constructs a new instance of {@code StringTrimmedResultSet}
074     * to wrap the specified {@code ResultSet}.
075     * @param rs ResultSet to wrap
076     */
077    public StringTrimmedResultSet(final ResultSet rs) {
078        super();
079        this.rs = rs;
080    }
081
082    /**
083     * Intercept calls to the {@code getString()} and
084     * {@code getObject()} methods and trim any Strings before they're
085     * returned.
086     *
087     * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
088     * @param proxy Not used; all method calls go to the internal result set
089     * @param method The method to invoke on the result set
090     * @param args The arguments to pass to the result set
091     * @return string trimmed result
092     * @throws Throwable error
093     */
094    @Override
095    public Object invoke(final Object proxy, final Method method, final Object[] args)
096        throws Throwable {
097
098        Object result = method.invoke(this.rs, args);
099
100        if (result instanceof String
101                && (method.getName().equals("getObject")
102                || method.getName().equals("getString"))) {
103            result = ((String) result).trim();
104        }
105
106        return result;
107    }
108
109}