[GitHub] Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings.

GitBox
Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings.
URL: https://github.com/apache/commons-lang/pull/395
 
 
   Replace String with the String which is nested in between two Strings.
   
   I have used this Method in order to generate dynamic HTML reports to send a mail.
   
   ```java    
   public class Test {
       public static void main(String[] args) throws Exception {
           String data = new String(Files.readAllBytes(Paths.get("./dynamicReport.html")));
           String rowSaperator = "<!--  [$$ListRow$$] -->";
           String rowDataSaperator = "[~$List$~]";
           
           List<String[]> dynamicRecords = new ArrayList<>();
           dynamicRecords.add( new String[]{ "1", "1" } );
           dynamicRecords.add( new String[]{ "1", "1" } );
           dynamicRecords.add( new String[]{ "2", "1" } );
           dynamicRecords.add( new String[]{ "1", "2" } );
           
           String finalTableData = dynamicRecordData(data, rowSaperator, rowDataSaperator, dynamicRecords);
           
           String targetFilePath = "D:/dynamicReport.txt";
           try (PrintWriter out = new PrintWriter( targetFilePath )) {
               out.println( finalTableData );
           }
       }
       
       public static String dynamicRecordData(String str, String rowSaperator, String rowDataSaperator, List<String[]> dynamicRecords) throws Exception {
           if( str.length() <= ( (rowSaperator.length())*2 + rowDataSaperator.length()) ) {
               return str;
           }
           
           String substringBetween = StringUtils.substringBetween(str, rowSaperator, rowSaperator);
           if( substringBetween == null) {
               return str;
           }
           
           int countMatches = StringUtils.countMatches(substringBetween, rowDataSaperator);
           if( str.length() <= ( (rowSaperator.length())*2 + (rowDataSaperator.length())*countMatches ) ) {
               return str;
           }
           
           StringBuffer dynamicRowData = new StringBuffer();
           
           String[] searchList = null;
           for (String[] dynamicRecord : dynamicRecords) {
               if( dynamicRecord.length == countMatches ) {
                   if( searchList == null ) {
                       searchList = new String[countMatches];
                       for (int i = 0; i < countMatches; i++) {
                           searchList[i] = rowDataSaperator;
                       }
                   }
                   
                   String replaceEach = StringUtils.replaceEach(substringBetween, searchList, dynamicRecord);
                   
                   dynamicRowData.append(replaceEach);
               } else {
                   throw new Exception("Records mismatch.");
               }
           }
           
           String finalTableData = StringUtils.replaceSubstringInBetween(str, dynamicRowData.toString(), rowSaperator, rowSaperator);
           return finalTableData;
       }
   }
   ```
   Sample HTML Template. Where i my case there are to many dynamic tables with provided list data to replace.
   ```html
   <table>
       <tbody>
           <tr><td>Header1</td><td>Header2</td>
           <!--  [$$ListRow$$] -->
           <tr><td>[~$List$~]</td><td>[~$List$~]</td>
           <!--  [$$ListRow$$] -->
       </tbody>
   </table>
   ```
   After code generated report.
   
   ```html
   <table>
       <tbody>
           <tr><td>Header1</td><td>Header2</td>
           <!--  [$$ListRow$$] -->
           <tr><td>1</td><td>1</td>
           
           <tr><td>1</td><td>1</td>
           
           <tr><td>2</td><td>2</td>
           
           <tr><td>1</td><td>1</td>
           <!--  [$$ListRow$$] -->
       </tbody>
   </table>
   ```
   Avoid previous pull requests [`393`, `394`](https://travis-ci.org/apache/commons-lang/pull_requests)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services