View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI 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. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support;
14  
15  import java.text.MessageFormat;
16  import java.util.HashMap;
17  import java.util.Map;
18  import java.util.MissingResourceException;
19  import java.util.ResourceBundle;
20  
21  public final class MessageSupport
22  {
23  	private static final Map<String, ResourceBundle> bundles = new HashMap<String, ResourceBundle>();
24  	private final Class<? extends Object> clazz;
25  
26  	public MessageSupport( Class<? extends Object> clazz )
27  	{
28  		this.clazz = clazz;
29  	}
30  
31  	public static String get( Class<? extends Object> clazz, String key, Object... args )
32  	{
33  		String result = get( clazz, key );
34  		return MessageFormat.format( result, args );
35  	}
36  
37  	public static String get( Class<? extends Object> clazz, String key )
38  	{
39  		ResourceBundle bundle = null;
40  
41  		try
42  		{
43  			bundle = getResourceBundleForClass( clazz );
44  
45  			if( bundle == null )
46  				return key;
47  
48  			String name = clazz.isMemberClass() ? clazz.getEnclosingClass().getSimpleName() : clazz.getSimpleName();
49  			return bundle.getString( name + '.' + key );
50  		}
51  		catch( MissingResourceException e )
52  		{
53  			try
54  			{
55  				return bundle.getString( key );
56  			}
57  			catch( MissingResourceException e1 )
58  			{
59  				return key;
60  			}
61  		}
62  	}
63  
64  	public static String[] getArray( Class<? extends Object> clazz, String key )
65  	{
66  		ResourceBundle bundle = null;
67  
68  		try
69  		{
70  			bundle = getResourceBundleForClass( clazz );
71  
72  			if( bundle == null )
73  				return new String[] { key };
74  
75  			String name = clazz.isMemberClass() ? clazz.getEnclosingClass().getSimpleName() : clazz.getSimpleName();
76  
77  			return bundle.getStringArray( name + '.' + key );
78  		}
79  		catch( MissingResourceException e )
80  		{
81  			try
82  			{
83  				return bundle.getStringArray( key );
84  			}
85  			catch( MissingResourceException e1 )
86  			{
87  				if( clazz.isMemberClass() )
88  					return getArray( clazz.getEnclosingClass(), key );
89  				else
90  					return new String[] { key };
91  			}
92  		}
93  	}
94  
95  	private static ResourceBundle getResourceBundleForClass( Class<? extends Object> clazz )
96  	{
97  		String packageName = clazz.getPackage().getName();
98  
99  		if>( !bundles.containsKey( packageName ) )
100 		{
101 			try
102 			{
103 				bundles.put( packageName, ResourceBundle.getBundle( packageName + ".messages" ) );
104 			}
105 			catch( MissingResourceException e )
106 			{
107 				try
108 				{
109 					bundles.put( packageName, ResourceBundle.getBundle( packageName + ".Bundle" ) );
110 				}
111 				catch( MissingResourceException e2 )
112 				{
113 				}
114 			}
115 		}
116 
117 		returng> bundles.get( packageName );
118 	}
119 
120 	public static MessageSupport getMessages( Class<? extends Object> name )
121 	{
122 		return new MessageSupport( name );
123 	}
124 
125 	public String get( String key )
126 	{
127 		return MessageSupport.get( clazz, key );
128 	}
129 
130 	public String get( String key, Object... args )
131 	{
132 		return MessageSupport.get( clazz, key, args );
133 	}
134 
135 	public String[] getArray( String key )
136 	{
137 		return MessageSupport.getArray( clazz, key );
138 	}
139 
140 	public boolean contains( String key )
141 	{
142 		ResourceBundle bundle = getResourceBundleForClass( clazz );
143 		if( bundle == null )
144 			return false;
145 
146 		try
147 		{
148 			return bundle.getString( key ) != null;
149 		}
150 		catch( MissingResourceException e )
151 		{
152 			return false;
153 		}
154 	}
155 
156 	public String[] getArray( String[] strings )
157 	{
158 		if( strings == null || strings.length == 0 )
159 			return strings;
160 
161 		String[] array = getArray( strings[0] );
162 		if( array[0].equals( strings[0] ) )
163 			return strings;
164 
165 		return array;
166 	}
167 }