001 // Copyright 2007 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.beaneditor;
016
017 import org.apache.tapestry5.services.ValidationConstraintGenerator;
018 import org.apache.tapestry5.services.Environment;
019 import org.apache.tapestry5.services.PropertyEditContext;
020 import org.apache.tapestry5.ioc.AnnotationProvider;
021 import org.apache.tapestry5.ioc.Messages;
022 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
023
024 import java.util.List;
025 import java.util.Arrays;
026 import java.util.regex.Pattern;
027
028 /**
029 * Generates constraints from the containing component's property file.
030 * Looks for a key in the form: propertyId-validate.
031 *
032 */
033 public class MessagesConstraintGenerator implements ValidationConstraintGenerator
034 {
035
036 private final Environment environment;
037 private final String format="%s-validate";
038 private final Pattern splitPattern;
039
040 public MessagesConstraintGenerator(final Environment environment) {
041 this.environment = environment;
042 splitPattern = Pattern.compile(ValidateAnnotationConstraintGenerator.VALIDATOR_PATTERN);
043 }
044
045 public List<String> buildConstraints(Class propertyType, AnnotationProvider annotationProvider)
046 {
047 EnvironmentMessages environmentMessages = environment.peek(EnvironmentMessages.class);
048 if (environmentMessages == null) {
049 return null;
050 }
051
052 String key = String.format(format,environmentMessages.getOverrideId());
053 Messages m = environmentMessages.getMessages();
054 if (!m.contains(key))
055 {
056 return null;
057 }
058
059 String result = m.get(key);
060 if (InternalUtils.isBlank(result))
061 {
062 return null;
063 }
064 return Arrays.asList(splitPattern.split(result));
065 }
066 }