package de.itemis.mosig.racecar.textconv;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HtmlPagesConverter {
    private static final String PAGE_BREAK = "PAGE_BREAK";
    private static final String HTML_LINE_BREAK = "<br />";

    private final String filename;
    private final List<Integer> breaks = new ArrayList<>();
    private final List<String> contents;

    public HtmlPagesConverter(List<String> fileContents) {
        filename = null;
        this.contents = new ArrayList<>(fileContents);
    }

    public HtmlPagesConverter(String filename) throws IOException {
        this.contents = null;
        this.filename = filename;

        this.breaks.add(0);
        BufferedReader reader = new BufferedReader(new FileReader(this.filename));
        int cumulativeCharCount = 0;
        String line = reader.readLine();
        while (line != null) {
            cumulativeCharCount += line.length() + 1; // add one for the newline
            if (line.contains(PAGE_BREAK)) {
                int pageBreakPosition = cumulativeCharCount;
                breaks.add(pageBreakPosition);
            }
            line = reader.readLine();
        }
        reader.close();
    }

    public String getHtmlPage(int pageNbr) throws IOException {
        if (filename == null) {
            switch (pageNbr) {
                case 0:
                    return "Hello<br />";
                case 1:
                    return "World<br />";
                case 2:
                    return "!<br />";
                default:
                    return null;
            }
        } else {
            return internalGetHtmlPageFromFile(pageNbr);
        }
    }

    private String internalGetHtmlPageFromFile(int pageNbr) throws IOException {
        String result = null;

        if (isValid(pageNbr)) {
            BufferedReader reader = new BufferedReader(new FileReader(this.filename));
            reader.skip(breaks.get(pageNbr));
            StringBuffer htmlPage = new StringBuffer();
            String line = reader.readLine();
            while (line != null) {
                if (line.contains(PAGE_BREAK)) {
                    break;
                }

                htmlPage.append(StringEscapeUtils.escapeHtml(line));
                htmlPage.append(HTML_LINE_BREAK);
                line = reader.readLine();
            }
            reader.close();
            result = htmlPage.toString();
        }

        return result;
    }

    private boolean isValid(int pageNbr) {
        return pageNbr > -1 && pageNbr < breaks.size();
    }

    public String getFilename() {
        return this.filename;
    }

}

@@ -1379,32 +1379,81 @@
 name == null) %7B%0A
+            if (pageNbr %3C contents.size()) %7B%0A    
             swit
@@ -1479,24 +1479,28 @@
             
+    
 case 0:%0A    
@@ -1519,21 +1519,38 @@
     
+ 
+   
 return 
-%22Hello
+contents.get(0) + %22
 %3Cbr 
@@ -1566,24 +1566,28 @@
             
+    
 case 1:%0A    
@@ -1594,32 +1594,36 @@
                 
+  
+  
 return %22World%3Cbr
@@ -1620,32 +1620,36 @@
  %22World%3Cbr /%3E%22;%0A
+    
                 
@@ -1668,32 +1668,36 @@
                 
+    
 return %22!%3Cbr /%3E%22
@@ -1714,16 +1714,20 @@
         
+ 
+   
 default:
@@ -1727,24 +1727,96 @@
 efault:%0A    
+                    return null;%0A                %7D%0A            %7D else %7B%0A
             

Test w/o page breaks is green.