1 /* 2 * This file is part of AceLogger. 3 * 4 * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU 5 * Lesser General Public License as published by the Free Software Foundation, either version 3 of 6 * the License, or (at your option) any later version. 7 * 8 * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * Lesser General Public License for more details. 11 * 12 * You should have received a copy of the GNU Lesser General Public License along with AceLogger. 13 * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. 14 */ 15 package net.sourceforge.acelogger.interpolator; 16 17 import java.util.MissingResourceException; 18 import java.util.ResourceBundle; 19 20 import net.sourceforge.acelogger.execution.LogController; 21 22 /** 23 * An implementation of {@link TextInterpolator} that first resolves a key in a 24 * {@link ResourceBundle} to a pattern than delegates the interpolation of this pattern to a 25 * different {@link TextInterpolator}. 26 * 27 * @author Zardi (https://sourceforge.net/users/daniel_zardi) 28 * @version 1.0.0 29 * @since 1.0.0 30 */ 31 public class MessageBundleInterpolator implements TextInterpolator { 32 33 /** 34 * The bundle used to resolved the keys used in log messages to patterns that will then be 35 * interpolated by a different {@link TextInterpolator}. 36 */ 37 private ResourceBundle resolver; 38 39 /** 40 * The interpolator used to format the log messages using the supplied parameters and the 41 * pattern resolved using the {@link ResourceBundle}. 42 */ 43 private TextInterpolator interpolator; 44 45 /** 46 * Creates a new MessageBundleInterpolator using the supplied {@link ResourceBundle} to resolve 47 * the patterns and then interpolation it with the supplied {@link TextInterpolator}. 48 * 49 * @param resolver 50 * The bundle used to resolve the patterns, using the keys in log messages. 51 * @param interpolator 52 * The interpolator used to produce the final message after resolving the pattern. 53 * @since 1.0.0 54 */ 55 public MessageBundleInterpolator(ResourceBundle resolver, TextInterpolator interpolator) { 56 this.resolver = resolver; 57 this.interpolator = interpolator; 58 } 59 60 /** 61 * Resolves the supplied key to a formatting pattern using a {@link ResourceBundle}, then the 62 * resolved pattern is interpolated using another {@link TextInterpolator}. 63 * 64 * @param bundleKey 65 * The string that will be used to locate the pattern in a {@link ResourceBundle}. 66 * @param params 67 * The parameters used to interpolate the message. 68 * @return A string containing the resolved pattern interpolated with the supplied parameters. 69 * @since 1.0.0 70 * @see TextInterpolator#interpolate(String, Object...) 71 */ 72 public String interpolate(String bundleKey, Object... params) { 73 String resolvedPattern = ""; 74 try { 75 resolvedPattern = resolver.getString(bundleKey); 76 } catch (NullPointerException e) { 77 LogController.getInternalLogger().error( 78 "The key {0} was resolved to a <null> value using bundle {1}", 79 e, bundleKey, resolver 80 ); 81 } catch (MissingResourceException e) { 82 LogController.getInternalLogger().error("The key {0} wasn't found using bundle {1}", 83 e, bundleKey, resolver 84 ); 85 } catch (ClassCastException e) { 86 LogController.getInternalLogger().error( 87 "The key {0} was resolved to an object other than a String using bundle {1}", 88 e, bundleKey, resolver 89 ); 90 } 91 return interpolator.interpolate(resolvedPattern, params); 92 } 93 94 }