You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2019/01/04 10:21:09 UTC

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

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 = TabularData_from_List.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`

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services