You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/10/26 08:50:34 UTC
svn commit: r588542 - in /velocity/tools/branches/2.x/src:
main/java/org/apache/velocity/tools/generic/DisplayTool.java
test/java/org/apache/velocity/tools/DisplayToolTests.java
Author: nbubna
Date: Thu Oct 25 23:50:31 2007
New Revision: 588542
URL: http://svn.apache.org/viewvc?rev=588542&view=rev
Log:
add cell(...) and space(int) methods, tests and misc fixes to DisplayTool
Added:
velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java (with props)
Modified:
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java?rev=588542&r1=588541&r2=588542&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java Thu Oct 25 23:50:31 2007
@@ -30,8 +30,10 @@
* Provides general utility methods for controlling the display of references.
* Currently, this class contains methods for "pretty printing" an array or
* {@link Collection}, methods for truncating the string value of a reference
- * at a configured or specified length, and methods for displaying an alternate
- * value when a specified value is null.
+ * at a configured or specified length, methods for displaying an alternate
+ * value when a specified value is null, a method for generating whitespace, and
+ * methods for forcing values into "cells" of equal size (via truncation or
+ * padding with whitespace).
*
* <p><b>Example Use:</b>
* <pre>
@@ -68,14 +70,18 @@
{
public static final String LIST_DELIM_KEY = "listDelim";
public static final String LIST_FINAL_DELIM_KEY = "listFinalDelim";
- public static final String TRUNCATE_MAX_LENGTH_KEY = "truncateMaxLength";
+ public static final String TRUNCATE_LENGTH_KEY = "truncateLength";
public static final String TRUNCATE_SUFFIX_KEY = "truncateSuffix";
+ public static final String CELL_LENGTH_KEY = "cellLength";
+ public static final String CELL_SUFFIX_KEY = "cellSuffix";
public static final String DEFAULT_ALTERNATE_KEY = "defaultAlternate";
private String defaultDelim = ", ";
private String defaultFinalDelim = " and ";
- private int defaultMaxLength = 30;
- private String defaultSuffix = "...";
+ private int defaultTruncateLength = 30;
+ private String defaultTruncateSuffix = "...";
+ private int defaultCellLength = 30;
+ private String defaultCellSuffix = "...";
private String defaultAlternate = "null";
/**
@@ -98,10 +104,10 @@
setListFinalDelimiter(listFinalDelim);
}
- Integer truncateMaxLength = values.getInteger(TRUNCATE_MAX_LENGTH_KEY);
- if (truncateMaxLength != null)
+ Integer truncateLength = values.getInteger(TRUNCATE_LENGTH_KEY);
+ if (truncateLength != null)
{
- setTruncateMaxLength(truncateMaxLength);
+ setTruncateLength(truncateLength);
}
String truncateSuffix = values.getString(TRUNCATE_SUFFIX_KEY);
@@ -110,6 +116,18 @@
setTruncateSuffix(truncateSuffix);
}
+ Integer cellLength = values.getInteger(CELL_LENGTH_KEY);
+ if (cellLength != null)
+ {
+ setCellLength(cellLength);
+ }
+
+ String cellSuffix = values.getString(CELL_SUFFIX_KEY);
+ if (cellSuffix != null)
+ {
+ setCellSuffix(cellSuffix);
+ }
+
String defaultAlternate = values.getString(DEFAULT_ALTERNATE_KEY);
if (defaultAlternate != null)
{
@@ -117,24 +135,69 @@
}
}
+ public String getListDelimiter()
+ {
+ return this.defaultDelim;
+ }
+
protected void setListDelimiter(String delim)
{
this.defaultDelim = delim;
}
+ public String getListFinalDelimiter()
+ {
+ return this.defaultFinalDelim;
+ }
+
protected void setListFinalDelimiter(String finalDelim)
{
this.defaultFinalDelim = finalDelim;
}
- protected void setTruncateMaxLength(int maxlen)
+ public int getTruncateLength()
+ {
+ return this.defaultTruncateLength;
+ }
+
+ protected void setTruncateLength(int maxlen)
{
- this.defaultMaxLength = maxlen;
+ this.defaultTruncateLength = maxlen;
+ }
+
+ public String getTruncateSuffix()
+ {
+ return this.defaultTruncateSuffix;
}
protected void setTruncateSuffix(String suffix)
{
- this.defaultSuffix = suffix;
+ this.defaultTruncateSuffix = suffix;
+ }
+
+ public String getCellSuffix()
+ {
+ return this.defaultCellSuffix;
+ }
+
+ protected void setCellSuffix(String suffix)
+ {
+ this.defaultCellSuffix = suffix;
+ }
+
+ public int getCellLength()
+ {
+ return this.defaultCellLength;
+ }
+
+ protected void setCellLength(int maxlen)
+ {
+ this.defaultCellLength = maxlen;
+ }
+
+ public String getDefaultAlternate()
+ {
+ return this.defaultAlternate;
}
protected void setDefaultAlternate(String dflt)
@@ -237,7 +300,7 @@
*/
public String truncate(Object truncateMe)
{
- return truncate(truncateMe, this.defaultMaxLength);
+ return truncate(truncateMe, this.defaultTruncateLength);
}
/**
@@ -251,7 +314,7 @@
*/
public String truncate(Object truncateMe, int maxLength)
{
- return truncate(truncateMe, maxLength, this.defaultSuffix);
+ return truncate(truncateMe, maxLength, this.defaultTruncateSuffix);
}
/**
@@ -266,7 +329,7 @@
*/
public String truncate(Object truncateMe, String suffix)
{
- return truncate(truncateMe, this.defaultMaxLength, suffix);
+ return truncate(truncateMe, this.defaultTruncateLength, suffix);
}
/**
@@ -281,7 +344,7 @@
*/
public String truncate(Object truncateMe, int maxLength, String suffix)
{
- if (truncateMe == null)
+ if (truncateMe == null || maxLength <= 0)
{
return null;
}
@@ -291,7 +354,97 @@
{
return string;
}
+ if (suffix == null || maxLength - suffix.length() <= 0)
+ {
+ // either no need or no room for suffix
+ return string.substring(0, maxLength);
+ }
+ // truncate early and append suffix
return string.substring(0, maxLength - suffix.length()) + suffix;
+ }
+
+ /**
+ * Returns a string of spaces of the specified length.
+ * @param length the number of spaces to return
+ */
+ public String space(int length)
+ {
+ if (length < 0)
+ {
+ return null;
+ }
+
+ StringBuilder space = new StringBuilder();
+ for (int i=0; i < length; i++)
+ {
+ space.append(' ');
+ }
+ return space.toString();
+ }
+
+ /**
+ * Truncates or pads the string value of the specified object as necessary
+ * to ensure that the returned string's length equals the default cell size.
+ * @param obj the value to be put in the 'cell'
+ */
+ public String cell(Object obj)
+ {
+ return cell(obj, this.defaultCellLength);
+ }
+
+ /**
+ * Truncates or pads the string value of the specified object as necessary
+ * to ensure that the returned string's length equals the specified cell size.
+ * @param obj the value to be put in the 'cell'
+ * @param cellsize the size of the cell into which the object must be placed
+ */
+ public String cell(Object obj, int cellsize)
+ {
+ return cell(obj, cellsize, this.defaultCellSuffix);
+ }
+
+ /**
+ * Truncates or pads the string value of the specified object as necessary
+ * to ensure that the returned string's length equals the default cell size.
+ * If truncation is necessary, the specified suffix will replace the end of
+ * the string value to indicate that.
+ * @param obj the value to be put in the 'cell'
+ * @param suffix the suffix to put at the end of any values that need truncating
+ * to indicate that they've been truncated
+ */
+ public String cell(Object obj, String suffix)
+ {
+ return cell(obj, this.defaultCellLength, suffix);
+ }
+
+ /**
+ * Truncates or pads the string value of the specified object as necessary
+ * to ensure that the returned string's length equals the specified cell size.
+ * @param obj the value to be put in the 'cell'
+ * @param cellsize the size of the cell into which the object must be placed
+ * @param suffix the suffix to put at the end of any values that need truncating
+ * to indicate that they've been truncated
+ */
+ public String cell(Object obj, int cellsize, String suffix)
+ {
+ if (obj == null || cellsize <= 0)
+ {
+ return null;
+ }
+
+ String value = String.valueOf(obj);
+ if (value.length() == cellsize)
+ {
+ return value;
+ }
+ else if (value.length() > cellsize)
+ {
+ return truncate(value, cellsize, suffix);
+ }
+ else
+ {
+ return value + space(cellsize - value.length());
+ }
}
/**
Added: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java?rev=588542&view=auto
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java (added)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java Thu Oct 25 23:50:31 2007
@@ -0,0 +1,282 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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.
+ */
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.velocity.tools.generic.ValueParser;
+
+/**
+ * <p>Tests for DisplayTool</p>
+ *
+ * @author Nathan Bubna
+ * @since VelocityTools 2.0
+ * @version $Id$
+ */
+public class DisplayToolTests {
+
+ public @Test void methodAlt_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertSame(display.alt(null), display.getDefaultAlternate());
+ Object notnull = new Object();
+ assertSame(display.alt(notnull), notnull);
+ }
+
+ public @Test void methodAlt_ObjectObject() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ Object notnull = new Object();
+ Object result = display.alt(null, notnull);
+ assertSame(result, notnull);
+ result = display.alt(notnull, "foo");
+ assertSame(result, notnull);
+ }
+
+ public @Test void methodCapitalize_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals("FOO", display.capitalize("fOO"));
+ assertEquals("Foo", display.capitalize("Foo"));
+ assertEquals("Foo", display.capitalize("foo"));
+ assertEquals("F", display.capitalize("f"));
+ assertEquals("", display.capitalize(""));
+ assertEquals(null, display.capitalize(null));
+ }
+
+ public @Test void methodCell_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setCellLength(4);
+ assertEquals(null, display.cell(null));
+ assertEquals("foo ", display.cell("foo"));
+ assertEquals("f...", display.cell("foobar"));
+ assertEquals("foob", display.cell("foob"));
+ }
+
+ public @Test void methodCell_ObjectString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setCellLength(5);
+ assertEquals("test>", display.cell("testing", ">"));
+ }
+
+ public @Test void methodCell_Objectint() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals("testing", display.cell("testing", 7));
+ assertEquals("testing ", display.cell("testing", 8));
+ assertEquals("tes...", display.cell("testing", 6));
+ }
+
+ public @Test void methodCell_ObjectintString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals("f", display.cell("foo", 1, null));
+ assertEquals("f", display.cell("foo", 1, "bar"));
+ assertEquals("fbar", display.cell("foobar", 4, "bar"));
+ assertEquals(null, display.cell("foo", 0, "bar"));
+ assertEquals(null, display.cell("foo", -1, "bar"));
+ }
+
+ public @Test void methodConfigure_Map() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ // change the inspected type to Map
+ Map<String,Object> conf = new HashMap<String,Object>();
+ conf.put(DisplayTool.LIST_DELIM_KEY, ";");
+ conf.put(DisplayTool.LIST_FINAL_DELIM_KEY, " und ");
+ conf.put(DisplayTool.TRUNCATE_LENGTH_KEY, "5");
+ conf.put(DisplayTool.TRUNCATE_SUFFIX_KEY, ">");
+ conf.put(DisplayTool.CELL_LENGTH_KEY, "4");
+ conf.put(DisplayTool.CELL_SUFFIX_KEY, "~");
+ conf.put(DisplayTool.DEFAULT_ALTERNATE_KEY, "n/a");
+ display.configure(conf);
+ assertEquals(";", display.getListDelimiter());
+ assertEquals(" und ", display.getListFinalDelimiter());
+ assertEquals(5, display.getTruncateLength());
+ assertEquals(">", display.getTruncateSuffix());
+ assertEquals("~", display.getCellSuffix());
+ assertEquals(4, display.getCellLength());
+ assertEquals("n/a", display.getDefaultAlternate());
+
+ // ensure that configure is locked now
+ conf.put(DisplayTool.LIST_DELIM_KEY, " & ");
+ display.configure(conf);
+ assertEquals(";", display.getListDelimiter());
+ }
+
+ public @Test void methodList_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ int[] nums = new int[] { 1, 2, 3 };
+ assertEquals("1, 2 and 3", display.list(nums));
+ display.setListDelimiter(" & ");
+ assertEquals("1 & 2 and 3", display.list(nums));
+ display.setListFinalDelimiter(" & ");
+ assertEquals("1 & 2 & 3", display.list(nums));
+ }
+
+ public @Test void methodList_ObjectString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ List<Integer> nums = new ArrayList<Integer>();
+ nums.add(1);
+ nums.add(2);
+ nums.add(3);
+ assertEquals(null, display.list(null, null));
+ assertEquals("1null2null3", display.list(nums, null));
+ assertEquals("1, 2, 3", display.list(nums, ", "));
+ }
+
+ public @Test void methodList_ObjectStringString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ int[] nums = new int[] { 1, 2, 3 };
+ assertEquals(null, display.list(null, null, null));
+ assertEquals("1null2null3", display.list(nums, null, null));
+ assertEquals("1 & 2null3", display.list(nums, " & ", null));
+ assertEquals("1null2 & 3", display.list(nums, null, " & "));
+ assertEquals("123", display.list(nums, "", ""));
+ assertEquals("1; 2 und 3", display.list(nums, "; ", " und "));
+ }
+
+ public @Test void methodSetCellLength_int() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setCellLength(10);
+ assertEquals(10, display.getCellLength());
+ }
+
+ public @Test void methodSetCellSuffix_String() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setCellSuffix("foo");
+ assertEquals("foo", display.getCellSuffix());
+ }
+
+ public @Test void methodSetDefaultAlternate_String() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setDefaultAlternate("foo");
+ assertEquals("foo", display.getDefaultAlternate());
+ }
+
+ public @Test void methodSetListDelimiter_String() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setListDelimiter("foo");
+ assertEquals("foo", display.getListDelimiter());
+ }
+
+ public @Test void methodSetListFinalDelimiter_String() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setListFinalDelimiter("foo");
+ assertEquals("foo", display.getListFinalDelimiter());
+ }
+
+ public @Test void methodSetTruncateLength_int() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setTruncateLength(5);
+ assertEquals(5, display.getTruncateLength());
+ }
+
+ public @Test void methodSetTruncateSuffix_String() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setTruncateSuffix("foo");
+ assertEquals("foo", display.getTruncateSuffix());
+ }
+
+ public @Test void methodSpace_int() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals(null, display.space(-1));
+ assertEquals("", display.space(0));
+ assertEquals(" ", display.space(1));
+ assertEquals(" ", display.space(5));
+ }
+
+ public @Test void methodTruncate_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setTruncateLength(4);
+ assertEquals(null, display.truncate(null));
+ assertEquals("f...", display.truncate("foobar"));
+ assertEquals("foob", display.truncate("foob"));
+ assertEquals("foo", display.truncate("foo"));
+ }
+
+ public @Test void methodTruncate_ObjectString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ display.setTruncateLength(4);
+ assertEquals(null, display.truncate(null, ">"));
+ assertEquals("foo>", display.truncate("foobar", ">"));
+ assertEquals("foob", display.truncate("foobar", null));
+ assertEquals("foob", display.truncate("foobar", "woogie"));
+ assertEquals("foo", display.truncate("foo", ">"));
+ }
+
+ public @Test void methodTruncate_Objectint() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals(null, display.truncate(null, 1));
+ assertEquals(null, display.truncate("foobar", -1));
+ assertEquals(null, display.truncate("foobar", 0));
+ assertEquals("f", display.truncate("foobar", 1));
+ assertEquals("fo", display.truncate("foobar", 2));
+ assertEquals("foo", display.truncate("foobar", 3));
+ assertEquals("f...", display.truncate("foobar", 4));
+ assertEquals("fo...", display.truncate("foobar", 5));
+ assertEquals("foobar", display.truncate("foobar", 6));
+ assertEquals("foobar", display.truncate("foobar", 7));
+ }
+
+ public @Test void methodTruncate_ObjectintString() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals(null, display.truncate(null, 0, null));
+ assertEquals(null, display.truncate("foo", 0, null));
+ assertEquals("f", display.truncate("foo", 1, null));
+ assertEquals("foob", display.truncate("foobar", 4, null));
+ assertEquals("foo>", display.truncate("foobar", 4, ">"));
+ }
+
+ public @Test void methodUncapitalize_Object() throws Exception
+ {
+ DisplayTool display = new DisplayTool();
+ assertEquals(null, display.uncapitalize(null));
+ assertEquals("", display.uncapitalize(""));
+ assertEquals("test", display.uncapitalize("test"));
+ assertEquals("test", display.uncapitalize("Test"));
+ assertEquals("tEST", display.uncapitalize("TEST"));
+ }
+
+
+}
+
Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
------------------------------------------------------------------------------
svn:mime-type = text/plain