shale-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gvanma...@apache.org
Subject svn commit: r472368 - in /shale/framework/trunk/shale-clay/src: main/java/org/apache/shale/clay/convert/ main/resources/META-INF/ main/resources/org/apache/shale/clay/ test/java/org/apache/shale/clay/convert/
Date Wed, 08 Nov 2006 02:45:14 GMT
Author: gvanmatre
Date: Tue Nov  7 18:45:13 2006
New Revision: 472368

URL: http://svn.apache.org/viewvc?view=rev&rev=472368
Log:
Added a JSF StringArrayConverter to support the partialTriggers component properties (SHALE-326).
 The new converter will allow a space delimited list to be converted into a string array and
vise versa.

Added:
    shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
  (with props)
    shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/
    shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
  (with props)
Modified:
    shale/framework/trunk/shale-clay/src/main/resources/META-INF/faces-config.xml
    shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/Bundle.properties

Added: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java?view=auto&rev=472368
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
(added)
+++ shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
Tue Nov  7 18:45:13 2006
@@ -0,0 +1,172 @@
+/*
+ * 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.shale.clay.convert;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+import org.apache.shale.util.Messages;
+
+/**
+ * <p>Converts a <code>String</code> to a <code>String[]</code>
or
+ * vise versa.  This converter is targeted at supporting the
+ * myfaces trinidad partialTriggers component properties.
+ * The default element <code>delimiter</code> is none and the default
+ * element <code>separator</code> is a space.</p>
+ */
+public class StringArrayConverter implements Converter {
+
+    /**
+     * <p>
+     * Message resources for this class.
+     * </p>
+     */
+    private static Messages messages = new Messages(
+            "org.apache.shale.clay.Bundle", StringArrayConverter.class
+                    .getClassLoader());
+
+    /**
+     * <p>Begining and ending element delimiter.  The default
+     * is <code>null</code> meaning no delimiter.</p>
+     */
+    private Character delimiter = null;
+
+    /**
+     * <p>The element seperator.  The default is a space.</p>
+     */
+    private Character separator = Character.valueOf(' ');
+
+    /**
+     * @return the element begining and ending delimiter
+     */
+    public Character getDelimiter() {
+        return delimiter;
+    }
+
+    /**
+     * @param delimiter element begining and ending delimiter char
+     */
+    public void setDelimiter(Character delimiter) {
+        this.delimiter = delimiter;
+    }
+
+    /**
+     * @return the character used to delimit elements
+     */
+    public Character getSeparator() {
+        return separator;
+    }
+
+    /**
+     * @param separator the character used to delimit elements
+     */
+    public void setSeparator(Character separator) {
+        this.separator = separator;
+    }
+
+
+    /**
+     * <p>Converts the <code>value</code> into a string array using
+     * the element <code>delimiter</code> and element <code>separator</code>.</p>
+     *
+     * @param facesContext faces context
+     * @param component value holder component
+     * @param value source value to converter to a String[]
+     * @return the target type is a <code>String[]</code>
+     */
+    public Object getAsObject(FacesContext facesContext, UIComponent component, String value)
{
+
+        List elements = new ArrayList();
+        StringBuffer buff = new StringBuffer(value != null ? value : "");
+        boolean isBlockOn = false;
+        for (int i = buff.length() - 1; i > -1; i--) {
+            if (delimiter != null
+                    && buff.charAt(i) == delimiter.charValue()) {
+
+                if (!isBlockOn) {
+                    buff.deleteCharAt(i);
+                } else {
+                    elements.add(0, buff.substring(i + 1));
+                    buff.delete(i, buff.length());
+                }
+                isBlockOn = !isBlockOn;
+            } else if (separator != null
+                    && buff.charAt(i) == separator.charValue()
+                    && !isBlockOn) {
+
+                if (i + 1 < buff.length()) {
+                   elements.add(0, buff.substring(i + 1));
+                }
+                buff.delete(i, buff.length());
+            }
+        }
+
+        if (buff.length() > 0) {
+            elements.add(0, buff.toString());
+        }
+
+        String[] values = new String[elements.size()];
+        elements.toArray(values);
+
+        return values;
+    }
+
+    /**
+     * <p>Converts a string array into a value delimited string.  The <code>delimiter</code>
+     * and <code>separator</code> properties are used to distinguish each element.</p>
+     *
+     * @param facesContext faces context
+     * @param component value owning component
+     * @param value source String array that is converter into a tokenized String
+     * @return delimited string holding all values of the source string array
+     */
+    public String getAsString(FacesContext facesContext, UIComponent component,
+            Object value) {
+
+        if (value == null) {
+            return "";
+        }
+
+        if (value instanceof String) {
+            return (String) value;
+        }
+
+        if (!(value instanceof String[])) {
+            throw new ConverterException(messages
+                    .getMessage("string.array.converter.invalid.type"));
+        }
+        StringBuffer buff = new StringBuffer();
+        String[] values = (String[]) value;
+        for (int i = 0; i < values.length; i++) {
+            if (i > 0) {
+                buff.append(separator);
+            }
+
+            buff.append(delimiter != null ? delimiter.toString() : "")
+                 .append(values[i])
+                 .append(delimiter != null ? delimiter.toString() : "");
+        }
+        return buff.toString();
+
+    }
+
+}

