/**
 * Copyright (c) 2016, Michael Sonntag
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the BSD License. See
 * LICENSE file in the root of the Project.
 */

package lib;

import java.util.ArrayList;
import java.util.List;

/**
 * Parse list of Strings to a List of tagged entities.
 */
public final class ParseList {

    public static List<TaggedEntity> parseStringList(final List<String> toParse) {

        List<TaggedEntity> retList = new ArrayList<>();

        System.out.println(
                String.join("", "[DEBUG] List has a length of: ",
                        Integer.toString(toParse.size()), "\n")
        );

        for (String currLine : toParse) {
            if (currLine.contains("(#)") & currLine.endsWith("#)")) {

                System.out.println("[DEBUG] TagLine");

                retList.add(new TaggedEntity());

                String[] ls = currLine
                        .substring(currLine.indexOf("(#)")+"(#)".length(),
                                currLine.length()-"#)".length())
                        .split(",");

                for (String s : ls) {
                    retList.get(retList.size()-1).getTags().add(s);
                }

            } else if (!retList.isEmpty())  {
                System.out.println("[DEBUG] BodyLine");

                String currString = retList.get(retList.size()-1).getBody();
                retList.get(retList.size()-1).setBody(
                        String.join("", currString, currLine, "\n")
                );
            } else {
                System.out.println("[WARNING] Found line not connected to a tag, will be omitted.");
            }
        }

        System.out.println(String.join("", "[DEBUG] # of Tagged entities: ",
                Integer.toString(retList.size()))
        );

        retList.forEach(c -> System.out.println(
                String.join("", "[DEBUG] ", "ID: ", c.getEntityID(), " Tags: ",
                        Integer.toString(c.getTags().size()), " ", c.getTags().toString(),
                        " Body: ", c.getBody()
                    )
                )
        );

        return retList;
    }

}

@@ -720,32 +720,200 @@
 n%22)%0A        );%0A%0A
+        // check whether we are in a proper tag block. if not ignore all body lines until a next%0A        // proper tag block starts.%0A        Boolean properTag = true;%0A%0A
         for (Str
@@ -982,11 +982,34 @@
 #)%22)
- & 
+) %7B%0A%0A                if (!
 curr
@@ -1023,33 +1023,331 @@
 dsWith(%22#)%22)) %7B%0A
-%0A
+                    System.out.println(String.join(%22%22, %22%5BWARNING%5D Encountered incomplete Tag line. %22,%0A                            %22All lines until the next proper tag line will be ignored.%22));%0A                    properTag = false;%0A                %7D else %7B%0A                    properTag = true;%0A    
                 
@@ -1394,32 +1394,36 @@
                 
+    
 retList.add(new 
@@ -1452,24 +1452,28 @@
             
+   
+ 
 String%5B%5D ls 
@@ -1483,16 +1483,20 @@
 urrLine%0A
+    
         
@@ -1549,9 +1549,11 @@
 #)%22)
-+
+ + 
 %22(#)
@@ -1596,16 +1596,20 @@
         
+  
+  
 currLine
@@ -1617,17 +1617,19 @@
 length()
--
+ - 
 %22#)%22.len
@@ -1659,16 +1659,20 @@
         
+ 
+   
 .split(%22
@@ -1685,32 +1685,36 @@
                 
+ 
+   
 for (String s : 
@@ -1715,24 +1715,28 @@
 g s : ls) %7B%0A
+    
             
@@ -1765,25 +1765,27 @@
 tList.size()
--
+ - 
 1).getTags()
@@ -1801,34 +1801,55 @@
                 
+    %7D%0A                
 %7D
-%0A
 %0A            %7D e
@@ -1874,16 +1874,28 @@
 sEmpty()
+ & properTag
 )  %7B%0A   

add incomplete tag line check