Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header option to SjppAntTask #2

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
5 changes: 5 additions & 0 deletions 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 Down Expand Up @@ -61,6 +62,10 @@ public void process() {
if (context.getMode() == ContextMode.REGULAR)
removeFirstHeader();

if (context.getHeader() != null) {
lines.addAll(0, context.getHeader().getLines());
}

ProcessMode mode = ProcessMode.NORMAL;

for (ListIterator<String> it = lines.listIterator(); it.hasNext();) {
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;
}

}