Skip to content

Commit

Permalink
Added loadGroupURL to STGroup and URL constructor to STGroupFile
Browse files Browse the repository at this point in the history
- Changes necessary to support classpath resources in a variety of
different deployment scenarios

See Antlr4 PR: antlr/antlr4#2280
  • Loading branch information
solussd committed Apr 24, 2018
1 parent 9cc59e1 commit 5410c37
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/org/stringtemplate/v4/STGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,23 +626,32 @@ protected void importTemplates(STGroup g, boolean clearOnUnload) {

public List<STGroup> getImportedGroups() { return imports; }

/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */
public void loadGroupFile(String prefix, String fileName) {
if ( verbose ) System.out.println(this.getClass().getSimpleName()+
".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
public void loadGroupURL(String prefix, URL url) {
GroupParser parser;
try {
URL f = new URL(fileName);
ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
ANTLRInputStream fs = new ANTLRInputStream(url.openStream(), encoding);
GroupLexer lexer = new GroupLexer(fs);
fs.name = fileName;
fs.name = url.toString();
CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new GroupParser(tokens);
parser.group(this, prefix);
}
catch (Exception e) {
errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, url.toString());
}
}

/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */
public void loadGroupFile(String prefix, String fileName) {
if ( verbose ) System.out.println(this.getClass().getSimpleName()+
".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
try {
URL url = new URL(fileName);
loadGroupURL(prefix, url);
} catch (Exception e) {
errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
}

}

/** Load template file into this group using absolute {@code fileName}. */
Expand Down
4 changes: 3 additions & 1 deletion src/org/stringtemplate/v4/STGroupFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class STGroupFile extends STGroup {
/** Load a file relative to current directory or from root or via CLASSPATH. */
public STGroupFile(String fileName) { this(fileName, '<', '>'); }

public STGroupFile(URL url) { this(url, "UTF-8",'<', '>'); }

public STGroupFile(String fileName, char delimiterStartChar, char delimiterStopChar) {
super(delimiterStartChar, delimiterStopChar);
if ( !fileName.endsWith(GROUP_FILE_EXTENSION) ) {
Expand Down Expand Up @@ -138,7 +140,7 @@ public void load() {
// no prefix since this group file is the entire group, nothing lives
// beneath it.
if ( verbose ) System.out.println("loading group file "+url.toString());
loadGroupFile("/", url.toString());
loadGroupURL("/", url);
if ( verbose ) System.out.println("found "+templates.size()+" templates in "+url.toString()+" = "+templates.keySet());
}

Expand Down

0 comments on commit 5410c37

Please sign in to comment.