From 728159dc4877a2c56fe8f0e7487d9ee5ba938d4e Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 19 Jun 2010 18:05:23 +0000 Subject: [PATCH] Replace the CommaSeparatedString transformer with an escaping TabSeparatedString transformer. Maybe I should just use StringUtil methods... --- .../com/samskivert/depot/Transformers.java | 22 ++++++++--- .../samskivert/depot/tests/TransformTest.java | 4 +- .../depot/tests/TransformersTest.java | 39 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/java/com/samskivert/depot/tests/TransformersTest.java diff --git a/src/java/com/samskivert/depot/Transformers.java b/src/java/com/samskivert/depot/Transformers.java index 1c0fdfd..983140e 100644 --- a/src/java/com/samskivert/depot/Transformers.java +++ b/src/java/com/samskivert/depot/Transformers.java @@ -37,14 +37,26 @@ import com.samskivert.depot.annotation.Column; public class Transformers { /** - * Combines the contents of a String[] column into a single string, separated by commas. + * Combines the contents of a String[] column into a single string, separated by tabs. Any tabs + * in the strings will be escaped. */ - public static class CommaSeparatedString implements Transformer { - public String toPersistent (String[] value) { - return Joiner.on(",").join(value); + public static class TabSeparatedString implements Transformer { + public String toPersistent (String[] values) { + StringBuffer buf = new StringBuffer(); + for (String value : values) { + if (buf.length() > 0) { + buf.append("\t"); + } + buf.append(value.replace("\t", "\\\t")); + } + return buf.toString(); } public String[] fromPersistent (Class ftype, String value) { - return value.split(","); + String[] values = value.replace("\\\t", "%%ESCTAB%%").split("\t"); + for (int ii = 0; ii < values.length; ii++) { + values[ii] = values[ii].replace("%%ESCTAB%%", "\t"); + } + return values; } } } diff --git a/src/java/com/samskivert/depot/tests/TransformTest.java b/src/java/com/samskivert/depot/tests/TransformTest.java index d946274..4274f56 100644 --- a/src/java/com/samskivert/depot/tests/TransformTest.java +++ b/src/java/com/samskivert/depot/tests/TransformTest.java @@ -127,7 +127,7 @@ public class TransformTest extends TestBase @Id public int recordId; - @Transform(Transformers.CommaSeparatedString.class) + @Transform(Transformers.TabSeparatedString.class) public String[] strings; public CustomType custom; @@ -151,7 +151,7 @@ public class TransformTest extends TestBase public static class BadTransformRecord extends PersistentRecord { - @Transform(Transformers.CommaSeparatedString.class) + @Transform(Transformers.TabSeparatedString.class) public Thread thread; public InvalidCustomType invalid; diff --git a/src/java/com/samskivert/depot/tests/TransformersTest.java b/src/java/com/samskivert/depot/tests/TransformersTest.java new file mode 100644 index 0000000..25bab20 --- /dev/null +++ b/src/java/com/samskivert/depot/tests/TransformersTest.java @@ -0,0 +1,39 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell +// +// This library 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 library 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 library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.tests; + +import org.junit.Test; +import static org.junit.Assert.*; + +import com.samskivert.depot.Transformers; + +/** + * Tests the stock transformers. + */ +public class TransformersTest +{ + @Test public void testTabSeparatedString () + { + String[] data = { "notabs", "\tpretab", "posttab\t", "in\ttab", "\t\t\tOMGtabs!\t\t" }; + Transformers.TabSeparatedString xform = new Transformers.TabSeparatedString(); + assertArrayEquals(data, xform.fromPersistent(null, xform.toPersistent(data))); + } +}