Replace the CommaSeparatedString transformer with an escaping
TabSeparatedString transformer. Maybe I should just use StringUtil methods...
This commit is contained in:
@@ -37,14 +37,26 @@ import com.samskivert.depot.annotation.Column;
|
|||||||
public class Transformers
|
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<String[], String> {
|
public static class TabSeparatedString implements Transformer<String[], String> {
|
||||||
public String toPersistent (String[] value) {
|
public String toPersistent (String[] values) {
|
||||||
return Joiner.on(",").join(value);
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ public class TransformTest extends TestBase
|
|||||||
|
|
||||||
@Id public int recordId;
|
@Id public int recordId;
|
||||||
|
|
||||||
@Transform(Transformers.CommaSeparatedString.class)
|
@Transform(Transformers.TabSeparatedString.class)
|
||||||
public String[] strings;
|
public String[] strings;
|
||||||
|
|
||||||
public CustomType custom;
|
public CustomType custom;
|
||||||
@@ -151,7 +151,7 @@ public class TransformTest extends TestBase
|
|||||||
|
|
||||||
public static class BadTransformRecord extends PersistentRecord
|
public static class BadTransformRecord extends PersistentRecord
|
||||||
{
|
{
|
||||||
@Transform(Transformers.CommaSeparatedString.class)
|
@Transform(Transformers.TabSeparatedString.class)
|
||||||
public Thread thread;
|
public Thread thread;
|
||||||
|
|
||||||
public InvalidCustomType invalid;
|
public InvalidCustomType invalid;
|
||||||
|
|||||||
@@ -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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user