incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1130776 - in /incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic: extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/ extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/ extval-shared/src/main/java/or...
Date Thu, 02 Jun 2011 20:10:11 GMT
Author: mbenson
Date: Thu Jun  2 20:10:11 2011
New Revision: 1130776

URL: http://svn.apache.org/viewvc?rev=1130776&view=rev
Log:
extract allowed values for UISelectOne components from Values/Values.Labeled constraints

Added:
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java
  (with props)
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java
  (with props)
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java
  (with props)
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java
  (with props)
Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/DynamicBeanValidationStartupListener.java

Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java?rev=1130776&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java
(added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java
Thu Jun  2 20:10:11 2011
@@ -0,0 +1,246 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bval.jsr303.dynamic.extval;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.reflect.Array;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UISelectItems;
+import javax.faces.component.UISelectOne;
+import javax.faces.component.html.HtmlSelectOneListbox;
+import javax.faces.component.html.HtmlSelectOneMenu;
+import javax.faces.component.html.HtmlSelectOneRadio;
+import javax.faces.model.SelectItem;
+import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.metadata.ElementDescriptor;
+
+import org.apache.bval.constraints.dynamic.Values;
+import org.apache.bval.constraints.dynamic.Values.Labeled.Item;
+import org.junit.Test;
+
+/**
+ * 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class AllowedValuesComponentInitializerTestCase extends
+    BaseDynamicBeanValPropertyValidationTestCase<AllowedValuesComponentInitializerTestCase.SelectableBean>
{
+
+    public static class SelectableBean {
+        @Values({ "foo", "bar", "baz" })
+        private String string;
+
+        @Values({ "0", "1", "2" })
+        private int code;
+
+        @Values.Labeled({ @Item(label = "field", value = "FIELD"), @Item(label = "method",
value = "METHOD"),
+            @Item(label = "type", value = "TYPE") })
+        private ElementType elementType;
+
+        public String getString() {
+            return string;
+        }
+
+        public void setString(String string) {
+            this.string = string;
+        }
+
+        public int getCode() {
+            return code;
+        }
+
+        public void setCode(int code) {
+            this.code = code;
+        }
+
+        public ElementType getElementType() {
+            return elementType;
+        }
+
+        public void setElementType(ElementType elementType) {
+            this.elementType = elementType;
+        }
+
+    }
+
+    protected HtmlSelectOneListbox selectOneListbox;
+    protected HtmlSelectOneMenu selectOneMenu;
+    protected HtmlSelectOneRadio selectOneRadio;
+
+    @Override
+    protected void setUpTestCase() {
+        super.setUpTestCase();
+        createComponents();
+    }
+
+    private void createComponents() {
+        UIComponent form = inputComponent1.findComponent(":form");
+        selectOneListbox = new HtmlSelectOneListbox();
+        selectOneMenu = new HtmlSelectOneMenu();
+        selectOneRadio = new HtmlSelectOneRadio();
+        form.getChildren().add(selectOneListbox);
+        form.getChildren().add(selectOneMenu);
+        form.getChildren().add(selectOneRadio);
+        selectOneListbox.setId("selectOneListbox");
+        selectOneMenu.setId("selectOneMenu");
+        selectOneRadio.setId("selectOneRadio");
+    }
+
+    @Override
+    protected void resetTestCase() {
+        super.resetTestCase();
+        selectOneListbox = null;
+        selectOneMenu = null;
+        selectOneRadio = null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected SelectableBean getBeanToTest() {
+        return new SelectableBean();
+    }
+
+    @Test
+    public void testSelectOneListbox() throws IOException {
+        createValueBindingForComponent(selectOneListbox, "#{testBean.string}");
+        selectOneListbox.encodeAll(facesContext);
+        UISelectItems items = findUISelectItems(selectOneListbox);
+        assertNotNull(items);
+        Object value = items.getValue();
+        assertNotNull(value);
+        assertTrue(value.getClass().isArray());
+        assertEquals(SelectItem.class, value.getClass().getComponentType());
+        Values constraint =
+            getRequiredConstraint(validatorContext().getValidator().getConstraintsForClass(SelectableBean.class)
+                .getConstraintsForProperty("string"), Values.class);
+        for (int i = 0, sz = Array.getLength(value); i < sz; i++) {
+            SelectItem selectItem = (SelectItem) Array.get(value, i);
+            assertNotNull(selectItem);
+            assertEquals(constraint.value()[i], selectItem.getValue());
+        }
+    }
+
+    @Test
+    public void testSelectOneListboxNoop() throws IOException {
+        createValueBindingForComponent(selectOneListbox, "#{testBean.string}");
+        UISelectItems empty = createEmptyUISelectItems();
+        selectOneListbox.getChildren().add(empty);
+        selectOneListbox.encodeAll(facesContext);
+        assertSame(empty, findUISelectItems(selectOneListbox));
+    }
+
+    @Test
+    public void testSelectOneMenu() throws IOException {
+        createValueBindingForComponent(selectOneMenu, "#{testBean.code}");
+        selectOneMenu.encodeAll(facesContext);
+        UISelectItems items = findUISelectItems(selectOneMenu);
+        assertNotNull(items);
+        Object value = items.getValue();
+        assertNotNull(value);
+        assertTrue(value.getClass().isArray());
+        assertEquals(SelectItem.class, value.getClass().getComponentType());
+        Values constraint =
+            getRequiredConstraint(validatorContext().getValidator().getConstraintsForClass(SelectableBean.class)
+                .getConstraintsForProperty("code"), Values.class);
+        for (int i = 0, sz = Array.getLength(value); i < sz; i++) {
+            SelectItem selectItem = (SelectItem) Array.get(value, i);
+            assertNotNull(selectItem);
+            assertEquals(constraint.value()[i], selectItem.getValue());
+        }
+    }
+
+    @Test
+    public void testSelectOneMenuNoop() throws IOException {
+        createValueBindingForComponent(selectOneMenu, "#{testBean.code}");
+        UISelectItems empty = createEmptyUISelectItems();
+        selectOneMenu.getChildren().add(empty);
+        selectOneMenu.encodeAll(facesContext);
+        assertSame(empty, findUISelectItems(selectOneMenu));
+    }
+
+    @Test
+    public void testSelectOneRadio() throws IOException {
+        createValueBindingForComponent(selectOneRadio, "#{testBean.elementType}");
+        selectOneRadio.encodeAll(facesContext);
+        UISelectItems items = findUISelectItems(selectOneRadio);
+        assertNotNull(items);
+        Object value = items.getValue();
+        assertNotNull(value);
+        assertTrue(value instanceof Map<?, ?>);
+        Values.Labeled constraint =
+            getRequiredConstraint(validatorContext().getValidator().getConstraintsForClass(SelectableBean.class)
+                .getConstraintsForProperty("elementType"), Values.Labeled.class);
+        int i = 0;
+        for (Map.Entry<?, ?> e : ((Map<?, ?>) value).entrySet()) {
+            Values.Labeled.Item labeledValueItem = constraint.value()[i++];
+            assertEquals(labeledValueItem.label(), e.getKey());
+            assertEquals(labeledValueItem.value(), e.getValue());
+        }
+    }
+
+    @Test
+    public void testSelectOneRadioNoop() throws IOException {
+        createValueBindingForComponent(selectOneRadio, "#{testBean.elementType}");
+        UISelectItems empty = createEmptyUISelectItems();
+        selectOneRadio.getChildren().add(empty);
+        selectOneRadio.encodeAll(facesContext);
+        assertSame(empty, findUISelectItems(selectOneRadio));
+    }
+
+    protected UISelectItems findUISelectItems(UISelectOne parent) {
+        for (UIComponent child : parent.getChildren()) {
+            if (child instanceof UISelectItems) {
+                return (UISelectItems) child;
+            }
+        }
+        return null;
+    }
+
+    protected UISelectItems createEmptyUISelectItems() {
+        UISelectItems empty = new UISelectItems();
+        empty.setValue(new SelectItem[0]);
+        return empty;
+    }
+
+    protected <T extends Annotation> T getRequiredConstraint(ElementDescriptor elem,
Class<T> type, Class<?>... groups) {
+        T result = getConstraint(elem, type, groups);
+        assertNotNull(result);
+        return result;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected <T extends Annotation> T getConstraint(ElementDescriptor elem, Class<T>
type, Class<?>... groups) {
+        for (ConstraintDescriptor<?> desc : elem.findConstraints().unorderedAndMatchingGroups(groups)
+            .getConstraintDescriptors()) {
+            if (type.equals(desc.getAnnotation().annotationType())) {
+                return (T) desc.getAnnotation();
+            }
+        }
+        return null;
+    }
+}

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared-tests/src/main/java/org/apache/bval/jsr303/dynamic/extval/AllowedValuesComponentInitializerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/DynamicBeanValidationStartupListener.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/DynamicBeanValidationStartupListener.java?rev=1130776&r1=1130775&r2=1130776&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/DynamicBeanValidationStartupListener.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/DynamicBeanValidationStartupListener.java
Thu Jun  2 20:10:11 2011
@@ -18,7 +18,9 @@ package org.apache.bval.jsr303.dynamic.e
 
 import javax.validation.ValidatorFactory;
 
+import org.apache.bval.jsr303.dynamic.extval.metadata.AllowedValuesComponentInitializer;
 import org.apache.bval.jsr303.dynamic.extval.metadata.transformer.NullValueMetaDataTransformer;
+import org.apache.bval.jsr303.dynamic.extval.metadata.transformer.ValuesMetaDataTransformer;
 import org.apache.myfaces.extensions.validator.beanval.BeanValidationModuleValidationInterceptor;
 import org.apache.myfaces.extensions.validator.core.ExtValContext;
 import org.apache.myfaces.extensions.validator.core.el.AbstractELHelperFactory;
@@ -57,6 +59,14 @@ public class DynamicBeanValidationStartu
         // handle NullValue constraints:
         ExtValUtils
             .registerValidationStrategyToMetaDataTransformerNameMapper(new NullValueMetaDataTransformer.NameMapper());
+
+        // handle Values/Values.Labeled constraints:
+        ExtValUtils
+            .registerValidationStrategyToMetaDataTransformerNameMapper(new ValuesMetaDataTransformer.NameMapper());
+        ExtValUtils
+            .registerValidationStrategyToMetaDataTransformerNameMapper(new ValuesMetaDataTransformer.Labeled.NameMapper());
+
+        ExtValContext.getContext().addComponentInitializer(new AllowedValuesComponentInitializer());
     }
 
     private <R extends RendererInterceptor, S extends R> void swap(ExtValContext extValContext,

Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java?rev=1130776&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java
(added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java
Thu Jun  2 20:10:11 2011
@@ -0,0 +1,115 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bval.jsr303.dynamic.extval.metadata;
+
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UISelectItems;
+import javax.faces.component.UISelectOne;
+import javax.faces.context.FacesContext;
+import javax.faces.model.SelectItem;
+
+import org.apache.myfaces.extensions.validator.core.initializer.component.ComponentInitializer;
+
+/**
+ * {@code ComponentInitializer} to handle allowed values.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class AllowedValuesComponentInitializer implements ComponentInitializer {
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void configureComponent(FacesContext facesContext, UIComponent uiComponent,
+        Map<String, Object> metaData) {
+        if (metaData.containsKey(MetaDataKeys.ALLOWED_VALUES) && uiComponent instanceof
UISelectOne) {
+            UISelectItems uiSelectItems = findUISelectItems((UISelectOne) uiComponent);
+            if (uiSelectItems != null) {
+                if (!this.isOwner(uiSelectItems)) {
+                    return;
+                }
+                uiComponent.getChildren().remove(uiSelectItems);
+            }
+            uiSelectItems = createUISelectItems();
+            setValue(uiSelectItems, metaData.get(MetaDataKeys.ALLOWED_VALUES));
+            uiComponent.getChildren().add(uiSelectItems);
+        }
+    }
+
+    /**
+     * Find the first existing {@link UISelectItems} child of a specified parent component.
+     * 
+     * @param parent
+     *            to search
+     * @return {@code UISelectItems} or {@code null}
+     */
+    protected UISelectItems findUISelectItems(UISelectOne parent) {
+        for (UIComponent child : parent.getChildren()) {
+            if (child instanceof UISelectItems) {
+                return (UISelectItems) child;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Generate a {@code UISelectItems} instance, adding our classname as the value of an
attribute mapping keyed by
+     * {@code ComponentInitializer}'s classname.
+     * 
+     * @param allowedValues
+     * @return {@code UISelectItems}
+     */
+    protected final UISelectItems createUISelectItems() {
+        UISelectItems result = new UISelectItems();
+        result.getAttributes().put(ComponentInitializer.class.getName(), getClass().getName());
+        return result;
+    }
+
+    /**
+     * Learn whether this {@code ComponentInitializer} generated (owns) the specified component.
+     * 
+     * @param component
+     *            to check
+     * @return {@code boolean}
+     */
+    protected final boolean isOwner(UIComponent component) {
+        return getClass().getName().equals(component.getAttributes().get(ComponentInitializer.class.getName()));
+    }
+
+    /**
+     * Set the value of {@code uiSelectItems} to {@code allowedValues}.
+     * 
+     * @param uiSelectItems
+     *            target
+     * @param allowedValues
+     *            src
+     */
+    protected void setValue(UISelectItems uiSelectItems, Object allowedValues) {
+        if (allowedValues instanceof Map<?, ?>) {
+            uiSelectItems.setValue(allowedValues);
+        } else if (allowedValues instanceof String[]) {
+            String[] s = (String[]) allowedValues;
+            SelectItem[] selectItems = new SelectItem[s.length];
+            for (int i = 0; i < s.length; i++) {
+                selectItems[i] = new SelectItem(s[i]);
+            }
+            uiSelectItems.setValue(selectItems);
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/AllowedValuesComponentInitializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java?rev=1130776&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java
(added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java
Thu Jun  2 20:10:11 2011
@@ -0,0 +1,26 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bval.jsr303.dynamic.extval.metadata;
+
+/**
+ * Dynamic Bean Validation metadata keys.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface MetaDataKeys {
+    public static final String ALLOWED_VALUES = "allowedValues";
+}

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/MetaDataKeys.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java?rev=1130776&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java
(added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java
Thu Jun  2 20:10:11 2011
@@ -0,0 +1,100 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bval.jsr303.dynamic.extval.metadata.transformer;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.validation.metadata.ConstraintDescriptor;
+
+import org.apache.bval.constraints.dynamic.Values;
+import org.apache.bval.jsr303.dynamic.extval.metadata.MetaDataKeys;
+import org.apache.myfaces.extensions.validator.beanval.metadata.transformer.AbstractBeanValidationMetaDataTransformer;
+import org.apache.myfaces.extensions.validator.beanval.metadata.transformer.mapper.AbstractBeanValidationVirtualValidationStrategyToMetaDataTransformerNameMapper;
+import org.apache.myfaces.extensions.validator.beanval.validation.strategy.BeanValidationVirtualValidationStrategy;
+
+/**
+ * {@code Values} {@code MetaDataTransformer}.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class ValuesMetaDataTransformer extends AbstractBeanValidationMetaDataTransformer<Values>
{
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected Map<String, Object> transformMetaData(ConstraintDescriptor<Values>
constraintDescriptor) {
+        return Collections.<String, Object> singletonMap(MetaDataKeys.ALLOWED_VALUES,
constraintDescriptor
+            .getAnnotation().value());
+    }
+
+    /**
+     * {@code Values} {@code MetaDataTransformer} {@code NameMapper}.
+     */
+    public static class NameMapper extends
+        AbstractBeanValidationVirtualValidationStrategyToMetaDataTransformerNameMapper {
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        protected String createBeanValidationTransformerName(BeanValidationVirtualValidationStrategy
validationStrategy) {
+            return validationStrategy.getConstraintDescriptor().getAnnotation().annotationType().getName()
+                .equals(Values.class.getName()) ? ValuesMetaDataTransformer.class.getName()
: null;
+        }
+
+    }
+
+    /**
+     * {@code Values.Labeled} {@code MetaDataTransformer}.
+     */
+    public static class Labeled extends AbstractBeanValidationMetaDataTransformer<Values.Labeled>
{
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        protected Map<String, Object> transformMetaData(
+            ConstraintDescriptor<org.apache.bval.constraints.dynamic.Values.Labeled>
constraintDescriptor) {
+            LinkedHashMap<String, String> valuesMap = new LinkedHashMap<String,
String>();
+            for (Values.Labeled.Item item : constraintDescriptor.getAnnotation().value())
{
+                valuesMap.put(item.label(), item.value());
+            }
+            return Collections.<String, Object> singletonMap(MetaDataKeys.ALLOWED_VALUES,
valuesMap);
+        }
+
+        /**
+         * {@code Values.Labeled} {@code MetaDataTransformer} {@code NameMapper}.
+         */
+        public static class NameMapper extends
+            AbstractBeanValidationVirtualValidationStrategyToMetaDataTransformerNameMapper
{
+
+            /**
+             * {@inheritDoc}
+             */
+            @Override
+            protected String createBeanValidationTransformerName(
+                BeanValidationVirtualValidationStrategy validationStrategy) {
+                return validationStrategy.getConstraintDescriptor().getAnnotation().annotationType().getName()
+                    .equals(Values.Labeled.class.getName()) ? ValuesMetaDataTransformer.Labeled.class.getName()
: null;
+            }
+
+        }
+    }
+}

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/extval-shared/src/main/java/org/apache/bval/jsr303/dynamic/extval/metadata/transformer/ValuesMetaDataTransformer.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message