package org.stringtemplate.v4;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.Token;
import org.stringtemplate.v4.compiler.CompiledST;
import org.stringtemplate.v4.compiler.STException;
import org.stringtemplate.v4.misc.ErrorType;
import org.stringtemplate.v4.misc.Misc;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class STGroupDir extends STGroup {
public String groupDirName;
public URL root;
public STGroupDir(String dirName) { this(dirName, '<', '>'); }
public STGroupDir(String dirName, char delimiterStartChar, char delimiterStopChar) {
super(delimiterStartChar, delimiterStopChar);
this.groupDirName = dirName;
File dir = new File(dirName);
if ( dir.exists() && dir.isDirectory() ) {
try {
root = dir.toURI().toURL();
}
catch (MalformedURLException e) {
throw new STException("can't load dir "+dirName, e);
}
if ( verbose ) System.out.println("STGroupDir("+dirName+") found at "+root);
}
else {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
root = cl.getResource(dirName);
if ( root==null ) {
cl = this.getClass().getClassLoader();
root = cl.getResource(dirName);
}
if ( verbose ) System.out.println("STGroupDir("+dirName+") found via CLASSPATH at "+root);
if ( root==null ) {
throw new IllegalArgumentException("No such directory: "+
dirName);
}
}
}
public STGroupDir(String dirName, String encoding) {
this(dirName, encoding, '<', '>');
}
public STGroupDir(String dirName, String encoding,
char delimiterStartChar, char delimiterStopChar)
{
this(dirName, delimiterStartChar, delimiterStopChar);
this.encoding = encoding;
}
public STGroupDir(URL root, String encoding,
char delimiterStartChar, char delimiterStopChar)
{
super(delimiterStartChar, delimiterStopChar);
this.groupDirName = new File(root.getFile()).getName();
this.root = root;
this.encoding = encoding;
}
@Override
public void importTemplates(Token fileNameToken) {
String msg =
"import illegal in group files embedded in STGroupDirs; "+
"import "+fileNameToken.getText()+" in STGroupDir "+this.getName();
throw new UnsupportedOperationException(msg);
}
@Override
protected CompiledST load(String name) {
if ( verbose ) System.out.println("STGroupDir.load("+name+")");
String parent = Misc.getParent(name);
String prefix = Misc.getPrefix(name);
URL groupFileURL;
try {
groupFileURL = new URL(root+parent+GROUP_FILE_EXTENSION);
}
catch (MalformedURLException e) {
errMgr.internalError(null, "bad URL: "+root+parent+GROUP_FILE_EXTENSION, e);
return null;
}
InputStream is = null;
try {
is = groupFileURL.openStream();
}
catch (IOException ioe) {
String unqualifiedName = Misc.getFileName(name);
return loadTemplateFile(prefix, unqualifiedName+TEMPLATE_FILE_EXTENSION);
}
finally {
try {
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);
return rawGetTemplate(name);
}
public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName) {
if ( verbose ) System.out.println("loadTemplateFile("+unqualifiedFileName+") in groupdir "+
"from "+root+" prefix="+prefix);
URL f;
try {
f = new URL(root+prefix+unqualifiedFileName);
}
catch (MalformedURLException me) {
errMgr.runTimeError(null, null, ErrorType.INVALID_TEMPLATE_NAME,
me, root + unqualifiedFileName);
return null;
}
ANTLRInputStream fs;
try {
fs = new ANTLRInputStream(f.openStream(), encoding);
fs.name = unqualifiedFileName;
}
catch (IOException ioe) {
if ( verbose ) System.out.println(root+"/"+unqualifiedFileName+" doesn't exist");
return null;
}
return loadTemplateFile(prefix, unqualifiedFileName, fs);
}
@Override
public String getName() { return groupDirName; }
@Override
public String getFileName() { return root.getFile(); }
@Override
public URL getRootDirURL() { return root; }
}