|
1 |
| package com.agical.rdoc.velocity.impl; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.StringWriter; |
|
5 |
| import java.io.UnsupportedEncodingException; |
|
6 |
| import java.util.Properties; |
|
7 |
| |
|
8 |
| import org.apache.velocity.Template; |
|
9 |
| import org.apache.velocity.VelocityContext; |
|
10 |
| import org.apache.velocity.app.VelocityEngine; |
|
11 |
| import org.apache.velocity.context.Context; |
|
12 |
| import org.apache.velocity.exception.MethodInvocationException; |
|
13 |
| import org.apache.velocity.exception.ParseErrorException; |
|
14 |
| import org.apache.velocity.exception.ResourceNotFoundException; |
|
15 |
| |
|
16 |
| import com.agical.rdoc.core.exception.RDocParseSystemException; |
|
17 |
| import com.agical.rdoc.core.exception.ResourceNotFoundSystemException; |
|
18 |
| import com.agical.rdoc.core.resourcemanager.ResourceProvider; |
|
19 |
| import com.agical.rdoc.core.resourcemanager.ResourceRoot; |
|
20 |
| import com.agical.rdoc.core.target.TargetProvider; |
|
21 |
| import com.agical.rdoc.velocity.VelocityGenerationSystemException; |
|
22 |
| import com.agical.rdoc.velocity.VelocityManager; |
|
23 |
| import com.agical.rmock.core.configuration.NameSpaceProvider; |
|
24 |
| import com.agical.rmock.core.exception.UnexpectedFatalSystemException; |
|
25 |
| |
|
26 |
| public class VelocityManagerImpl implements VelocityManager { |
|
27 |
| private ResourceProvider resourceProvider; |
|
28 |
| private VelocityEngine engine; |
|
29 |
| private TargetProvider targetProvider; |
|
30 |
| private NameSpaceProvider nameSpaceProvider = NameSpaceProvider.NULL; |
|
31 |
| |
|
32 |
24
| private synchronized VelocityEngine getEngine() {
|
|
33 |
24
| if (engine == null) {
|
|
34 |
13
| engine = new VelocityEngine();
|
|
35 |
13
| try {
|
|
36 |
13
| Properties properties = new Properties();
|
|
37 |
13
| ResourceRoot[] roots = resourceProvider.getRoots();
|
|
38 |
13
| StringBuffer loadersProperty = new StringBuffer();
|
|
39 |
13
| for (int i = 0; i < roots.length; i++) {
|
|
40 |
48
| String root = "resourceroot"+i;
|
|
41 |
48
| loadersProperty.append(root).append(", ");
|
|
42 |
48
| properties.setProperty(root+".resource.loader.description", root);
|
|
43 |
48
| properties.setProperty(root+".resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
|
|
44 |
48
| properties.setProperty(root+".resource.loader.path", roots[i].getUrl().getPath());
|
|
45 |
48
| properties.setProperty(root+".resource.loader.cache", "true");
|
|
46 |
48
| properties.setProperty(root+".resource.loader.modificationCheckInterval", "2");
|
|
47 |
| } |
|
48 |
13
| properties.setProperty("targetroot.resource.loader.description", "targetroot");
|
|
49 |
13
| properties.setProperty("targetroot.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
|
|
50 |
13
| properties.setProperty("targetroot.resource.loader.path", targetProvider.getTarget().getAbsolutePath());
|
|
51 |
13
| properties.setProperty("targetroot.resource.loader.cache", "true");
|
|
52 |
13
| properties.setProperty("targetroot.resource.loader.modificationCheckInterval", "2");
|
|
53 |
| |
|
54 |
13
| properties.setProperty("classpath.resource.loader.description", "Velocity Classpath Resource Loader");
|
|
55 |
13
| properties.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
56 |
| |
|
57 |
13
| loadersProperty.append("targetroot, classpath");
|
|
58 |
| |
|
59 |
13
| properties.setProperty("resource.loader", loadersProperty.toString());
|
|
60 |
13
| engine.init(properties);
|
|
61 |
| } catch (RuntimeException e) { |
|
62 |
0
| throw e;
|
|
63 |
| } |
|
64 |
| catch (Exception e) { |
|
65 |
0
| throw new UnexpectedFatalSystemException("Problems initializing velocity", e);
|
|
66 |
| } |
|
67 |
| } |
|
68 |
24
| return engine;
|
|
69 |
| } |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
24
| public void setResourceProvider(ResourceProvider resourceProvider) {
|
|
76 |
24
| this.resourceProvider = resourceProvider;
|
|
77 |
| } |
|
78 |
| |
|
79 |
13
| public Template getTemplate(String template) {
|
|
80 |
13
| try {
|
|
81 |
13
| return getEngine().getTemplate(template);
|
|
82 |
| } catch (ResourceNotFoundException e) { |
|
83 |
0
| throw new ResourceNotFoundSystemException(e);
|
|
84 |
| } catch (ParseErrorException e) { |
|
85 |
0
| throw new RDocParseSystemException(e);
|
|
86 |
| } catch (Exception e) { |
|
87 |
0
| throw new UnexpectedFatalSystemException("Error getting the template <"+template+">", e);
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| |
|
91 |
24
| public void setTargetProvider(TargetProvider targetProvider) {
|
|
92 |
24
| this.targetProvider = targetProvider;
|
|
93 |
| } |
|
94 |
| |
|
95 |
26
| public Context createContext() {
|
|
96 |
26
| return new VelocityContext( new VelocityContext(nameSpaceProvider.getNameSpaces()) );
|
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
12
| public String evaluate(String template, Context velocityContext) {
|
|
101 |
12
| if (template == null) {
|
|
102 |
1
| return "";
|
|
103 |
| } |
|
104 |
| |
|
105 |
11
| StringWriter stringWriter = new StringWriter();
|
|
106 |
11
| try {
|
|
107 |
11
| getEngine().evaluate( velocityContext, stringWriter, "", template );
|
|
108 |
11
| stringWriter.close();
|
|
109 |
11
| return stringWriter.toString();
|
|
110 |
| } catch (ResourceNotFoundException e) { |
|
111 |
0
| throw new ResourceNotFoundSystemException(e);
|
|
112 |
| } catch (ParseErrorException e) { |
|
113 |
0
| throw new VelocityGenerationSystemException(e);
|
|
114 |
| } catch (MethodInvocationException e) { |
|
115 |
0
| throw new VelocityGenerationSystemException(e);
|
|
116 |
| } catch (UnsupportedEncodingException e) { |
|
117 |
0
| throw new UnexpectedFatalSystemException("Failed to encode to UTF-8", e);
|
|
118 |
| } catch (IOException e) { |
|
119 |
0
| throw new UnexpectedFatalSystemException(e);
|
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
25
| public void setNameSpaceProvider(NameSpaceProvider nameSpaceProvider) {
|
|
125 |
25
| this.nameSpaceProvider = nameSpaceProvider;
|
|
126 |
| } |
|
127 |
| |
|
128 |
| } |