Superseded by Joe Mocker's Version
which allows Wiki Pages and Attachments as XML and XSL sources.
which allows Wiki Pages and Attachments as XML and XSL sources.
package net.panonet;
import java.io.File;
import java.net.URL;
import java.util.Map;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
import com.ecyrd.jspwiki.plugin.PluginException;
import java.io.StringWriter;
/**
* JSPWiki plugin for processing XML files into HTML in a Wiki page using XSLT.
*
* The plugin has two parameters from the perspective of the Wiki page:
*
* xsl='<xslfile>' Name of the XSLT file to use
* xml='<xslfile>' Name of the XML file to process
*
* 29.05.2004 Jan Wessely <info@jawe.net>: Relative paths are resolved to the wiki's base URL.
*/
public class XSLTPlugin implements WikiPlugin
{
/**
* Main plugin entry point.
*/
public String execute(WikiContext context,
Map params)
throws PluginException
{
// Get the names of the XML and XSL files to use
String xsl = getAbsolutePath((String)params.get( "xsl" ), context);
String xml = getAbsolutePath((String)params.get( "xml" ), context);
String result = "";
try
{
// Create a Transformer
TransformerFactory transformerFactory
= TransformerFactory.newInstance();
Transformer transformer
= transformerFactory.newTransformer( new StreamSource( xsl ) );
// Transform the given XML file using the given XSL file, placing
// it in a String.
StringWriter resultWriter = new StringWriter();
transformer.transform( new StreamSource( xml ),
new StreamResult( resultWriter ) );
result = resultWriter.toString();
}
catch( Exception e )
{
System.out.println( e.toString() );
throw new PluginException( "Exception performing transformation.",
e );
}
return result;
}
private String getAbsolutePath(String path, WikiContext context) {
if (!(path.startsWith("http:")
|| path.startsWith("file:"))) {
path = context.getEngine().getBaseURL()
+ (path.startsWith("/") ? path : "/" + path);
} else if (path.startsWith("file:")) {
throw new IllegalArgumentException("file: URLs are not allowed.");
}
return path;
}
}