You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mk...@apache.org on 2006/05/22 22:30:41 UTC

svn commit: r408756 - in /myfaces/tomahawk/trunk/sandbox/core/src/main: java/org/apache/myfaces/custom/convertStringUtils/ resources-facesconfig/META-INF/ tld/

Author: mkienenb
Date: Mon May 22 13:30:40 2006
New Revision: 408756

URL: http://svn.apache.org/viewvc?rev=408756&view=rev
Log:
Fix for TOMAHAWK-420 -- new StringUtilsConverter.

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java   (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java   (with props)
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java?rev=408756&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java Mon May 22 13:30:40 2006
@@ -0,0 +1,156 @@
+package org.apache.myfaces.custom.convertStringUtils;
+
+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;
+
+import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.WordUtils;
+
+/**
+ * Provides runtime modification of a string. Uses Apache Lang StringUtils and WordUtils
+ * to peform operations.
+ * <p>
+ * Example:
+ * <code>
+ * <h:outputText value="#{backingBean.customer.name}">
+ *     <t:convertStringUtils format="capitalize" trim="true" maxLength="50"/>
+ * </h:outputText>
+ * </code>
+ * <p>
+ * @author Julian Ray
+ */
+public class StringUtilsConverter implements Converter, StateHolder {
+    public static final String CONVERTER_ID = org.apache.myfaces.custom.convertStringUtils.StringUtilsConverter.class.getName();
+
+    protected boolean _transient;
+    
+    protected String format = null;
+    protected Boolean trim = null;
+    protected Integer maxLength = null;
+    protected Boolean appendEllipsesDuringOutput = null;
+    protected Boolean appendEllipsesDuringInput = null;
+	
+	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
+		return null == value ? null : format(value.toString(), false);
+	}
+
+	public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
+		return value == null ? "" : format(value.toString(), true);
+	}
+
+	private String format(String val, boolean duringOutput) throws ConverterException {
+
+		String str;
+		if (BooleanUtils.isTrue(trim)) {
+			str = val.trim();
+		} else {
+			str = val;
+		}
+		// Any decorations first
+		if (StringUtils.isNotEmpty(format)) {
+			if ("uppercase".equalsIgnoreCase(format)) {
+				str = StringUtils.upperCase(str);
+			} else if ("lowercase".equalsIgnoreCase(format)) {
+				str = StringUtils.lowerCase(str);
+			} else if ("capitalize".equalsIgnoreCase(format)) {
+				str = WordUtils.capitalizeFully(str);
+			} else {
+				throw new ConverterException("Invalid format '" + format + "'");
+			}
+		}
+        
+        boolean appendEllipses = 
+            ((duringOutput)
+                && ((null != appendEllipsesDuringOutput)
+                && (appendEllipsesDuringOutput.booleanValue())))
+          || ((false == duringOutput)
+                && ((null != appendEllipsesDuringInput)
+                && (appendEllipsesDuringInput.booleanValue()))) ;
+        
+        if (appendEllipses)
+        {
+            // See if we need to abbreviate/truncate this string
+            if (null != maxLength && maxLength.intValue() > 4) {
+                str = StringUtils.abbreviate(str, maxLength.intValue());
+            }
+        }
+        else
+        {
+            // See if we need to truncate this string
+            if (null != maxLength) {
+                str = str.substring(0, maxLength.intValue());
+            }
+        }
+		return str;
+	}
+    public void restoreState(FacesContext context, Object state) {
+        Object values[] = (Object[]) state;
+        this.format = (String) values[0];
+        this.trim = (Boolean) values[1];
+        this.maxLength = (Integer) values[2];
+        this.appendEllipsesDuringOutput = (Boolean) values[3];
+        this.appendEllipsesDuringInput = (Boolean) values[4];
+    }
+
+    public Object saveState(FacesContext context) {
+        Object[] values = new Object[5];
+        values[0] = this.format;
+        values[1] = this.trim;
+        values[2] = this.maxLength;
+        values[3] = this.appendEllipsesDuringOutput;
+        values[4] = this.appendEllipsesDuringInput;
+        return values;
+    }
+    
+    public boolean isTransient() {
+        return _transient;
+    }
+
+    public void setTransient(boolean _transient) {
+        this._transient = _transient;
+    }
+
+    public String getFormat() {
+		return format;
+	}
+
+	public void setFormat(String format) {
+		this.format = format;
+	}
+
+    public Integer getMaxLength() {
+        return maxLength;
+    }
+
+	public void setMaxLength(Integer maxLength) {
+		this.maxLength = maxLength;
+	}
+
+    public Boolean isAppendEllipsesDuringOutput() {
+        return appendEllipsesDuringOutput;
+    }
+
+    public void setAppendEllipsesDuringOutput(Boolean appendEllipsesDuringOutput) {
+        this.appendEllipsesDuringOutput = appendEllipsesDuringOutput;
+    }
+
+    public Boolean isAppendEllipsesDuringInput() {
+        return appendEllipsesDuringInput;
+    }
+
+    public void setAppendEllipsesDuringInput(Boolean appendEllipsesDuringInput) {
+        this.appendEllipsesDuringInput = appendEllipsesDuringInput;
+    }
+
+	public Boolean getTrim() {
+		return trim;
+	}
+
+	public void setTrim(Boolean trim) {
+		this.trim = trim;
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java?rev=408756&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java Mon May 22 13:30:40 2006
@@ -0,0 +1,159 @@
+package org.apache.myfaces.custom.convertStringUtils;
+
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.el.ValueBinding;
+import javax.faces.webapp.ConverterTag;
+import javax.faces.webapp.UIComponentTag;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
+
+/**
+ * A tag that invokes the {@link StringUtilsConverter} and lets the developer
+ * specify how to convert a string.
+ * <p>
+ * Example:
+ * <code>
+ * <h:outputText value="#{backingBean.customer.name}">
+ *     <t:caseConverter format="capitalize" trim="true" maxLength="50"/>
+ * </h:outputText>
+ * </code>
+ * <p>
+ * @author Julian ray
+ */
+public class StringUtilsConverterTag extends ConverterTag {
+	
+    private static final long serialVersionUID = 9006143486961806695L;
+
+    private String format;
+	private String maxLength;
+	private String trim;
+    private String appendEllipsesDuringInput;
+    private String appendEllipsesDuringOutput;
+    
+	public StringUtilsConverterTag() {
+		setConverterId(StringUtilsConverter.CONVERTER_ID);		
+	}
+	
+	public void release() {
+		format = null;
+		maxLength = null;
+        trim = null;
+        appendEllipsesDuringInput = null;
+        appendEllipsesDuringOutput = null;
+	}
+    public void setPageContext(PageContext context) {
+        super.setPageContext(context);
+        setConverterId(StringUtilsConverter.CONVERTER_ID);
+    }
+
+    protected Converter createConverter() throws JspException {
+        StringUtilsConverter converter = (StringUtilsConverter) super.createConverter();
+
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        setConverterFormatValue(facesContext, converter, this.format);
+        setConverterMaxLengthValue(facesContext, converter, this.maxLength);
+        setConverterTrimValue(facesContext, converter, this.trim);
+        setConverterAppendEllipsesDuringInputValue(facesContext, converter, this.appendEllipsesDuringInput);
+        setConverterAppendEllipsesDuringOutputValue(facesContext, converter, this.appendEllipsesDuringOutput);
+
+        return converter;
+    }
+    private static void setConverterFormatValue(FacesContext facesContext, StringUtilsConverter converter, String value) {
+        if (null == value) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setFormat((String) vb.getValue(facesContext));
+        } else {
+            converter.setFormat(value);
+        }
+    }
+
+    private static void setConverterMaxLengthValue(FacesContext facesContext, StringUtilsConverter converter, String value) {
+        if (null == value) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setMaxLength((Integer) vb.getValue(facesContext));
+        } else {
+            converter.setMaxLength(Integer.valueOf(value));
+        }
+    }
+    private static void setConverterTrimValue(FacesContext facesContext, StringUtilsConverter converter, String value) {
+        if (null == value) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setTrim((Boolean) vb.getValue(facesContext));
+        } else {
+            converter.setTrim(Boolean.valueOf(value));
+        }
+    }
+    private static void setConverterAppendEllipsesDuringInputValue(FacesContext facesContext, StringUtilsConverter converter, String value) {
+        if (null == value) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setAppendEllipsesDuringInput((Boolean) vb.getValue(facesContext));
+        } else {
+            converter.setAppendEllipsesDuringInput(Boolean.valueOf(value));
+        }
+    }
+    private static void setConverterAppendEllipsesDuringOutputValue(FacesContext facesContext, StringUtilsConverter converter, String value) {
+        if (null == value) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setAppendEllipsesDuringOutput((Boolean) vb.getValue(facesContext));
+        } else {
+            converter.setAppendEllipsesDuringOutput(Boolean.valueOf(value));
+        }
+    }
+		
+	public String getFormat() {
+		return format;
+	}
+
+	public void setFormat(String format) {
+		this.format = format;
+	}
+
+	public String getMaxLength() {
+		return maxLength;
+	}
+
+	public void setMaxLength(String maxLength) {
+		this.maxLength = maxLength;
+	}
+
+	public String getTrim() {
+		return trim;
+	}
+
+	public void setTrim(String trim) {
+		this.trim = trim;
+	}
+
+    public String getAppendEllipsesDuringInput() {
+        return appendEllipsesDuringInput;
+    }
+
+    public void setAppendEllipsesDuringInput(String appendEllipsesDuringInput) {
+        this.appendEllipsesDuringInput = appendEllipsesDuringInput;
+    }
+
+    public String getAppendEllipsesDuringOutput() {
+        return appendEllipsesDuringOutput;
+    }
+
+    public void setAppendEllipsesDuringOutput(String appendEllipsesDuringOutput) {
+        this.appendEllipsesDuringOutput = appendEllipsesDuringOutput;
+    }
+
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/convertStringUtils/StringUtilsConverterTag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml?rev=408756&r1=408755&r2=408756&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml Mon May 22 13:30:40 2006
@@ -197,6 +197,11 @@
   </converter>
 
   <converter>
