Return-Path: Delivered-To: apmail-myfaces-users-archive@www.apache.org Received: (qmail 94238 invoked from network); 19 Jun 2007 20:27:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 19 Jun 2007 20:27:26 -0000 Received: (qmail 10219 invoked by uid 500); 19 Jun 2007 20:27:27 -0000 Delivered-To: apmail-myfaces-users-archive@myfaces.apache.org Received: (qmail 9225 invoked by uid 500); 19 Jun 2007 20:27:25 -0000 Mailing-List: contact users-help@myfaces.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "MyFaces Discussion" Delivered-To: mailing list users@myfaces.apache.org Received: (qmail 9213 invoked by uid 99); 19 Jun 2007 20:27:25 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jun 2007 13:27:25 -0700 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE X-Spam-Check-By: apache.org Received-SPF: neutral (herse.apache.org: local policy) Received: from [68.230.241.39] (HELO fed1rmmtao107.cox.net) (68.230.241.39) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jun 2007 13:27:20 -0700 Received: from fed1rmimpo01.cox.net ([70.169.32.71]) by fed1rmmtao107.cox.net (InterMail vM.7.08.02.01 201-2186-121-102-20070209) with ESMTP id <20070619202658.IYHE2558.fed1rmmtao107.cox.net@fed1rmimpo01.cox.net>; Tue, 19 Jun 2007 16:26:58 -0400 Received: from [IPv6:::1] ([68.6.79.24]) by fed1rmimpo01.cox.net with bizsmtp id DYSw1X00C0XU6iC0000000; Tue, 19 Jun 2007 16:26:58 -0400 In-Reply-To: <46782FE3.9020203@stillsecure.com> References: <46782FE3.9020203@stillsecure.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: multipart/alternative; boundary=Apple-Mail-4--33331416 Message-Id: Cc: Bryan Basham From: kindsol Subject: Re: Creating a custom converter tag Date: Tue, 19 Jun 2007 13:26:55 -0700 To: MyFaces Discussion X-Mailer: Apple Mail (2.752.2) X-Virus-Checked: Checked by ClamAV on apache.org --Apple-Mail-4--33331416 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Thanks for the reply, Brian. It looks like the MyFaces mailing list strips off attachments. Can you send me those files directly? Thank again! -Sol On Jun 19, 2007, at 12:34 PM, Bryan Basham wrote: > Hello "kind sole" ;-) > > I have just created a converter with a tag. Nice little exercise. > > My app has lots of "names" for Boolean values, such as > "yes" / "no" and "enabled" / "disabled" and so on. I used > to create a separate accessor method to convert from the > Boolean to a String, but I wanted something more generic > so I created the BooleanConverter and BooleanConverterTag > to handle this situation. > > So, for example in a JSF page you might want to display a > boolean value as yes/no: > >

> Did the student fill in the form? > // the bean > isFormFilledIn() method must return boolean > > >

