1 /*
2 * Copyright (c) 2003
3 * Information Desire GmbH
4 * All rights reserved.
5 */
6 package com.infodesire.infobit.external.impl;
7
8 import com.infodesire.infobit.InfobitManager;
9 import com.infodesire.infobit.InfobitException;
10
11 import com.infodesire.infobit.external.InfobitExporter;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.apache.velocity.VelocityContext;
17 import org.apache.velocity.app.Velocity;
18 import org.apache.velocity.app.VelocityEngine;
19 import org.apache.velocity.runtime.RuntimeConstants;
20 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
21
22 import java.io.InputStream;
23 import java.io.Writer;
24
25 import java.util.Collection;
26 import java.util.Properties;
27
28 /***
29 * Converts infobits to XML (as defined by {@link InfobitExporter}, relying on a
30 * {@link VelocityRenderer}.
31 *
32 * @author peter2
33 * @created August 26, 2003
34 * @version $Revision: 1.2 $
35 */
36 public class VelocityExporter implements InfobitExporter {
37
38 private final static Log _log = LogFactory.getLog(VelocityExporter.class);
39
40 /***
41 * Resource path to the tmeplate formatting infobit collections
42 */
43 private final static String COLLECTION_TEMPLATE =
44 "com/infodesire/infobit/external/impl/infobit-collection.vm";
45
46 /***
47 * Resource path to the velocity properties file applicable for infobit XML
48 * export
49 */
50 private final static String VELO_PROP_PATH =
51 "/com/infodesire/infobit/external/impl/exporterVelocity.properties";
52
53 /***
54 * The collaborating manager
55 */
56 private static InfobitManager _manager;
57
58 /***
59 * Used engine
60 */
61 private VelocityEngine _engine = null;
62
63 /***
64 * Locally reused instance
65 */
66 private ExportRenderHelper _helper = new ExportRenderHelper();
67
68
69 /***
70 * Initializes an incomplete instance.
71 */
72 public VelocityExporter() {
73 _engine = new VelocityEngine();
74 }
75
76
77 /***
78 * Gets the Manager attribute of the VelocityRenderer object
79 *
80 * @return The Manager value
81 */
82 public InfobitManager getManager() {
83 return _manager;
84 }
85
86
87 /***
88 * Sets the Manager attribute of the VelocityRenderer object
89 *
90 * @param manager The new Manager value
91 */
92 public void setManager(InfobitManager manager) {
93 _manager = manager;
94 }
95
96
97 /***
98 * Prepares the instance to be used.
99 *
100 * @exception InfobitException Description of Exception
101 */
102 public void init() throws InfobitException {
103 try {
104 Properties vp = new Properties();
105 InputStream ps = getClass().getResourceAsStream(VELO_PROP_PATH);
106 vp.load(ps);
107 _engine.init(vp);
108
109 _helper.init();
110 } catch (Exception e) {
111 _log.error("unable to init velocity", e);
112 throw new InfobitException(e);
113 }
114 }
115
116
117 /***
118 * Converts the specified set of infobits into an XML representation.
119 *
120 * @param infobits The infobits to externalize. Infobits are
121 * externalized in the order obtained from than argument. Collection of
122 * {@link Infobit}.
123 * @param writer The destination where to write the XML
124 * representation of <code>infobits</code> to
125 * @exception InfobitException Unexpected error condition
126 */
127 public synchronized void export(Collection infobits, Writer writer)
128 throws InfobitException {
129
130 try {
131 VelocityContext vc = new VelocityContext();
132 vc.put("manager", _manager);
133 vc.put("infobits", infobits);
134 vc.put("helper", _helper);
135
136 _engine.mergeTemplate(COLLECTION_TEMPLATE, vc, writer);
137 } catch (Exception e) {
138 _log.error("failure converting infobits to XML", e);
139 throw new InfobitException(e);
140 }
141 }
142
143 }
This page was automatically generated by Maven