Skip to content

Commit

Permalink
Fix for antlr#293
Browse files Browse the repository at this point in the history
Fix bug that prevented loading template groups from a template group dir if the path returned by getResource() ended in a '/' by removing the initial '/' from the parent if the root ends with '/'.
  • Loading branch information
Clement Cherlin committed Feb 7, 2022
1 parent 139f342 commit d1e590a
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/org/stringtemplate/v4/STGroupDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,33 @@ protected CompiledST load(String name) {
// return loadTemplateFile("/", name+TEMPLATE_FILE_EXTENSION); // load t.st file
// }

// Avoid creating invalid file URLs when root ends with '/'
// and parent starts with '/'
if (root.toString().endsWith("/")) {
if (parent.startsWith("/")) {
// root: 'a/', parent: '/b' -> 'b'
parent = parent.substring(1);
}
}
// Avoid creating invalid file URLs when root doesn't end with '/'
// and parent doesn't start with '/'
else if (!parent.startsWith("/")) {
// root: 'a', parent: 'b' -> '/b'
parent = "/" + parent;
}

String groupFileString = root+parent+GROUP_FILE_EXTENSION;

URL groupFileURL;
try { // see if parent of template name is a group file
groupFileURL = new URI(root+parent+GROUP_FILE_EXTENSION).normalize().toURL();
groupFileURL = new URI(groupFileString).normalize().toURL();
}
catch (MalformedURLException e) {
badURLError(groupFileString, e);
return null;
}
catch (MalformedURLException | URISyntaxException e) {
errMgr.internalError(null, "bad URL: "+root+parent+GROUP_FILE_EXTENSION, e);
catch (URISyntaxException e) {
badURLError(groupFileString, e);
return null;
}

Expand All @@ -145,17 +166,21 @@ protected CompiledST load(String name) {
}
finally { // clean up
try {
if (is!=null ) is.close();
if (is!=null) is.close();
}
catch (IOException ioe) {
errMgr.internalError(null, "can't close template file stream "+name, ioe);
}
}

loadGroupFile(prefix, root+parent+GROUP_FILE_EXTENSION);
loadGroupFile(prefix, groupFileString);
return rawGetTemplate(name);
}

private void badURLError(String fullPath, Exception e) {
errMgr.internalError(null, "bad URL: "+fullPath, e);
}

/** Load .st as relative file name relative to root by {@code prefix}. */
public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName) {
if ( verbose ) System.out.println("loadTemplateFile("+unqualifiedFileName+") in groupdir "+
Expand All @@ -164,9 +189,12 @@ public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName) {
try {
f = new URI(root+prefix+unqualifiedFileName).normalize().toURL();
}
catch (MalformedURLException | URISyntaxException me) {
errMgr.runTimeError(null, null, ErrorType.INVALID_TEMPLATE_NAME,
me, root + unqualifiedFileName);
catch (MalformedURLException e) {
invalidTemplateError(unqualifiedFileName, e);
return null;
}
catch (URISyntaxException e) {
invalidTemplateError(unqualifiedFileName, e);
return null;
}

Expand All @@ -184,6 +212,11 @@ public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName) {
return loadTemplateFile(prefix, unqualifiedFileName, fs);
}

private void invalidTemplateError(String unqualifiedFileName, Exception e) {
errMgr.runTimeError(null, null, ErrorType.INVALID_TEMPLATE_NAME,
e, root + unqualifiedFileName);
}

@Override
public String getName() { return groupDirName; }
@Override
Expand Down

0 comments on commit d1e590a

Please sign in to comment.