Skip to content

Commit

Permalink
Add encoding attribute to ABLDuck
Browse files Browse the repository at this point in the history
Issue #388
  • Loading branch information
clement-brodu authored and gquerret committed Dec 12, 2019
1 parent 3d4cd17 commit 5b4c828
Showing 1 changed file with 62 additions and 23 deletions.
85 changes: 62 additions & 23 deletions src/java/za/co/mip/ablduck/ABLDuck.java
Expand Up @@ -18,10 +18,14 @@


import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
Expand Down Expand Up @@ -89,6 +93,8 @@ public class ABLDuck extends PCT {
private Boolean dataFilesOnly = false; private Boolean dataFilesOnly = false;
private List<FileSet> filesets = new ArrayList<>(); private List<FileSet> filesets = new ArrayList<>();
protected Path propath = null; protected Path propath = null;
private String encoding = null;
private Charset inputCharset = null;


public ABLDuck() { public ABLDuck() {
super(); super();
Expand Down Expand Up @@ -121,6 +127,15 @@ public void setTitle(String title) {
this.title = title; this.title = title;
} }


/**
* Codepage to use when reading files
*
* @param encoding String
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}

/** /**
* Set the propath to be used when running the procedure * Set the propath to be used when running the procedure
* *
Expand Down Expand Up @@ -213,25 +228,30 @@ public void execute() {
String ext = file.getName().substring(extPos); String ext = file.getName().substring(extPos);
boolean isClass = ".cls".equalsIgnoreCase(ext); boolean isClass = ".cls".equalsIgnoreCase(ext);


ICompilationUnit root = astMgr.createAST(file, astContext, monitor, try (InputStream input = new FileInputStream(file);
IASTManager.EXPAND_ON, IASTManager.DLEVEL_FULL); Reader fsr = new InputStreamReader(input, getCharset())) {
if (isClass) { ICompilationUnit root = astMgr.createAST(fsr, astContext, monitor,
ABLDuckClassVisitor visitor = new ABLDuckClassVisitor(pp); IASTManager.EXPAND_ON, IASTManager.DLEVEL_FULL);
log("Executing AST ClassVisitor " + file.getAbsolutePath(), if (isClass) {
Project.MSG_VERBOSE); ABLDuckClassVisitor visitor = new ABLDuckClassVisitor(pp);
root.accept(visitor); log("Executing AST ClassVisitor " + file.getAbsolutePath(),

Project.MSG_VERBOSE);
CompilationUnit cu = visitor.getCompilationUnit(); root.accept(visitor);
classes.put(cu.name, cu);
} else { CompilationUnit cu = visitor.getCompilationUnit();

classes.put(cu.name, cu);
ABLDuckProcedureVisitor visitor = new ABLDuckProcedureVisitor(dsfiles[i]); } else {
log("Executing AST ProcedureVisitor " + file.getAbsolutePath(),
Project.MSG_VERBOSE); ABLDuckProcedureVisitor visitor = new ABLDuckProcedureVisitor(dsfiles[i]);
root.accept(visitor); log("Executing AST ProcedureVisitor " + file.getAbsolutePath(),

Project.MSG_VERBOSE);
CompilationUnit cu = visitor.getCompilationUnit(); root.accept(visitor);
procedures.put(cu.name, cu);
CompilationUnit cu = visitor.getCompilationUnit();
procedures.put(cu.name, cu);
}
} catch (IOException e) {
log(e.getMessage(), Project.MSG_WARN);
} }
} }
} }
Expand Down Expand Up @@ -342,7 +362,8 @@ public void execute() {
} }


File outputFile = new File(baseDir, cu.name + ".js"); File outputFile = new File(baseDir, cu.name + ".js");
try (FileWriter file = new FileWriter(outputFile.toString())) { try (OutputStreamWriter file = new OutputStreamWriter(
new FileOutputStream(outputFile.toString()), StandardCharsets.UTF_8)) {
file.write("Ext.data.JsonP." + cu.name.replace(".", "_") + "(" + gson.toJson(cu) file.write("Ext.data.JsonP." + cu.name.replace(".", "_") + "(" + gson.toJson(cu)
+ ");"); + ");");
} catch (IOException ex) { } catch (IOException ex) {
Expand All @@ -362,7 +383,8 @@ public void execute() {


String filename = cu.name.replace(".", "_").replace("/", "_"); String filename = cu.name.replace(".", "_").replace("/", "_");
File outputFile = new File(baseDir, filename + ".js"); File outputFile = new File(baseDir, filename + ".js");
try (FileWriter file = new FileWriter(outputFile.toString())) { try (OutputStreamWriter file = new OutputStreamWriter(
new FileOutputStream(outputFile.toString()), StandardCharsets.UTF_8)) {
file.write("Ext.data.JsonP." + filename + "(" + gson.toJson(cu) + ");"); file.write("Ext.data.JsonP." + filename + "(" + gson.toJson(cu) + ");");
} catch (IOException ex) { } catch (IOException ex) {
throw new BuildException(ex); throw new BuildException(ex);
Expand Down Expand Up @@ -502,13 +524,30 @@ public void createSearch(CompilationUnit cu) {
+ member.name; + member.name;
search.icon = ICON_PREFIX + member.tagname; search.icon = ICON_PREFIX + member.tagname;
search.url = "#!/" + cu.tagname + "/" search.url = "#!/" + cu.tagname + "/"
+ ("procedure".equals(cu.tagname) ? cu.name : member.owner) + "-" + member.tagname + ("procedure".equals(cu.tagname) ? cu.name : member.owner) + "-"
+ "-" + member.name; + member.tagname + "-" + member.name;
search.sort = 3; search.sort = 3;
search.meta = member.meta; search.meta = member.meta;


data.search.add(search); data.search.add(search);
} }
} }


/**
* Get the Charset from encoding parameter
*
* @return Charset
*/
protected Charset getCharset() {
if (inputCharset == null) {
try {
inputCharset = Charset.forName(this.encoding);
} catch (IllegalArgumentException caught) {
inputCharset = Charset.defaultCharset();
}
}

return inputCharset;
}

} }

0 comments on commit 5b4c828

Please sign in to comment.