MCPcopy Create free account
hub / github.com/apache/freemarker / FileTemplateLoader

Class FileTemplateLoader

src/main/java/freemarker/cache/FileTemplateLoader.java:77–235  ·  view source on GitHub ↗

A TemplateLoader that uses files in a specified directory as the source of templates. If contains security checks that will prevent it serving templates outside the template directory (like <include /etc/passwd> . It compares canonical paths for this, so templates that are sym

Source from the content-addressed store, hash-verified

75 * @version $Id: FileTemplateLoader.java,v 1.26 2004/03/29 08:06:22 szegedia Exp $
76 */
77public class FileTemplateLoader implements TemplateLoader
78{
79 private static final boolean SEP_IS_SLASH = File.separatorChar == '/';
80 public final File baseDir;
81 private final String canonicalPath;
82
83 /**
84 * Creates a new file template cache that will use the current directory
85 * (the value of the system property <code>user.dir</code> as the base
86 * directory for loading templates. It will not allow access to template
87 * files that are accessible through symlinks that point outside the
88 * base directory.
89 */
90 public FileTemplateLoader()
91 throws
92 IOException
93 {
94 this(new File(SecurityUtilities.getSystemProperty("user.dir")));
95 }
96
97 /**
98 * Creates a new file template loader that will use the specified directory
99 * as the base directory for loading templates. It will not allow access to
100 * template files that are accessible through symlinks that point outside
101 * the base directory.
102 * @param baseDir the base directory for loading templates
103 */
104 public FileTemplateLoader(final File baseDir)
105 throws
106 IOException
107 {
108 this(baseDir, false);
109 }
110
111 /**
112 * Creates a new file template loader that will use the specified directory
113 * as the base directory for loading templates.
114 * @param baseDir the base directory for loading templates
115 * @param allowLinking if true, it will allow
116 */
117 public FileTemplateLoader(final File baseDir, final boolean allowLinking)
118 throws
119 IOException
120 {
121 try {
122 Object[] retval = (Object[]) AccessController.doPrivileged(new PrivilegedExceptionAction() {
123 public Object run() throws IOException {
124 if (!baseDir.exists()) {
125 throw new FileNotFoundException(baseDir + " does not exist.");
126 }
127 if (!baseDir.isDirectory()) {
128 throw new IOException(baseDir + " is not a directory.");
129 }
130 Object[] retval = new Object[2];
131 if(allowLinking) {
132 retval[0] = baseDir;
133 retval[1] = null;
134 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…