Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/freemarker/TemplateLoader.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.uma.freemarker;2223import java.io.File;24import java.io.IOException;25import java.net.MalformedURLException;26import java.net.URL;27import java.util.HashMap;28import java.util.Set;29import java.util.Vector;3031import com.ibm.uma.UMA;32import com.ibm.uma.UMAException;33import com.ibm.uma.util.Logger;3435import freemarker.cache.URLTemplateLoader;3637public class TemplateLoader extends URLTemplateLoader {3839HashMap<String, URL> allTemplates = new HashMap<String, URL>();40HashMap<String, URL> allIncludeTemplates = new HashMap<String, URL>();4142public TemplateLoader() throws UMAException {43// Find and cache all templates in the source tree44addAllTemplatesFoundInDirectory(new File(UMA.getUma().getRootDirectory()));45}4647public Set<String> getTemplates() {48return allTemplates.keySet();49}5051void addAllTemplatesFoundInDirectory(File dir) throws UMAException {52String rootDir = UMA.getUma().getRootDirectory();53File [] dirListing = dir.listFiles();54Vector<File> directories = new Vector<File>();55for ( File file : dirListing ) {56if ( file.isDirectory() ) {57directories.add(file);58} else if ( file.getName().endsWith(".ftl") || file.getName().endsWith(".iftl")) {59String templatePath = file.getParent();60if ( templatePath.equalsIgnoreCase(rootDir) || rootDir.equalsIgnoreCase(templatePath+File.separator)) {61templatePath = "";62} else {63templatePath = file.getParent().substring(rootDir.length());64}65templatePath = templatePath.replace(File.separator, "/");66String templateName = (templatePath.length()==0?"":templatePath+"/") + file.getName();67URL url;68try {69url = new URL("file:///" + file.getCanonicalPath());70} catch (MalformedURLException e) {71throw new UMAException(e);72} catch (IOException e) {73throw new UMAException(e);74}75HashMap<String, URL> templates = allTemplates;76if (file.getName().endsWith(".inc.ftl") || file.getName().endsWith(".iftl")) {77templates = allIncludeTemplates;78}79templates.put(templateName, url);80Logger.getLogger().println(Logger.InformationL2Log, "Found template " + templateName + " URL: " + url.toExternalForm());81}82}83for ( File file : directories ) {84addAllTemplatesFoundInDirectory(file);85}8687}88@Override89protected URL getURL(String arg0) {90URL url = allTemplates.get(arg0);91if ( url == null ) {92url = allIncludeTemplates.get(arg0);93}94return url;95}9697}9899100