View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.extensions.validator.beanval.validation.message.interpolator;
20  
21  import org.apache.myfaces.extensions.validator.core.validation.message.resolver.MessageResolver;
22  import org.apache.myfaces.extensions.validator.core.validation.message.resolver.AbstractValidationErrorMessageResolver;
23  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
24  import org.apache.myfaces.extensions.validator.internal.UsageCategory;
25  
26  import javax.validation.MessageInterpolator;
27  import java.util.Locale;
28  
29  /**
30   * @since x.x.3
31   */
32  @UsageInformation(UsageCategory.INTERNAL)
33  public class ExtValMessageInterpolatorAdapter extends DefaultMessageInterpolator
34  {
35      private MessageResolver messageResolver;
36  
37      public ExtValMessageInterpolatorAdapter(MessageInterpolator wrapped, MessageResolver messageResolver)
38      {
39          super(wrapped);
40          this.messageResolver = messageResolver;
41      }
42  
43      @Override
44      public String interpolate(String messageOrKey, Context context)
45      {
46          return interpolate(messageOrKey, context, getCurrentLocale());
47      }
48  
49      @Override
50      public String interpolate(String messageOrKey, Context context, Locale locale)
51      {
52          if(this.messageResolver != null)
53          {
54              if(isBeanValidationMessageKeyFormat(messageOrKey))
55              {
56                  String newMessageOrKey = this.messageResolver.getMessage(extractKey(messageOrKey), getCurrentLocale());
57  
58                  if(isValideMessage(newMessageOrKey))
59                  {
60                      messageOrKey = newMessageOrKey;
61                  }
62              }
63              else
64              {
65                  this.logger.finest("you tried to use an extval message-resolver for" +
66                          "jsr303 validation with an invalid key -> using a default interpolator");
67              }
68          }
69          return super.interpolate(messageOrKey, context, getCurrentLocale());
70      }
71  
72      private boolean isBeanValidationMessageKeyFormat(String messageOrKey)
73      {
74          return messageOrKey.startsWith("{") && messageOrKey.endsWith("}");
75      }
76  
77      private String extractKey(String messageOrKey)
78      {
79          return messageOrKey.substring(1, messageOrKey.length() - 1);
80      }
81  
82      private boolean isValideMessage(String newMessageOrKey)
83      {
84          return !(newMessageOrKey.startsWith(AbstractValidationErrorMessageResolver.MISSING_RESOURCE_MARKER) &&
85                          newMessageOrKey.endsWith(AbstractValidationErrorMessageResolver.MISSING_RESOURCE_MARKER));
86      }
87  }