Said support allows to use the #iterate & #argument #enditerate parameters. Which will duplicate the text block inside of it x amount of times with arguments being replaced. The idea being that if code can be replicated the iterate option would simplify the process. Multiple arguments can be defined (they will be not part of the output) and it will simply insert them. Note that all argument parameters have to be equal size. Otherwise the iterator processor will fail. Iterators can be stacked too to create layers upon layers. On top of that the iterator is part of the Template parser and not of the template processor meaning its a Pre processor step. And all mappers still apply themselves on to the output of the iterators!
62 lines
1.7 KiB
Java
62 lines
1.7 KiB
Java
package speiger.src.builder.base;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.StringJoiner;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class SegmentIterator {
|
|
public static void iterateSegment(String fileName, String currentLine, List<String> lines, int currentIndex, int startIndex) {
|
|
List<String[]> arguments = new ArrayList<>();
|
|
StringJoiner toIterate = new StringJoiner("\n");
|
|
int skip = 0;
|
|
int end = 1;
|
|
for(int i = currentIndex+1;i<lines.size();i++, end++)
|
|
{
|
|
String s = lines.get(i);
|
|
String trimmed = s.trim();
|
|
if(trimmed.startsWith("#"))
|
|
{
|
|
if(trimmed.startsWith("#enditerate"))
|
|
{
|
|
skip--;
|
|
if(skip < 0)
|
|
{
|
|
end++;
|
|
break;
|
|
}
|
|
}
|
|
if(trimmed.startsWith("#iterate")) skip++;
|
|
if(skip > 0)
|
|
{
|
|
toIterate.add(s);
|
|
continue;
|
|
}
|
|
if(trimmed.startsWith("#argument"))
|
|
{
|
|
arguments.add(trimmed.substring(9).trim().split(" "));
|
|
continue;
|
|
}
|
|
}
|
|
toIterate.add(s);
|
|
}
|
|
int size = arguments.get(0).length;
|
|
for(int i = 1,m=arguments.size();i<m;i++) {
|
|
if(arguments.get(i).length != size) throw new RuntimeException("Iteration arguments in file ["+fileName+"] are not equal size. Arugments="+arguments.stream().flatMap(Arrays::stream).collect(Collectors.toList()));
|
|
}
|
|
for(int i = 0;i<end;i++) {
|
|
lines.remove(currentIndex);
|
|
}
|
|
String toInsert = toIterate.toString();
|
|
for(int i = size-1;i>=1;i--) {
|
|
String temp = toInsert;
|
|
for(int j = 0;j<arguments.size();j++) {
|
|
String[] argument = arguments.get(j);
|
|
temp = temp.replace(argument[0], argument[i]);
|
|
}
|
|
lines.addAll(currentIndex, Arrays.asList(temp.split("\n", -1)));
|
|
}
|
|
}
|
|
}
|