Skip to content

Commit

Permalink
Add header option to SjppAntTask
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsodelavega committed Aug 1, 2023
1 parent d57e279 commit a621c3c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
12 changes: 12 additions & 0 deletions sjpp/src/main/java/sjpp/Context.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sjpp;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -15,6 +17,8 @@ public class Context {

public static final String GENERATED = "// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.";
private Define define;
private Header header;

private final ContextMode mode;
private final List<JavaFile> files = new ArrayList<JavaFile>();

Expand All @@ -33,6 +37,14 @@ public void addDefine(String id) {
this.define = new Define(id);
}

public void addHeader(List<String> headerLines) throws IOException {
this.header = new Header(headerLines);
}

public Header getHeader() {
return header;
}

public boolean doesApplyOn(String s) {
return define.doesApplyOn(s);
}
Expand Down
21 changes: 21 additions & 0 deletions sjpp/src/main/java/sjpp/Header.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sjpp;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Header {

private List<String> headerLines;

public Header(List<String> headerLines) throws IOException {
this.headerLines = headerLines;
}

public List<String> getLines() {
return headerLines;
}
}
8 changes: 7 additions & 1 deletion sjpp/src/main/java/sjpp/JavaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

Expand All @@ -17,7 +18,12 @@ public class JavaFile {
public JavaFile(Context context, Path path) throws IOException {
this.path = path;
this.context = context;
this.lines = Files.readAllLines(path);

lines = new ArrayList<>();
if (context.getHeader() != null) {
lines.addAll(context.getHeader().getLines());
}
lines.addAll(Files.readAllLines(path));

for (String s : lines) {
if (s.startsWith("package ")) {
Expand Down
25 changes: 22 additions & 3 deletions sjpp/src/main/java/sjpp/SjppAntTask.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
package sjpp;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SjppAntTask extends Task {

private String src;
private String dest;
private String define;
private String header;

@Override
public void execute() throws BuildException {
this.log("Starting SimpleJava PreProcessor.");
this.log("src: " + src);
this.log("dest: " + dest);
this.log("define: " + define);
this.log("header: " + header);

final Path root = Paths.get(src);

final Context context = new Context(ContextMode.REGULAR, root);
context.addDefine(define);


if (header != null) {
try {
context.addHeader(Files.readAllLines(Paths.get(header)));
} catch (IOException e) {
e.printStackTrace();
this.log("Error " + e.toString());
}
}


final Path out = Paths.get(dest);
try {
context.process(out);
Expand All @@ -46,4 +61,8 @@ public final void setDefine(String define) {
this.define = define;
}

public final void setHeader(String header) {
this.header = header;
}

}

0 comments on commit a621c3c

Please sign in to comment.