1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.dbutils.wrappers; 18 19 import java.lang.reflect.InvocationHandler; 20 import java.lang.reflect.Method; 21 import java.sql.ResultSet; 22 23 import org.apache.commons.dbutils.ProxyFactory; 24 25 /** 26 * Wraps a {@code ResultSet} to trim strings returned by the 27 * {@code getString()} and {@code getObject()} methods. 28 * 29 * <p> 30 * Usage Example: 31 * This example shows how to decorate ResultSets so processing continues as 32 * normal but all Strings are trimmed before being returned from the 33 * {@code ResultSet}. 34 * </p> 35 * 36 * <pre> 37 * ResultSet resultSet = // somehow get a ResultSet; 38 * 39 * // Substitute wrapped ResultSet with additional behavior for real ResultSet 40 * resultSet = StringTrimmedResultSet.wrap(resultSet); 41 * 42 * // Pass wrapped ResultSet to processor 43 * List list = new BasicRowProcessor().toBeanList(resultSet); 44 * </pre> 45 */ 46 public class StringTrimmedResultSet implements InvocationHandler { 47 48 /** 49 * Wraps the {@code ResultSet} in an instance of this class. This is 50 * equivalent to: 51 * <pre> 52 * ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet)); 53 * </pre> 54 * 55 * @param resultSet The {@code ResultSet} to wrap. 56 * @return wrapped ResultSet 57 */ 58 public static ResultSet wrap(final ResultSet resultSet) { 59 return ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet)); 60 } 61 62 /** 63 * The wrapped result. 64 */ 65 private final ResultSet resultSet; 66 67 /** 68 * Constructs a new instance of {@code StringTrimmedResultSet} 69 * to wrap the specified {@code ResultSet}. 70 * @param resultSet ResultSet to wrap 71 */ 72 public StringTrimmedResultSet(final ResultSet resultSet) { 73 this.resultSet = resultSet; 74 } 75 76 /** 77 * Intercept calls to the {@code getString()} and 78 * {@code getObject()} methods and trim any Strings before they're 79 * returned. 80 * 81 * @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[]) 82 * @param proxy Not used; all method calls go to the internal result set 83 * @param method The method to invoke on the result set 84 * @param args The arguments to pass to the result set 85 * @return string trimmed result 86 * @throws Throwable error 87 */ 88 @Override 89 public Object invoke(final Object proxy, final Method method, final Object[] args) 90 throws Throwable { 91 92 Object result = method.invoke(this.resultSet, args); 93 94 if (result instanceof String 95 && (method.getName().equals("getObject") 96 || method.getName().equals("getString"))) { 97 result = ((String) result).trim(); 98 } 99 100 return result; 101 } 102 103 }