Propchange: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/convert/StringArrayConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: shale/framework/trunk/shale-clay/src/main/resources/META-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/resources/META-INF/faces-config.xml?view=diff&rev=472368&r1=472367&r2=472368
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/resources/META-INF/faces-config.xml (original)
+++ shale/framework/trunk/shale-clay/src/main/resources/META-INF/faces-config.xml Tue Nov
 7 18:45:13 2006
@@ -94,4 +94,16 @@
 		</converter-class>
 	</converter>
 
+	<converter>
+		<description>
+			This converter will be used by the
+			org.apache.shale.util.ConverterHelper to populate the target
+			object properties.  It converts a String[] to a String.
+		</description>
+		<converter-for-class>[Ljava.lang.String;</converter-for-class>
+		<converter-class>
+			org.apache.shale.clay.convert.StringArrayConverter
+		</converter-class>
+	</converter>
+
 </faces-config>

Modified: shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/Bundle.properties
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/Bundle.properties?view=diff&rev=472368&r1=472367&r2=472368
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/Bundle.properties
(original)
+++ shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/Bundle.properties
Tue Nov  7 18:45:13 2006
@@ -159,4 +159,7 @@
 clayparent.notfound=The symbol tag must be nested under a clay tag.
 
 #org.apache.shale.clay.utils.TldParserUtil
-tag.load.error=Unable to load tag handler "{0}" with the class name of "{1}".
\ No newline at end of file
+tag.load.error=Unable to load tag handler "{0}" with the class name of "{1}".
+
+#org.apache.shale.clay.convert.StringArrayConverter
+string.array.converter.invalid.type=Value is not a String[]
\ No newline at end of file

Added: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java?view=auto&rev=472368
==============================================================================
--- shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
(added)
+++ shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
Tue Nov  7 18:45:13 2006
@@ -0,0 +1,92 @@
+/*
+ * 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.shale.clay.convert;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.html.HtmlInputText;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.shale.test.base.AbstractViewControllerTestCase;
+import org.apache.shale.util.ConverterHelper;
+
+public class StringArrayConverterTestCase extends
+        AbstractViewControllerTestCase {
+
+    // Construct a new instance of this test case.
+    public StringArrayConverterTestCase(String name) {
+        super(name);
+    }
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+        return (new TestSuite(StringArrayConverterTestCase.class));
+    }
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        Class clazz = Class.forName("[Ljava.lang.String;");
+        
+        facesContext.getApplication().addConverter(clazz,
+                 "org.apache.shale.clay.convert.StringArrayConverter");
+    }
+
+    // test how Clay would use it to convert a string property into a target String[]
+    public void testDefaults() {
+        ConverterHelper helper = new ConverterHelper();
+        String[] partialTriggers = {"one", "two", "three"};
+        String asString = helper.asString(facesContext, String[].class, partialTriggers);
+        
+        assertNotNull(asString);
+        assertEquals("one two three", asString);
+        
+        String[] asObject = (String[]) helper.asObject(facesContext, String[].class, asString);
+        assertNotNull(asObject);
+        
+        assertEquals(partialTriggers .length, asObject.length);
+        for (int i = 0; i < partialTriggers .length; i++) {
+            assertEquals("item " + i + ":", partialTriggers[i], asObject[i]);
+        }
+        
+    }
+    
+    public void testCustom() {
+        StringArrayConverter converter = (StringArrayConverter) facesContext.getApplication()
+                                                      .createConverter(String[].class);
+        converter.setDelimiter(Character.valueOf('"'));
+        converter.setSeparator(Character.valueOf(','));
+        
+        UIComponent dummy = new HtmlInputText();
+        
+        String[] partialTriggers  = {"one", "two", "three"};
+        String asString = converter.getAsString(facesContext, dummy, partialTriggers);
+        
+        assertNotNull(asString);
+        assertEquals("\"one\",\"two\",\"three\"", asString);
+        
+        String[] asObject = (String[]) converter.getAsObject(facesContext, dummy, asString);
+        assertNotNull(asObject);
+        
+        assertEquals(partialTriggers.length, asObject.length);
+        for (int i = 0; i < partialTriggers.length; i++) {
+            assertEquals("item " + i + ":", partialTriggers[i], asObject[i]);
+        }
+
+    }
+
+}

Propchange: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/convert/StringArrayConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



Mime
View raw message