View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.plugins.oval.validation;
18  
19  import java.lang.reflect.Method;
20  import java.util.ResourceBundle;
21  
22  import net.sf.oval.context.ClassContext;
23  import net.sf.oval.context.ConstructorParameterContext;
24  import net.sf.oval.context.FieldContext;
25  import net.sf.oval.context.MethodEntryContext;
26  import net.sf.oval.context.MethodExitContext;
27  import net.sf.oval.context.MethodParameterContext;
28  import net.sf.oval.context.MethodReturnValueContext;
29  import net.sf.oval.context.OValContext;
30  import net.sf.oval.localization.context.OValContextRenderer;
31  
32  import org.seasar.cubby.util.Messages;
33  
34  /**
35   * 現在の実行スレッドに関連付けられた要求に対応するメッセージ用の {@link ResourceBundle} からメッセージを翻訳する
36   * {@link OValContextRenderer} です。
37   * 
38   * @author baba
39   */
40  public class RequestLocaleOValContextRenderer implements OValContextRenderer {
41  
42  	/**
43  	 * {@inheritDoc}
44  	 */
45  	public String render(final OValContext ovalContext) {
46  		final String fieldName;
47  		if (ovalContext instanceof ClassContext) {
48  			final ClassContext ctx = (ClassContext) ovalContext;
49  			fieldName = ctx.getClazz().getSimpleName();
50  		} else if (ovalContext instanceof FieldContext) {
51  			final FieldContext ctx = (FieldContext) ovalContext;
52  			fieldName = ctx.getField().getName();
53  		} else if (ovalContext instanceof ConstructorParameterContext) {
54  			final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext;
55  			fieldName = ctx.getParameterName();
56  		} else if (ovalContext instanceof MethodParameterContext) {
57  			final MethodParameterContext ctx = (MethodParameterContext) ovalContext;
58  			fieldName = ctx.getParameterName();
59  		} else if (ovalContext instanceof MethodEntryContext) {
60  			final MethodEntryContext ctx = (MethodEntryContext) ovalContext;
61  			final Method method = ctx.getMethod();
62  			if (isGetter(method)) {
63  				fieldName = toPropertyName(method);
64  			} else {
65  				fieldName = method.getName();
66  			}
67  		} else if (ovalContext instanceof MethodExitContext) {
68  			final MethodExitContext ctx = (MethodExitContext) ovalContext;
69  			final Method method = ctx.getMethod();
70  			if (isGetter(method)) {
71  				fieldName = toPropertyName(method);
72  			} else {
73  				fieldName = method.getName();
74  			}
75  		} else if (ovalContext instanceof MethodReturnValueContext) {
76  			final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext;
77  			final Method method = ctx.getMethod();
78  			if (isGetter(method)) {
79  				fieldName = toPropertyName(method);
80  			} else {
81  				fieldName = method.getName();
82  			}
83  		} else {
84  			return ovalContext.toString();
85  		}
86  
87  		final String fieldNameKey;
88  
89  		final OValValidationContext context = OValValidationContext.get();
90  		final String resourceKeyPrefix = context.getResourceKeyPrefix();
91  		if (resourceKeyPrefix == null) {
92  			fieldNameKey = fieldName;
93  		} else {
94  			fieldNameKey = resourceKeyPrefix + fieldName;
95  		}
96  
97  		final String fieldNameMessage = Messages.getText(fieldNameKey);
98  		return fieldNameMessage;
99  	}
100 
101 	/**
102 	 * 指定されたメソッドが getter メソッドかどうかを示します。
103 	 * 
104 	 * @param method
105 	 *            メソッド
106 	 * @return getter メソッドの場合は <code>true</code>、そうでない場合は <code>false</code>
107 	 */
108 	protected boolean isGetter(final Method method) {
109 		if (method.getReturnType().equals(boolean.class)) {
110 			if (!method.getName().startsWith("is")) {
111 				return false;
112 			}
113 		} else {
114 			if (!method.getName().startsWith("get")) {
115 				return false;
116 			}
117 		}
118 		if (method.getParameterTypes().length > 0) {
119 			return false;
120 		}
121 		if (method.getReturnType().equals(void.class)) {
122 			return false;
123 		}
124 		return true;
125 	}
126 
127 	/**
128 	 * 指定された getter メソッドがアクセスするプロパティ名を取得します。
129 	 * 
130 	 * @param method
131 	 *            メソッド
132 	 * @return プロパティ名
133 	 */
134 	protected String toPropertyName(final Method method) {
135 		final String methodName = method.getName();
136 		final String str1;
137 		if (method.getReturnType().equals(boolean.class)) {
138 			str1 = methodName.substring(2);
139 		} else {
140 			str1 = methodName.substring(3);
141 		}
142 		final String propertyName = Character.toUpperCase(str1.charAt(0))
143 				+ str1.substring(1);
144 		return propertyName;
145 	}
146 
147 }