diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/mustache/SplitStringLambda.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/mustache/SplitStringLambda.java new file mode 100644 index 000000000000..06c81332a9dd --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/mustache/SplitStringLambda.java @@ -0,0 +1,86 @@ +package io.swagger.codegen.mustache; + +import java.io.IOException; +import java.io.Writer; + +import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Template.Fragment; + +/** + * Splits long fragments into smaller strings and uses a StringBuilder to merge + * them back. + * + * Register: + * + *
+ * additionalProperties.put("lambdaSplitString", new SplitStringLambda()); + *+ * + * Use: + * + *
+ * {{#lambdaSplitString}}{{summary}}{{/lambdaSplitString}} + *+ */ +public class SplitStringLambda implements Mustache.Lambda { + private static final int DEFAULT_MAX_LENGTH = 65535; + + private static final String SPLIT_INIT = "new StringBuilder(%d)"; + + private static final String SPLIT_PART = ".append(\"%s\")"; + + private static final String SPLIT_SUFFIX = ".toString()"; + + private final int maxLength; + + public SplitStringLambda() { + this(DEFAULT_MAX_LENGTH); + } + + public SplitStringLambda(int maxLength) { + this.maxLength = maxLength; + } + + @Override + public void execute(Fragment fragment, Writer writer) throws IOException { + String input = fragment.execute(); + int inputLength = input.length(); + + StringBuilder builder = new StringBuilder(); + if (inputLength > maxLength) { + + // Initialize a StringBuilder + builder.append(String.format(SPLIT_INIT, inputLength)); + + int currentPosition = 0; + int currentStringLength = 0; + char currentLastChar = '\\'; + + // Split input into parts of at most maxLength and not ending with an escape character + // Append each part to the StringBuilder + while (currentPosition + maxLength < input.length()) { + currentStringLength = maxLength; + currentLastChar = input.charAt(currentPosition + currentStringLength - 1); + if (currentLastChar == '\\') { + --currentStringLength; + } + + builder.append(String.format(SPLIT_PART, input.substring(currentPosition, currentPosition + currentStringLength))); + currentPosition += currentStringLength; + } + + // Append last part if necessary + if (currentPosition < input.length()) { + builder.append(String.format(SPLIT_PART, input.substring(currentPosition))); + } + + // Close the builder and merge everything back to a string + builder.append(SPLIT_SUFFIX); + } else { + builder.append(String.format("\"%s\"", input)); + } + + writer.write(builder.toString()); + } + +} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/mustache/SplitStringLambdaTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/mustache/SplitStringLambdaTest.java new file mode 100644 index 000000000000..9d9eeed8f16a --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/mustache/SplitStringLambdaTest.java @@ -0,0 +1,85 @@ +package io.swagger.codegen.mustache; + +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.samskivert.mustache.Template.Fragment; + +public class SplitStringLambdaTest { + private static final String INPUT_STRING = "1112223334"; + + private static final Map