Author: ppitonak(a)redhat.com
Date: 2011-03-29 12:51:04 -0400 (Tue, 29 Mar 2011)
New Revision: 22330
Added:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/CollectionConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplateNameConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplatesListConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/ValueConverter.java
Removed:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplateNameConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplatesListConverter.java
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java
Log:
converters moved to separate package
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java 2011-03-29
16:50:19 UTC (rev 22329)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/Attribute.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -34,6 +34,7 @@
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
+import org.richfaces.tests.metamer.converter.CollectionConverter;
/**
* Representation an attribute of a JSF component.
Deleted:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java 2011-03-29
16:50:19 UTC (rev 22329)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -1,107 +0,0 @@
-/*******************************************************************************
- * JBoss, Home of Professional Open Source
- * Copyright 2010-2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- *******************************************************************************/
-package org.richfaces.tests.metamer;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.apache.commons.lang.StringUtils;
-
-/**
- * @author <a href="mailto:lfryc@redhat.com">Pavol Pitonak</a>
- * @version $Revision$
- */
-public final class CollectionConverter<E, C extends Collection<E>> {
-
- Class<C> collectionClass;
- Class<E> memberClass;
-
- public CollectionConverter(Class<?> collectionClass, Class<?>
memberClass) {
- this.collectionClass = (Class<C>) collectionClass;
- this.memberClass = (Class<E>) memberClass;
- }
-
- public static <E, C extends Collection<E>> CollectionConverter<E,
Collection<E>> getInstance(
- Class<E> collectionClass, Class<C> memberClass) {
- return new CollectionConverter<E, Collection<E>>(collectionClass,
memberClass);
- }
-
- public C convert(Object value) {
- Collection<?> collection = getTransformedValue(value);
-
- if (collection.isEmpty()) {
- return createCollection();
- }
-
- ValueConverter<E> memberConverter =
ValueConverter.getInstance(memberClass);
-
- C result = createCollection();
-
- for (Object object : collection) {
- result.add(memberConverter.convert(object));
- }
-
- return result;
- }
-
- private Collection<? extends Object> getTransformedValue(Object value) {
- if (value == null) {
- return Collections.EMPTY_LIST;
- } else if (value instanceof String) {
- String[] splitted = StringUtils.split((String) value, ",[] ");
- return Arrays.asList(splitted);
- } else if (value instanceof Collection) {
- return (Collection) value;
- } else {
- throw new UnsupportedOperationException("the value '" + value +
"' of class " + value.getClass()
- + " is not supported in collection transformer");
- }
- }
-
- private C createCollection() {
- try {
- return getConcreteClass().newInstance();
- } catch (Exception e) {
- throw new IllegalStateException("wasn't able to construct new
collection of given type ("
- + collectionClass.getName() + ")");
- }
- }
-
- @SuppressWarnings("unchecked")
- private Class<? extends C> getConcreteClass() {
- if (!collectionClass.isInterface()) {
- return collectionClass;
- } else {
- if (Set.class.isAssignableFrom(collectionClass)) {
- return (Class<? extends C>) (Object) TreeSet.class;
- }
- if (Collection.class.isAssignableFrom(collectionClass)) {
- return (Class<? extends C>) (Object) LinkedList.class;
- }
- throw new IllegalStateException("Unsupported collection of interface
'" + collectionClass.getName() + "'");
- }
- }
-}
Deleted:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplateNameConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplateNameConverter.java 2011-03-29
16:50:19 UTC (rev 22329)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplateNameConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -1,65 +0,0 @@
-/*******************************************************************************
- * JBoss, Home of Professional Open Source
- * Copyright 2010-2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- *******************************************************************************/
-
-package org.richfaces.tests.metamer;
-
-import javax.faces.FacesException;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.convert.Converter;
-import javax.faces.convert.FacesConverter;
-
-/**
- * Converter used for view parameter "template".
- *
- * @author <a href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
- * @version $Revision$
- */
-@FacesConverter(value = "templateNameConverter")
-public class TemplateNameConverter implements Converter {
-
- /**
- * {@inheritDoc}
- */
- public Object getAsObject(FacesContext context, UIComponent component, String value)
{
- try {
- return Template.valueOf(value.toUpperCase());
- } catch (IllegalArgumentException iae) {
- throw new FacesException("Cannot convert parameter \"" + value
+ "\" to the name of template.", iae);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public String getAsString(FacesContext context, UIComponent component, Object value)
{
- if (value instanceof String) {
- return (String) value;
- }
-
- if (value instanceof Template) {
- return ((Template) value).toString();
- }
-
- throw new FacesException("Cannot convert parameter \"" + value +
"\" to the name of template.");
- }
-}
Deleted:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplatesListConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplatesListConverter.java 2011-03-29
16:50:19 UTC (rev 22329)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplatesListConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -1,71 +0,0 @@
-/*******************************************************************************
- * JBoss, Home of Professional Open Source
- * Copyright 2010-2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- *******************************************************************************/
-
-package org.richfaces.tests.metamer;
-
-import javax.faces.FacesException;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.convert.Converter;
-import javax.faces.convert.FacesConverter;
-
-/**
- * Converter used for view parameter "template".
- *
- * @author <a href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
- * @version $Revision$
- */
-@FacesConverter(value = "templatesListConverter")
-public class TemplatesListConverter implements Converter {
-
- /**
- * {@inheritDoc}
- */
- public Object getAsObject(FacesContext context, UIComponent component, String value)
{
- try {
- TemplatesList list = new TemplatesList();
-
- for (String template : value.split(",")) {
- list.add(Template.valueOf(template.toUpperCase()));
- }
-
- return list;
- } catch (IllegalArgumentException iae) {
- throw new FacesException("Cannot convert parameter \"" + value
+ "\" to the list of templates.", iae);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public String getAsString(FacesContext context, UIComponent component, Object value)
{
- if (value instanceof String) {
- return (String) value;
- }
-
- if (value instanceof TemplatesList) {
- return value.toString();
- }
-
- throw new FacesException("Cannot convert parameter \"" + value +
"\" to the list of templates.");
- }
-}
Deleted:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java 2011-03-29
16:50:19 UTC (rev 22329)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -1,74 +0,0 @@
-/*******************************************************************************
- * JBoss, Home of Professional Open Source
- * Copyright 2010-2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- *******************************************************************************/
-package org.richfaces.tests.metamer;
-
-import java.lang.reflect.Constructor;
-
-/**
- * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
- * @version $Revision$
- */
-public final class ValueConverter<T> {
-
- Class<T> type;
-
- private ValueConverter(Class<?> type) {
- this.type = (Class<T>) type;
- }
-
- public static <T> ValueConverter<T> getInstance(Class<T> type) {
- return new ValueConverter<T>(type);
- }
-
- public T convert(Object value) {
- if (value == null) {
- return null;
- } else if (type.isAssignableFrom(String.class)) {
- return (T) value;
- } else if (value instanceof String) {
- return construct(value);
- } else {
- throw new UnsupportedOperationException("Can't convert the object of
'" + value.getClass().getName()
- + "' to '" + type.getName() + "'");
- }
- }
-
- private T construct(Object value) {
- try {
- return getConstructor().newInstance(value);
- } catch (Exception e) {
- throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
- + "' from String");
- }
-
- }
-
- public Constructor<T> getConstructor() {
- try {
- return type.getConstructor(String.class);
- } catch (Exception e) {
- throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
- + "' from String");
- }
- }
-
-}
Copied:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/CollectionConverter.java
(from rev 22329,
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/CollectionConverter.java)
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/CollectionConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/CollectionConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010-2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.converter;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public final class CollectionConverter<E, C extends Collection<E>> {
+
+ Class<C> collectionClass;
+ Class<E> memberClass;
+
+ public CollectionConverter(Class<?> collectionClass, Class<?>
memberClass) {
+ this.collectionClass = (Class<C>) collectionClass;
+ this.memberClass = (Class<E>) memberClass;
+ }
+
+ public static <E, C extends Collection<E>> CollectionConverter<E,
Collection<E>> getInstance(
+ Class<E> collectionClass, Class<C> memberClass) {
+ return new CollectionConverter<E, Collection<E>>(collectionClass,
memberClass);
+ }
+
+ public C convert(Object value) {
+ Collection<?> collection = getTransformedValue(value);
+
+ if (collection.isEmpty()) {
+ return createCollection();
+ }
+
+ ValueConverter<E> memberConverter =
ValueConverter.getInstance(memberClass);
+
+ C result = createCollection();
+
+ for (Object object : collection) {
+ result.add(memberConverter.convert(object));
+ }
+
+ return result;
+ }
+
+ private Collection<? extends Object> getTransformedValue(Object value) {
+ if (value == null) {
+ return Collections.EMPTY_LIST;
+ } else if (value instanceof String) {
+ String[] splitted = StringUtils.split((String) value, ",[] ");
+ return Arrays.asList(splitted);
+ } else if (value instanceof Collection) {
+ return (Collection) value;
+ } else {
+ throw new UnsupportedOperationException("the value '" + value +
"' of class " + value.getClass()
+ + " is not supported in collection transformer");
+ }
+ }
+
+ private C createCollection() {
+ try {
+ return getConcreteClass().newInstance();
+ } catch (Exception e) {
+ throw new IllegalStateException("wasn't able to construct new
collection of given type ("
+ + collectionClass.getName() + ")");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Class<? extends C> getConcreteClass() {
+ if (!collectionClass.isInterface()) {
+ return collectionClass;
+ } else {
+ if (Set.class.isAssignableFrom(collectionClass)) {
+ return (Class<? extends C>) (Object) TreeSet.class;
+ }
+ if (Collection.class.isAssignableFrom(collectionClass)) {
+ return (Class<? extends C>) (Object) LinkedList.class;
+ }
+ throw new IllegalStateException("Unsupported collection of interface
'" + collectionClass.getName() + "'");
+ }
+ }
+}
Copied:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplateNameConverter.java
(from rev 22329,
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplateNameConverter.java)
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplateNameConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplateNameConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010-2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+
+package org.richfaces.tests.metamer.converter;
+
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.FacesConverter;
+import org.richfaces.tests.metamer.Template;
+
+/**
+ * Converter used for view parameter "template".
+ *
+ * @author <a href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
+ * @version $Revision$
+ */
+@FacesConverter(value = "templateNameConverter")
+public class TemplateNameConverter implements Converter {
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getAsObject(FacesContext context, UIComponent component, String value)
{
+ try {
+ return Template.valueOf(value.toUpperCase());
+ } catch (IllegalArgumentException iae) {
+ throw new FacesException("Cannot convert parameter \"" + value
+ "\" to the name of template.", iae);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getAsString(FacesContext context, UIComponent component, Object value)
{
+ if (value instanceof String) {
+ return (String) value;
+ }
+
+ if (value instanceof Template) {
+ return ((Template) value).toString();
+ }
+
+ throw new FacesException("Cannot convert parameter \"" + value +
"\" to the name of template.");
+ }
+}
Copied:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplatesListConverter.java
(from rev 22329,
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/TemplatesListConverter.java)
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplatesListConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/TemplatesListConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010-2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+
+package org.richfaces.tests.metamer.converter;
+
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.FacesConverter;
+import org.richfaces.tests.metamer.Template;
+import org.richfaces.tests.metamer.TemplatesList;
+
+/**
+ * Converter used for view parameter "template".
+ *
+ * @author <a href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
+ * @version $Revision$
+ */
+@FacesConverter(value = "templatesListConverter")
+public class TemplatesListConverter implements Converter {
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getAsObject(FacesContext context, UIComponent component, String value)
{
+ try {
+ TemplatesList list = new TemplatesList();
+
+ for (String template : value.split(",")) {
+ list.add(Template.valueOf(template.toUpperCase()));
+ }
+
+ return list;
+ } catch (IllegalArgumentException iae) {
+ throw new FacesException("Cannot convert parameter \"" + value
+ "\" to the list of templates.", iae);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getAsString(FacesContext context, UIComponent component, Object value)
{
+ if (value instanceof String) {
+ return (String) value;
+ }
+
+ if (value instanceof TemplatesList) {
+ return value.toString();
+ }
+
+ throw new FacesException("Cannot convert parameter \"" + value +
"\" to the list of templates.");
+ }
+}
Copied:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/ValueConverter.java
(from rev 22329,
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/ValueConverter.java)
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/ValueConverter.java
(rev 0)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/converter/ValueConverter.java 2011-03-29
16:51:04 UTC (rev 22330)
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010-2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.converter;
+
+import java.lang.reflect.Constructor;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public final class ValueConverter<T> {
+
+ Class<T> type;
+
+ private ValueConverter(Class<?> type) {
+ this.type = (Class<T>) type;
+ }
+
+ public static <T> ValueConverter<T> getInstance(Class<T> type) {
+ return new ValueConverter<T>(type);
+ }
+
+ public T convert(Object value) {
+ if (value == null) {
+ return null;
+ } else if (type.isAssignableFrom(String.class)) {
+ return (T) value;
+ } else if (value instanceof String) {
+ return construct(value);
+ } else {
+ throw new UnsupportedOperationException("Can't convert the object of
'" + value.getClass().getName()
+ + "' to '" + type.getName() + "'");
+ }
+ }
+
+ private T construct(Object value) {
+ try {
+ return getConstructor().newInstance(value);
+ } catch (Exception e) {
+ throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
+ + "' from String");
+ }
+
+ }
+
+ public Constructor<T> getConstructor() {
+ try {
+ return type.getConstructor(String.class);
+ } catch (Exception e) {
+ throw new UnsupportedOperationException("Can't construct the object
of class '" + type.getName()
+ + "' from String");
+ }
+ }
+
+}