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

loadGroupURL to STGroup and URL constructor to STGroupFile #199

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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