View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  
38  	public static String get( Class<? extends Object> clazz, String key )
39  	{
40  		ResourceBundle bundle = null;
41  		
42  		try
43  		{
44  			bundle = getResourceBundleForClass( clazz );
45  			
46  			if( bundle == null )
47  				return key;
48  			
49  			String name = clazz.isMemberClass() ? clazz.getEnclosingClass().getSimpleName() : clazz.getSimpleName();
50  			return bundle.getString( name + '.' + key );
51  		}
52  		catch( MissingResourceException e )
53  		{
54  			try
55  			{
56  				return bundle.getString( key );
57  			}
58  			catch( MissingResourceException e1 )
59  			{
60  				return key;
61  			}
62  		}
63  	}
64  
65  	public static String [] getArray( Class<? extends Object> clazz, String key )
66  	{
67  		ResourceBundle bundle = null;
68  		
69  		try
70  		{
71  			bundle = getResourceBundleForClass( clazz );
72  			
73  			if( bundle == null )
74  				return new String[] {key};
75  			
76  			String name = clazz.isMemberClass() ? clazz.getEnclosingClass().getSimpleName() : clazz.getSimpleName();
77  			
78  			return bundle.getStringArray( name + '.' + key );
79  		}
80  		catch( MissingResourceException e )
81  		{
82  			try
83  			{
84  				return bundle.getStringArray( key );
85  			}
86  			catch( MissingResourceException e1 )
87  			{
88  				if( clazz.isMemberClass() )
89  					return getArray( clazz.getEnclosingClass(), key );
90  				else
91  					return new String[] {key};
92  			}
93  		}
94  	}
95  
96  	
97  	private static ResourceBundle getResourceBundleForClass( Class<? extends Object> clazz )
98  	{
99  		String packageName = clazz.getPackage().getName();
100 		
101 		if>( !bundles.containsKey( packageName ))
102 		{
103 			try
104 			{
105 				bundles.put( packageName, ResourceBundle.getBundle( packageName + ".messages" ) );
106 			}
107 			catch( MissingResourceException e )
108 			{
109 				try
110 				{
111 					bundles.put( packageName, ResourceBundle.getBundle( packageName + ".Bundle" ) );
112 				}
113 				catch( MissingResourceException e2 )
114 				{
115 				}
116 			}
117 		}
118 		
119 		returng> bundles.get( packageName );
120 	}
121 
122 	public static MessageSupport getMessages( Class<? extends Object> name )
123 	{
124 		return new MessageSupport( name );
125 	}
126 
127 	public String get( String key )
128 	{
129 		return MessageSupport.get( clazz, key );
130 	}
131 	
132 	public String get( String key, Object ... args )
133 	{
134 		return MessageSupport.get( clazz, key, args );
135 	}
136 
137 	public String[] getArray( String key )
138 	{
139 		return MessageSupport.getArray( clazz, key );
140 	}
141 
142 	public boolean contains( String key )
143 	{
144 		ResourceBundle bundle = getResourceBundleForClass( clazz );
145 		if( bundle == null )
146 			return false;
147 		
148 		try
149 		{
150 			return bundle.getString( key ) != null;
151 		}
152 		catch( MissingResourceException e )
153 		{
154 			return false;
155 		}
156 	}
157 
158 	public String[] getArray( String[] strings )
159 	{
160 		if( strings == null || strings.length == 0 )
161 			return strings;
162 		
163 		String[] array = getArray( strings[0] );
164 		if( array[0].equals( strings[0] ))
165 			return strings;
166 		
167 		return array;
168 	}
169 }