+    <converter-id>org.apache.myfaces.custom.convertStringUtils.StringUtilsConverter</converter-id>
+    <converter-class>org.apache.myfaces.custom.convertStringUtils.StringUtilsConverter</converter-class>
+  </converter> 
+	
+  <converter>
      <converter-id>org.apache.myfaces.custom.convertDateTime.DateTimeConverter</converter-id>
      <converter-class>org.apache.myfaces.custom.convertDateTime.DateTimeConverter</converter-class>
   </converter>

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld?rev=408756&r1=408755&r2=408756&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld Mon May 22 13:30:40 2006
@@ -461,6 +461,39 @@
 	</tag>
 
 	<tag>
+		<name>convertStringUtils</name>
+		<tag-class>org.apache.myfaces.custom.convertStringUtils.StringUtilsConverter</tag-class>
+		<display-name>StringUtils Converter</display-name>
+		<description>Converts the format of a string</description> 
+
+		<attribute>
+			<name>appendEllipsesDuringInput</name>
+			<required>false</required>
+			<description>Boolean value determining if data should be truncated with ellipses during input conversion. Default = false</description>
+		</attribute> 
+		<attribute>
+			<name>appendEllipsesDuringOutput</name>
+			<required>false</required>
+			<description>Boolean value determining if data should be truncated with ellipses during output conversion. Default = false</description>
+		</attribute> 
+		<attribute>
+			<name>format</name>
+			<required>false</required>
+			<description>Specifies the output case of the string. One of uppercase | lowercase | capitalize</description>
+		</attribute>
+		<attribute>
+			<name>maxLength</name>
+			<required>false</required>
+			<description>Integer value for the maximum length of the rendered string.  Strings longer than maxValue will be truncated at (maxValue - 3) and an ellipsis '...' will be appended.</description>
+		</attribute>
+		<attribute>
+			<name>trim</name>
+			<required>false</required>
+			<description>Boolean value determining is the string should be trimmed before any other formatting takes place. Default = false</description>
+		</attribute> 
+	</tag>
+
+	<tag>
 		<name>convertDateTime</name>
 		<tag-class>org.apache.myfaces.custom.convertDateTime.ConvertDateTimeTag</tag-class>
 		<display-name>DateTime Converter</display-name>