org.zodiac.template.base.impl.SearchExtensionsStrategy Maven / Gradle / Ivy
The newest version!
package org.zodiac.template.base.impl;
import java.util.List;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
/**
* 假如当前模板名后缀无法找到模板,试着以其它后缀寻找。
*
* 例如:模板test.vm
不存在,那么就尝试一下test.jsp
、 test.ftl
等。
*
*/
public class SearchExtensionsStrategy implements TemplateSearchingStrategy {
private final String[] availableExtensions;
public SearchExtensionsStrategy(String[] extensions) {
this.availableExtensions = AssertUtil.assertNotNull(extensions, "extensions");
}
public Object getKey(String templateName) {
return null;
}
public boolean findTemplate(TemplateMatcher matcher) {
List testedExtensions = CollUtil.list(availableExtensions.length);
boolean found = false;
String ext = matcher.getExtension();
if (ext != null) {
testedExtensions.add(ext);
found = matcher.findTemplate();
}
for (int i = 0; !found && i < availableExtensions.length; i++) {
ext = availableExtensions[i];
if (!testedExtensions.contains(ext)) {
testedExtensions.add(ext);
matcher.setExtension(ext);
found = matcher.findTemplate();
}
}
return found;
}
}