Skip to content

Commit

Permalink
fix to custom code substitution #13
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 13, 2023
1 parent 8b06fee commit d78652b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.function.SimpleValue;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.lang.helpers.StringUtils;

public class ExtractCustomCode {

Expand Down Expand Up @@ -59,4 +63,58 @@ public static String addCustomContent( CharSequence text, String startTag, Strin
} );
}

private static String addWithCondition( CharSequence text, String customContent, Function<LineCursor, Boolean> condition ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
List<String> lines = reader.lines().collect( Collectors.toList() );
LineCursor cursor = new LineCursor();
cursor.lines = lines;
for ( cursor.index = 0; cursor.index<lines.size(); cursor.index++ ) {
String currentLine = lines.get( cursor.index );
if ( condition.apply( cursor ).booleanValue() ) {
writer.println( customContent );
}
writer.println( currentLine );
}
return buffer.toString();
}
} );
}

public static String addAfterPackageClassEnd( CharSequence text, String customContent ) {
return addWithCondition(text, customContent, c -> {
boolean ok = false;
String previousLine = c.getPreviousLine();
if ( StringUtils.isNotEmpty( previousLine ) ) {
ok = previousLine.trim().startsWith( "package" );
}
return ok;
} );
}

public static String addBeforeClassEnd( CharSequence text, String customContent ) {
return addWithCondition(text, customContent, c -> c.isLast() && c.getCurrentLine().trim().equals( "}" ) );
}

}

class LineCursor {

public List<String> lines;

public int index;

public boolean isLast() {
return this.lines.size()-1 == this.index;
}

public String getPreviousLine() {
return this.index==0 ? null : this.lines.get( this.index-1 );
}

public String getCurrentLine() {
return this.lines.get( this.index );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,43 @@ public void test001() {
String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END );
Assert.assertNotNull( result );
log.info( "result : {}", result );
// try to correct
log.info( "try correct : {}", fileReal.getCanonicalPath() );
String realContent = FileIO.readString( fileReal );
// test real mode 1
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END, result );
Assert.assertNotNull( resultReal );
// test real mode 2
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
String resultRealAlt = ExtractCustomCode.addBeforeClassEnd( realContent , result );
log.info( "real result alt {}", resultRealAlt );
Assert.assertNotNull( resultRealAlt );
} );
}

@Test
public void test002() {
SafeFunction.apply( () -> {
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java";
File file = new File( path );
File fileReal = new File( pathReal );
log.info( "try source : {}", file.getCanonicalPath() );
String result = ExtractCustomCode.extractCustom( file , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END );
Assert.assertNotNull( result );
log.info( "result : {}", result );
// try to correct
log.info( "try correct : {}", fileReal.getCanonicalPath() );
String realContent = FileIO.readString( fileReal );
// test real mode 1
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END, result );
Assert.assertNotNull( resultReal );
// test real mode 2
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
String resultRealAlt = ExtractCustomCode.addAfterPackageClassEnd( realContent , result );
log.info( "real result alt {}", resultRealAlt );
Assert.assertNotNull( resultRealAlt );
} );

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ private void handleRealFileCorrect( File file2, Properties params, PrintWriter r
if ( StringUtils.isNotEmpty( contentCode ) || StringUtils.isNotEmpty( contentImport ) ) {
String realFileContent = FileIO.readString( realFile );
if ( StringUtils.isNotEmpty( contentCode ) ) {
realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customCodeStart, customCodeEnd, contentCode );
realFileContent = ExtractCustomCode.addBeforeClassEnd( realFileContent, contentCode );
}
if ( StringUtils.isNotEmpty( contentImport ) ) {
realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customImportStart, customImportEnd, contentImport );
realFileContent = ExtractCustomCode.addAfterPackageClassEnd( realFileContent, contentImport );
}
FileIO.writeString( realFileContent , realFile );
report.print( " real file customized! "+realFileContent );
Expand Down

0 comments on commit d78652b

Please sign in to comment.