Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/freemarker/TemplateLoader.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.uma.freemarker;
23
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.MalformedURLException;
27
import java.net.URL;
28
import java.util.HashMap;
29
import java.util.Set;
30
import java.util.Vector;
31
32
import com.ibm.uma.UMA;
33
import com.ibm.uma.UMAException;
34
import com.ibm.uma.util.Logger;
35
36
import freemarker.cache.URLTemplateLoader;
37
38
public class TemplateLoader extends URLTemplateLoader {
39
40
HashMap<String, URL> allTemplates = new HashMap<String, URL>();
41
HashMap<String, URL> allIncludeTemplates = new HashMap<String, URL>();
42
43
public TemplateLoader() throws UMAException {
44
// Find and cache all templates in the source tree
45
addAllTemplatesFoundInDirectory(new File(UMA.getUma().getRootDirectory()));
46
}
47
48
public Set<String> getTemplates() {
49
return allTemplates.keySet();
50
}
51
52
void addAllTemplatesFoundInDirectory(File dir) throws UMAException {
53
String rootDir = UMA.getUma().getRootDirectory();
54
File [] dirListing = dir.listFiles();
55
Vector<File> directories = new Vector<File>();
56
for ( File file : dirListing ) {
57
if ( file.isDirectory() ) {
58
directories.add(file);
59
} else if ( file.getName().endsWith(".ftl") || file.getName().endsWith(".iftl")) {
60
String templatePath = file.getParent();
61
if ( templatePath.equalsIgnoreCase(rootDir) || rootDir.equalsIgnoreCase(templatePath+File.separator)) {
62
templatePath = "";
63
} else {
64
templatePath = file.getParent().substring(rootDir.length());
65
}
66
templatePath = templatePath.replace(File.separator, "/");
67
String templateName = (templatePath.length()==0?"":templatePath+"/") + file.getName();
68
URL url;
69
try {
70
url = new URL("file:///" + file.getCanonicalPath());
71
} catch (MalformedURLException e) {
72
throw new UMAException(e);
73
} catch (IOException e) {
74
throw new UMAException(e);
75
}
76
HashMap<String, URL> templates = allTemplates;
77
if (file.getName().endsWith(".inc.ftl") || file.getName().endsWith(".iftl")) {
78
templates = allIncludeTemplates;
79
}
80
templates.put(templateName, url);
81
Logger.getLogger().println(Logger.InformationL2Log, "Found template " + templateName + " URL: " + url.toExternalForm());
82
}
83
}
84
for ( File file : directories ) {
85
addAllTemplatesFoundInDirectory(file);
86
}
87
88
}
89
@Override
90
protected URL getURL(String arg0) {
91
URL url = allTemplates.get(arg0);
92
if ( url == null ) {
93
url = allIncludeTemplates.get(arg0);
94
}
95
return url;
96
}
97
98
}
99
100