001package 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. 002 003import java.util.*; 004import votorola.g.lang.*; 005 006 007/** A composite resource bundle and string formatter. 008 */ 009public @ThreadRestricted class BundleFormatter 010{ 011 012 /** Constructs a BundleFormatter. 013 * 014 * @param bundle the bundle, per {@linkplain #bundle() bundle}() 015 */ 016 public BundleFormatter( final ResourceBundle bundle ) 017 { 018 this.bundle = bundle; 019 formatter = new Formatter( new StringBuilder( /*initial capacity, guess*/250 ), 020 bundle.getLocale() ); 021 } 022 023 024 025 // ------------------------------------------------------------------------------------ 026 027 028 /** The local resource bundle. 029 */ 030 public final ResourceBundle bundle() { return bundle; } 031 032 033 protected final ResourceBundle bundle; 034 035 036 037 /** Returns a printf-style formatted string, generated using 038 * Formatter.{@linkplain Formatter#format(String,Object[]) format}(format,args). 039 */ 040 public final String format( final String format, final Object... args ) 041 { 042 final StringBuilder sB = stringBuilder(); 043 final int length0 = sB.length(); 044 try 045 { 046 formatter.format( format, args ); 047 return sB.substring( length0, sB.length() ); 048 } 049 finally 050 { 051 sB.delete( length0, sB.length() ); // thus leaving the buffer unaffected 052 } 053 } 054 055 056 057 /** Returns a localized string. 058 * 059 * @param key {@linkplain #bundle() bundle} key of the string 060 * @param args arguments for insertion in the string, 061 * per {@linkplain Formatter#format(String,Object[]) format}(string,args) 062 */ 063 public final String l( final String key, final Object... args ) 064 { 065 final StringBuilder sB = stringBuilder(); 066 final int length0 = sB.length(); 067 try 068 { 069 lappend( key, args ); 070 return sB.substring( length0, sB.length() ); 071 } 072 finally 073 { 074 sB.delete( length0, sB.length() ); // thus leaving the buffer unaffected 075 } 076 } 077 078 079 080 /** The locale for this bundle formatter. 081 */ 082 public final Locale locale() { return formatter.locale(); } 083 084 085 086 /** For properties that are keyed by Java classname (typical case), this is the name 087 * prefix that is assumed, by convention. In other words, property 'foo' in class 088 * votorola.a.Bar is usually keyed as 'a.Bar.foo' (not as 'votorola.a.Bar.foo'). 089 */ 090 public static final String ASSUMED_PACKAGE_PREFIX = "votorola."; 091 092 093 094 // ==================================================================================== 095 096 097 /** A provider of a general (G) bundle formatter. 098 */ 099 public interface GProvider 100 { 101 102 103 // - G - P r o v i d e r ---------------------------------------------------------- 104 105 106 /** The general (G) bundle formatter. It uses bundle base name 107 * 'votorola.g.locale.G'. 108 * 109 * @see <a href='../../../../../g/locale/G.properties'> 110 * locale/G.properties</a> 111 */ 112 public BundleFormatter bunG(); 113 114 115 } 116 117 118 119//// P r i v a t e /////////////////////////////////////////////////////////////////////// 120 121 122 /** The formatter for appending printf-style format strings. 123 */ 124 protected final Formatter formatter; 125 126 127 128 /** Appends a localized string to the buffer. 129 * 130 * @param key {@linkplain #bundle() bundle} key of the string 131 * @param args arguments for insertion in the string, 132 * per {@linkplain Formatter#format(String,Object[]) format}(string,args) 133 */ 134 protected BundleFormatter lappend( final String key, final Object... args ) 135 { 136 final String string = bundle.getString( key ); 137 if( args.length > 0 ) formatter.format( string, args ); 138 else stringBuilder().append( string ); 139 return BundleFormatter.this; 140 } 141 142 143 144 /** The character buffer. 145 */ 146 protected final StringBuilder stringBuilder() { return (StringBuilder)formatter.out(); } 147 148 149 150}