Skip to content

Commit 886995b

Browse files
committed
Added a new mustache lambda to trim whitespace in fragments
1 parent e1e9420 commit 886995b

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
* Copyright 2018 SmartBear Software
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.openapitools.codegen.templating.mustache;
19+
20+
import java.io.IOException;
21+
import java.io.Writer;
22+
23+
import com.samskivert.mustache.Mustache;
24+
import com.samskivert.mustache.Template.Fragment;
25+
26+
/**
27+
* Replaces duplicate whitespace characters in a fragment with single space.
28+
*
29+
* Register:
30+
* <pre>
31+
* additionalProperties.put("lambdaTrimWhitespace", new TrimWhitespaceLambda());
32+
* </pre>
33+
*
34+
* Use:
35+
* <pre>
36+
* {{#lambdaTrimWhitespace}}{{name}}{{/lambdaTrimWhitespace}}
37+
* </pre>
38+
*/
39+
public class TrimWhitespaceLambda implements Mustache.Lambda {
40+
private static final String SINGLE_SPACE = " ";
41+
42+
private static final String WHITESPACE_REGEX = "\\s+";
43+
44+
@Override
45+
public void execute(Fragment fragment, Writer writer) throws IOException {
46+
writer.write(fragment.execute().replaceAll(WHITESPACE_REGEX, SINGLE_SPACE));
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
* Copyright 2018 SmartBear Software
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.openapitools.codegen.templating.mustache;
19+
20+
import static org.mockito.Mockito.when;
21+
import static org.testng.Assert.assertEquals;
22+
23+
import java.io.IOException;
24+
import java.io.StringWriter;
25+
26+
import org.mockito.Mock;
27+
import org.mockito.Mockito;
28+
import org.mockito.MockitoAnnotations;
29+
import org.testng.annotations.AfterMethod;
30+
import org.testng.annotations.BeforeMethod;
31+
import org.testng.annotations.Test;
32+
33+
import com.samskivert.mustache.Template.Fragment;
34+
35+
public class TrimWhitespaceLambdaTest {
36+
37+
@Mock
38+
private Fragment fragment;
39+
40+
@BeforeMethod
41+
public void init() {
42+
MockitoAnnotations.initMocks(this);
43+
}
44+
45+
@AfterMethod
46+
public void reset() {
47+
Mockito.reset(fragment);
48+
}
49+
50+
@Test
51+
public void testTrimWhitespace() throws IOException {
52+
when(fragment.execute()).thenReturn("\t a b\t\tc \t");
53+
54+
StringWriter output = new StringWriter();
55+
new TrimWhitespaceLambda().execute(fragment, output);
56+
assertEquals(output.toString(), " a b c ");
57+
}
58+
59+
}

0 commit comments

Comments
 (0)