package net.panonet; import java.io.IOException; import java.io.File; import java.net.URL; import java.util.Map; import java.io.StringWriter; import java.io.StringReader; 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.WikiEngine; import com.ecyrd.jspwiki.plugin.WikiPlugin; import com.ecyrd.jspwiki.plugin.PluginException; import com.ecyrd.jspwiki.attachment.AttachmentManager; import com.ecyrd.jspwiki.attachment.Attachment; import com.ecyrd.jspwiki.providers.ProviderException; import com.ecyrd.jspwiki.providers.WikiPageProvider; import com.ecyrd.jspwiki.providers.WikiAttachmentProvider; /** * 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='' Name of the XSLT file to use * xml='' Name of the XML file to process * * 29.05.2004 Jan Wessely : Relative paths are resolved to the wiki's base URL. * 12.01.2005 Joe Mocker : Added ability to use Wiki Attachments & Pages */ 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 StreamSource xml = getSource((String) params.get("xml"), context); StreamSource xsl = getSource((String) params.get("xsl"), context); String result = ""; try { // Create a Transformer TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer( xsl ); // Transform the given XML file using the given XSL file, placing // it in a String. StringWriter resultWriter = new StringWriter(); transformer.transform( xml, new StreamResult( resultWriter ) ); result = resultWriter.toString(); } catch( Exception e ) { e.printStackTrace(); throw new PluginException( "Exception performing transformation.", e); } return result; } private StreamSource getSource(String path, WikiContext context) throws PluginException { WikiEngine engine = context.getEngine(); StreamSource source = null; if (path.startsWith("http:")) { source = new StreamSource(path); } else if (!path.startsWith("file:")) { // first, see if the parameter is a WikiAttachment try { AttachmentManager mgr = engine.getAttachmentManager(); WikiAttachmentProvider provider = mgr.getCurrentProvider(); Attachment att = mgr.getAttachmentInfo( context, path); if (att != null) { // The jspwiki.baseURL property in jspwiki.properties // must be defined for this to work. source = new StreamSource(provider.getAttachmentData(att)); } } catch (ProviderException e) { throw new PluginException( "Attachment info failed: "+e.getMessage() ); } catch (IOException e) { throw new PluginException( "Attachmeand read failed: " + e.getMessage() ); } // next, see if this parameter is a WikiPage & not ourselves if (source == null && engine.pageExists(path) && !path.equals(context.getPage().getName())) { String contents = engine.getPureText(path, WikiPageProvider.LATEST_VERSION); source = new StreamSource(new StringReader(contents)); } // last, assume the parameter is just a file on the webserver. if (source == null) { path = context.getEngine().getBaseURL() + path; source = new StreamSource(path); } } else if (path.startsWith("file:")) { throw new IllegalArgumentException("file: URLs are not allowed."); } return source; } }