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