|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) |
| 4 | + * Copyright 2018 SmartBear Software |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.openapitools.codegen.templating.mustache; |
| 20 | + |
| 21 | +import com.samskivert.mustache.Mustache; |
| 22 | +import com.samskivert.mustache.Template; |
| 23 | +import org.apache.commons.lang3.StringUtils; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.Writer; |
| 27 | + |
| 28 | +import static org.openapitools.codegen.utils.StringUtils.camelize; |
| 29 | + |
| 30 | +/** |
| 31 | + * Converts text in a fragment from camelCase or PascalCase to a space separated string |
| 32 | + * |
| 33 | + * Register: |
| 34 | + * <pre> |
| 35 | + * additionalProperties.put("uncamelize", new UncamelizeLambda()); |
| 36 | + * </pre> |
| 37 | + * |
| 38 | + * Use: |
| 39 | + * <pre> |
| 40 | + * {{#uncamelize}}{{name}}{{/uncamelize}} |
| 41 | + * </pre> |
| 42 | + */ |
| 43 | +public class UncamelizeLambda implements Mustache.Lambda { |
| 44 | + |
| 45 | + public UncamelizeLambda() {} |
| 46 | + @Override |
| 47 | + public void execute(Template.Fragment fragment, Writer writer) throws IOException { |
| 48 | + String input = fragment.execute(); |
| 49 | + String text = StringUtils.capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(input.trim()), StringUtils.SPACE)); |
| 50 | + writer.write(text.trim().replaceAll(" +", " ")); |
| 51 | + } |
| 52 | +} |
| 53 | + |
0 commit comments