View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.oval.validation;
17  
18  import java.lang.reflect.Method;
19  import java.util.ResourceBundle;
20  
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.localization.context.OValContextRenderer;
30  
31  import org.seasar.cubby.util.Messages;
32  
33  /**
34   * 現在の実行スレッドに関連付けられた要求に対応するメッセージ用の {@link ResourceBundle} からメッセージを翻訳する
35   * {@link OValContextRenderer} です。
36   * 
37   * @author baba
38   */
39  public class RequestLocaleOValContextRenderer implements OValContextRenderer {
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	public String render(final OValContext ovalContext) {
45  		final String fieldName;
46  		if (ovalContext instanceof ClassContext) {
47  			final ClassContext ctx = (ClassContext) ovalContext;
48  			fieldName = ctx.getClazz().getSimpleName();
49  		} else if (ovalContext instanceof FieldContext) {
50  			final FieldContext ctx = (FieldContext) ovalContext;
51  			fieldName = ctx.getField().getName();
52  		} else if (ovalContext instanceof ConstructorParameterContext) {
53  			final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext;
54  			fieldName = ctx.getParameterName();
55  		} else if (ovalContext instanceof MethodParameterContext) {
56  			final MethodParameterContext ctx = (MethodParameterContext) ovalContext;
57  			fieldName = ctx.getParameterName();
58  		} else if (ovalContext instanceof MethodEntryContext) {
59  			final MethodEntryContext ctx = (MethodEntryContext) ovalContext;
60  			final Method method = ctx.getMethod();
61  			if (isGetter(method)) {
62  				fieldName = toPropertyName(method);
63  			} else {
64  				fieldName = method.getName();
65  			}
66  		} else if (ovalContext instanceof MethodExitContext) {
67  			final MethodExitContext ctx = (MethodExitContext) ovalContext;
68  			final Method method = ctx.getMethod();
69  			if (isGetter(method)) {
70  				fieldName = toPropertyName(method);
71  			} else {
72  				fieldName = method.getName();
73  			}
74  		} else if (ovalContext instanceof MethodReturnValueContext) {
75  			final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext;
76  			final Method method = ctx.getMethod();
77  			if (isGetter(method)) {
78  				fieldName = toPropertyName(method);
79  			} else {
80  				fieldName = method.getName();
81  			}
82  		} else {
83  			return ovalContext.toString();
84  		}
85  
86  		final String fieldNameKey;
87  
88  		final OValValidationContext context = OValValidationContext.get();
89  		final String resourceKeyPrefix = context.getResourceKeyPrefix();
90  		if (resourceKeyPrefix == null) {
91  			fieldNameKey = fieldName;
92  		} else {
93  			fieldNameKey = resourceKeyPrefix + fieldName;
94  		}
95  
96  		final String fieldNameMessage = Messages.getText(fieldNameKey);
97  		return fieldNameMessage;
98  	}
99  
100 	/**
101 	 * 指定されたメソッドが getter メソッドかどうかを示します。
102 	 * 
103 	 * @param method
104 	 *            メソッド
105 	 * @return getter メソッドの場合は <code>true</code>、そうでない場合は <code>false</code>
106 	 */
107 	protected boolean isGetter(final Method method) {
108 		if (method.getReturnType().equals(boolean.class)) {
109 			if (!method.getName().startsWith("is")) {
110 				return false;
111 			}
112 		} else {
113 			if (!method.getName().startsWith("get")) {
114 				return false;
115 			}
116 		}
117 		if (method.getParameterTypes().length > 0) {
118 			return false;
119 		}
120 		if (method.getReturnType().equals(void.class)) {
121 			return false;
122 		}
123 		return true;
124 	}
125 
126 	/**
127 	 * 指定された getter メソッドがアクセスするプロパティ名を取得します。
128 	 * 
129 	 * @param method
130 	 *            メソッド
131 	 * @return プロパティ名
132 	 */
133 	protected String toPropertyName(final Method method) {
134 		final String methodName = method.getName();
135 		final String str1;
136 		if (method.getReturnType().equals(boolean.class)) {
137 			str1 = methodName.substring(2);
138 		} else {
139 			str1 = methodName.substring(3);
140 		}
141 		final String propertyName = Character.toUpperCase(str1.charAt(0))
142 				+ str1.substring(1);
143 		return propertyName;
144 	}
145 
146 }