if (categoryPath == null) {
elements.add(template);
}
else {
String[] categoryPaths = categoryPath.split("/");
TemplateCategory lastCategory = null;
String path = "";
for (String categoryName : categoryPaths) {
boolean topLevel;
if (path.length() == 0) {
path = categoryName;
topLevel = true;
}
else {
path += "/" + categoryName;
topLevel = false;
}
TemplateCategory category = pathToCategory.get(path);
if (category == null) {
category = new TemplateCategory(categoryName);
pathToCategory.put(path, category);
if (topLevel) {
elements.add(category);
}