Package: TemplateManager

TemplateManager

nameinstructionbranchcomplexitylinemethod
TemplateManager(String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
TemplateManager(String, Class)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
constructTemplate(String, StringBuffer)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
derivePath(String, Class)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getRoot()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTemplate(Class)
M: 3 C: 39
93%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 2 C: 9
82%
M: 0 C: 1
100%
getTemplateData(Class)
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getTemplateLoaderRef()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTemplatePath(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
initNodeTemplateFileNames()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isUserDefined(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setUserTemplatePath(Class, Class, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.merging;
23:
24: import java.io.File;
25: import java.io.IOException;
26: import java.io.StringReader;
27: import java.util.HashMap;
28:
29: import org.apache.velocity.Template;
30: import org.apache.velocity.runtime.RuntimeServices;
31: import org.apache.velocity.runtime.RuntimeSingleton;
32: import org.apache.velocity.runtime.parser.ParseException;
33: import org.apache.velocity.runtime.parser.node.SimpleNode;
34: import org.overture.codegen.ir.INode;
35: import org.overture.codegen.utils.GeneralUtils;
36:
37: public class TemplateManager
38: {
39:         public static final String TEMPLATE_FILE_EXTENSION = ".vm";
40:
41:         /**
42:          * Relative paths for user-defined template files
43:          */
44:         protected HashMap<Class<? extends INode>, TemplateData> userDefinedPaths;
45:
46:         /**
47:          * cache
48:          */
49:         final protected HashMap<Class<? extends INode>, Template> cache = new HashMap<Class<? extends INode>, Template>();
50:
51:         protected String root;
52:
53:         private Class<?> templateLoadRef = null;
54:
55:         /**
56:          * Not for use with extensions. Use {@link #TemplateManager(String, Class)} instead.
57:          *
58:          * @param root
59:          * The template root folder
60:          */
61:         public TemplateManager(String root)
62:         {
63:                 this(root, null);
64:         }
65:
66:         /**
67:          * This is an extensibility version of the template manager constructor for the benefit of OSGi extensions. <br>
68:          * <br>
69:          * Because of the way classloaders work in OSGi, the template lookups fail if the templates are located in another
70:          * bundle. This provides a workaround by using templateLoadRef to provide a specific class. Just pass it any class
71:          * that is native to the extension bundle and it should work.
72:          *
73:          * @param root
74:          * The template root folder
75:          * @param templateLoadRef
76:          */
77:         public TemplateManager(String root, Class<?> templateLoadRef)
78:         {
79:                 this.root = root;
80:
81:•                if (templateLoadRef != null)
82:                 {
83:                         this.templateLoadRef = templateLoadRef;
84:                 } else
85:                 {
86:                         this.templateLoadRef = this.getClass();
87:                 }
88:
89:                 initNodeTemplateFileNames();
90:         }
91:
92:         public Class<?> getTemplateLoaderRef()
93:         {
94:                 return templateLoadRef;
95:         }
96:
97:         public String getRoot()
98:         {
99:                 return root;
100:         }
101:
102:         protected void initNodeTemplateFileNames()
103:         {
104:                 this.userDefinedPaths = new HashMap<>();
105:         }
106:
107:         public Template getTemplate(Class<? extends INode> nodeClass)
108:                         throws ParseException
109:         {
110:•                if (cache.containsKey(nodeClass))
111:                 {
112:                         return cache.get(nodeClass);
113:                 }
114:
115:                 try
116:                 {
117:                         TemplateData td = getTemplateData(nodeClass);
118:
119:                         StringBuffer buffer = GeneralUtils.readFromFile(td.getTemplatePath(), td.getTemplateLoaderRef());
120:
121:•                        if (buffer == null)
122:                         {
123:                                 return null;
124:                         }
125:
126:                         Template template = constructTemplate(td.getTemplatePath(), buffer);
127:                         cache.put(nodeClass, template);
128:                         return template;
129:
130:                 } catch (IOException e)
131:                 {
132:                         return null;
133:                 }
134:         }
135:
136:         public String getTemplatePath(Class<? extends INode> nodeClass)
137:         {
138:                 return getTemplateData(nodeClass).getTemplatePath();
139:         }
140:
141:         public TemplateData getTemplateData(Class<? extends INode> nodeClass)
142:         {
143:•                if (isUserDefined(nodeClass))
144:                 {
145:                         return userDefinedPaths.get(nodeClass);
146:                 } else
147:                 {
148:                         return new TemplateData(templateLoadRef, derivePath(root, nodeClass));
149:                 }
150:         }
151:
152:         protected Template constructTemplate(String name, StringBuffer buffer)
153:                         throws ParseException
154:         {
155:                 Template template = new Template();
156:                 RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
157:                 StringReader reader = new StringReader(buffer.toString());
158:
159:                 SimpleNode simpleNode = runtimeServices.parse(reader, name);
160:                 template.setRuntimeServices(runtimeServices);
161:                 template.setData(simpleNode);
162:                 template.initDocument();
163:
164:                 return template;
165:
166:         }
167:
168:         public void setUserTemplatePath(Class<?> templateLoaderRef,
169:                         Class<? extends INode> nodeClass, String templatePath)
170:         {
171:                 userDefinedPaths.put(nodeClass, new TemplateData(templateLoadRef, templatePath));
172:         }
173:
174:         public boolean isUserDefined(Class<? extends INode> nodeClass)
175:         {
176:                 return userDefinedPaths.containsKey(nodeClass);
177:         }
178:
179:         public static String derivePath(String root,
180:                         Class<? extends INode> nodeClass)
181:         {
182:                 return root + File.separatorChar
183:                                 + nodeClass.getName().replace('.', File.separatorChar)
184:                                 + TEMPLATE_FILE_EXTENSION;
185:         }
186: }