1 /*
2 * Copyright (c) 2001, 2002 The XDoclet team
3 * All rights reserved.
4 */
5 package xdoclet.plugins;
6
7 import org.apache.commons.jelly.JellyContext;
8 import org.apache.commons.jelly.JellyException;
9 import org.apache.commons.jelly.XMLOutput;
10 import org.apache.commons.logging.LogFactory;
11 import org.dom4j.io.HTMLWriter;
12 import org.dom4j.io.OutputFormat;
13 import org.dom4j.io.XMLWriter;
14 import org.xml.sax.SAXException;
15
16 import xdoclet.XDocletException;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.Writer;
22
23 import java.net.URL;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 /***
29 * Plugin backed by <a href="http://jakarta.apache.org/commons/jelly/">Jelly</a>.
30 *
31 * @bean.class name="jelly"
32 * displayName="Jelly Plugin"
33 * shortDescription="A plugin that uses Jelly Scripts"
34 *
35 * @bean.icon color16="jelly16.gif"
36 *
37 * @author <a href="mailto:aslak.hellesoy at netcom.no">Aslak Hellesøy</a>
38 * @version $Revision: 1.9 $
39 */
40 public class JellyPlugin extends ScriptPlugin {
41 protected final void generate(File file, Collection metaData) throws IOException, XDocletException {
42 // We're just ignoring the metaData. Our superclass has put it on the context.
43 super.generate( file, metaData );
44 generate(file);
45 }
46
47 protected final void generate(File file, Object metaData) throws IOException, XDocletException {
48 // We're just ignoring the metaData. Our superclass has put it on the context.
49 super.generate( file, metaData );
50 generate(file);
51 }
52
53 /***
54 * Invokes Jelly to render a template.
55 */
56 private final void generate(File file) throws IOException, XDocletException {
57 try {
58 // Find the template
59 URL templateURL = getXDoclet().getClasspathManager().getResource(getTemplatePath());
60
61 if (templateURL == null) {
62 throw new XDocletException("Couldn't find " + getTemplatePath() + " on the classpath: "
63 + getXDoclet().getClasspathManager().getClasspath());
64 }
65
66 // Make a JellyContext holding all the context objects
67 JellyContext context = new JellyContext();
68
69 Map props = getContextObjects();
70 Iterator iter = props.keySet().iterator();
71 while (iter.hasNext()) {
72 String key = (String) iter.next();
73 context.setVariable(key, props.get(key));
74 }
75
76 // Create output format with proper encoding
77 Writer writer = new FileWriter(file);
78 OutputFormat format = OutputFormat.createPrettyPrint();
79
80 format.setEncoding(getEncoding());
81
82 // Set pretty printing based on the file extension
83 boolean isHtml = file.getName().endsWith(".html");
84 final XMLWriter xmlWriter = (isHtml) ? new HTMLWriter(writer, format) : new XMLWriter(writer, format);
85
86 // Create the output
87 XMLOutput xmlOutput = new XMLOutput() {
88 public void close()
89 throws IOException {
90 xmlWriter.close();
91 }
92 };
93
94 xmlOutput.setContentHandler(xmlWriter);
95 xmlOutput.setLexicalHandler(xmlWriter);
96
97 xmlOutput.startDocument();
98 context.runScript(templateURL, xmlOutput);
99 xmlOutput.endDocument();
100
101 writer.flush();
102 writer.close();
103 } catch (JellyException e) {
104 LogFactory.getLog(JellyPlugin.class).error(e.getMessage(), e);
105 throw new XDocletException(e.getMessage(), e);
106 } catch (SAXException e) {
107 LogFactory.getLog(JellyPlugin.class).error(e.getMessage(), e);
108 throw new XDocletException(e.getMessage(), e);
109 }
110 }
111 }
This page was automatically generated by Maven