KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > WorkflowSerializer


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import org.apache.velocity.Template;
8 import org.apache.velocity.VelocityContext;
9 import org.apache.velocity.app.VelocityEngine;
10
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.OutputStreamWriter JavaDoc;
13
14
15 /**
16  * DOCUMENT ME!
17  *
18  * @author $author$
19  * @version $Revision: 1.2 $
20  */

21 public class WorkflowSerializer {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     private VelocityEngine engine;
25
26     //~ Methods ////////////////////////////////////////////////////////////////
27

28     public byte[] generateWorkflowXML(WorkflowDescriptor wf) throws Exception JavaDoc {
29         initVelocity();
30
31         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
32         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(baos);
33         Template template = engine.getTemplate("workflowtemplate.vm");
34         VelocityContext ctx = new VelocityContext();
35         ctx.put("workflow", wf);
36         template.merge(ctx, writer);
37         writer.flush();
38         baos.flush();
39
40         return baos.toByteArray();
41     }
42
43     protected void initVelocity() throws Exception JavaDoc {
44         if (engine == null) {
45             engine = new VelocityEngine();
46             engine.setProperty("resource.loader", "class");
47             engine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
48             engine.init();
49         }
50     }
51 }
52
Popular Tags