-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
3,077 additions
and
111 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/ExtractCustomCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.fugerit.java.daogen.base.gen.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.PrintWriter; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
|
||
import org.fugerit.java.core.function.SafeFunction; | ||
import org.fugerit.java.core.function.SimpleValue; | ||
import org.fugerit.java.core.io.FileIO; | ||
|
||
public class ExtractCustomCode { | ||
|
||
private ExtractCustomCode() {} | ||
|
||
public static String extractCustom( File file, String startTag, String endTag ) { | ||
return SafeFunction.get( () -> extractCustom( FileIO.readString( file ) , startTag, endTag ) ); | ||
} | ||
|
||
public static String extractCustom( CharSequence text, String startTag, String endTag ) { | ||
return SafeFunction.get( () -> { | ||
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) ); | ||
StringWriter buffer = new StringWriter(); | ||
PrintWriter writer = new PrintWriter( buffer, true ) ) { | ||
SimpleValue<Boolean> customCode = new SimpleValue<>( false ); | ||
reader.lines().forEach( line -> { | ||
if ( line.contains( startTag ) ) { | ||
customCode.setValue( true ); | ||
} else if ( line.contains( endTag ) ) { | ||
customCode.setValue( false ); | ||
} else if ( customCode.getValue().booleanValue() ) { | ||
writer.println( line ); | ||
} | ||
} ); | ||
return buffer.toString(); | ||
} | ||
} ); | ||
} | ||
|
||
public static String addCustomContent( CharSequence text, String startTag, String endTag, String customContent ) { | ||
return SafeFunction.get( () -> { | ||
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) ); | ||
StringWriter buffer = new StringWriter(); | ||
PrintWriter writer = new PrintWriter( buffer, true ) ) { | ||
SimpleValue<Boolean> customCode = new SimpleValue<>( false ); | ||
reader.lines().forEach( line -> { | ||
if ( line.contains( startTag ) ) { | ||
customCode.setValue( true ); | ||
} else if ( line.contains( endTag ) ) { | ||
writer.println( customContent ); // append custom content | ||
customCode.setValue( false ); | ||
} | ||
// all lines myst be written anyway | ||
writer.println( line ); | ||
} ); | ||
return buffer.toString(); | ||
} | ||
} ); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/util/TestExtractCustomCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package test.org.fugerit.java.daogen.util; | ||
|
||
import java.io.File; | ||
|
||
import org.fugerit.java.core.function.SafeFunction; | ||
import org.fugerit.java.core.io.FileIO; | ||
import org.fugerit.java.core.javagen.SimpleJavaGenerator; | ||
import org.fugerit.java.daogen.base.gen.util.ExtractCustomCode; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class TestExtractCustomCode { | ||
|
||
@Test | ||
public void test001() { | ||
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"; | ||
File file = new File( path ); | ||
log.info( "try source : {}", file.getCanonicalPath() ); | ||
String content = FileIO.readString( file ); | ||
String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END ); | ||
log.info( "result : {}", result ); | ||
Assert.assertNotNull( result ); | ||
} ); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.