01: package org.obe.runtime.tool;
02:
03: import java.io.*;
04: import java.lang.reflect.InvocationTargetException;
05: import org.apache.commons.logging.Log;
06: import org.apache.commons.logging.LogFactory;
07: import org.apache.velocity.VelocityContext;
08: import org.apache.velocity.app.Velocity;
09: import org.apache.velocity.context.Context;
10: import org.obe.client.api.repository.RepositoryException;
11: import org.obe.client.api.repository.XFormMetaData;
12: import org.obe.client.api.tool.Parameter;
13: import org.obe.client.api.tool.ToolInvocation;
14: import org.obe.engine.EngineContext;
15:
16: /**
17: * A tool agent for displaying a W3C XForms user interface.
18: *
19: * @author Adrian Price
20: */
21: public final class XForm extends AbstractToolAgent {
22: private static final Log _logger = LogFactory.getLog(XForm.class);
23: private final XFormMetaData _metadata;
24: private final String _xForm;
25:
26: public XForm(XFormMetaData metadata, Parameter[] parameters)
27: throws Exception {
28:
29: _metadata = metadata;
30: _xForm = createXForm(parameters);
31: }
32:
33: public String getXForm() {
34: return _xForm;
35: }
36:
37: private String createXForm(Parameter[] parameters) throws Exception {
38: Reader in = null;
39: Writer out = null;
40: try {
41: Context velocityCtx = new VelocityContext();
42: velocityCtx.put("context", EngineContext.peekContext());
43: velocityCtx.put("metadata", _metadata);
44: velocityCtx.put("parms", parameters);
45:
46: in = getTemplate();
47: out = new StringWriter();
48:
49: Velocity.init();
50: if (!Velocity.evaluate(velocityCtx, out, "velocity", in))
51: _logger.warn("Velocity template processing failed");
52: } finally {
53: if (in != null)
54: in.close();
55: if (out != null)
56: out.close();
57: }
58: return out.toString();
59: }
60:
61: private Reader getTemplate() throws IOException, /*XMLException,*/
62: RepositoryException {
63:
64: // XForm template can be supplied by the XPDL as an extended attribute,
65: // or can be defined in the tool agent factory configuration file,
66: // either inline or in an external file.
67: Reader in;
68: // String source = null;
69: // if (source == null)
70: // source = _metadata.getTemplate();
71: String source = _metadata.getTemplate();
72: if (_metadata.isFile()) {
73: // Assume default encoding.
74: InputStream is = getWorkflowContext().getServiceManager()
75: .getResourceRepository().findEntity(source);
76: if (is == null)
77: throw new FileNotFoundException(source);
78: in = new InputStreamReader(is);
79: } else {
80: in = new StringReader(_metadata.getTemplate());
81: }
82: return in;
83: }
84:
85: protected int _invokeApplication(ToolInvocation ti)
86: throws InvocationTargetException {
87:
88: try {
89: // TODO: provide a proper implementation that displays the XForm.
90: System.out.println("XForm: ");
91: System.out.println(_xForm);
92: } catch (Exception e) {
93: throw new InvocationTargetException(e);
94: }
95:
96: return 0;
97: }
98: }
|