diff --git a/java/google/registry/tools/CreateLrpTokensCommand.java b/java/google/registry/tools/CreateLrpTokensCommand.java index 7a530e2a6..61802a31a 100644 --- a/java/google/registry/tools/CreateLrpTokensCommand.java +++ b/java/google/registry/tools/CreateLrpTokensCommand.java @@ -24,6 +24,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; +import com.google.common.base.CharMatcher; import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.FluentIterable; @@ -96,6 +97,11 @@ public final class CreateLrpTokensCommand implements RemoteApiCommand { @Inject StringGenerator stringGenerator; private static final int BATCH_SIZE = 20; + + // Ensures that all of the double quotes to the right of a comma are balanced. In a well-formed + // CSV line, there can be no leading double quote preceding the comma. + private static final String COMMA_EXCEPT_WHEN_QUOTED_REGEX = + ",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"; @Override public void run() throws Exception { @@ -124,7 +130,12 @@ public final class CreateLrpTokensCommand implements RemoteApiCommand { for (String token : generateTokens(BATCH_SIZE)) { line = reader.readLine(); if (!isNullOrEmpty(line)) { - ImmutableList values = ImmutableList.copyOf(Splitter.on(',').split(line)); + ImmutableList values = + ImmutableList.copyOf( + Splitter.onPattern(COMMA_EXCEPT_WHEN_QUOTED_REGEX) + // Results should not be surrounded in double quotes. + .trimResults(CharMatcher.is('\"')) + .split(line)); LrpTokenEntity.Builder tokenBuilder = new LrpTokenEntity.Builder() .setAssignee(values.get(0)) .setToken(token) diff --git a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java index 64d16a066..84c2af85b 100644 --- a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java +++ b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java @@ -109,6 +109,34 @@ public class CreateLrpTokensCommandTest extends CommandTestCase