Coverage Report - org.seasar.cubby.plugins.oval.validation.RequestLocaleOValContextRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestLocaleOValContextRenderer
0%
0/56
0%
0/34
8.333
 
 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  0
 public class RequestLocaleOValContextRenderer implements OValContextRenderer {
 40  
 
 41  
         /**
 42  
          * {@inheritDoc}
 43  
          */
 44  
         public String render(final OValContext ovalContext) {
 45  
                 final String fieldName;
 46  0
                 if (ovalContext instanceof ClassContext) {
 47  0
                         final ClassContext ctx = (ClassContext) ovalContext;
 48  0
                         fieldName = ctx.getClazz().getSimpleName();
 49  0
                 } else if (ovalContext instanceof FieldContext) {
 50  0
                         final FieldContext ctx = (FieldContext) ovalContext;
 51  0
                         fieldName = ctx.getField().getName();
 52  0
                 } else if (ovalContext instanceof ConstructorParameterContext) {
 53  0
                         final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext;
 54  0
                         fieldName = ctx.getParameterName();
 55  0
                 } else if (ovalContext instanceof MethodParameterContext) {
 56  0
                         final MethodParameterContext ctx = (MethodParameterContext) ovalContext;
 57  0
                         fieldName = ctx.getParameterName();
 58  0
                 } else if (ovalContext instanceof MethodEntryContext) {
 59  0
                         final MethodEntryContext ctx = (MethodEntryContext) ovalContext;
 60  0
                         final Method method = ctx.getMethod();
 61  0
                         if (isGetter(method)) {
 62  0
                                 fieldName = toPropertyName(method);
 63  
                         } else {
 64  0
                                 fieldName = method.getName();
 65  
                         }
 66  0
                 } else if (ovalContext instanceof MethodExitContext) {
 67  0
                         final MethodExitContext ctx = (MethodExitContext) ovalContext;
 68  0
                         final Method method = ctx.getMethod();
 69  0
                         if (isGetter(method)) {
 70  0
                                 fieldName = toPropertyName(method);
 71  
                         } else {
 72  0
                                 fieldName = method.getName();
 73  
                         }
 74  0
                 } else if (ovalContext instanceof MethodReturnValueContext) {
 75  0
                         final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext;
 76  0
                         final Method method = ctx.getMethod();
 77  0
                         if (isGetter(method)) {
 78  0
                                 fieldName = toPropertyName(method);
 79  
                         } else {
 80  0
                                 fieldName = method.getName();
 81  
                         }
 82  0
                 } else {
 83  0
                         return ovalContext.toString();
 84  
                 }
 85  
 
 86  
                 final String fieldNameKey;
 87  
 
 88  0
                 final OValValidationContext context = OValValidationContext.get();
 89  0
                 final String resourceKeyPrefix = context.getResourceKeyPrefix();
 90  0
                 if (resourceKeyPrefix == null) {
 91  0
                         fieldNameKey = fieldName;
 92  
                 } else {
 93  0
                         fieldNameKey = resourceKeyPrefix + fieldName;
 94  
                 }
 95  
 
 96  0
                 final String fieldNameMessage = Messages.getText(fieldNameKey);
 97  0
                 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  0
                 if (method.getReturnType().equals(boolean.class)) {
 109  0
                         if (!method.getName().startsWith("is")) {
 110  0
                                 return false;
 111  
                         }
 112  
                 } else {
 113  0
                         if (!method.getName().startsWith("get")) {
 114  0
                                 return false;
 115  
                         }
 116  
                 }
 117  0
                 if (method.getParameterTypes().length > 0) {
 118  0
                         return false;
 119  
                 }
 120  0
                 if (method.getReturnType().equals(void.class)) {
 121  0
                         return false;
 122  
                 }
 123  0
                 return true;
 124  
         }
 125  
 
 126  
         /**
 127  
          * 指定された getter メソッドがアクセスするプロパティ名を取得します。
 128  
          * 
 129  
          * @param method
 130  
          *            メソッド
 131  
          * @return プロパティ名
 132  
          */
 133  
         protected String toPropertyName(final Method method) {
 134  0
                 final String methodName = method.getName();
 135  
                 final String str1;
 136  0
                 if (method.getReturnType().equals(boolean.class)) {
 137  0
                         str1 = methodName.substring(2);
 138  
                 } else {
 139  0
                         str1 = methodName.substring(3);
 140  
                 }
 141  0
                 final String propertyName = Character.toUpperCase(str1.charAt(0))
 142  
                                 + str1.substring(1);
 143  0
                 return propertyName;
 144  
         }
 145  
 
 146  
 }