package votorola.g.locale; // Copyright 2008-2009, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.util.*; import votorola.g.lang.*; /** A composite resource bundle and string formatter. */ public @ThreadRestricted class BundleFormatter { /** Constructs a BundleFormatter. * * @param bundle the bundle, per {@linkplain #bundle() bundle}() */ public BundleFormatter( final ResourceBundle bundle ) { this.bundle = bundle; formatter = new Formatter( new StringBuilder( /*initial capacity, guess*/250 ), bundle.getLocale() ); } // ------------------------------------------------------------------------------------ /** The local resource bundle. */ public final ResourceBundle bundle() { return bundle; } protected final ResourceBundle bundle; /** Returns a printf-style formatted string, generated using * Formatter.{@linkplain Formatter#format(String,Object[]) format}(format,args). */ public final String format( final String format, final Object... args ) { final StringBuilder sB = stringBuilder(); final int length0 = sB.length(); try { formatter.format( format, args ); return sB.substring( length0, sB.length() ); } finally { sB.delete( length0, sB.length() ); // thus leaving the buffer unaffected } } /** Returns a localized string. * * @param key {@linkplain #bundle() bundle} key of the string * @param args arguments for insertion in the string, * per {@linkplain Formatter#format(String,Object[]) format}(string,args) */ public final String l( final String key, final Object... args ) { final StringBuilder sB = stringBuilder(); final int length0 = sB.length(); try { lappend( key, args ); return sB.substring( length0, sB.length() ); } finally { sB.delete( length0, sB.length() ); // thus leaving the buffer unaffected } } /** The locale for this bundle formatter. */ public final Locale locale() { return formatter.locale(); } /** For properties that are keyed by Java classname (typical case), this is the name * prefix that is assumed, by convention. In other words, property 'foo' in class * votorola.a.Bar is usually keyed as 'a.Bar.foo' (not as 'votorola.a.Bar.foo'). */ public static final String ASSUMED_PACKAGE_PREFIX = "votorola."; // ==================================================================================== /** A provider of a general (G) bundle formatter. */ public interface GProvider { // - G - P r o v i d e r ---------------------------------------------------------- /** The general (G) bundle formatter. It uses bundle base name * 'votorola.g.locale.G'. * * @see * locale/G.properties */ public BundleFormatter bunG(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** The formatter for appending printf-style format strings. */ protected final Formatter formatter; /** Appends a localized string to the buffer. * * @param key {@linkplain #bundle() bundle} key of the string * @param args arguments for insertion in the string, * per {@linkplain Formatter#format(String,Object[]) format}(string,args) */ protected BundleFormatter lappend( final String key, final Object... args ) { final String string = bundle.getString( key ); if( args.length > 0 ) formatter.format( string, args ); else stringBuilder().append( string ); return BundleFormatter.this; } /** The character buffer. */ protected final StringBuilder stringBuilder() { return (StringBuilder)formatter.out(); } }