|
| 1 | +/* |
| 2 | + * Copyright 2026 OpenAPI-Generator Contributors (https://openapi-generator.tech) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.openapitools.codegen.templating.mustache; |
| 18 | + |
| 19 | +import com.samskivert.mustache.Mustache; |
| 20 | +import com.samskivert.mustache.Template; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.Writer; |
| 24 | + |
| 25 | +/** |
| 26 | + * Prefixes rendered fragment lines without applying additional indentation. |
| 27 | + */ |
| 28 | +public class PrefixLambda implements Mustache.Lambda { |
| 29 | + private final String prefix; |
| 30 | + private final boolean prefixFirstLine; |
| 31 | + |
| 32 | + public PrefixLambda(String prefix, boolean prefixFirstLine) { |
| 33 | + this.prefix = prefix; |
| 34 | + this.prefixFirstLine = prefixFirstLine; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void execute(Template.Fragment fragment, Writer writer) throws IOException { |
| 39 | + String text = fragment.execute(); |
| 40 | + if (text == null || text.isEmpty()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + String[] lines = text.split("\n", -1); |
| 45 | + StringBuilder sb = new StringBuilder(); |
| 46 | + for (int i = 0; i < lines.length; i++) { |
| 47 | + if (prefixFirstLine || i > 0) { |
| 48 | + sb.append(prefix); |
| 49 | + } |
| 50 | + sb.append(lines[i]); |
| 51 | + if (i < lines.length - 1) { |
| 52 | + sb.append("\n"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + writer.write(sb.toString()); |
| 57 | + } |
| 58 | +} |
| 59 | + |
0 commit comments