|
1 |
| package com.agical.rdoc.velocity.render; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.OutputStream; |
|
5 |
| import java.io.OutputStreamWriter; |
|
6 |
| import java.io.UnsupportedEncodingException; |
|
7 |
| import java.io.Writer; |
|
8 |
| |
|
9 |
| import org.apache.velocity.Template; |
|
10 |
| import org.apache.velocity.context.Context; |
|
11 |
| import org.apache.velocity.exception.MethodInvocationException; |
|
12 |
| import org.apache.velocity.exception.ParseErrorException; |
|
13 |
| import org.apache.velocity.exception.ResourceNotFoundException; |
|
14 |
| |
|
15 |
| import com.agical.rdoc.core.converter.ObjectConverter; |
|
16 |
| import com.agical.rdoc.core.exception.ResourceNotFoundSystemException; |
|
17 |
| import com.agical.rdoc.velocity.VelocityGenerationSystemException; |
|
18 |
| import com.agical.rmock.core.exception.UnexpectedFatalSystemException; |
|
19 |
| |
|
20 |
| public class VelocityTemplateConverter implements ObjectConverter { |
|
21 |
| private Context context; |
|
22 |
| |
|
23 |
12
| public VelocityTemplateConverter(Context context) {
|
|
24 |
12
| this.context = context;
|
|
25 |
| } |
|
26 |
| |
|
27 |
11
| public void convert(Object object, OutputStream target) throws IOException {
|
|
28 |
11
| try {
|
|
29 |
11
| Template template = (Template) object;
|
|
30 |
11
| Writer writer = new OutputStreamWriter(target, "UTF-8");
|
|
31 |
11
| template.merge(context, writer);
|
|
32 |
11
| writer.flush();
|
|
33 |
| } catch (ResourceNotFoundException e) { |
|
34 |
0
| throw new ResourceNotFoundSystemException(e);
|
|
35 |
| } catch (ParseErrorException e) { |
|
36 |
0
| throw new VelocityGenerationSystemException(e);
|
|
37 |
| } catch (MethodInvocationException e) { |
|
38 |
0
| throw new VelocityGenerationSystemException(e);
|
|
39 |
| } catch (UnsupportedEncodingException e) { |
|
40 |
0
| throw new UnexpectedFatalSystemException("Failed to encode to UTF-8", e);
|
|
41 |
| } catch (RuntimeException e) { |
|
42 |
0
| throw (RuntimeException) e;
|
|
43 |
| } |
|
44 |
| catch (Exception e) { |
|
45 |
0
| throw new VelocityGenerationSystemException(e);
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| } |