View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.localization.context;
14  
15  import java.util.Enumeration;
16  import java.util.Locale;
17  import java.util.MissingResourceException;
18  import java.util.ResourceBundle;
19  
20  import net.sf.oval.Validator;
21  import net.sf.oval.context.ClassContext;
22  import net.sf.oval.context.ConstructorParameterContext;
23  import net.sf.oval.context.FieldContext;
24  import net.sf.oval.context.MethodEntryContext;
25  import net.sf.oval.context.MethodExitContext;
26  import net.sf.oval.context.MethodParameterContext;
27  import net.sf.oval.context.MethodReturnValueContext;
28  import net.sf.oval.context.OValContext;
29  import net.sf.oval.internal.Log;
30  
31  /**
32   * This renderer searches for a resource file that is in the same package and has the same name as the validated class.
33   * It then tries to lookup a localized version of the validation context, e.g.<br>
34   * <b>com.acme.model.Person.java<br>
35   * com.acme.model.Person.properties<br>
36   * com.acme.model.Person_de.properties<br>
37   * com.acme.model.Person_fr.properties</b>
38   *
39   * <p>
40   * The properties file is expected to have values following this scheme
41   * <pre>
42   * label.class=My translated name of the class name
43   * label.field.firstname=My translated name of the field "firstname"
44   * label.field.lastname=My translated name of the field "lastname"
45   * label.parameter.amount=My translated name of a constructor/method parameter "amount"
46   * label.method.increase=My translated name of the method "increase"
47   * </pre>
48   * @author Sebastian Thomschke
49   */
50  public class ResourceBundleValidationContextRenderer implements OValContextRenderer
51  {
52  	private static final Log LOG = Log.getLog(ResourceBundleValidationContextRenderer.class);
53  
54  	public static final ResourceBundleValidationContextRenderer INSTANCE = new ResourceBundleValidationContextRenderer();
55  
56  	private static boolean containsKey(final ResourceBundle bundle, final String key)
57  	{
58  		for (final Enumeration<String> en = bundle.getKeys(); en.hasMoreElements();)
59  			if (en.nextElement().equals(key)) return true;
60  		return false;
61  	}
62  
63  	protected Locale getLocale()
64  	{
65  		return Validator.getLocaleProvider().getLocale();
66  	}
67  
68  	public String render(final OValContext ovalContext)
69  	{
70  		final String baseName;
71  		final String key;
72  		if (ovalContext instanceof ClassContext)
73  		{
74  			final ClassContext ctx = (ClassContext) ovalContext;
75  			baseName = ctx.getClazz().getName();
76  			key = "label.class";
77  		}
78  		else if (ovalContext instanceof FieldContext)
79  		{
80  			final FieldContext ctx = (FieldContext) ovalContext;
81  			baseName = ctx.getField().getDeclaringClass().getName();
82  			final String fieldName = ctx.getField().getName();
83  			key = "label.field." + fieldName;
84  		}
85  		else if (ovalContext instanceof ConstructorParameterContext)
86  		{
87  			final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext;
88  			baseName = ctx.getConstructor().getDeclaringClass().getName();
89  			key = "label.parameter." + ctx.getParameterName();
90  		}
91  		else if (ovalContext instanceof MethodParameterContext)
92  		{
93  			final MethodParameterContext ctx = (MethodParameterContext) ovalContext;
94  			baseName = ctx.getMethod().getDeclaringClass().getName();
95  			key = "label.parameter." + ctx.getParameterName();
96  		}
97  		else if (ovalContext instanceof MethodEntryContext)
98  		{
99  			final MethodEntryContext ctx = (MethodEntryContext) ovalContext;
100 			baseName = ctx.getMethod().getDeclaringClass().getName();
101 			key = "label.method." + ctx.getMethod().getName();
102 		}
103 		else if (ovalContext instanceof MethodExitContext)
104 		{
105 			final MethodExitContext ctx = (MethodExitContext) ovalContext;
106 			baseName = ctx.getMethod().getDeclaringClass().getName();
107 			key = "label.method." + ctx.getMethod().getName();
108 		}
109 		else if (ovalContext instanceof MethodReturnValueContext)
110 		{
111 			final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext;
112 			baseName = ctx.getMethod().getDeclaringClass().getName();
113 			key = "label.method." + ctx.getMethod().getName();
114 		}
115 		else
116 			return ovalContext.toString();
117 
118 		try
119 		{
120 			final ResourceBundle bundle = ResourceBundle.getBundle(baseName, getLocale());
121 			if (containsKey(bundle, key)) return bundle.getString(key);
122 			LOG.debug("Key {1} not found in bundle {2}", key, baseName);
123 		}
124 		catch (final MissingResourceException ex)
125 		{
126 			LOG.debug("Bundle {1} not found", baseName, ex);
127 		}
128 		return ovalContext.toString();
129 	}
130 }