> > I have attached my classes for this converter and tag. > Don't forget that you need to declare this tag in your own TLD file. > > HTH, > Bryan > > > kindsol wrote: >> I want to create a custom converter as it's own tag (e.g. not >> using tag. Is there any documentation online or >> elsewhere that you can point me to? >> >> thanks in advance! >> >> -Sol >> >> > > package org.stillsecure.cobia.web.converters.tags; > > import org.stillsecure.cobia.web.converters.BooleanConverter; > > import javax.faces.convert.Converter; > import javax.faces.webapp.ConverterTag; > import javax.servlet.jsp.JspException; > > public class BooleanConverterTag extends ConverterTag > { > public BooleanConverterTag() > { > super(); > setConverterId(BooleanConverter.CONVERTER_ID); > } > > private String type; > > public void setType(String type) throws JspException > { > // Check that this type is a valid Boolean type name > if ( type == null ) > throw new JspException(NULL_TYPE_ERROR); > if ( ! BooleanConverter.isValidType(type) ) > throw new JspException(String.format > (NOT_A_KNOWN_TYPE_ERROR, type)); > > this.type = type; > } > private static final String NULL_TYPE_ERROR > = "The 'type' attribute value must not be null."; > private static final String NOT_A_KNOWN_TYPE_ERROR > = "The 'type' attribute value (%s) is not a registered > Boolean type."; > > // > // JSF methods > // > > @Override > protected Converter createConverter() throws JspException > { > BooleanConverter converter = (BooleanConverter) > super.createConverter(); > converter.setType(this.type); > return converter; > } > } > package org.stillsecure.cobia.web.converters; > > import org.stillsecure.cobia.web.util.WebContext; > > import java.util.HashMap; > import java.util.Map; > > import javax.faces.application.FacesMessage; > import javax.faces.component.StateHolder; > import javax.faces.component.UIComponent; > import javax.faces.context.FacesContext; > import javax.faces.convert.Converter; > import javax.faces.convert.ConverterException; > > /** > * This class implements a JSF converter to convert from boolean > values > * to specialized text strings, such as yes/no or enabled/disabled. > */ > public class BooleanConverter implements Converter, StateHolder > { > /** The JSF id for this converter. Must be the same as defined > in the framework-faces-config.xml file. */ > public final static String CONVERTER_ID = "converter.Boolean"; > > // > // Registering Boolean "types" > // > > public static void registerType(String type, String trueName, > String falseName) > { > BOOLEAN_NAME_PAIRS_MAP.put(type, new String[] > {trueName.toLowerCase(), falseName.toLowerCase()}); > } > public static boolean isValidType(String type) > { > return BOOLEAN_NAME_PAIRS_MAP.containsKey(type); > } > public static String[] getType(String type) > { > return BOOLEAN_NAME_PAIRS_MAP.get(type); > } > private static final Map > BOOLEAN_NAME_PAIRS_MAP = new HashMap(); > > // > // Default Boolean name types > // > > public static final String STANDARD = "STANDARD"; > public static final String YES_NO = "YES/NO"; > public static final String ENABLE = "OP-MODE"; > public static final String UP_DOWN = "UP/DOWN"; > > static > { > registerType(STANDARD, "true", "false"); > registerType(YES_NO, "yes", "no"); > registerType(ENABLE, "enabled", "disabled"); > registerType(UP_DOWN, "up", "down"); > } > > // > // Attributes > // > > private String type; > > /** > * Sets the type of Boolean conversion. > * > * @param type must be a registered Boolean type name > */ > public void setType(String type) > { > this.type = type; > } > > public void checkType(String type) > { > // Check converter state > if ( type == null ) > throw new ConverterException > (WebContext.createFacesMessage(NO_TYPE_ERROR)); > if ( ! isValidType(type) ) > throw new ConverterException > (WebContext.createFacesMessage(BAD_TYPE_ERROR, this.type)); > } > private static final String NO_TYPE_ERROR = > "BASE_BooleanConverter_noTypeError"; > private static final String BAD_TYPE_ERROR = > "BASE_BooleanConverter_badTypeError"; > > // > // JSF methods > // > > /** > * This method converts from the string representation to a > Boolean object. > */ > public Object getAsObject(FacesContext ctx, UIComponent comp, > String displayString) throws ConverterException > { > checkType(this.type); > > Boolean result = null; > > String[] pair = getType(this.type); > if ( pair[0].equalsIgnoreCase(displayString) ) > { > result = Boolean.TRUE; > } > else if ( pair[1].equalsIgnoreCase(displayString) ) > { > result = Boolean.FALSE; > } > else > { > FacesMessage message > = WebContext.createFacesMessage > (BOOLEAN_CONVERTER_TO_OBJECT_FAILED, displayString); > throw new ConverterException(message); > } > > return result; > } > private static final String BOOLEAN_CONVERTER_TO_OBJECT_FAILED > = "BASE_BooleanConverter_getAsObjectFailed"; > > /** > * This method converts from the Boolean object to the string > representation. > */ > public String getAsString(FacesContext ctx, UIComponent comp, > Object object) throws ConverterException > { > checkType(this.type); > > String result = null; > > String[] pair = getType(this.type); > try > { > Boolean o = (Boolean) object; > if ( o == null ) > result = ""; > else if ( o.equals(Boolean.TRUE) ) > result = pair[0]; > else if ( o.equals(Boolean.FALSE) ) > result = pair[1]; > else > assert false : "Unknown object" + o; > } > catch (ClassCastException e) > { > assert false : e; > } > > return result; > } > > // > // StateHolder methods > // > > private boolean transientFlag; > > public void setTransient(boolean transientValue) > { > this.transientFlag = transientValue; > } > > public boolean isTransient() > { > return transientFlag; > } > > public Object saveState(FacesContext context) > { > Object[] values = new Object[1]; > values[0] = this.type; > return values; > } > > public void restoreState(FacesContext context, Object state) > { > Object[] values = (Object[]) state; > this.type = (String) values[0]; > } > > } --Apple-Mail-4--33331416 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 Thanks for the reply, = Brian.

It looks = like the MyFaces mailing list strips off attachments.=A0 Can you send me = those files directly?

Thank = again!

-Sol=A0
=

On Jun 19, 2007, at 12:34 PM, Bryan Basham = wrote:

Hello "kind sole" ;-)
=
I have just created a converter with a tag.=A0 Nice little = exercise.

My app has lots of "names" for Boolean values, such = as
"yes" / "no" and "enabled" / "disabled" and so on.=A0 I used
= to create a separate accessor method to convert from the
Boolean to = a String, but I wanted something more generic
so I created the = BooleanConverter and BooleanConverterTag
to handle this = situation.

So, for example in a JSF page you might want to = display a
boolean value as yes/no:

<p>
Did the = student fill in the form?
<h:outputText = value=3D'#{bean.formFilledIn}'>=A0 // the bean isFormFilledIn() = method must return boolean
=A0 <my:booleanConverter type=3D'YES/NO'= />
</h:outputText>
</p>

I have attached = my classes for this converter and tag.
Don't forget that you need to = declare this tag in your own TLD file.

HTH,
Bryan


=
kindsol wrote:
I want to create a custom converter as it's own tag (e.g. = not using <f:converter> tag.=A0 Is there any documentation online = or elsewhere that you can point me to?

thanks in = advance!

-Sol


=

package = org.stillsecure.cobia.web.converters.tags;

import = org.stillsecure.cobia.web.converters.BooleanConv= erter;

import javax.faces.convert.Converter;
import javax.faces.webapp.ConverterTag;
import javax.servlet.jsp.JspException;

public = class BooleanConverterTag extends ConverterTag
{
=A0 =A0 public = BooleanConverterTag()
=A0 =A0 {
=A0 =A0 =A0 =A0 = super();
=A0 =A0 =A0 =A0 = setConverterId(BooleanConverter.CONVERTER_ID);
=A0 =A0 = }

=A0 =A0 = private String type;

=A0 =A0 public void = setType(String type) throws JspException
=A0 =A0 {
=A0 =A0 =A0 =A0 = // Check that this type is a valid Boolean type name
=A0 =A0 =A0 =A0 = if ( type =3D=3D null )
=A0 =A0 =A0 =A0 =A0 =A0 throw new = JspException(NULL_TYPE_ERROR);
=A0 =A0 =A0 =A0 if ( ! = BooleanConverter.isValidType(type) )
=A0 =A0 =A0 =A0 =A0 =A0 throw new = JspException(String.format(NOT_A_KNOWN_TYPE_ERROR, type));

=A0 =A0 =A0 =A0 this.type =3D = type;
=A0 =A0 }
=A0 =A0 = private static final String NULL_TYPE_ERROR
=A0 =A0 =A0 =A0 = =3D "The 'type' attribute value must not be null.";
=A0 =A0 = private static final String NOT_A_KNOWN_TYPE_ERROR
=A0 =A0 =A0 =A0 = =3D "The 'type' attribute value (%s) is not a registered Boolean = type.";

=A0 =A0 = //
=A0 =A0 // JSF methods
=A0 =A0 = //

=A0 =A0 = @Override
=A0 =A0 protected Converter = createConverter() throws JspException
=A0 =A0 {
=A0 =A0 =A0 =A0 = BooleanConverter converter =3D (BooleanConverter) = super.createConverter();
=A0 =A0 =A0 =A0 = converter.setType(this.type);
=A0 =A0 =A0 =A0 return = converter;
=A0 =A0 }
}
package = org.stillsecure.cobia.web.converters;

import = org.stillsecure.cobia.web.util.WebContext;

import = java.util.HashMap;
import = java.util.Map;

import = javax.faces.application.FacesMessage;
import = javax.faces.component.StateHolder;
import = javax.faces.component.UIComponent;
import = javax.faces.context.FacesContext;
import = javax.faces.convert.Converter;
import = javax.faces.convert.ConverterException;

/**
=A0* = This class implements a JSF converter to convert from boolean = values
=A0* to specialized text strings, = such as yes/no or enabled/disabled.
=A0*/
public class BooleanConverter implements Converter, = StateHolder
{
=A0 =A0 /** The JSF id for this = converter.=A0 Must be the = same as defined in the framework-faces-config.xml file. */
=A0 =A0 = public final static String CONVERTER_ID =3D = "converter.Boolean";
=A0 =A0 //
=A0 =A0 = // Registering Boolean "types"
=A0 =A0 //

=A0 =A0 public static void = registerType(String type, String trueName, String falseName)
=A0 =A0 = {
=A0 =A0 =A0 =A0 = BOOLEAN_NAME_PAIRS_MAP.put(type, new String[] = {trueName.toLowerCase(), falseName.toLowerCase()});
=A0 =A0 = }
=A0 =A0 public static boolean = isValidType(String type)
=A0 =A0 {
=A0 =A0 =A0 =A0 = return BOOLEAN_NAME_PAIRS_MAP.containsKey(type);
=A0 =A0 = }
=A0 =A0 public static String[] = getType(String type)
=A0 =A0 {
=A0 =A0 =A0 =A0 = return BOOLEAN_NAME_PAIRS_MAP.get(type);
=A0 =A0 = }
=A0 =A0 private static final = Map<String, String[]> BOOLEAN_NAME_PAIRS_MAP =3D new = HashMap<String, String[]>();

=A0 =A0 //
=A0 =A0 = // Default Boolean name types
=A0 =A0 //

=A0 =A0 public static final = String STANDARD =3D "STANDARD";
=A0 =A0 public static final = String YES_NO =3D "YES/NO";
=A0 =A0 public static final = String ENABLE =3D "OP-MODE";
=A0 =A0 public static final = String UP_DOWN =3D "UP/DOWN";

=A0 =A0 static
=A0 =A0 = {
=A0 =A0 =A0 =A0 = registerType(STANDARD, "true", "false");
=A0 =A0 =A0 =A0 = registerType(YES_NO, "yes", "no");
=A0 =A0 =A0 =A0 = registerType(ENABLE, "enabled", "disabled");
=A0 =A0 =A0 =A0 = registerType(UP_DOWN, "up", "down");
=A0 =A0 }

=A0 =A0 //
=A0 =A0 = // Attributes
=A0 =A0 //

=A0 =A0 private String = type;

=A0 =A0 = /**
=A0=A0 =A0 * Sets the type of = Boolean conversion.
=A0=A0 =A0 *=A0
=A0=A0 =A0 * @param type must be = a registered Boolean type name
=A0=A0 =A0 */
=A0 =A0 = public void setType(String type)
=A0 =A0 {
=A0 =A0 =A0 =A0 = this.type =3D type;
=A0 =A0 }

=A0 =A0 public void = checkType(String type)
=A0 =A0 {
=A0 =A0 =A0 =A0 = // Check converter state
=A0 =A0 =A0 =A0 if ( type =3D=3D = null )
=A0 =A0 =A0 =A0 =A0 =A0 throw new = ConverterException(WebContext.createFacesMessage(NO_TYPE_ERROR));
=A0 =A0 =A0 =A0 = if ( ! isValidType(type) )
=A0 =A0 =A0 =A0 =A0 =A0 throw new = ConverterException(WebContext.createFacesMessage(BAD_TYPE_ERROR, = this.type));
=A0 =A0 }
=A0 =A0 = private static final String NO_TYPE_ERROR =3D = "BASE_BooleanConverter_noTypeError";
=A0 =A0 private static final = String BAD_TYPE_ERROR =3D = "BASE_BooleanConverter_badTypeError";

=A0 =A0 //
=A0 =A0 = // JSF methods
=A0 =A0 //

=A0 =A0 /**
=A0=A0 =A0 = * This method converts from the string representation to a = Boolean object.
=A0=A0 =A0 */
=A0 =A0 = public Object getAsObject(FacesContext ctx, UIComponent comp, = String displayString) throws ConverterException
=A0 =A0 = {
=A0 =A0 =A0 =A0 = checkType(this.type);

=A0 =A0 =A0 =A0 Boolean result =3D = null;

=A0 =A0 =A0 =A0 = String[] pair =3D getType(this.type);
=A0 =A0 =A0 =A0 = if ( pair[0].equalsIgnoreCase(displayString) )
=A0 =A0 =A0 =A0 = {
=A0 =A0 =A0 =A0 =A0 =A0 result =3D = Boolean.TRUE;
=A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 = else if ( pair[1].equalsIgnoreCase(displayString) )
=A0 =A0 =A0 =A0 = {
=A0 =A0 =A0 =A0 =A0 =A0 result =3D = Boolean.FALSE;
=A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 = else
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 = =A0 =A0 FacesMessage message
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =3D= WebContext.createFacesMessage(BOOLEAN_CONVERTER_TO_OBJECT_FAILED, = displayString);
=A0 =A0 =A0 =A0 =A0 =A0 throw new = ConverterException(message);
=A0 =A0 =A0 =A0 }

=A0 =A0 =A0 =A0 return = result;
=A0 =A0 }
=A0 =A0 = private static final String BOOLEAN_CONVERTER_TO_OBJECT_FAILED =3D = "BASE_BooleanConverter_getAsObjectFailed";

=A0 =A0 /**
=A0=A0 =A0 = * This method converts from the Boolean object to the string = representation.
=A0=A0 =A0 */
=A0 =A0 = public String getAsString(FacesContext ctx, UIComponent comp, = Object object) throws ConverterException
=A0 =A0 {
=A0 =A0 =A0 =A0 = checkType(this.type);

=A0 =A0 =A0 =A0 String result =3D = null;

=A0 =A0 =A0 =A0 = String[] pair =3D getType(this.type);
=A0 =A0 =A0 =A0 = try
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 = =A0 =A0 Boolean o =3D (Boolean) object;
=A0 =A0 =A0 =A0 = =A0 =A0 if ( o =3D=3D null )
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = result =3D "";
=A0 =A0 =A0 =A0 =A0 =A0 else if ( = o.equals(Boolean.TRUE) )
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = result =3D pair[0];
=A0 =A0 =A0 =A0 =A0 =A0 else if ( = o.equals(Boolean.FALSE) )
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = result =3D pair[1];
=A0 =A0 =A0 =A0 =A0 =A0 = else
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = assert false : "Unknown object" + o;
=A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 = catch (ClassCastException e)
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 = =A0 =A0 assert false : e;
=A0 =A0 =A0 =A0 }

=A0 =A0 =A0 =A0 return = result;
=A0 =A0 }

=A0 =A0 //
=A0 =A0 = // StateHolder methods
=A0 =A0 //

=A0 =A0 private boolean = transientFlag;

=A0 =A0 = public void setTransient(boolean transientValue)
=A0 =A0 = {
=A0 =A0 =A0 =A0 = this.transientFlag =3D transientValue;
=A0 =A0 = }

=A0 =A0 = public boolean isTransient()
=A0 =A0 {
=A0 =A0 =A0 =A0 = return transientFlag;
=A0 =A0 }

=A0 =A0 public Object = saveState(FacesContext context)
=A0 =A0 {
=A0 =A0 =A0 =A0 = Object[] values =3D new Object[1];
=A0 =A0 =A0 =A0 values[0] =3D = this.type;
=A0 =A0 =A0 =A0 return = values;
=A0 =A0 }

=A0 =A0 public void = restoreState(FacesContext context, Object state)
=A0 =A0 = {
=A0 =A0 =A0 =A0 Object[] values =3D= (Object[]) state;
=A0 =A0 =A0 =A0 this.type =3D = (String) values[0];
=A0 =A0 }

}
=

= --Apple-Mail-4--33331416--