Skip to content

Commit

Permalink
text wrap function (using zero with space)
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Aug 8, 2024
1 parent d6f9fdf commit b21cdbe
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [playground-quarkus] Config conversion user interface
- [freemarker] text wrap function (using zero with space `​`)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.fugerit.java.doc.base.helper;

import org.fugerit.java.core.function.SafeFunction;

public class TextWrapHelper {

private TextWrapHelper() {}

public static final String ZERO_WITH_SPACE = "​";

public static String padZeroWithSpace( String input ) {
return SafeFunction.getIfNotNull( input, () -> {
StringBuilder builder = new StringBuilder();
for( int i=0; i<input.length(); i++ ) {
builder.append( input.charAt( i ) );
builder.append( ZERO_WITH_SPACE );
}
return builder.toString();
} );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test.org.fugerit.java.doc.base.helper;

import org.fugerit.java.doc.base.helper.TextWrapHelper;
import org.junit.Assert;
import org.junit.Test;

public class TestTextWrapHelper {

@Test
public void testWrap() {
String input = "123";
String test = "1"+TextWrapHelper.ZERO_WITH_SPACE+"2"+TextWrapHelper.ZERO_WITH_SPACE+"3"+TextWrapHelper.ZERO_WITH_SPACE;
String out = TextWrapHelper.padZeroWithSpace( input );
Assert.assertEquals( test, out );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.fugerit.java.doc.freemarker.fun;

import freemarker.template.TemplateModelException;

import java.util.List;

public class FMFunHelper {

private FMFunHelper() {}

public static void checkFirstRequredWithMessage( List<Object> arguments, String message ) throws TemplateModelException {
if ( arguments.isEmpty() ) {
throw new TemplateModelException( message );
}
}

public static void checkFirstRequred( List<Object> arguments ) throws TemplateModelException {
checkFirstRequredWithMessage( arguments, "At least one parameter is needed" );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public class SimpleMessageFun implements TemplateMethodModelEx {

@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
if ( arguments.isEmpty() ) {
throw new TemplateModelException( "At least one parameter is needed" );
}
FMFunHelper.checkFirstRequred( arguments );
String key = ((TemplateScalarModel)arguments.get( 0 )).getAsString();
Object[] args = new Object[ arguments.size()-1 ];
for ( int k=1; k<arguments.size(); k++ ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.fugerit.java.doc.freemarker.fun;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
import org.fugerit.java.doc.base.helper.TextWrapHelper;

import java.text.MessageFormat;
import java.util.List;

public class TextWrapFun implements TemplateMethodModelEx {

@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
FMFunHelper.checkFirstRequred( arguments );
String input = ((TemplateScalarModel)arguments.get( 0 )).getAsString();
return new SimpleScalar(TextWrapHelper.padZeroWithSpace( input ));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<chainStep stepType="function">
<function name="messageFormat" value="org.fugerit.java.doc.freemarker.fun.SimpleMessageFun"/>
<function name="sumLong" value="org.fugerit.java.doc.freemarker.fun.SimpleSumLongFun"/>
<function name="textWrap" value="org.fugerit.java.doc.freemarker.fun.TextWrapFun"/>
</chainStep>
</docChain>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<para align="right">Test right</para>

<para align="right">${messageFormat('test format -> {0} {1}', 'param1', 'param2')}</para>

<para align="right">${textWrap('test text wrap')}</para>


<br/>
<br/>
Expand Down

0 comments on commit b21cdbe

Please sign in to comment.